use of de.janrufmonitor.util.math.Point in project janrufmonitor by tbrandt77.
the class GeoCodingAction method run.
public void run() {
Viewer v = this.m_app.getApplication().getViewer();
if (v != null && v instanceof Viewer) {
final IStructuredSelection selection = (IStructuredSelection) v.getSelection();
if (!selection.isEmpty()) {
Iterator i = selection.iterator();
ICallerList list = this.getRuntime().getCallerFactory().createCallerList(selection.size());
Object o = null;
while (i.hasNext()) {
o = i.next();
if (o instanceof ICaller) {
Point p = GeoCoder.getInstance().getCoordinates(((ICaller) o).getAttributes());
if (p != null) {
((ICaller) o).setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_ACC, Integer.toString(p.getAccurance())));
((ICaller) o).setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG, Double.toString(p.getLongitude())));
((ICaller) o).setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT, Double.toString(p.getLatitude())));
list.add((ICaller) o);
}
}
}
if (list.size() > 0) {
this.m_app.getController().updateElement(list);
}
m_app.updateViews(true);
}
}
}
use of de.janrufmonitor.util.math.Point in project janrufmonitor by tbrandt77.
the class GeoCoder method getCoordinates.
public Point getCoordinates(IAttributeMap atts) {
String result = null;
StringBuffer query = new StringBuffer();
if (atts.contains(IJAMConst.ATTRIBUTE_NAME_STREET) && atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue().length() > 0) {
// check if street no is available
if (!atts.contains(IJAMConst.ATTRIBUTE_NAME_STREET_NO)) {
this.m_logger.info("Splitting street attribute: " + atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue());
if (hasStreetNo(atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue())) {
String no = getStreetNo(atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue());
atts.add(PIMRuntime.getInstance().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET_NO, no));
atts.add(PIMRuntime.getInstance().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET, atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue().substring(0, atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue().length() - no.length()).trim()));
}
}
query.append("&str=");
query.append(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_STREET).getValue()));
}
if (atts.contains(IJAMConst.ATTRIBUTE_NAME_STREET_NO) && atts.get(IJAMConst.ATTRIBUTE_NAME_STREET_NO).getValue().length() > 0) {
query.append("&no=");
query.append(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_STREET_NO).getValue()));
}
if (atts.contains(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE) && atts.get(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE).getValue().length() > 0) {
query.append("&pcode=");
query.append(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE).getValue()));
}
if (atts.contains(IJAMConst.ATTRIBUTE_NAME_CITY) && atts.get(IJAMConst.ATTRIBUTE_NAME_CITY).getValue().length() > 0) {
query.append("&city=");
query.append(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_CITY).getValue()));
// 2013/06/01: check if city is a forbidden city name
if (this.isForbiddenCity(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_CITY).getValue())))
return null;
}
if (atts.contains(IJAMConst.ATTRIBUTE_NAME_COUNTRY) && atts.get(IJAMConst.ATTRIBUTE_NAME_COUNTRY).getValue().length() > 0) {
query.append("&cntry=");
query.append(encode(atts.get(IJAMConst.ATTRIBUTE_NAME_COUNTRY).getValue()));
}
if (query.length() == 0) {
this.m_logger.info("No relevant attributes for geo coding found.");
return null;
}
if (this.m_cache.containsKey(query.toString())) {
this.m_logger.info("Geo coordinates determined from local cache. ");
return (Point) this.m_cache.get(query.toString());
}
try {
URL url = new URL("http://geo.janrufmonitor.de/code_v2.php?" + query.toString());
URLConnection c = url.openConnection();
c.setDoInput(true);
c.setRequestProperty("User-Agent", "jAnrufmonitor GeoCoder " + IJAMConst.VERSION_DISPLAY);
c.connect();
if (this.m_logger != null && this.m_logger.isLoggable(Level.INFO))
this.m_logger.info("Querying URL " + url);
Object o = c.getInputStream();
if (o != null && o instanceof InputStream) {
if (this.m_logger != null)
this.m_logger.info("Content successfully retrieved from " + url);
BufferedInputStream bin = new BufferedInputStream((InputStream) o);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Stream.copy(bin, bout, true);
result = bout.toString();
if (this.m_logger != null)
this.m_logger.info("Geocoding raw result: " + result);
bin.close();
}
} catch (MalformedURLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
} catch (IOException e) {
this.m_logger.log(Level.INFO, e.getMessage(), e);
}
if (result != null) {
String[] values = result.split(",");
if (values[0].equalsIgnoreCase("200")) {
this.m_logger.info("geo.janrufmonitor.de [200] data: " + result);
Point p = new Point(Double.parseDouble(values[3]), Double.parseDouble(values[2]), Integer.parseInt(values[1]));
this.m_cache.put(query.toString(), p);
return p;
}
if (values[0].equalsIgnoreCase("201")) {
this.m_logger.info("geo.janrufmonitor.de [201] cached data: " + result);
Point p = new Point(Double.parseDouble(values[3]), Double.parseDouble(values[2]), Integer.parseInt(values[1]));
this.m_cache.put(query.toString(), p);
return p;
}
}
return null;
}
use of de.janrufmonitor.util.math.Point in project janrufmonitor by tbrandt77.
the class GeoCoding method modifyObject.
public void modifyObject(Object o) {
if (o instanceof ICall) {
o = ((ICall) o).getCaller();
}
if (o instanceof ICaller) {
ICaller caller = ((ICaller) o);
if (caller.getPhoneNumber().isClired())
return;
if (caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT) != null && caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG) != null) {
this.m_logger.info("Caller already set with geo data ...");
return;
}
Point p = GeoCoder.getInstance().getCoordinates(caller.getAttributes());
if (p != null) {
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_ACC, Integer.toString(p.getAccurance())));
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT, Double.toString(p.getLatitude())));
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG, Double.toString(p.getLongitude())));
}
}
}
use of de.janrufmonitor.util.math.Point in project janrufmonitor by tbrandt77.
the class GeoCoding method receivedValidRule.
public void receivedValidRule(ICall call) {
if (call == null)
return;
if (call.getCaller().getPhoneNumber().isClired())
return;
ICaller caller = call.getCaller();
if (caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT) != null && caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG) != null) {
this.m_logger.info("Call already set with geo data ...");
return;
}
Point p = GeoCoder.getInstance().getCoordinates(caller.getAttributes());
if (p != null) {
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_ACC, Integer.toString(p.getAccurance())));
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT, Double.toString(p.getLatitude())));
caller.setAttribute(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG, Double.toString(p.getLongitude())));
List callManagerList = this.getRuntime().getCallManagerFactory().getAllCallManagers();
ICallManager icm = null;
IEventBroker eventBroker = this.getRuntime().getEventBroker();
for (int i = 0; i < callManagerList.size(); i++) {
icm = (ICallManager) callManagerList.get(i);
// check if the repository manager allows read/write access
if (icm.isActive() && icm.isSupported(IWriteCallRepository.class)) {
((IWriteCallRepository) icm).updateCall(call);
this.m_logger.info("Call update sent with geocoding: " + call);
eventBroker.send(this, eventBroker.createEvent(IEventConst.EVENT_TYPE_UPDATE_CALL, call));
}
}
} else {
this.m_logger.info("Geocoding not successfully for call: " + call);
}
}
use of de.janrufmonitor.util.math.Point in project janrufmonitor by tbrandt77.
the class GeoCoding method performOk.
public boolean performOk() {
boolean ok = super.performOk();
IAttributeMap address = getRuntime().getCallerFactory().createAttributeMap();
if (getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_street").trim().length() > 0) {
address.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET, getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_street").trim()));
}
if (getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_streetno").trim().length() > 0) {
address.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_STREET_NO, getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_streetno").trim()));
}
if (getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_pcode").trim().length() > 0) {
address.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE, getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_pcode").trim()));
}
if (getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_city").trim().length() > 0) {
address.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CITY, getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_city").trim()));
}
if (getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_country").trim().length() > 0) {
address.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_COUNTRY, getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "local_country").trim()));
}
// String address = getRuntime().getConfigManagerFactory().getConfigManager().getProperty(CONFIG_NAMESPACE, "address");
if (address != null && address.size() > 0) {
Point loc = GeoCoder.getInstance().getCoordinates(address);
if (loc != null) {
getRuntime().getConfigManagerFactory().getConfigManager().setProperty(CONFIG_NAMESPACE, "local-geo-lng", Double.toString(loc.getLongitude()));
getRuntime().getConfigManagerFactory().getConfigManager().setProperty(CONFIG_NAMESPACE, "local-geo-lat", Double.toString(loc.getLatitude()));
getRuntime().getConfigManagerFactory().getConfigManager().setProperty(CONFIG_NAMESPACE, "local-geo-acc", Integer.toString(loc.getAccurance()));
}
GeoCoder.invalidate();
}
return ok;
}
Aggregations