Search in sources :

Example 26 with IAttribute

use of de.janrufmonitor.framework.IAttribute in project janrufmonitor by tbrandt77.

the class HsqldbMultiPhoneCallerDatabaseHandler method internalUpdate.

protected void internalUpdate(ICaller c) throws SQLException {
    // check 1) image already exists and gets exchanged
    if (internalHasImage(c)) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Caller " + c.toString() + " has already an image. Updating image.");
        PreparedStatement ps = this.getStatement("UPDATE_IMAGE");
        if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEPATH.");
            String imgpath = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
            File img = new File(PathResolver.getInstance(getRuntime()).resolve(imgpath));
            if (img.exists()) {
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEPATH: " + img.getAbsolutePath());
                try {
                    FileInputStream fim = new FileInputStream(img);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    Base64Encoder b64 = new Base64Encoder(encodedOut);
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    IAttribute img_ref = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
                    String uuid = null;
                    if (img_ref != null && img_ref.getValue().length() > 0) {
                        uuid = img_ref.getValue();
                    } else
                        uuid = c.getUUID();
                    updateImage(ps, uuid, encodedOut.toByteArray());
                    if (uuid != null)
                        c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, uuid));
                } catch (FileNotFoundException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                } catch (IOException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                }
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
            } else {
                // file does not exist so remove image
                internalDelete(c);
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
                c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
            }
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEBINARY.");
            String imgdata = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY).getValue();
            IAttribute img_ref = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
            String uuid = null;
            if (img_ref != null && img_ref.getValue().length() > 0) {
                uuid = img_ref.getValue();
            } else
                uuid = c.getUUID();
            updateImage(ps, uuid, imgdata.getBytes());
            if (uuid != null)
                c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, uuid));
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Updating image from ATTRIBUTE_NAME_IMAGEURL.");
            // TODO update from URL
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEURL);
        }
        ps.executeBatch();
    } else // check 2) image does not exist and has to be insert
    if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH) || c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL) || c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
        if (this.m_logger.isLoggable(Level.INFO))
            this.m_logger.info("Caller " + c.toString() + " has not yet an image. Inserting image.");
        PreparedStatement ps = this.getStatement("INSERT_IMAGE");
        if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
            String imgpath = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
            File img = new File(PathResolver.getInstance(getRuntime()).resolve(imgpath));
            if (img.exists()) {
                if (this.m_logger.isLoggable(Level.INFO))
                    this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEPATH: " + img.getAbsolutePath());
                try {
                    FileInputStream fim = new FileInputStream(img);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    Base64Encoder b64 = new Base64Encoder(encodedOut);
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    createImage(ps, c.getUUID(), encodedOut.toByteArray());
                    c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, c.getUUID()));
                } catch (FileNotFoundException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                } catch (IOException e) {
                    this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                }
            }
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEBINARY.");
            String imgdata = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY).getValue();
            createImage(ps, c.getUUID(), imgdata.getBytes());
            c.getAttributes().add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF, c.getUUID()));
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY);
        } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEURL)) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Inserting image from ATTRIBUTE_NAME_IMAGEURL.");
            // TODO insert from URL
            c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEURL);
        }
        ps.executeBatch();
    } else // check 3) image gets deleted
    {
        internalDelete(c);
        c.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
    }
}
Also used : IAttribute(de.janrufmonitor.framework.IAttribute) FileNotFoundException(java.io.FileNotFoundException) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder)

Example 27 with IAttribute

use of de.janrufmonitor.framework.IAttribute in project janrufmonitor by tbrandt77.

the class HsqldbMultiPhoneCallerDatabaseHandler method buildCallerList.

protected ICallerList buildCallerList(IFilter[] filters, ISearchTerm[] searchTerms) throws SQLException {
    ICallerList cl = this.getRuntime().getCallerFactory().createCallerList();
    if (!isConnected())
        return cl;
    StringBuffer sql = new StringBuffer();
    Statement stmt = m_con.createStatement();
    // build SQL statement
    sql.append("SELECT content FROM callers");
    if (hasAttributeFilter(filters))
        sql.append(", attributes");
    if (filters != null && filters.length > 0 && filters[0] != null) {
        IFilter f = null;
        sql.append(" WHERE ");
        for (int i = 0; i < filters.length; i++) {
            f = filters[i];
            if (i > 0)
                sql.append(" AND ");
            if (f.getType() == FilterType.PHONENUMBER) {
                IPhonenumber pn = (IPhonenumber) f.getFilterObject();
                // sql.append("country='"+pn.getIntAreaCode()+"' AND areacode='"+pn.getAreaCode()+"'");
                ResultSet rs = stmt.executeQuery("SELECT ref FROM phones WHERE country='" + pn.getIntAreaCode() + "' AND areacode='" + pn.getAreaCode() + "';");
                if (rs.next()) {
                    sql.append("uuid='");
                    sql.append(rs.getString(1));
                    sql.append("'");
                    while (rs.next()) {
                        sql.append(" OR uuid='");
                        sql.append(rs.getString(1));
                        sql.append("'");
                    }
                }
            }
            if (f.getType() == FilterType.ATTRIBUTE) {
                IAttributeMap m = ((AttributeFilter) f).getAttributeMap();
                if (m != null && m.size() > 0) {
                    sql.append("(");
                    sql.append("callers.uuid=attributes.ref AND (");
                    Iterator iter = m.iterator();
                    IAttribute a = null;
                    while (iter.hasNext()) {
                        a = (IAttribute) iter.next();
                        sql.append("attributes.name='");
                        sql.append(a.getName());
                        sql.append("'");
                        sql.append(" AND ");
                        sql.append("attributes.value='");
                        sql.append(a.getValue());
                        sql.append("'");
                        if (iter.hasNext())
                            sql.append(" OR ");
                    }
                    sql.append("))");
                }
            }
            if (f.getType() == FilterType.CHARACTER) {
                sql.append("(");
                sql.append("callers.uuid=attributes.ref AND (");
                sql.append("attributes.name='");
                sql.append(((CharacterFilter) f).getAttributeName());
                sql.append("'");
                sql.append(" AND ");
                sql.append("(attributes.value like '");
                sql.append(((CharacterFilter) f).getCharacter().toUpperCase());
                sql.append("%'");
                sql.append(" OR attributes.value like '");
                sql.append(((CharacterFilter) f).getCharacter().toLowerCase());
                sql.append("%'");
                sql.append(")))");
            }
        }
        if (searchTerms != null && searchTerms.length > 0) {
            sql.append(" AND");
            sql.append(createSearchTerm(searchTerms));
        }
    } else {
        if (searchTerms != null && searchTerms.length > 0) {
            sql.append(" WHERE");
            sql.append(createSearchTerm(searchTerms));
        }
    }
    sql.append(";");
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info(sql.toString());
    ResultSet rs = stmt.executeQuery(sql.toString());
    while (rs.next()) {
        try {
            cl.add(Serializer.toCaller(rs.getString("content").getBytes(), this.getRuntime()));
        } catch (SerializerException e) {
            this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        }
    }
    return cl;
}
Also used : CharacterFilter(de.janrufmonitor.repository.filter.CharacterFilter) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SerializerException(de.janrufmonitor.util.io.SerializerException) ICallerList(de.janrufmonitor.framework.ICallerList) IFilter(de.janrufmonitor.repository.filter.IFilter) ResultSet(java.sql.ResultSet) AttributeFilter(de.janrufmonitor.repository.filter.AttributeFilter) Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 28 with IAttribute

use of de.janrufmonitor.framework.IAttribute in project janrufmonitor by tbrandt77.

the class HsqldbMultiPhoneCallerDatabaseHandler method internalDelete.

protected void internalDelete(ICaller c) throws SQLException {
    PreparedStatement ps = this.getStatement("DELETE_IMAGE");
    if (ps != null) {
        this.deleteImage(ps, c.getUUID());
        ps.executeBatch();
    }
    IAttribute img_ref = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
    if (img_ref != null && img_ref.getValue().length() > 0) {
        ps = this.getStatement("DELETE_IMAGE");
        this.deleteImage(ps, img_ref.getValue());
        ps.executeBatch();
    }
}
Also used : IAttribute(de.janrufmonitor.framework.IAttribute) PreparedStatement(java.sql.PreparedStatement)

Example 29 with IAttribute

use of de.janrufmonitor.framework.IAttribute in project janrufmonitor by tbrandt77.

the class AbstractDatabaseHandler method createAttributes.

protected void createAttributes(PreparedStatement ps, String uuid, IAttributeMap m) throws SQLException {
    Iterator i = m.iterator();
    IAttribute att = null;
    while (i.hasNext()) {
        att = (IAttribute) i.next();
        ps.clearParameters();
        ps.setString(1, uuid);
        ps.setString(2, att.getName());
        ps.setString(3, att.getValue());
        ps.addBatch();
    }
}
Also used : Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute)

Example 30 with IAttribute

use of de.janrufmonitor.framework.IAttribute in project janrufmonitor by tbrandt77.

the class AbstractConfigurableCallerManager method applyFilters.

protected void applyFilters(ICallerList cl, IFilter[] filters) {
    if (cl == null)
        return;
    if (filters == null)
        return;
    IFilter f = null;
    for (int i = 0; i < filters.length; i++) {
        f = filters[i];
        if (f.getType() == FilterType.CHARACTER) {
            CharacterFilter cf = ((CharacterFilter) f);
            ICaller c = null;
            for (int j = cl.size() - 1; j >= 0; j--) {
                c = cl.get(j);
                if (!c.getAttributes().contains(cf.getAttributeName())) {
                    cl.remove(c);
                } else if (c.getAttributes().contains(cf.getAttributeName())) {
                    if (!c.getAttribute(cf.getAttributeName()).getValue().startsWith(cf.getCharacter())) {
                        cl.remove(c);
                    }
                }
            }
        }
        if (f.getType() == FilterType.PHONENUMBER) {
            PhonenumberFilter cf = ((PhonenumberFilter) f);
            ICaller c = null;
            for (int j = cl.size() - 1; j >= 0; j--) {
                c = cl.get(j);
                if (!c.getPhoneNumber().getIntAreaCode().equalsIgnoreCase(cf.getPhonenumber().getIntAreaCode())) {
                    cl.remove(c);
                } else if (!c.getPhoneNumber().getAreaCode().equalsIgnoreCase(cf.getPhonenumber().getAreaCode())) {
                    cl.remove(c);
                }
            }
        }
        if (f.getType() == FilterType.ATTRIBUTE) {
            AttributeFilter cf = ((AttributeFilter) f);
            ICaller c = null;
            for (int j = cl.size() - 1; j >= 0; j--) {
                c = cl.get(j);
                IAttributeMap atts = cf.getAttributeMap();
                Iterator iter = atts.iterator();
                IAttribute a = null;
                while (iter.hasNext()) {
                    a = (IAttribute) iter.next();
                    if (!c.getAttributes().contains(a)) {
                        cl.remove(c);
                    } else if (c.getAttributes().contains(a) && !c.getAttribute(a.getName()).getValue().equalsIgnoreCase(a.getValue())) {
                        cl.remove(c);
                    }
                }
            }
        }
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) CharacterFilter(de.janrufmonitor.repository.filter.CharacterFilter) IFilter(de.janrufmonitor.repository.filter.IFilter) AttributeFilter(de.janrufmonitor.repository.filter.AttributeFilter) Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) PhonenumberFilter(de.janrufmonitor.repository.filter.PhonenumberFilter) IAttributeMap(de.janrufmonitor.framework.IAttributeMap)

Aggregations

IAttribute (de.janrufmonitor.framework.IAttribute)111 ICaller (de.janrufmonitor.framework.ICaller)44 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)40 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)39 List (java.util.List)34 ICallerList (de.janrufmonitor.framework.ICallerList)31 ArrayList (java.util.ArrayList)29 Iterator (java.util.Iterator)25 ICall (de.janrufmonitor.framework.ICall)19 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)15 SQLException (java.sql.SQLException)15 File (java.io.File)14 AttributeFilter (de.janrufmonitor.repository.filter.AttributeFilter)12 IOException (java.io.IOException)12 Message (de.janrufmonitor.exception.Message)11 ComFailException (com.jacob.com.ComFailException)10 Date (java.util.Date)10 IMsn (de.janrufmonitor.framework.IMsn)9 Dispatch (com.jacob.com.Dispatch)8 UUID (de.janrufmonitor.util.uuid.UUID)8