package com.lbslocal.cc.view;
import com.lbslocal.cc.common.Functions;
import com.lbslocal.cc.core.CommonProximity;
import com.lbslocal.cc.objects.v0.common.Extent;
import com.lbslocal.cc.objects.v0.common.Point;
import com.lbslocal.cc.objects.v3.proximity.ProximityInfo;
import com.lbslocal.cc.objects.v3.proximity.ProximityOptions;
import com.lbslocal.cc.objects.v4.addressFinder.Address;
import com.lbslocal.cc.objects.v4.addressFinder.AddressOptions;
import com.lbslocal.cc.objects.v4.addressFinder.ResultRange;
public class CProximity {
CommonProximity px;
CAddressFinderAPI af;
long iniDate;
// private String _maquina = "";
public CProximity() {
px = new CommonProximity();
}
public CProximity(String maquina, String dominio) {
// this._maquina = maquina;
px = new CommonProximity();
af = new CAddressFinderAPI(maquina, "", 11188, "", "", "", "");
}
public ProximityInfo findClosestFromAddress(Address ad, ProximityOptions po, int idLicenca) throws Exception {
iniDate = System.currentTimeMillis();
ProximityInfo pi = new ProximityInfo();
try {
Point point = new Point();
if (Functions.IsNullOrEmpty(ad.getStreet()) && Functions.IsNullOrEmpty(ad.getHouseNumber()) && Functions.IsNullOrEmpty(ad.getZip())
&& (!Functions.IsNullOrEmpty(ad.getCity().getName()) && !Functions.IsNullOrEmpty(ad.getCity().getState()))) {
try {
point = af.findCity(ad.getCity(), new AddressOptions(true, 10, new ResultRange(1, 1)), idLicenca).getCityLocation()[0].getPoint();
} catch (Exception ex) {
throw (new Exception("Nao foi possivel geocodificar o endereco."));
}
} else {
point = af.getXY(ad, idLicenca);
}
if (point.getX() == 0)
throw (new Exception("Nao foi possivel geocodificar o endereco."));
pi = px.findClosestFromXY(point, po, idLicenca);
/*** Gravar acesso no log ***/
Functions.gravarLogUnique(idLicenca, CommonProximity._findClosestFromAddress, 0, iniDate, 0);
} catch (Exception e) {
throw e;
}
return pi;
}
public ProximityInfo findRadiusFromAddress(Address ad, int radius, ProximityOptions po, int idLicenca) throws Exception {
iniDate = System.currentTimeMillis();
ProximityInfo pi = new ProximityInfo();
try {
Point point = new Point();
if (Functions.IsNullOrEmpty(ad.getStreet()) && Functions.IsNullOrEmpty(ad.getHouseNumber()) && Functions.IsNullOrEmpty(ad.getZip())
&& (!Functions.IsNullOrEmpty(ad.getCity().getName()) && !Functions.IsNullOrEmpty(ad.getCity().getState()))) {
try {
point = af.findCity(ad.getCity(), new AddressOptions(true, 0, new ResultRange(1, 1)), idLicenca).getCityLocation()[0].getPoint();
} catch (Exception ex) {
throw (new Exception("Nao foi possivel geocodificar o endereco."));
}
} else {
point = af.getXY(ad, idLicenca);
}
if (point.getX() == 0)
throw (new Exception("Nao foi possivel geocodificar o endereco."));
pi = px.findRadiusFromXY(point, radius, po, idLicenca);
/*** Gravar acesso no log ***/
Functions.gravarLogUnique(idLicenca, CommonProximity._findRadiusFromAddress, 0, iniDate, 0);
} catch (Exception e) {
throw e;
}
return pi;
}
public ProximityInfo findRadiusFromXY(Point point, int radius, ProximityOptions po, int idLicenca) throws Exception {
iniDate = System.currentTimeMillis();
ProximityInfo pi = new ProximityInfo();
try {
pi = px.findRadiusFromXY(point, radius, po, idLicenca);
/*** Gravar acesso no log ***/
Functions.gravarLogUnique(idLicenca, CommonProximity._findRadiusFromXY, 0, iniDate, 0);
} catch (Exception e) {
throw e;
}
return pi;
}
public ProximityInfo findFromExtent(Extent ext, ProximityOptions po, int idLicenca) throws Exception {
iniDate = System.currentTimeMillis();
ProximityInfo pi = new ProximityInfo();
try {
pi = px.findFromExtent(ext, po, idLicenca);
/*** Gravar acesso no log ***/
Functions.gravarLogUnique(idLicenca, CommonProximity._findFromExtent, 0, iniDate, 0);
} catch (Exception e) {
throw e;
}
return pi;
}
public ProximityInfo findClosestFromXY(Point point, ProximityOptions po, int idLicenca) throws Exception {
iniDate = System.currentTimeMillis();
ProximityInfo pi = new ProximityInfo();
try {
pi = px.findClosestFromXY(point, po, idLicenca);
/*** Gravar acesso no log ***/
Functions.gravarLogUnique(idLicenca, CommonProximity._findClosestFromXY, 0, iniDate, 0);
} catch (Exception e) {
throw e;
}
return pi;
}
}