package com.lbslocal.api.view;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lbslocal.master.common.ConfigurationManager;
import com.lbslocal.api.revgeo.exception.RevGeoException;
import com.lbslocal.api.utils.CommonRest;
import com.lbslocal.api.utils.CommonRest.ObjectsHelper;
import com.lbslocal.api.utils.CommonRest.PostHelper;
import com.lbslocal.cc.objects.v0.common.MapInfo;
import com.lbslocal.cc.objects.v0.common.Point;
import com.lbslocal.cc.objects.v3.route.RoadType;
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.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.Vehicle;
import com.lbslocal.cc.view.CRouteAPI;
public class Route extends HttpServlet
{
private static final long serialVersionUID = 1L;
public Route()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
ConfigurationManager cm = ConfigurationManager.getInstance();
int id_licenca = Integer.parseInt(cm.getAppSettings("ID"));
CRouteAPI cRoute = new CRouteAPI(
cm.getAppSettings("MAQUINA"),
cm.getAppSettings("SERVER"),
11188,
cm.getAppSettings("OUTPUT"),
cm.getAppSettings("DNS"),
cm.getAppSettings("M_BASEDECARTA_ROTAS"),
cm.getAppSettings("M_BASEDECARTA_MAPAS"));
CommonRest cr = new CommonRest();
PostHelper ph = cr.getDataFromPost(request);
String strLicenca = cr.getFieldFromXML(ph.XMLFile, "token");
try {
id_licenca = Integer.parseInt(strLicenca);
} catch (Exception e) {
}
if (ph.fields[0].equals("getRoute"))
{
String fullCoords = cr.getFieldFromXML(ph.XMLFile, "coords");
RouteInfo ri = new RouteInfo();
ObjectsHelper[] routeStopDependencyObjects = new ObjectsHelper[3];
routeStopDependencyObjects[0] = cr.new ObjectsHelper();
routeStopDependencyObjects[0].object = new Point();
routeStopDependencyObjects[0].alias = "point";
routeStopDependencyObjects[1] = cr.new ObjectsHelper();
routeStopDependencyObjects[1].object = new RouteStop();
routeStopDependencyObjects[1].alias = "RouteStop";
routeStopDependencyObjects[2] = cr.new ObjectsHelper();
routeStopDependencyObjects[2].object = new RouteStop[0];
routeStopDependencyObjects[2].alias = "rs";
ObjectsHelper[] routeOptionsDependencyObjects = new ObjectsHelper[7];
routeOptionsDependencyObjects[0] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[0].object = new RouteDetails();
routeOptionsDependencyObjects[0].alias = "routeDetails";
routeOptionsDependencyObjects[1] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[1].object = new Vehicle();
routeOptionsDependencyObjects[1].alias = "vehicle";
routeOptionsDependencyObjects[2] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[2].object = new RouteLine[0];
routeOptionsDependencyObjects[2].alias = "routeLine";
routeOptionsDependencyObjects[3] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[3].object = new RouteLine();
routeOptionsDependencyObjects[3].alias = "RouteLine";
routeOptionsDependencyObjects[4] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[4].object = new Point[0];
routeOptionsDependencyObjects[4].alias = "barrierPoints";
routeOptionsDependencyObjects[5] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[5].object = new String[0];
routeOptionsDependencyObjects[5].alias = "barriersList";
routeOptionsDependencyObjects[6] = cr.new ObjectsHelper();
routeOptionsDependencyObjects[6].object = new String[0];
routeOptionsDependencyObjects[6].alias = "poiRoute";
RouteStop[] rs = (RouteStop[]) cr.getObjectFromXML(ph.XMLFile, "rs", RouteStop.class, routeStopDependencyObjects);
RouteOptions ro = (RouteOptions) cr.getObjectFromXML(ph.XMLFile, "ro", RouteOptions.class, routeOptionsDependencyObjects);
try
{
ri = cRoute.getRoute(rs, ro, id_licenca);
if (fullCoords.equals("true")){
ri.setCoords(cRoute.getRouteFullCoords(ri.getRouteId()));
}
}
catch (Exception e)
{
e.printStackTrace();
}
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
ObjectsHelper[] dependencyObjects = new ObjectsHelper[6];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new MapInfo();
dependencyObjects[0].alias = "MapInfo";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new SegmentDescription();
dependencyObjects[1].alias = "SegmentDescription";
dependencyObjects[2] = cr.new ObjectsHelper();
dependencyObjects[2].object = new RouteTotals();
dependencyObjects[2].alias = "routeTotals";
dependencyObjects[3] = cr.new ObjectsHelper();
dependencyObjects[3].object = new RouteSummary();
dependencyObjects[3].alias = "routeSummary";
dependencyObjects[4] = cr.new ObjectsHelper();
dependencyObjects[4].object = new RoadType();
dependencyObjects[4].alias = "roadType";
dependencyObjects[5] = cr.new ObjectsHelper();
dependencyObjects[5].object = new RouteInfo();
dependencyObjects[5].alias = "RouteInfo";
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(ri, dependencyObjects) + "");
}
if (ph.fields[0].equals("setRouteFence"))
{
String routeId = cr.getFieldFromXML(ph.XMLFile, "routeId");
double meters = Double.parseDouble(cr.getFieldFromXML(ph.XMLFile, "meters"));
boolean generate = true;
try
{
generate = cRoute.setRouteFence(routeId, meters, id_licenca);
}
catch (Exception e)
{
generate = false;
e.printStackTrace();
}
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n<setRouteFence><FenceGenerate>" + generate + "</FenceGenerate></setRouteFence>");
}
if (ph.fields[0].equals("checkFence"))
{
boolean dentro = false;
String routeId = cr.getFieldFromXML(ph.XMLFile, "routeId");
Point point = (Point) cr.getObjectFromXML(ph.XMLFile, "point", Point.class, null);
try
{
dentro = cRoute.checkFence(routeId, point, id_licenca);
}
catch (Exception e)
{
e.printStackTrace();
}
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n<checkFence><insideFence>" + dentro + "</insideFence></checkFence>");
}
}
catch (NumberFormatException e)
{
response.getWriter().write(RevGeoException.getErrorMessage(RevGeoException.NUMBER_ERROR));
}
}
}