Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. public class DistanceDB implements DistanceDBIF {
  2. private AddressDBIF addressDB;
  3.  
  4. private static final String FIND_DISTANCE = "Select distance, destinationAddressIdFk, sourceAddressIdFk from Distance";
  5. private PreparedStatement findDistancePs;
  6.  
  7. public DistanceDB() throws DataAccessException {
  8. init();
  9. this.addressDB = new AddressDB();
  10. }
  11.  
  12. public DistanceDB(AddressDB addressDB) throws DataAccessException {
  13. init();
  14. this.addressDB = addressDB;
  15. }
  16.  
  17. private void init() throws DataAccessException {
  18. Connection con = DBConnection.getInstance().getConnection();
  19. try {
  20. findDistancePs = con.prepareStatement(FIND_DISTANCE);
  21. } catch (SQLException e) {
  22. throw new DataAccessException("Can not connect to distance", e);
  23. }
  24. }
  25.  
  26. public DistanceEdge bulidObject(ResultSet rs, boolean fullAsso) throws DataAccessException {
  27. DistanceEdge distance = new DistanceEdge();
  28. AddressVertex destination = null;
  29. AddressVertex source = null;
  30. try {
  31. source = addressDB.findId(rs.getInt("sourceAddressIdFk"));
  32. destination = addressDB.findId(rs.getInt("destinationAddressIdFk"));
  33. distance.setDistance(rs.getInt("distance"));
  34. distance.setSource(source);
  35. distance.setDestination(destination);
  36. return distance;
  37. } catch (SQLException e) {
  38. throw new DataAccessException("Mistake in bulidObject", e);
  39. }
  40. }
  41.  
  42. public List<DistanceEdge> bulidObjects(ResultSet rs) throws DataAccessException {
  43. List<DistanceEdge> listEdges = new ArrayList<>();
  44. try {
  45. while (rs.next()) {
  46. DistanceEdge currEdge = bulidObject(rs, false);
  47. listEdges.add(currEdge);
  48. }
  49. } catch (SQLException e) {
  50. throw new DataAccessException("Could not bulid objects", e);
  51. }
  52. return listEdges;
  53. }
  54.  
  55. public Graph getGraph(Graph graph, List<AddressVertex> listAddresses) throws DataAccessException {
  56. ResultSet rs;
  57. Graph g = graph;
  58. g.AddVertexRange();
  59. try {
  60. rs = findDistancePs.executeQuery();
  61. List<DistanceEdge> listEdges = bulidObjects(rs);
  62. for(int i = 0; i < listEdges.size(); i++)
  63. {
  64. AddressVertex source = listEdges.get(i).getSource();
  65. AddressVertex destination = listEdges.get(i).getDestination();
  66. int weight = listEdges.get(i).getDistance();
  67. g.AddEdge(source, destination, weight);
  68. }
  69. } catch (Exception e) {
  70. throw new DataAccessException("Kan ikke bygge graph", e);
  71. }
  72. return g;
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. // OTHER CLASS
  80.  
  81. public class AddressDB implements AddressDBIF {
  82. private DeliveryDBIF deliveryDB;
  83. private DistanceDBIF distanceDB;
  84.  
  85. private static final String GET_MAP = "select MapAddress.address, DeliveryOrder.mapAddressIdFk from MapAddress, DeliveryOrder where DeliveryOrder.mapAddressIdFk = ? and DeliveryOrder.mapAddressIdFk = mapAddressId";
  86. private PreparedStatement getMapPS;
  87.  
  88. private static final String FIND_ALL = "Select mapAddressId, address, postalCode from MapAddress";
  89. private PreparedStatement findAllPs;
  90.  
  91. private static final String FIND_ID = FIND_ALL + " where mapAddressId = ?";
  92. private PreparedStatement findIdPs;
  93.  
  94. public AddressDB() throws DataAccessException {
  95. init();
  96. this.deliveryDB = new DeliveryDB(this);
  97. this.distanceDB = new DistanceDB(this);
  98. }
  99.  
  100. public AddressDB(DeliveryDB deliverDB) throws DataAccessException {
  101. init();
  102. this.deliveryDB = deliveryDB;
  103. }
  104.  
  105. public AddressDB(DistanceDB distanceDB) throws DataAccessException
  106. {
  107. init();
  108. this.distanceDB = distanceDB;
  109. }
  110.  
  111.  
  112. private void init() throws DataAccessException{
  113. Connection con = DBConnection.getInstance().getConnection();
  114. try {
  115. getMapPS = con.prepareStatement(GET_MAP);
  116. findAllPs = con.prepareStatement(FIND_ALL);
  117. findIdPs = con.prepareStatement(FIND_ID);
  118. } catch (Exception e) {
  119. throw new DataAccessException("Mistake in init deliveryDB", e);
  120. }
  121. }
  122.  
  123. private AddressVertex bulidObject(ResultSet rs, boolean fullAsso) throws DataAccessException {
  124. AddressVertex address = new AddressVertex();
  125. try {
  126. address.setAddressId(rs.getInt("mapAddressId"));
  127. address.setAddress(rs.getString("address"));
  128. if (fullAsso)
  129. {
  130. // Person skal findes på deliveryOrder, den kan revudere i anden
  131. // use case. Edges addes på Når man bygger en graph.
  132. }
  133. } catch (Exception e) {
  134. // TODO: handle exception
  135. }
  136. return address;
  137. }
  138.  
  139. private List<AddressVertex> bulidObjects(ResultSet rs) throws DataAccessException {
  140. List<AddressVertex> listAddresses = new ArrayList<>();
  141. try {
  142. while (rs.next()) {
  143. AddressVertex currAddress = bulidObject(rs, false);
  144. listAddresses.add(currAddress);
  145. }
  146. } catch (SQLException e) {
  147. throw new DataAccessException("Could not bulid objects", e);
  148. }
  149. return listAddresses;
  150. }
  151.  
  152. public List<DeliveryOrder> bulidVodskov() throws DataAccessException
  153. {
  154. ResultSet rs;
  155. try {
  156. rs = findAllPs.executeQuery();
  157. List<AddressVertex> listAddresses = bulidObjects(rs);
  158. Graph g = new Graph(false);
  159. for(int i = 0; i < listAddresses.size(); i++)
  160. {
  161. System.out.println(listAddresses.get(i).getAddress());
  162. g.AddVertex(listAddresses.get(i));
  163. }
  164.  
  165. Graph intimeGraph = distanceDB.getGraph(g, listAddresses);
  166.  
  167. Dijkstra dij = new Dijkstra(intimeGraph);
  168.  
  169. List<DeliveryOrder> deliveries = deliveryDB.findDeliveries();
  170.  
  171. NearstNeighbor nn = new NearstNeighbor(dij);
  172.  
  173. DeliveryOrder deliveryStart = deliveries.get(1);
  174.  
  175. deliveries.remove(deliveryStart);
  176.  
  177. // get a sorted fastest path
  178. List<DeliveryOrder> sos = nn.method(deliveries, deliveries.get(1));
  179. return sos;
  180. } catch (SQLException e) {
  181. throw new DataAccessException("could not bulid object", e);
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement