Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DistanceDB implements DistanceDBIF {
- private AddressDBIF addressDB;
- private static final String FIND_DISTANCE = "Select distance, destinationAddressIdFk, sourceAddressIdFk from Distance";
- private PreparedStatement findDistancePs;
- public DistanceDB() throws DataAccessException {
- init();
- this.addressDB = new AddressDB();
- }
- public DistanceDB(AddressDB addressDB) throws DataAccessException {
- init();
- this.addressDB = addressDB;
- }
- private void init() throws DataAccessException {
- Connection con = DBConnection.getInstance().getConnection();
- try {
- findDistancePs = con.prepareStatement(FIND_DISTANCE);
- } catch (SQLException e) {
- throw new DataAccessException("Can not connect to distance", e);
- }
- }
- public DistanceEdge bulidObject(ResultSet rs, boolean fullAsso) throws DataAccessException {
- DistanceEdge distance = new DistanceEdge();
- AddressVertex destination = null;
- AddressVertex source = null;
- try {
- source = addressDB.findId(rs.getInt("sourceAddressIdFk"));
- destination = addressDB.findId(rs.getInt("destinationAddressIdFk"));
- distance.setDistance(rs.getInt("distance"));
- distance.setSource(source);
- distance.setDestination(destination);
- return distance;
- } catch (SQLException e) {
- throw new DataAccessException("Mistake in bulidObject", e);
- }
- }
- public List<DistanceEdge> bulidObjects(ResultSet rs) throws DataAccessException {
- List<DistanceEdge> listEdges = new ArrayList<>();
- try {
- while (rs.next()) {
- DistanceEdge currEdge = bulidObject(rs, false);
- listEdges.add(currEdge);
- }
- } catch (SQLException e) {
- throw new DataAccessException("Could not bulid objects", e);
- }
- return listEdges;
- }
- public Graph getGraph(Graph graph, List<AddressVertex> listAddresses) throws DataAccessException {
- ResultSet rs;
- Graph g = graph;
- g.AddVertexRange();
- try {
- rs = findDistancePs.executeQuery();
- List<DistanceEdge> listEdges = bulidObjects(rs);
- for(int i = 0; i < listEdges.size(); i++)
- {
- AddressVertex source = listEdges.get(i).getSource();
- AddressVertex destination = listEdges.get(i).getDestination();
- int weight = listEdges.get(i).getDistance();
- g.AddEdge(source, destination, weight);
- }
- } catch (Exception e) {
- throw new DataAccessException("Kan ikke bygge graph", e);
- }
- return g;
- }
- // OTHER CLASS
- public class AddressDB implements AddressDBIF {
- private DeliveryDBIF deliveryDB;
- private DistanceDBIF distanceDB;
- private static final String GET_MAP = "select MapAddress.address, DeliveryOrder.mapAddressIdFk from MapAddress, DeliveryOrder where DeliveryOrder.mapAddressIdFk = ? and DeliveryOrder.mapAddressIdFk = mapAddressId";
- private PreparedStatement getMapPS;
- private static final String FIND_ALL = "Select mapAddressId, address, postalCode from MapAddress";
- private PreparedStatement findAllPs;
- private static final String FIND_ID = FIND_ALL + " where mapAddressId = ?";
- private PreparedStatement findIdPs;
- public AddressDB() throws DataAccessException {
- init();
- this.deliveryDB = new DeliveryDB(this);
- this.distanceDB = new DistanceDB(this);
- }
- public AddressDB(DeliveryDB deliverDB) throws DataAccessException {
- init();
- this.deliveryDB = deliveryDB;
- }
- public AddressDB(DistanceDB distanceDB) throws DataAccessException
- {
- init();
- this.distanceDB = distanceDB;
- }
- private void init() throws DataAccessException{
- Connection con = DBConnection.getInstance().getConnection();
- try {
- getMapPS = con.prepareStatement(GET_MAP);
- findAllPs = con.prepareStatement(FIND_ALL);
- findIdPs = con.prepareStatement(FIND_ID);
- } catch (Exception e) {
- throw new DataAccessException("Mistake in init deliveryDB", e);
- }
- }
- private AddressVertex bulidObject(ResultSet rs, boolean fullAsso) throws DataAccessException {
- AddressVertex address = new AddressVertex();
- try {
- address.setAddressId(rs.getInt("mapAddressId"));
- address.setAddress(rs.getString("address"));
- if (fullAsso)
- {
- // Person skal findes på deliveryOrder, den kan revudere i anden
- // use case. Edges addes på Når man bygger en graph.
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- return address;
- }
- private List<AddressVertex> bulidObjects(ResultSet rs) throws DataAccessException {
- List<AddressVertex> listAddresses = new ArrayList<>();
- try {
- while (rs.next()) {
- AddressVertex currAddress = bulidObject(rs, false);
- listAddresses.add(currAddress);
- }
- } catch (SQLException e) {
- throw new DataAccessException("Could not bulid objects", e);
- }
- return listAddresses;
- }
- public List<DeliveryOrder> bulidVodskov() throws DataAccessException
- {
- ResultSet rs;
- try {
- rs = findAllPs.executeQuery();
- List<AddressVertex> listAddresses = bulidObjects(rs);
- Graph g = new Graph(false);
- for(int i = 0; i < listAddresses.size(); i++)
- {
- System.out.println(listAddresses.get(i).getAddress());
- g.AddVertex(listAddresses.get(i));
- }
- Graph intimeGraph = distanceDB.getGraph(g, listAddresses);
- Dijkstra dij = new Dijkstra(intimeGraph);
- List<DeliveryOrder> deliveries = deliveryDB.findDeliveries();
- NearstNeighbor nn = new NearstNeighbor(dij);
- DeliveryOrder deliveryStart = deliveries.get(1);
- deliveries.remove(deliveryStart);
- // get a sorted fastest path
- List<DeliveryOrder> sos = nn.method(deliveries, deliveries.get(1));
- return sos;
- } catch (SQLException e) {
- throw new DataAccessException("could not bulid object", e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement