Package com.lbslocal.cc.view

Source Code of com.lbslocal.cc.view.CRouteAPI

package com.lbslocal.cc.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


import com.lbslocal.cc.common.EncodedPolyline;
import com.lbslocal.cc.common.Functions;
import com.lbslocal.cc.common.PolylineEncoder;
import com.lbslocal.cc.common.dataBase;
import com.lbslocal.cc.core.CommonRoute;
import com.lbslocal.cc.objects.v0.common.City;
import com.lbslocal.cc.objects.v0.common.Extent;
import com.lbslocal.cc.objects.v0.common.MapInfo;
import com.lbslocal.cc.objects.v0.common.MapOptions;
import com.lbslocal.cc.objects.v0.common.MapSize;
import com.lbslocal.cc.objects.v0.common.Point;
import com.lbslocal.cc.objects.v3.route.POIDetails;
import com.lbslocal.cc.objects.v3.route.RoadType;
import com.lbslocal.cc.objects.v3.route.RouteArray;
import com.lbslocal.cc.objects.v3.route.RouteDetails;
import com.lbslocal.cc.objects.v3.route.RouteInfo;
import com.lbslocal.cc.objects.v3.route.RouteLine;
import com.lbslocal.cc.objects.v3.route.RouteOptions;
import com.lbslocal.cc.objects.v3.route.RouteProximityInfo;
import com.lbslocal.cc.objects.v3.route.RouteProximityOptions;
import com.lbslocal.cc.objects.v3.route.RouteStop;
import com.lbslocal.cc.objects.v3.route.RouteSummary;
import com.lbslocal.cc.objects.v3.route.RouteTotals;
import com.lbslocal.cc.objects.v3.route.SegmentDescription;
import com.lbslocal.cc.objects.v3.route.TollFeeDetails;
import com.lbslocal.cc.objects.v3.route.Vehicle;
import com.lbslocal.master.common.Utils;
import com.sun.rowset.CachedRowSetImpl;

public class CRouteAPI {
  private String _server;
  private int _port;
  private String _maquina;
  private String _output;
  private String _dns;
  private String _dataSource;
  private String _dataSourceMapa;

  long iniDate;

  public CRouteAPI(String maquina, String server, int port, String output, String dns, String dataSource, String dataSourceMapa) {
    this._maquina = maquina;
    this._server = server;
    this._port = port;
    this._output = output;
    this._dns = dns;
    this._dataSource = dataSource;
    this._dataSourceMapa = dataSourceMapa;
  }

  public static String convertFromDecarta(int precision, String coords) {
    List<Point> ptList = new ArrayList<Point>();

    int y = 0;

    String format = "0.";

    for (int x = 0; x < precision; x++)
      format += "0";

    String[] co = coords.split(",");

    for (int z = 0; z < co.length; z++) {

      if (((co[z + 1]).toString().replace("|", "").length() < (precision + 2))) {
        DecimalFormat df = new DecimalFormat(format.replace("0", "#"));
        df.setMinimumFractionDigits(precision);

        Point pt = new Point();
        pt.setY(Double.parseDouble(df.format(
            (Integer.parseInt(df.format(ptList.get(y).getY()).replace(",", "").replace(".", "")) + Integer.parseInt(co[z]))
                * Math.pow(10, -precision)).replace(",", ".")));
        pt.setX(Double.parseDouble(df.format(
            (Integer.parseInt(df.format(ptList.get(y).getX()).replace(",", "").replace(".", "")) + Integer.parseInt(co[z + 1]))
                * Math.pow(10, -precision)).replace(",", ".")));

        ptList.add(pt);

        y++;
      } else {
        DecimalFormat df = new DecimalFormat(format.replace("0", "#"));
        df.setMinimumFractionDigits(precision);

        Point pt = new Point();
        pt.setY(Double.parseDouble(df.format(Integer.parseInt(co[z]) * Math.pow(10, -precision)).replace(",", ".")));
        pt.setX(Double.parseDouble(df.format(Integer.parseInt(co[z + 1]) * Math.pow(10, -precision)).replace(",", ".")));

        ptList.add(pt);
      }

      z++;
    }

    String coordsRet = "";
    for (int x = 0; x < ptList.size(); x++) {
      coordsRet += String.valueOf(ptList.get(x).getX()) + " " + String.valueOf(ptList.get(x).getY()) + ";";
    }

    return coordsRet;
  }

  public RouteInfo getRoute(RouteStop[] rs, RouteOptions ro, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();
    RouteProximityInfo rpi = new RouteProximityInfo();
    RouteInfo ri = new RouteInfo();

    try {

      Validation("getRoute", idLicenca, rs, ro);

      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());
      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      rpi = r.getRoute(idLicenca, rs, routeOptions, CommonRoute._getRoute);
     
      ri.setMapInfo(new MapInfo());
      ri.getMapInfo().setExtent(new Extent());
      ri.getMapInfo().getExtent().setXMax(rpi.getMapInfo().getExtent().getXMax());
      ri.getMapInfo().getExtent().setXMin(rpi.getMapInfo().getExtent().getXMin());
      ri.getMapInfo().getExtent().setYMax(rpi.getMapInfo().getExtent().getYMax());
      ri.getMapInfo().getExtent().setYMin(rpi.getMapInfo().getExtent().getYMin());
      ri.getMapInfo().setUrl(rpi.getMapInfo().getUrl());

      ri.setRoadType(new RoadType());
      ri.setRoadType(rpi.getRoadType());

      ri.setRouteId(rpi.getRouteId());
      if (rpi.getRouteSummary() != null) {
        ri.setRouteSummary(new RouteSummary[rpi.getRouteSummary().length]);

        for (int x = 0; x < rpi.getRouteSummary().length; x++) {
          ri.getRouteSummary()[x] = new RouteSummary();
          ri.getRouteSummary()[x].setDescription(rpi.getRouteSummary()[x].getDescription());
          ri.getRouteSummary()[x].setDistance(rpi.getRouteSummary()[x].getDistance());
          ri.getRouteSummary()[x].setPoint(new Point());
          ri.getRouteSummary()[x].setPoint(rpi.getRouteSummary()[x].getPoint());
        }
      }
      ri.setRouteTotals(new RouteTotals());
      ri.setRouteTotals(rpi.getRouteTotals());
      double totalTime = 0;
      if (rpi.getSegDescription() != null) {
        ri.setSegDescription(new SegmentDescription[rpi.getSegDescription().length]);

        for (int x = 0; x < rpi.getSegDescription().length; x++) {
          ri.getSegDescription()[x] = new SegmentDescription();
          ri.getSegDescription()[x].setCity(new City());
          ri.getSegDescription()[x].getCity().setName(rpi.getSegDescription()[x].getCity().getName());
          ri.getSegDescription()[x].getCity().setState(rpi.getSegDescription()[x].getCity().getState());

          ri.getSegDescription()[x].setCommand(rpi.getSegDescription()[x].getCommand());
          ri.getSegDescription()[x].setCumulativeDistance(rpi.getSegDescription()[x].getCumulativeDistance());
          ri.getSegDescription()[x].setDescription(rpi.getSegDescription()[x].getDescription());
          ri.getSegDescription()[x].setDistance(rpi.getSegDescription()[x].getDistance());
          ri.getSegDescription()[x].setPoint(new Point());
          ri.getSegDescription()[x].getPoint().setX(rpi.getSegDescription()[x].getPoint().getX());
          ri.getSegDescription()[x].getPoint().setY(rpi.getSegDescription()[x].getPoint().getY());
          ri.getSegDescription()[x].setPoiRoute(rpi.getSegDescription()[x].getPoiRoute());

          ri.getSegDescription()[x].setTime(rpi.getSegDescription()[x].getTime());
         
          if (!rpi.getSegDescription()[x].getTime().equals(""))
            totalTime += Double.parseDouble(rpi.getSegDescription()[x].getTime().substring(0,rpi.getSegDescription()[x].getTime().indexOf(' ')));
         
//          String teste="";
//          if (!ri.getSegDescription()[x].getFullCoords().equals(""))
//            teste = convertFromDecarta(7, ri.getSegDescription()[x].getFullCoords());
         
          int z = 0;

          if (rpi.getSegDescription()[x].getPoiRouteDetails() != null) {
            ri.getSegDescription()[x].setPoiRouteDetails(new POIDetails[rpi.getSegDescription()[x].getPoiRouteDetails().length]);

            for (z = 0; z < rpi.getSegDescription()[x].getPoiRouteDetails().length; z++) {
              ri.getSegDescription()[x].getPoiRouteDetails()[z] = new POIDetails();
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setAddressInfo(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getAddressInfo());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setClientID(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getClientID());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setName(rpi.getSegDescription()[x].getPoiRouteDetails()[z].getName());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setPoint(new Point());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().setX(
                  rpi.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().getX());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().setY(
                  rpi.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().getY());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setSource(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getSource());
            }
          }

          ri.getSegDescription()[x].setRoadType(rpi.getSegDescription()[x].getRoadType());
          ri.getSegDescription()[x].setTollFee(rpi.getSegDescription()[x].getTollFee());

          if (!Functions.IsNullOrEmpty(rpi.getSegDescription()[x].getTollFeeDetails().getAddress())) {
            ri.getSegDescription()[x].setTollFeeDetails(new TollFeeDetails());
            ri.getSegDescription()[x].getTollFeeDetails().setAddress(rpi.getSegDescription()[x].getTollFeeDetails().getAddress());
            ri.getSegDescription()[x].getTollFeeDetails().setConcession(rpi.getSegDescription()[x].getTollFeeDetails().getConcession());
            ri.getSegDescription()[x].getTollFeeDetails().setDirection(rpi.getSegDescription()[x].getTollFeeDetails().getDirection());
            ri.getSegDescription()[x].getTollFeeDetails().setName(rpi.getSegDescription()[x].getTollFeeDetails().getName());
            ri.getSegDescription()[x].getTollFeeDetails().setPhone(rpi.getSegDescription()[x].getTollFeeDetails().getPhone());
            ri.getSegDescription()[x].getTollFeeDetails().setPrice(rpi.getSegDescription()[x].getTollFeeDetails().getPrice());
            ri.getSegDescription()[x].getTollFeeDetails().setPricePerAxle(
                rpi.getSegDescription()[x].getTollFeeDetails().getPricePerAxle());
            ri.getSegDescription()[x].getTollFeeDetails().setState(rpi.getSegDescription()[x].getTollFeeDetails().getState());
          }
        }
      }
      ri.getRouteTotals().setTotalTime(CommonRoute.getTotalTime(totalTime));
      Functions.gravarLogUnique(idLicenca, CommonRoute._getRoute, rs.length, iniDate, 0);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ri;
  }

  public EncodedPolyline getRouteCoordsGoogleEncoded(String routeID, Extent ext, MapSize ms, int idLicenca, String dataSource) throws Exception {
    iniDate = System.currentTimeMillis();

    EncodedPolyline ep = null;

    try {
      ext = doZoom(ext, .5);
      ms.setHeight((int) (ms.getHeight() + ms.getHeight() * .5));
      ms.setWidth((int) (ms.getWidth() + ms.getWidth() * .5));

      Point[] pt = new Point[0];
      PolylineEncoder pe = new PolylineEncoder();
      PolylineEncoder.PointsToTrack(pt);

      ep = pe.Encode(PolylineEncoder.PointsToTrack(pt));
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ep;
  }

  Extent doZoom(Extent ext, double percZoom) {
    if (percZoom == 0)
      return ext; // n�o faz nada

    if (percZoom > 1 || percZoom < -1)
      percZoom = 0;

    double deltaX = ((ext.getXMax() - ext.getXMin()) * percZoom) / 2;
    double deltaY = ((ext.getYMax() - ext.getYMin()) * percZoom) / 2;

    if (deltaX < 0)
      deltaX = -deltaX;
    if (deltaY < 0)
      deltaY = -deltaY;

    if (percZoom > 0) // zoomOut - aumenta o extent
    {
      ext.setXMax(ext.getXMax() + deltaX);
      ext.setXMin(ext.getXMin() - deltaX);
      ext.setYMax(ext.getYMax() + deltaY);
      ext.setYMin(ext.getYMin() - deltaY);
    } else
    // zoomIn - diminui o extent
    {
      ext.setXMax(ext.getXMax() - deltaX);
      ext.setXMin(ext.getXMin() + deltaX);
      ext.setYMax(ext.getYMax() - deltaY);
      ext.setYMin(ext.getYMin() + deltaY);
    }

    return ext;
  }
  public String getRouteFullCoords(String routeID) throws Exception {
    iniDate = System.currentTimeMillis();

    String ret;

    try {
      //CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      ret = CommonRoute.getRouteFullCoords(routeID);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ret;
  }
  public String getRouteCoords(String routeID, Extent ext, MapSize ms, int idLicenca, String dataSource) throws Exception {
    iniDate = System.currentTimeMillis();

    String ret;

    try {
      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      ret = r.getRouteCoords(routeID, (ext.getXMax() - ext.getXMin()) / ms.getWidth(), ext, idLicenca, dataSource);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ret;
  }

  /*
   * Gera o resumo da rota
   */
  public RouteSummary[] getRouteSummary(RouteStop[] rs, RouteOptions ro, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();

    RouteSummary[] routeSummary = new RouteSummary[0];

    try {
      Validation("getRouteSummary", idLicenca, rs, ro);
      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());

      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      routeSummary = r.getRouteSummary(idLicenca, rs, routeOptions, CommonRoute._getRouteSummary);

      Functions.gravarLogUnique(idLicenca, CommonRoute._getRouteSummary, rs.length, iniDate, 0);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }
    return routeSummary;
  }

  /*
   * Gera a descri��o da rota
   */
  public SegmentDescription[] getRouteDescription(RouteStop[] rs, RouteOptions ro, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();

    SegmentDescription[] segDescription = new SegmentDescription[0];

    try {
      Validation("getRouteDescription", idLicenca, rs, ro);
      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());

      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      segDescription = r.getRouteDescription(idLicenca, rs, routeOptions, CommonRoute._getRouteDescription);

      Functions.gravarLogUnique(idLicenca, CommonRoute._getRouteDescription, rs.length, iniDate, 0);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return segDescription;
  }

  /*
   * Calcula os totais da rota informada
   */
  public RouteTotals getRouteTotals(RouteStop[] rs, RouteOptions ro, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();

    RouteTotals rt = new RouteTotals();

    try {
      Validation("getRouteTotals", idLicenca, rs, ro);
      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());

      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      rt = r.getRouteTotals(idLicenca, rs, routeOptions, CommonRoute._getRouteTotals);

      Functions.gravarLogUnique(idLicenca, CommonRoute._getRouteTotals, rs.length, iniDate, 0);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }
    return rt;
  }

  public RouteTotals[] getRouteTotalsBatch(RouteArray[] ra, RouteOptions ro, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();

    RouteTotals[] rt = new RouteTotals[ra.length];

    try {
      if (ra.length < 1)
        throw (new Exception("getRouteTotalsBatch: Deve haver ao menos uma origem e um destino. Quantidade de pontos informados: "
            + ra.length));

      if (ra.length > 30)
        throw (new Exception("getRouteTotalsBatch: Numero de pontos de parada excedeu o limite(30). Quantidade de pontos informados: "
            + ra.length));

      for (int i = 0; i < ra.length; i++) {
        RouteStop[] rs = new RouteStop[2];
        rs[0] = ra[i].getOrigin();
        rs[1] = ra[i].getDest();
      }

      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());

      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      rt = r.getRouteTotalsBatch(idLicenca, ra, routeOptions);

      Functions.gravarLogUnique(idLicenca, CommonRoute._getRouteTotals, ra.length, iniDate, 0);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return rt;
  }

  // valida numero do contrato com rela��o dos poiRoute
  private boolean validadePoiRoute(int idLicenca, String[] poiRoute) throws SQLException {
    String sql = "";

    String poiDataSource = "','";

    for (int i = 0; i < poiRoute.length; i++)
      if (poiRoute[i].length() > 0)
        poiDataSource += poiRoute[i] + "','";
      else
        return false;

    if (poiDataSource.length() > 3)
      poiDataSource = poiDataSource.substring(2, poiDataSource.length() - 2);
    else
      return false;

    sql = "SELECT poiRoute ";
    sql += " FROM TB_CLIENTE_POIROUTE as a, TB_POIROUTE as b";
    sql += " WHERE a.idPoiroute = b.id and a.idLicenca = " + idLicenca + " and ";
    sql += " (b.poiRoute in (" + poiDataSource + "))";

    Connection con = dataBase.getConnection("DADOS_WEBSERVICES");
    Statement st = con.createStatement();

    ResultSet rr = st.executeQuery(sql.toUpperCase());
    CachedRowSetImpl crs = new CachedRowSetImpl();

    crs.populate(rr);

    if (crs.size() == poiRoute.length)
      return true;
    else
      return false;

  }

  /*
   * Calcula a rota e gera a imagem do mapa
   */
  public RouteInfo getRouteWithMap(RouteStop[] rs, RouteOptions ro, MapOptions mo, int idLicenca) throws Exception {
    iniDate = System.currentTimeMillis();

    RouteInfo ri = new RouteInfo();
    RouteProximityInfo rpi = new RouteProximityInfo();

    try {
      Validation("getRouteWithMap", idLicenca, rs, ro);
      RouteProximityOptions routeOptions = new RouteProximityOptions();

      routeOptions.setLanguage(ro.getLanguage());
      routeOptions.setRouteDetails(new RouteDetails());
      routeOptions.getRouteDetails().setDescriptionType(ro.getRouteDetails().getDescriptionType());
      routeOptions.getRouteDetails().setOptimizeRoute(ro.getRouteDetails().isOptimizeRoute());
      routeOptions.getRouteDetails().setPoiRoute(ro.getRouteDetails().getPoiRoute());
      routeOptions.getRouteDetails().setRouteType(ro.getRouteDetails().getRouteType());

      if (ro.getRouteLine() != null) {
        routeOptions.setRouteLine(new RouteLine[ro.getRouteLine().length]);

        for (int x = 0; x < ro.getRouteLine().length; x++) {
          routeOptions.getRouteLine()[x] = new RouteLine();
          routeOptions.getRouteLine()[x].setRGB(ro.getRouteLine()[x].getRGB());
          routeOptions.getRouteLine()[x].setTransparency(ro.getRouteLine()[x].getTransparency());
          routeOptions.getRouteLine()[x].setWidth(ro.getRouteLine()[x].getWidth());
        }
      }

      routeOptions.setVehicle(new Vehicle());
      routeOptions.getVehicle().setAverageConsumption(ro.getVehicle().getAverageConsumption());
      routeOptions.getVehicle().setAverageSpeed(ro.getVehicle().getAverageSpeed());
      routeOptions.getVehicle().setFuelPrice(ro.getVehicle().getFuelPrice());
      routeOptions.getVehicle().setTankCapacity(ro.getVehicle().getTankCapacity());
      routeOptions.getVehicle().setTollFeeCat(ro.getVehicle().getTollFeeCat());

      CommonRoute r = new CommonRoute(_maquina, _server, _port, _output, _dns, _dataSource, _dataSourceMapa);
      rpi = r.getRouteWithMap(idLicenca, CommonRoute._getRouteWithMap, rs, routeOptions, mo);

      ri.setMapInfo(new MapInfo());
      ri.getMapInfo().setExtent(new Extent());
      ri.getMapInfo().getExtent().setXMax(rpi.getMapInfo().getExtent().getXMax());
      ri.getMapInfo().getExtent().setXMin(rpi.getMapInfo().getExtent().getXMin());
      ri.getMapInfo().getExtent().setYMax(rpi.getMapInfo().getExtent().getYMax());
      ri.getMapInfo().getExtent().setYMin(rpi.getMapInfo().getExtent().getYMin());
      ri.getMapInfo().setUrl(rpi.getMapInfo().getUrl());

      ri.setRoadType(new RoadType());
      ri.setRoadType(rpi.getRoadType());

      ri.setRouteId(rpi.getRouteId());

      if (rpi.getRouteSummary() != null) {
        ri.setRouteSummary(new RouteSummary[rpi.getRouteSummary().length]);

        for (int x = 0; x < rpi.getRouteSummary().length; x++) {
          ri.getRouteSummary()[x] = new RouteSummary();
          ri.getRouteSummary()[x].setDescription(rpi.getRouteSummary()[x].getDescription());
          ri.getRouteSummary()[x].setDistance(rpi.getRouteSummary()[x].getDistance());
          ri.getRouteSummary()[x].setPoint(new Point());
          ri.getRouteSummary()[x].setPoint(rpi.getRouteSummary()[x].getPoint());
        }
      }

      ri.setRouteTotals(new RouteTotals());
      ri.setRouteTotals(rpi.getRouteTotals());

      if (rpi.getSegDescription() != null) {
        ri.setSegDescription(new SegmentDescription[rpi.getSegDescription().length]);

        for (int x = 0; x < rpi.getSegDescription().length; x++) {
          ri.getSegDescription()[x] = new SegmentDescription();
          ri.getSegDescription()[x].setCity(new City());
          ri.getSegDescription()[x].getCity().setName(rpi.getSegDescription()[x].getCity().getName());
          ri.getSegDescription()[x].getCity().setState(rpi.getSegDescription()[x].getCity().getState());

          ri.getSegDescription()[x].setCommand(rpi.getSegDescription()[x].getCommand());
          ri.getSegDescription()[x].setCumulativeDistance(rpi.getSegDescription()[x].getCumulativeDistance());
          ri.getSegDescription()[x].setDescription(rpi.getSegDescription()[x].getDescription());
          ri.getSegDescription()[x].setDistance(rpi.getSegDescription()[x].getDistance());
          ri.getSegDescription()[x].setPoint(new Point());
          ri.getSegDescription()[x].getPoint().setX(rpi.getSegDescription()[x].getPoint().getX());
          ri.getSegDescription()[x].getPoint().setY(rpi.getSegDescription()[x].getPoint().getY());
          ri.getSegDescription()[x].setPoiRoute(rpi.getSegDescription()[x].getPoiRoute());

          int z = 0;

          if (rpi.getSegDescription()[x].getPoiRouteDetails() != null) {
            ri.getSegDescription()[x].setPoiRouteDetails(new POIDetails[rpi.getSegDescription()[x].getPoiRouteDetails().length]);

            for (z = 0; z < rpi.getSegDescription()[x].getPoiRouteDetails().length; z++) {
              ri.getSegDescription()[x].getPoiRouteDetails()[z] = new POIDetails();
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setAddressInfo(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getAddressInfo());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setClientID(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getClientID());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setName(rpi.getSegDescription()[x].getPoiRouteDetails()[z].getName());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setPoint(new Point());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().setX(
                  rpi.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().getX());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().setY(
                  rpi.getSegDescription()[x].getPoiRouteDetails()[z].getPoint().getY());
              ri.getSegDescription()[x].getPoiRouteDetails()[z].setSource(rpi.getSegDescription()[x].getPoiRouteDetails()[z]
                  .getSource());
            }
          }

          ri.getSegDescription()[x].setRoadType(rpi.getSegDescription()[x].getRoadType());
          ri.getSegDescription()[x].setTollFee(rpi.getSegDescription()[x].getTollFee());

          if (!Functions.IsNullOrEmpty(rpi.getSegDescription()[x].getTollFeeDetails().getAddress())) {
            ri.getSegDescription()[x].setTollFeeDetails(new TollFeeDetails());
            ri.getSegDescription()[x].getTollFeeDetails().setAddress(rpi.getSegDescription()[x].getTollFeeDetails().getAddress());
            ri.getSegDescription()[x].getTollFeeDetails().setConcession(rpi.getSegDescription()[x].getTollFeeDetails().getConcession());
            ri.getSegDescription()[x].getTollFeeDetails().setDirection(rpi.getSegDescription()[x].getTollFeeDetails().getDirection());
            ri.getSegDescription()[x].getTollFeeDetails().setName(rpi.getSegDescription()[x].getTollFeeDetails().getName());
            ri.getSegDescription()[x].getTollFeeDetails().setPhone(rpi.getSegDescription()[x].getTollFeeDetails().getPhone());
            ri.getSegDescription()[x].getTollFeeDetails().setPrice(rpi.getSegDescription()[x].getTollFeeDetails().getPrice());
            ri.getSegDescription()[x].getTollFeeDetails().setPricePerAxle(
                rpi.getSegDescription()[x].getTollFeeDetails().getPricePerAxle());
            ri.getSegDescription()[x].getTollFeeDetails().setState(rpi.getSegDescription()[x].getTollFeeDetails().getState());
          }
        }
      }

      Functions.gravarLogUnique(idLicenca, CommonRoute._getRouteWithMap, rs.length, iniDate, 0);
    } catch (Exception e) {
      Utils.LogError("CC", "getRouteWithMap",
          " Maquina : " + _maquina + " | Server : " + _server + " | ex: " + e.getMessage() + "|" + e.getStackTrace());

      throw (new Exception(e.getMessage()));
    }

    return ri;
  }

  private void Validation(String Metodo, int idLicenca, RouteStop[] rs, RouteOptions ro) throws Exception {
    if (rs.length < 2)
      throw (new Exception(Metodo + ": Deve haver ao menos dois pontos de parada. Quantidade de pontos informados: " + rs.length));

    if (rs.length > 100)
      throw (new Exception(Metodo + ": Numero de pontos de parada excedeu o limite(100). Quantidade de pontos informados: " + rs.length));

    if (ro.getRouteLine() != null) {
      if (ro.getRouteLine().length > 2)
        throw (new Exception(Metodo + ": Numero de Lines excedeu o limite(2). Quantidade de Lines informados: " + ro.getRouteLine().length));
    }

    if (ro.getRouteDetails().getPoiRoute() != null)
      if (ro.getRouteDetails().getPoiRoute().length > 0)
        if (!validadePoiRoute(idLicenca, ro.getRouteDetails().getPoiRoute()))
          throw (new Exception(Metodo + ": poiRoute informado invalido."));
  }

  public boolean setRouteFence(String routeID, double meters, int id_licenca) throws Exception {
    iniDate = System.currentTimeMillis();

    boolean ret;

    try {
      ret = CommonRoute.setRouteFence(routeID, meters);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ret;
   
  }

  public boolean checkFence(String routeId, Point point, int id_licenca) throws Exception {
    boolean ret;

    try {
      ret = CommonRoute.checkFence(routeId, point);
    } catch (Exception e) {
      throw (new Exception(e.getMessage()));
    }

    return ret;
   
  }
}
TOP

Related Classes of com.lbslocal.cc.view.CRouteAPI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.