Search in sources :

Example 1 with SerializerException

use of de.janrufmonitor.util.io.SerializerException in project janrufmonitor by tbrandt77.

the class TextFileCallManager method loadDatabase.

private void loadDatabase(String database) {
    long startDate = System.currentTimeMillis();
    File db = new File(database);
    try {
        FileReader dbReader = new FileReader(db);
        BufferedReader bufReader = new BufferedReader(dbReader);
        String line = null;
        ICall aCall = null;
        while (bufReader.ready()) {
            line = bufReader.readLine();
            // check for old entries
            if (line.indexOf(CALL_SEPARATOR) > -1)
                line = this.transformOldTextfileData(line);
            try {
                aCall = Serializer.toCall(line.getBytes(), this.getRuntime());
                this.m_callList.add(aCall);
            } catch (SerializerException e) {
                this.m_logger.severe(e.getMessage());
            }
        }
        bufReader.close();
        dbReader.close();
    } catch (FileNotFoundException ex) {
        this.m_logger.warning("Cannot find call database: " + database);
        this.m_logger.info("Creating call database: " + database);
        File dbnew = new File(database);
        if (!dbnew.exists()) {
            try {
                dbnew.createNewFile();
            } catch (IOException e) {
                this.m_logger.severe("Cannot create file: " + database + ". Check read/write permissions.");
            }
        }
    } catch (IOException ex) {
        this.m_logger.severe("IOException on file " + database);
    } catch (NullPointerException ex) {
        this.m_logger.warning("Final character in file " + database + " is null and filtered out.");
    }
    long endDate = System.currentTimeMillis();
    this.m_logger.info("Successfully loaded call database " + database);
    this.m_logger.info("Found " + new Integer(this.m_callList.size()).toString() + " call items in " + Float.toString((endDate - startDate) / 1000) + " secs.");
}
Also used : ICall(de.janrufmonitor.framework.ICall) SerializerException(de.janrufmonitor.util.io.SerializerException)

Example 2 with SerializerException

use of de.janrufmonitor.util.io.SerializerException in project janrufmonitor by tbrandt77.

the class HsqldbCallerDatabaseHandler method buildCallerList.

protected ICallerList buildCallerList(IFilter[] filters) 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() + "'");
            }
            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(")))");
            }
        }
    }
    sql.append(";");
    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) Statement(java.sql.Statement) SerializerException(de.janrufmonitor.util.io.SerializerException) ICallerList(de.janrufmonitor.framework.ICallerList) IFilter(de.janrufmonitor.repository.filter.IFilter) AttributeFilter(de.janrufmonitor.repository.filter.AttributeFilter) Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) ResultSet(java.sql.ResultSet) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 3 with SerializerException

use of de.janrufmonitor.util.io.SerializerException 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 4 with SerializerException

use of de.janrufmonitor.util.io.SerializerException in project janrufmonitor by tbrandt77.

the class AbstractCallerDatabaseHandler method insertOrUpdateCallerList.

/**
 * Insert the callers in the list or update the callers if it already exists.
 *
 * @param cl
 * @throws SQLException
 */
public void insertOrUpdateCallerList(ICallerList cl) throws SQLException {
    if (!isConnected())
        try {
            this.connect();
        } catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
    PreparedStatement insert_caller = this.getStatement("INSERT_CALLER");
    PreparedStatement insert_attributes = this.getStatement("INSERT_ATTRIBUTE");
    PreparedStatement update_caller = this.getStatement("UPDATE_CALLER");
    PreparedStatement update_caller_phone = this.getStatement("UPDATE_CALLER_PHONE");
    PreparedStatement update_attributes = this.getStatement("UPDATE_ATTRIBUTE");
    insert_caller.clearBatch();
    insert_attributes.clearBatch();
    update_caller.clearBatch();
    update_caller_phone.clearBatch();
    update_attributes.clearBatch();
    List uuid_check = new ArrayList(cl.size());
    ICaller c = null;
    IPhonenumber pn = null;
    String uuid = null;
    for (int i = 0, j = cl.size(); i < j; i++) {
        c = cl.get(i);
        if (this.m_logger.isLoggable(Level.INFO) && c != null)
            this.m_logger.info("Adding to database: " + c.toString());
        // check if redundant uuid could occure
        uuid = c.getUUID();
        if (uuid_check.contains(uuid)) {
            this.m_logger.warning("Found duplicated UUID: " + c.toString());
            c.setUUID(new UUID().toString());
            uuid = c.getUUID();
        }
        uuid_check.add(uuid);
        pn = c.getPhoneNumber();
        if (this.existsCaller(c)) {
            // do an update
            try {
                this.updateCaller(update_caller, c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber(), pn.getTelephoneNumber(), Serializer.toByteArray(c));
                this.updateAttributes(update_attributes, c.getUUID(), c.getAttributes());
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else if (this.existsCaller(pn)) {
            try {
                this.updateCallerPhone(update_caller_phone, c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber(), pn.getTelephoneNumber(), Serializer.toByteArray(c));
                this.createAttributes(insert_attributes, c.getUUID(), c.getAttributes());
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else {
            // do an insert
            try {
                this.createCaller(insert_caller, c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber(), pn.getTelephoneNumber(), Serializer.toByteArray(c));
                this.createAttributes(insert_attributes, c.getUUID(), c.getAttributes());
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        }
        if (i % this.commit_count == 0) {
            try {
                insert_caller.executeBatch();
                insert_caller.clearBatch();
                insert_attributes.executeBatch();
                insert_attributes.clearBatch();
                update_caller.executeBatch();
                update_caller.clearBatch();
                update_caller_phone.executeBatch();
                update_caller_phone.clearBatch();
                update_attributes.executeBatch();
                update_attributes.clearBatch();
                this.m_logger.info("-------------------> executed Batch");
            } catch (SQLException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage() + c.toString(), e);
            // throw new SQLException("Batch execution failed: ");
            }
        }
    }
    // execute the rest batch content
    insert_caller.executeBatch();
    insert_attributes.executeBatch();
    update_caller.executeBatch();
    update_caller_phone.executeBatch();
    update_attributes.executeBatch();
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) ICallerList(de.janrufmonitor.framework.ICallerList) UUID(de.janrufmonitor.util.uuid.UUID) SerializerException(de.janrufmonitor.util.io.SerializerException) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 5 with SerializerException

use of de.janrufmonitor.util.io.SerializerException in project janrufmonitor by tbrandt77.

the class AbstractMultiPhoneCallerDatabaseHandler method insertOrUpdateCallerList.

/**
 * Insert the callers in the list or update the callers if it already
 * exists.
 *
 * @param cl
 * @throws SQLException
 */
public void insertOrUpdateCallerList(ICallerList cl) throws SQLException {
    if (!isConnected())
        try {
            this.connect();
        } catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
    // check prepared statements
    PreparedStatement insert_caller = this.getStatement("INSERT_CALLER");
    PreparedStatement insert_attributes = this.getStatement("INSERT_ATTRIBUTE");
    PreparedStatement insert_phones = this.getStatement("INSERT_PHONE");
    PreparedStatement update_caller = this.getStatement("UPDATE_CALLER");
    PreparedStatement delete_phones = this.getStatement("DELETE_PHONE");
    PreparedStatement delete_phones2 = this.getStatement("DELETE_PHONE2");
    PreparedStatement delete_attributes = this.getStatement("DELETE_ATTRIBUTE");
    // clean up prpared statements
    insert_caller.clearBatch();
    insert_attributes.clearBatch();
    insert_phones.clearBatch();
    update_caller.clearBatch();
    delete_attributes.clearBatch();
    delete_phones.clearBatch();
    delete_phones2.clearBatch();
    // list for redundant UUIDs checks
    List uuid_check = new ArrayList(cl.size());
    ICaller c = null;
    String uuid = null;
    List phones = new ArrayList(1);
    for (int i = 0, j = cl.size(); i < j; i++) {
        c = cl.get(i);
        if (this.m_logger.isLoggable(Level.INFO) && c != null)
            this.m_logger.info("Adding to database: " + c.toString());
        // check if redundant uuid could occure
        uuid = c.getUUID();
        if (uuid_check.contains(uuid)) {
            this.m_logger.warning("Found duplicated UUID: " + c.toString());
            c.setUUID(new UUID().toString());
            uuid = c.getUUID();
        }
        uuid_check.add(uuid);
        // check which type of Interface the caller object implements
        if (!(c instanceof IMultiPhoneCaller)) {
            if (this.m_logger.isLoggable(Level.INFO) && c != null)
                this.m_logger.info("Caller is not type IMultiPhoneCaller. Transforming is triggered... : " + c.toString());
            c = this.getRuntime().getCallerFactory().toMultiPhoneCaller(c);
            // 2008/11/30: added to fix duplicated address book entries after category assignement
            c.setUUID(uuid);
        }
        phones.clear();
        phones.addAll(((IMultiPhoneCaller) c).getPhonenumbers());
        // check if single caller (uuid) is already in DB
        if (existsCaller(c)) {
            internalUpdate(c);
            if (this.m_logger.isLoggable(Level.INFO) && c != null)
                this.m_logger.info("Caller already exists in database. Update is triggered: " + c.toString());
            try {
                // update caller table
                this.updateCaller(update_caller, c.getUUID(), Serializer.toByteArray(c));
                // update attributes table
                this.deleteAttributes(delete_attributes, c.getUUID());
                this.createAttributes(insert_attributes, c.getUUID(), c.getAttributes());
                // update phones table
                this.deletePhone(delete_phones, c.getUUID());
                IPhonenumber p = null;
                for (int a = 0, b = phones.size(); a < b; a++) {
                    p = (IPhonenumber) phones.get(a);
                    this.createPhone(insert_phones, c.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber(), p.getTelephoneNumber());
                }
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else if (this.existsPhones(phones).size() > 0) {
            internalInsert(c);
            // check if phone numnbers are already in DB and overwrite them
            List existingPhones = this.existsPhones(phones);
            IPhonenumber pn = null;
            for (int a = 0, b = existingPhones.size(); a < b; a++) {
                pn = (IPhonenumber) existingPhones.get(a);
                if (this.m_logger.isLoggable(Level.INFO) && c != null)
                    this.m_logger.info("Phone already exists in database. Update is triggered: " + pn.toString());
                this.deletePhone(delete_phones2, pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber(), pn.getTelephoneNumber());
            }
            try {
                this.createCaller(insert_caller, c.getUUID(), Serializer.toByteArray(c));
                this.createAttributes(insert_attributes, c.getUUID(), c.getAttributes());
                IPhonenumber p = null;
                for (int a = 0, b = phones.size(); a < b; a++) {
                    p = (IPhonenumber) phones.get(a);
                    this.createPhone(insert_phones, c.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber(), p.getTelephoneNumber());
                }
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else {
            internalInsert(c);
            // insert new caller
            try {
                this.createCaller(insert_caller, c.getUUID(), Serializer.toByteArray(c));
                this.createAttributes(insert_attributes, c.getUUID(), c.getAttributes());
                IPhonenumber p = null;
                for (int a = 0, b = phones.size(); a < b; a++) {
                    p = (IPhonenumber) phones.get(a);
                    this.createPhone(insert_phones, c.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber(), p.getTelephoneNumber());
                }
            } catch (SerializerException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage(), e);
            }
        }
        if (i % this.commit_count == 0) {
            try {
                delete_attributes.executeBatch();
                delete_attributes.clearBatch();
                this.m_logger.info("Batch DELETE_ATTRIBUTES executed...");
                delete_phones.executeBatch();
                delete_phones.clearBatch();
                this.m_logger.info("Batch DELETE_PHONES executed...");
                delete_phones2.executeBatch();
                delete_phones2.clearBatch();
                this.m_logger.info("Batch DELETE_PHONES2 executed...");
                insert_caller.executeBatch();
                insert_caller.clearBatch();
                this.m_logger.info("Batch INSERT_CALLER executed...");
                insert_attributes.executeBatch();
                insert_attributes.clearBatch();
                this.m_logger.info("Batch INSERT_ATTRIBUTES executed...");
                insert_phones.executeBatch();
                insert_phones.clearBatch();
                this.m_logger.info("Batch INSERT_PHONES executed...");
                update_caller.executeBatch();
                update_caller.clearBatch();
                this.m_logger.info("Batch UPDATE_CALLER executed...");
            } catch (SQLException e) {
                this.m_logger.log(Level.SEVERE, e.getMessage() + c.toString(), e);
            // throw new SQLException("Batch execution failed: ");
            }
        }
    }
    // execute the rest batch content
    delete_attributes.executeBatch();
    delete_attributes.clearBatch();
    this.m_logger.info("Batch DELETE_ATTRIBUTES executed...");
    delete_phones.executeBatch();
    delete_phones.clearBatch();
    this.m_logger.info("Batch DELETE_PHONES executed...");
    delete_phones2.executeBatch();
    delete_phones2.clearBatch();
    this.m_logger.info("Batch DELETE_PHONES2 executed...");
    insert_caller.executeBatch();
    insert_caller.clearBatch();
    this.m_logger.info("Batch INSERT_CALLER executed...");
    insert_attributes.executeBatch();
    insert_attributes.clearBatch();
    this.m_logger.info("Batch INSERT_ATTRIBUTES executed...");
    insert_phones.executeBatch();
    insert_phones.clearBatch();
    this.m_logger.info("Batch INSERT_PHONES executed...");
    update_caller.executeBatch();
    update_caller.clearBatch();
    this.m_logger.info("Batch UPDATE_CALLER executed...");
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) ICallerList(de.janrufmonitor.framework.ICallerList) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) UUID(de.janrufmonitor.util.uuid.UUID) SerializerException(de.janrufmonitor.util.io.SerializerException) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

SerializerException (de.janrufmonitor.util.io.SerializerException)14 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)7 PreparedStatement (java.sql.PreparedStatement)7 SQLException (java.sql.SQLException)6 ICaller (de.janrufmonitor.framework.ICaller)5 ICallerList (de.janrufmonitor.framework.ICallerList)5 ResultSet (java.sql.ResultSet)5 ICall (de.janrufmonitor.framework.ICall)4 UUID (de.janrufmonitor.util.uuid.UUID)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 IAttribute (de.janrufmonitor.framework.IAttribute)3 Statement (java.sql.Statement)3 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)2 ICallList (de.janrufmonitor.framework.ICallList)2 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)2 CallerNotFoundException (de.janrufmonitor.repository.CallerNotFoundException)2