public AddressInfo findAddress(Address address, AddressOptions ao, int idLicenca) throws Exception {
String tbRuas = "";
int iCount = 0, houseNumber = 0;
AddressInfo ai = new AddressInfo();
Pagination pg;
if ((address.getStreet() != "" && address.getStreet() != null) || (address.getZip() != "" && address.getZip() != null)
|| (address.getDistrict() != "" && address.getDistrict() != null)) {
String country = "";
String state = "";
if (!Functions.IsNullOrEmpty(address.getCity().getCountry()))
country = address.getCity().getCountry();
else
country = "BRASIL";
String dataB = "";
if (ao.getSearchType() == 10) {
if (address.getCity().getCountry().indexOf("|") > 0) {
ao.setSearchType(0);
dataB = address.getCity().getCountry().split(Pattern.quote("|"))[0];
tbRuas = address.getCity().getCountry().split(Pattern.quote("|"))[1];
address.getCity().setCountry(Functions.getCountryByDataBase(dataB).getName());
} else {
ao.setSearchType(0);
dataB = address.getCity().getCountry();
address.getCity().setCountry(Functions.getCountryByDataBase(dataB).getName());
}
} else
dataB = Functions.getDataBase(idLicenca, country).getSource();
// transformar valores null em "" para não dar null pointer
// exception
if (address.getStreet() == null)
address.setStreet("");
if (address.getHouseNumber() == null)
address.setHouseNumber("");
if (address.getZip() == null)
address.setZip("");
if (address.getDistrict() == null)
address.setDistrict("");
if (address.getCity().getName() == null)
address.getCity().setName("");
if (address.getCity().getState() == null)
address.getCity().setState("");
// validando número
if (!address.getHouseNumber().equals("")) {
if (Functions.isNumeric(address.getHouseNumber().replace(".", "").replace(",", "").replace("-", "").replace("e", "")))
houseNumber = Integer.parseInt(address.getHouseNumber());
}
if (houseNumber == 0)
address.setHouseNumber("");
else
address.setHouseNumber(String.valueOf(houseNumber));
// Se for diferente de 0,1,2 ou 3, seta o valor padr�o de 2 -
// anywhere
if ((ao.getSearchType() != AddressOptions.Exact) && (ao.getSearchType() != AddressOptions.BeginsWith)
&& (ao.getSearchType() != AddressOptions.Anywhere) && (ao.getSearchType() != AddressOptions.AnywhereDistrict)
&& (ao.getSearchType() != AddressOptions.EndsWith) && (ao.getSearchType() != AddressOptions.Auto)) {
if (((ao.getSearchType() == AddressOptions.ExactUTM) || (ao.getSearchType() == AddressOptions.BeginsWithUTM)
|| (ao.getSearchType() == AddressOptions.AnywhereUTM) || (ao.getSearchType() == AddressOptions.EndsWithUTM))
&& ao.getSearchType() >= 100) {
ao.setSearchType(ao.getSearchType() - 100);
} else
ao.setSearchType(AddressOptions.Anywhere);
}
if (address.getCity().getName() != "" && Functions.IsNullOrEmpty(tbRuas))
tbRuas = Functions.getTabelaRuas(address.getCity(), idLicenca, dataB);
country = Functions.getCountryByDataBase(dataB).getName();
if (!Functions.IsNullOrEmpty(address.getHouseNumber()))
address.setHouseNumber(address.getHouseNumber().replace(" ", ""));
Connection con = dataBase.getConnection(dataB);
CallableStatement cst = con.prepareCall("{call CP_WS_ST_RUAS_FONETICO(?,?,?,?,?,?,?)}");
cst.setString(1, tbRuas);
cst.setString(2, address.getStreet());
cst.setString(3, address.getHouseNumber());
cst.setString(4, address.getZip().replace("-", ""));
cst.setString(5, address.getDistrict());
cst.setInt(6, (ao.isUsePhonetic()) ? 1 : 0);
cst.setInt(7, ao.getSearchType());
cst.execute();
ResultSet rs1 = cst.getResultSet();
CachedRowSetImpl crs = new CachedRowSetImpl();
crs.populate(rs1);
int TotalRecords = crs.size();
AddressLocation[] al;
if (TotalRecords > 0) {
pg = Functions.definePage(TotalRecords, ao.getResultRange().getRecordsPerPage(), ao.getResultRange().getPageIndex());
if (country.toLowerCase().equals("brasil")) {
Statement stEstado = con.createStatement();
crs.next();
String sql = " select FNCCAMELCASE(estado) estado from TB_CIDADES where geocode = '" + crs.getString("TABELA") + "' ";
ResultSet rrEstado = stEstado.executeQuery(sql.toUpperCase());
CachedRowSetImpl crsEstado = new CachedRowSetImpl();
crsEstado.populate(rrEstado);
if (crsEstado.size() != 0) {
crsEstado.next();
state = crsEstado.getString("ESTADO");
}
if (pg.getRecordsInitial() <= TotalRecords) {
al = new AddressLocation[(pg.getRecordsFinal() - pg.getRecordsInitial()) + 1];
for (int intI = pg.getRecordsInitial(); intI <= pg.getRecordsFinal(); intI++) {
if (rs1.absolute(intI)) {
al[iCount] = new AddressLocation();
al[iCount].setZipL(rs1.getString("CEPL"));
al[iCount].setZipR(rs1.getString("CEPR"));
al[iCount].setCarAccess(rs1.getBoolean("ROTAPE"));
// classe address
al[iCount].getAddress().setStreet(rs1.getString("LOGRAC"));
al[iCount].getAddress().setDistrict(rs1.getString("BAIRRO"));
al[iCount].getAddress().getCity().setName(rs1.getString("MUNICIPIO"));
al[iCount].getAddress().getCity().setCountry(country);
// al[iCount].getAddress().setZone(rs1.getString("REGIAO"));
if (!country.toLowerCase().equals("brasil"))
al[iCount].getAddress().getCity().setState(state);
else
al[iCount].getAddress().getCity().setState(rs1.getString("TABELA").substring(0, 2).toUpperCase());
if (!al[iCount].getZipL().equals(""))
al[iCount].getAddress().setZip(al[iCount].getZipL());
if (!al[iCount].getZipR().equals(""))
al[iCount].getAddress().setZip(al[iCount].getZipR());
al[iCount].getAddress().setHouseNumber(rs1.getString("MEDIA"));
try {
al[iCount].setPoint(Functions.getGeocodeSQL(rs1.getInt("L_F_ADD"), rs1.getInt("L_T_ADD"), rs1.getInt("R_F_ADD"),
rs1.getInt("R_T_ADD"), al[iCount].getAddress().getHouseNumber(), rs1.getString("COORDS")));
} catch (Exception ex) {
al[iCount].getPoint().setX(0.0);
al[iCount].getPoint().setY(0.0);
}
iCount++;
}
}
ai.setRecordCount((int) pg.getRecordCount());
ai.setPageCount(pg.getPageCount());
} else {
al = new AddressLocation[0];
// throw (new
// Exception("Endere�o n�o encontrado em nossa base de dados!"));
}
ai.setAddressLocation(al);
}
} else {
if (address.getStreet() == "" && (!Functions.IsNullOrEmpty(address.getZip()))) {
String cmd = " select CEP, CORREIO_ENDERECO AS ENDERECO, CORREIO_MUNICIPIO AS CIDADE, CORREIO_UF AS ESTADO, "
+ " COALESCE(X,0) as X, COALESCE(Y,0) as Y, COALESCE(CORREIO_BAIRRO_INI,'') AS BAIRRO " + " FROM TB_CEP_BRASIL "
+ " WHERE CEP = REPLACE('" + address.getZip() + "','-','') " + " AND X IS NOT NULL".toUpperCase();
con = dataBase.getConnection(dataB);
Statement st = con.createStatement();
ResultSet rr = st.executeQuery(cmd);
crs = new CachedRowSetImpl();
crs.populate(rr);
if (crs.size() == 0) {
crs.next();
al = new AddressLocation[1];
al[0] = new AddressLocation();
al[0].setAddress(new Address());
al[0].getAddress().setStreet(crs.getString("Endereco"));
al[0].getAddress().setZip(crs.getString("CEP"));
al[0].getAddress().setDistrict(crs.getString("BAIRRO"));
al[0].getAddress().setHouseNumber("");
al[0].getAddress().setCity(new City());
al[0].getAddress().getCity().setName(crs.getString("Cidade"));
al[0].getAddress().getCity().setState(crs.getString("EStado"));
al[0].setZipL(crs.getString("CEP"));
al[0].setZipR(crs.getString("CEP"));
Point point = new Point();
point.setX(crs.getDouble("X"));
point.setY(crs.getDouble("Y"));
al[0].setPoint(point);
ai.setRecordCount(1);
ai.setAddressLocation(al);
} else {
al = new AddressLocation[0];
ai.setAddressLocation(al);
}
} else {
al = new AddressLocation[0];
ai.setAddressLocation(al);
}
}
return ai;
} else {
ai.setAddressLocation(new AddressLocation[0]);
ai.setPageCount(0);
ai.setRecordCount(0);
return ai;
}
}