Search in sources :

Example 1 with Point

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);
        }
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) ICallerList(de.janrufmonitor.framework.ICallerList) Iterator(java.util.Iterator) Viewer(org.eclipse.jface.viewers.Viewer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Point(de.janrufmonitor.util.math.Point)

Example 2 with Point

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Point(de.janrufmonitor.util.math.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 3 with Point

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())));
        }
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) ICall(de.janrufmonitor.framework.ICall) Point(de.janrufmonitor.util.math.Point)

Example 4 with Point

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);
    }
}
Also used : ICallManager(de.janrufmonitor.repository.ICallManager) ICaller(de.janrufmonitor.framework.ICaller) List(java.util.List) Point(de.janrufmonitor.util.math.Point) IEventBroker(de.janrufmonitor.framework.event.IEventBroker) Point(de.janrufmonitor.util.math.Point) IWriteCallRepository(de.janrufmonitor.repository.types.IWriteCallRepository)

Example 5 with Point

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;
}
Also used : IAttributeMap(de.janrufmonitor.framework.IAttributeMap) Point(de.janrufmonitor.util.math.Point)

Aggregations

Point (de.janrufmonitor.util.math.Point)6 ICaller (de.janrufmonitor.framework.ICaller)4 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)1 ICall (de.janrufmonitor.framework.ICall)1 ICallerList (de.janrufmonitor.framework.ICallerList)1 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)1 IEventBroker (de.janrufmonitor.framework.event.IEventBroker)1 ICallManager (de.janrufmonitor.repository.ICallManager)1 IWriteCallRepository (de.janrufmonitor.repository.types.IWriteCallRepository)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 DecimalFormat (java.text.DecimalFormat)1 Iterator (java.util.Iterator)1 List (java.util.List)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1