Search in sources :

Example 26 with ICallerList

use of de.janrufmonitor.framework.ICallerList 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 27 with ICallerList

use of de.janrufmonitor.framework.ICallerList 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)

Example 28 with ICallerList

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

the class AbstractDatabaseCallerManager method getCallers.

public ICallerList getCallers(IFilter[] filters, ISearchTerm[] searchTerms) {
    try {
        m_isRunningProcess = true;
        ICallerList cl = getDatabaseHandler().getCallerList(filters, searchTerms);
        m_isRunningProcess = false;
        return cl;
    } catch (SQLException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
    }
    m_isRunningProcess = false;
    return this.getRuntime().getCallerFactory().createCallerList();
}
Also used : ICallerList(de.janrufmonitor.framework.ICallerList) SQLException(java.sql.SQLException)

Example 29 with ICallerList

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

the class AbstractDatabaseCallerManager method updateCaller.

public synchronized void updateCaller(ICaller caller) {
    ICallerList cl = this.getRuntime().getCallerFactory().createCallerList(1);
    cl.add(caller);
    try {
        this.addSystemAttributes(caller);
        m_isRunningProcess = true;
        getDatabaseHandler().insertOrUpdateCallerList(cl);
        getDatabaseHandler().commit();
    } catch (SQLException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        try {
            getDatabaseHandler().rollback();
        } catch (SQLException e1) {
            this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
        }
    }
    m_isRunningProcess = false;
}
Also used : ICallerList(de.janrufmonitor.framework.ICallerList) SQLException(java.sql.SQLException)

Example 30 with ICallerList

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

the class EabFileCallerImporter method doImport.

public ICallerList doImport() {
    this.m_current = 0;
    m_callerList = PIMRuntime.getInstance().getCallerFactory().createCallerList();
    Date startDate = new Date();
    File db = new File(m_filename);
    try {
        FileInputStream in = new FileInputStream(db);
        Properties addressbook = new Properties();
        addressbook.load(in);
        in.close();
        m_total = Integer.parseInt(addressbook.getProperty("total", "1"));
        boolean hasMore = true;
        int i = 1;
        String key = null;
        while (hasMore && addressbook.size() > 0) {
            key = addressbook.getProperty(i + ".phonecount");
            addressbook.remove(i + ".phonecount");
            // found old format
            if (key == null) {
                key = addressbook.getProperty(i + ".phone");
                addressbook.remove(i + ".phone");
                if (key != null && key.length() > 0) {
                    IPhonenumber pn = PIMRuntime.getInstance().getCallerFactory().createPhonenumber(false);
                    pn.setCallNumber(key);
                    pn.setAreaCode(addressbook.getProperty(i + ".area"));
                    addressbook.remove(i + ".area");
                    key = addressbook.getProperty(i + ".intarea");
                    addressbook.remove(i + ".intarea");
                    if (key == null || key.length() == 0)
                        key = PIMRuntime.getInstance().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_INTAREA);
                    pn.setIntAreaCode(key);
                    IName name = PIMRuntime.getInstance().getCallerFactory().createName("", "");
                    IAttributeMap m = PIMRuntime.getInstance().getCallerFactory().createAttributeMap();
                    Enumeration en = addressbook.keys();
                    List keys = new ArrayList();
                    while (en.hasMoreElements()) {
                        key = (String) en.nextElement();
                        if (key.startsWith(i + ".")) {
                            keys.add(key);
                            m.add(PIMRuntime.getInstance().getCallerFactory().createAttribute(key.substring((i + ".").length()), addressbook.getProperty(key)));
                        }
                    }
                    if (keys.size() > 0) {
                        for (int j = 0; j < keys.size(); j++) {
                            addressbook.remove(keys.get(j));
                        }
                        keys.clear();
                        keys = null;
                    }
                    ICaller caller = PIMRuntime.getInstance().getCallerFactory().createCaller(name, pn);
                    caller.setAttributes(m);
                    this.m_callerList.add(caller);
                    i++;
                } else {
                    this.m_logger.info("Found no more valid keys. Left data: " + addressbook);
                    hasMore = false;
                }
            } else {
                // new format since 5.0.0
                int phonecount = Integer.parseInt(key);
                List phones = new ArrayList(1);
                ICaller caller = null;
                for (int a = 0; a < phonecount; a++) {
                    key = addressbook.getProperty(i + ".phone." + a);
                    addressbook.remove(i + ".phone." + a);
                    if (key != null && key.length() > 0) {
                        IPhonenumber pn = PIMRuntime.getInstance().getCallerFactory().createPhonenumber(false);
                        pn.setCallNumber(key);
                        pn.setAreaCode(addressbook.getProperty(i + ".area." + a));
                        addressbook.remove(i + ".area." + a);
                        key = addressbook.getProperty(i + ".intarea." + a);
                        addressbook.remove(i + ".intarea." + a);
                        if (key == null || key.length() == 0)
                            key = PIMRuntime.getInstance().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_INTAREA);
                        pn.setIntAreaCode(key);
                        phones.add(pn);
                    }
                }
                IName name = PIMRuntime.getInstance().getCallerFactory().createName("", "");
                IAttributeMap m = PIMRuntime.getInstance().getCallerFactory().createAttributeMap();
                Enumeration en = addressbook.keys();
                List keys = new ArrayList();
                while (en.hasMoreElements()) {
                    key = (String) en.nextElement();
                    if (key.startsWith(i + ".")) {
                        keys.add(key);
                        m.add(PIMRuntime.getInstance().getCallerFactory().createAttribute(key.substring((i + ".").length()), addressbook.getProperty(key)));
                    }
                }
                if (keys.size() > 0) {
                    for (int j = 0; j < keys.size(); j++) {
                        addressbook.remove(keys.get(j));
                    }
                    keys.clear();
                    keys = null;
                }
                caller = PIMRuntime.getInstance().getCallerFactory().createCaller(name, phones);
                caller.setAttributes(m);
                this.m_callerList.add(caller);
                i++;
                this.m_current++;
            }
        }
    } catch (FileNotFoundException ex) {
        this.m_logger.warning("Cannot find caller backup file " + m_filename);
        return this.m_callerList;
    } catch (IOException ex) {
        this.m_logger.severe("IOException on file " + m_filename);
        return this.m_callerList;
    }
    Date endDate = new Date();
    this.m_logger.info("Successfully imported caller file " + m_filename);
    this.m_logger.info("Found " + new Integer(this.m_callerList.size()).toString() + " caller items in " + new Float((endDate.getTime() - startDate.getTime()) / 1000).toString() + " secs.");
    return this.m_callerList;
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) FileInputStream(java.io.FileInputStream) ICaller(de.janrufmonitor.framework.ICaller) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) List(java.util.List) ICallerList(de.janrufmonitor.framework.ICallerList) File(java.io.File) IName(de.janrufmonitor.framework.IName) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

ICallerList (de.janrufmonitor.framework.ICallerList)81 ICaller (de.janrufmonitor.framework.ICaller)40 List (java.util.List)36 ArrayList (java.util.ArrayList)32 IAttribute (de.janrufmonitor.framework.IAttribute)24 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)24 SQLException (java.sql.SQLException)24 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)18 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)17 IOException (java.io.IOException)15 Message (de.janrufmonitor.exception.Message)14 Iterator (java.util.Iterator)11 Map (java.util.Map)11 File (java.io.File)10 HashMap (java.util.HashMap)10 Viewer (org.eclipse.jface.viewers.Viewer)10 ICallerManager (de.janrufmonitor.repository.ICallerManager)9 ComFailException (com.jacob.com.ComFailException)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)8 AttributeFilter (de.janrufmonitor.repository.filter.AttributeFilter)7