package com.lbslocal.api.connect;
import java.util.ArrayList;
import java.util.regex.Pattern;
import com.lbslocal.master.common.ConfigurationManager;
import com.lbslocal.api.objects.TokenValidated;
import com.lbslocal.api.proxy.Proxy;
import com.lbslocal.api.utils.Common;
import com.lbslocal.cc.objects.v0.common.Extent;
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.RouteInfo;
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.view.CRouteAPI;
public class ControlRoute {
final static int getRoute = 0;
final static int getRouteTotals = 1;
final static int getRouteDescription = 2;
final static int getRouteWithMap = 3;
final static int getRouteSummary = 4;
final static int getCoords = 5;
final static int getCoordsFull = 6;
final static int checkFence = 100;
final static int setRouteFence = 101;
@SuppressWarnings("unchecked")
public static String runService(Proxy proxy, TokenValidated tk) throws Exception {
String respostaWs = "", plusRodoviaria="";
Object[] o;
if (proxy.GetService().indexOf("Rodoviaria") > -1)
{
plusRodoviaria = "_RODOVIARIA";
proxy.setService(proxy.GetService().replace("Rodoviaria", ""));
}
ConfigurationManager cm = ConfigurationManager.getInstance();
CRouteAPI route = new CRouteAPI(
cm.getAppSettings("Maquina"),
cm.getAppSettings("Server"),
11188,
cm.getAppSettings("dirOutPut"),
cm.getAppSettings("webOutPut"),
cm.getAppSettings("M_BASEDECARTA_ROTAS" + plusRodoviaria),
cm.getAppSettings("M_BASEDECARTA_MAPAS" + plusRodoviaria)
);
switch (getService(proxy.GetService()))
{
case getRoute:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { RouteStop.class, RouteOptions.class });
RouteInfo ri = route.getRoute((RouteStop[])tranformToArrayList((Object[])o[0]).toArray(new RouteStop[0]), (RouteOptions)o[1], tk.getId());
respostaWs = Common.SerializeJSObject(ri);
break;
case getRouteTotals:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { RouteStop.class, RouteOptions.class });
RouteTotals rt = route.getRouteTotals((RouteStop[])tranformToArrayList((Object[])o[0]).toArray(new RouteStop[0]), (RouteOptions)o[1], tk.getId());
respostaWs = Common.SerializeJSObject(rt);
break;
case getRouteDescription:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { RouteStop.class, RouteOptions.class });
SegmentDescription[] sd = route.getRouteDescription((RouteStop[])tranformToArrayList((Object[])o[0]).toArray(new RouteStop[0]), (RouteOptions)o[1], tk.getId());
respostaWs = Common.SerializeJSObject(sd);
break;
case getRouteWithMap:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { RouteStop.class, RouteOptions.class, MapOptions.class });
RouteInfo rwm = route.getRouteWithMap((RouteStop[])tranformToArrayList((Object[])o[0]).toArray(new RouteStop[0]), (RouteOptions)o[1], (MapOptions)o[2], tk.getId());
respostaWs = Common.SerializeJSObject(rwm);
break;
case getRouteSummary:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { RouteStop.class, RouteOptions.class });
RouteSummary[] rs = route.getRouteSummary((RouteStop[])tranformToArrayList((Object[])o[0]).toArray(new RouteStop[0]), (RouteOptions)o[1], tk.getId());
respostaWs = Common.SerializeJSObject(rs);
break;
case setRouteFence:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { String.class, Double.class });
boolean rotaSelecionada = route.setRouteFence((String)o[0],((Double)o[1]), tk.getId());
respostaWs = Common.SerializeJSObject(rotaSelecionada);
break;
case checkFence:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { String.class, Point.class });
boolean dentro = route.checkFence((String)o[0], (Point)o[1], tk.getId());
respostaWs = Common.SerializeJSObject(dentro);
break;
case getCoords:
o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { String.class, String.class, String.class });
Extent ext = new Extent();
MapSize msz = new MapSize();
String idroute = (String)o[0];
String ex = (String)o[1];
String ms = (String)o[2];
ext.setXMin(Double.parseDouble(ex.split(Pattern.quote("|"))[0].replace(",", ".")));
ext.setXMax(Double.parseDouble(ex.split(Pattern.quote("|"))[1].replace(",", ".")));
ext.setYMin(Double.parseDouble(ex.split(Pattern.quote("|"))[2].replace(",", ".")));
ext.setYMax(Double.parseDouble(ex.split(Pattern.quote("|"))[3].replace(",", ".")));
msz.setWidth(Integer.parseInt(ms.split(Pattern.quote("|"))[0].toString()));
msz.setHeight(Integer.parseInt(ms.split(Pattern.quote("|"))[1].toString()));
//CommonControls.
respostaWs = route.getRouteCoords(idroute, ext, msz, tk.getId(), cm.getAppSettings("M_BASEDECARTA_ROTAS" + plusRodoviaria));
break;
case getCoordsFull:
//o = Common.getArrayByJSon(proxy.getParameters("tjss"), new Class[] { String) });
// CommonRouteDeCarta croute = new CommonRouteDeCarta(cm.getAppSettings("Maquina"),
// cm.getAppSettings("Server"),
// 11188,
// cm.getAppSettings("dirOutPut"),
// cm.getAppSettings("webOutPut"),
// cm.getAppSettings("M_BASEDECARTA_ROTAS" + plusRodoviaria),
// cm.getAppSettings("M_BASEDECARTA_MAPAS" + plusRodoviaria)
// );
//Point[] points = croute.getRouteCoords((String)o[0], new Extent(), new MapSize(), tk.id, ConfigurationManager.AppSettings["M_BASEDECARTA_ROTAS"], 0);
//respostaWs = croute.getRouteCoordsDecarta((String)o[0], new Extent(), new MapSize(), tk.id, ConfigurationManager.AppSettings["M_BASEDECARTA_ROTAS"], 0);
break;
}
return respostaWs;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static ArrayList tranformToArrayList(Object[] o){
ArrayList array = new ArrayList(o.length);
for (int i = 0; i < o.length; i++)
array.add(o[i]);
return array;
}
private static int getService(String service){
int serv = -1;
if (service.equals("getRoute")) serv = getRoute;
if (service.equals("getRouteTotals")) serv = getRouteTotals;
if (service.equals("getRouteDescription")) serv = getRouteDescription;
if (service.equals("getRouteWithMap")) serv = getRouteWithMap;
if (service.equals("getRouteSummary")) serv = getRouteSummary;
if (service.equals("getCoords")) serv = getCoords;
if (service.equals("getCoordsFull")) serv = getCoordsFull;
if (service.equals("setRouteFence")) serv = setRouteFence;
if (service.equals("checkFence")) serv = checkFence;
return serv;
}
}