/** * * @author Diorgenes Felipe Grzesiuk * @copyright Copyright 2007-2008 Prognus * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package br.com.prognus.psync.items.manager; import java.sql.Timestamp; import java.util.List; import br.com.prognus.psync.exception.EntityException; import br.com.prognus.psync.exception.PIMDBAccessException; import br.com.prognus.psync.items.dao.PIMContactDAO; import br.com.prognus.psync.items.dao.PIMEntityDAO; import br.com.prognus.psync.items.model.ContactWrapper; import com.funambol.common.pim.contact.Contact; import com.funambol.framework.security.Sync4jPrincipal; import com.funambol.framework.tools.merge.MergeResult; public class PIMContactManager extends PIMEntityManager { // ------------------------------------------------------------- Private // data private PIMContactDAO dao; // ------------------------------------------------------------- // Constructors /** * @see PIMEntityDAO#PIMEntityDAO(String, String) */ public PIMContactManager(String jndiDataSourceName, Sync4jPrincipal principal) { this.dao = new PIMContactDAO(jndiDataSourceName, principal); super.dao = this.dao; if (log.isTraceEnabled()) { log.trace("Created new PIMContactManager for user ID " + principal.getUsername() + "\n"); } } // ----------------------------------------------------------- Public // methods /** * Updates a contact. * * @param uid * the UID of the contact * @param c * a Contact object * @param t * the last update time that will be forced as the last update * time of the updated contact * @return the UID of the contact (it should be the same as the argument) * @throws EntityException */ public String updateItem(String uid, Contact c, Timestamp t) throws EntityException { if (log.isTraceEnabled()) { log.trace("Updating contact with ID " + uid + " on the server, setting " + t + " as its last update timestamp."); } ContactWrapper cw = new ContactWrapper(uid, this.dao.getUserId(), c); cw.setLastUpdate(t); try { return this.dao.updateItem(cw); } catch (PIMDBAccessException dbae) { throw new EntityException("Error updating item.", dbae); } } /** * @see PIMContactDAO#addItem(Contact) */ public String addItem(Contact c, Timestamp t) throws EntityException { try { ContactWrapper cw = createContactWrapper(c); cw.setLastUpdate(t); this.dao.addItem(cw); return cw.getId(); } catch (PIMDBAccessException dbae) { throw new EntityException("Error adding item.", dbae); } } /** * @see PIMContactDAO#getItem(String) */ public ContactWrapper getItem(String id) throws EntityException { try { return this.dao.getItem(id); } catch (PIMDBAccessException dbae) { throw new EntityException("Error getting item. " + dbae); } } /** * @see PIMContactDAO#getTwinItems(Contact) */ public List getTwins(Contact c) throws EntityException { try { return this.dao.getTwinItems(c); } catch (PIMDBAccessException dbae) { throw new EntityException("Error getting twins of an item.", dbae); } } public boolean mergeItems(String serverContactID, Contact clientContact, Timestamp t) throws EntityException { if (log.isTraceEnabled()) { log.trace("PIMContactManager mergeItems begin"); log.trace("Merging server item " + serverContactID + " with its client counterpart."); } try { ContactWrapper serverContactWrapper = this.dao .getItem(serverContactID); Contact serverContact = serverContactWrapper.getContact(); MergeResult mergeResult = clientContact.merge(serverContact); if (mergeResult.isSetBRequired()) { updateItem(serverContactID, serverContact, t); } if (log.isTraceEnabled()) { log.trace("MergeResult: " + mergeResult); } return mergeResult.isSetARequired(); } catch (Exception e) { log.error("SyncSource error merging the items", e); throw new EntityException("Error merging items. " + e); } } // ---------------------------------------------------------- Private // methods private ContactWrapper createContactWrapper(Contact c) { if (log.isTraceEnabled()) { log.trace("Created a new ContactWrapper (UID not yet generated)."); } return new ContactWrapper(null, this.dao.getUserId(), c); } }