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.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.City;
import com.lbslocal.cc.objects.v0.common.Point;
import com.lbslocal.cc.objects.v4.addressFinder.Address;
import com.lbslocal.cc.objects.v4.addressFinder.AddressInfo;
import com.lbslocal.cc.objects.v4.addressFinder.AddressLocation;
import com.lbslocal.cc.objects.v4.addressFinder.AddressOptions;
import com.lbslocal.cc.objects.v4.addressFinder.CityLocation;
import com.lbslocal.cc.objects.v4.addressFinder.CityLocationInfo;
import com.lbslocal.cc.objects.v4.addressFinder.IntersectionInfo;
import com.lbslocal.cc.objects.v4.addressFinder.IntersectionLocation;
import com.lbslocal.cc.objects.v4.addressFinder.POIInfo;
import com.lbslocal.cc.objects.v4.addressFinder.POILocation;
import com.lbslocal.cc.objects.v4.addressFinder.ResultRange;
import com.lbslocal.cc.view.CAddressFinderAPI;
import com.lbslocal.master.common.ConfigurationManager;
public class AddressFinder extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddressFinder() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ConfigurationManager cm = ConfigurationManager.getInstance();
CAddressFinderAPI cAf = new CAddressFinderAPI(cm.getAppSettings("MAQUINA"), cm.getAppSettings("SERVER"), 11188, cm.getAppSettings("OUTPUT"),
cm.getAppSettings("DNS"), cm.getAppSettings("M_BASEDECARTA_MAPA"), cm.getAppSettings("M_BASEDECARTA_REVGEO"));
CommonRest cr = new CommonRest();
Point point = new Point(Double.parseDouble(request.getParameter("lng")), Double.parseDouble(request.getParameter("lat")));
Double tolerance = 50D;
AddressLocation al = new AddressLocation();
try {
al = cAf.getAddress(point, tolerance, Integer.parseInt(cm.getAppSettings("ID")));
} catch (Exception e) {
e.printStackTrace();
}
ObjectsHelper[] dependencyObjects = new ObjectsHelper[3];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new Address();
dependencyObjects[0].alias = "address";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new Point();
dependencyObjects[1].alias = "point";
dependencyObjects[2] = cr.new ObjectsHelper();
dependencyObjects[2].object = new AddressLocation();
dependencyObjects[2].alias = "AddressLocation";
String valor = "<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(al, dependencyObjects);
response.getWriter().write(new String(valor.getBytes("UTF-8"), "ISO-8859-1"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
ConfigurationManager cm = ConfigurationManager.getInstance();
int id_licenca = Integer.parseInt(cm.getAppSettings("ID"));
CAddressFinderAPI cAf = new CAddressFinderAPI(cm.getAppSettings("MAQUINA"), cm.getAppSettings("SERVER"), 11188, cm
.getAppSettings("OUTPUT"), cm.getAppSettings("DNS"), cm.getAppSettings("M_BASEDECARTA_MAPA"), cm
.getAppSettings("M_BASEDECARTA_REVGEO"));
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("findAddress")) {
AddressInfo ai = new AddressInfo();
ObjectsHelper[] addressDependencyObjects = new ObjectsHelper[1];
addressDependencyObjects[0] = cr.new ObjectsHelper();
addressDependencyObjects[0].object = new City();
addressDependencyObjects[0].alias = "city";
ObjectsHelper[] addressOptionsDependencyObjects = new ObjectsHelper[1];
addressOptionsDependencyObjects[0] = cr.new ObjectsHelper();
addressOptionsDependencyObjects[0].object = new ResultRange();
addressOptionsDependencyObjects[0].alias = "resultRange";
Address address = (Address) cr.getObjectFromXML(ph.XMLFile, "address", Address.class, addressDependencyObjects);
AddressOptions ao = (AddressOptions) cr.getObjectFromXML(ph.XMLFile, "ao", AddressOptions.class, addressOptionsDependencyObjects);
try {
ai = cAf.findAddress(address, ao, id_licenca);
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
ObjectsHelper[] dependencyObjects = new ObjectsHelper[2];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new AddressInfo();
dependencyObjects[0].alias = "AddressInfo";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new AddressLocation();
dependencyObjects[1].alias = "AddressLocation";
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(ai, dependencyObjects) + "");
}
if (ph.fields[0].equals("findCity")) {
CityLocationInfo ci = new CityLocationInfo();
ObjectsHelper[] addressOptionsDependencyObjects = new ObjectsHelper[1];
addressOptionsDependencyObjects[0] = cr.new ObjectsHelper();
addressOptionsDependencyObjects[0].object = new ResultRange();
addressOptionsDependencyObjects[0].alias = "resultRange";
City city = (City) cr.getObjectFromXML(ph.XMLFile, "cidade", City.class, null);
AddressOptions ao = (AddressOptions) cr.getObjectFromXML(ph.XMLFile, "ao", AddressOptions.class, addressOptionsDependencyObjects);
try {
ci = cAf.findCity(city, ao, id_licenca);
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
ObjectsHelper[] dependencyObjects = new ObjectsHelper[2];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new CityLocationInfo();
dependencyObjects[0].alias = "CityLocationInfo";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new CityLocation();
dependencyObjects[1].alias = "CityLocation";
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(ci, dependencyObjects) + "");
}
if (ph.fields[0].equals("findPOI")) {
POIInfo pi = new POIInfo();
String name = cr.getFieldFromXML(ph.XMLFile, "name");
City city = (City) cr.getObjectFromXML(ph.XMLFile, "city", City.class, null);
ResultRange resultRange = (ResultRange) cr.getObjectFromXML(ph.XMLFile, "resultRange", ResultRange.class, null);
try {
pi = cAf.findPOI(name, city, resultRange, id_licenca);
} catch (Exception e) {
e.printStackTrace();
}
ObjectsHelper[] dependencyObjects = new ObjectsHelper[2];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new POILocation();
dependencyObjects[0].alias = "POILocation";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new POIInfo();
dependencyObjects[1].alias = "POIInfo";
response.getWriter().write("<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(pi, dependencyObjects) + "");
}
if (ph.fields[0].equals("getAddress")) {
AddressLocation al = new AddressLocation();
Double tolerance = Double.valueOf(cr.getFieldFromXML(ph.XMLFile, "tolerance"));
Point point = (Point) cr.getObjectFromXML(ph.XMLFile, "point", Point.class, null);
try {
al = cAf.getAddress(point, tolerance, id_licenca);
} catch (Exception e) {
e.printStackTrace();
}
ObjectsHelper[] dependencyObjects = new ObjectsHelper[3];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new Address();
dependencyObjects[0].alias = "address";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new Point();
dependencyObjects[1].alias = "point";
dependencyObjects[2] = cr.new ObjectsHelper();
dependencyObjects[2].object = new AddressLocation();
dependencyObjects[2].alias = "AddressLocation";
String valor = "<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(al, dependencyObjects);
response.getWriter().write(new String(valor.getBytes("UTF-8"), "ISO-8859-1"));
}
if (ph.fields[0].equals("getIntersection")) {
City city = (City) cr.getObjectFromXML(ph.XMLFile, "city", City.class, null);
String street1 =cr.getFieldFromXML(ph.XMLFile, "street1");
String street2 =cr.getFieldFromXML(ph.XMLFile, "street2");
IntersectionInfo ii = null;
try {
ii = cAf.getIntersection(city, street1, street2, id_licenca);
} catch (Exception e) {
e.printStackTrace();
}
ObjectsHelper[] dependencyObjects = new ObjectsHelper[2];
dependencyObjects[0] = cr.new ObjectsHelper();
dependencyObjects[0].object = new IntersectionInfo();
dependencyObjects[0].alias = "IntersectionInfo";
dependencyObjects[1] = cr.new ObjectsHelper();
dependencyObjects[1].object = new IntersectionLocation();
dependencyObjects[1].alias = "IntersectionLocation";
String valor = "<?xml version='1.0' encoding='UTF-8'?>\n" + cr.getXMLFromObject(ii, dependencyObjects);
response.getWriter().write(new String(valor.getBytes("UTF-8"), "ISO-8859-1"));
}
} catch (NumberFormatException e) {
response.getWriter().write(RevGeoException.getErrorMessage(RevGeoException.NUMBER_ERROR));
}
}
}