/** * * @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.PIMCalendarDAO; import br.com.prognus.psync.items.dao.PIMEntityDAO; import br.com.prognus.psync.items.model.CalendarWrapper; import com.funambol.common.pim.calendar.Calendar; import com.funambol.framework.security.Sync4jPrincipal; import com.funambol.framework.tools.merge.MergeResult; public class PIMCalendarManager extends PIMEntityManager { // ------------------------------------------------------------- Private // data private PIMCalendarDAO dao; // ------------------------------------------------------------- // Constructors /** * @see PIMEntityDAO#PIMEntityDAO(String, String) */ public PIMCalendarManager(String jndiDataSourceName, Sync4jPrincipal principal) { this.dao = new PIMCalendarDAO(jndiDataSourceName, principal); super.dao = this.dao; log.trace("Created new PIMCalendarManager for user ID " + principal.getUsername()); } // ----------------------------------------------------------- Public // methods /** * Updates a calendar. * * @param uid * the UID of the calendar * @param c * a Calendar object * @param t * the last update time that will be forced as the last update * time of the updated calendar * @return the UID of the calendar (it should be the same as the argument) * @throws EntityException */ public String updateItem(String uid, Calendar c, Timestamp t) throws EntityException { log.trace("Updating calendar with ID " + uid + " on the server, setting " + t + " as its last update timestamp."); CalendarWrapper cw = new CalendarWrapper(uid, this.dao.getUserId(), c); cw.setLastUpdate(t); try { this.dao.updateItem(cw); return cw.getId(); } catch (Exception e) { throw new EntityException("Error updating item.", e); } } /** * @see PIMCalendarDAO#addItem(Calendar) */ public String addItem(Calendar c, Timestamp t) throws EntityException { try { CalendarWrapper cw = createCalendarWrapper(c); cw.setLastUpdate(t); this.dao.addItem(cw); return cw.getId(); } catch (PIMDBAccessException dbae) { throw new EntityException("Error adding item.", dbae); } } /** * @see PIMCalendarDAO#getItem(String) */ public CalendarWrapper getItem(String id) throws EntityException { try { return this.dao.getItem(id); } catch (PIMDBAccessException dbae) { throw new EntityException("Error getting item. " + dbae); } } /** * @see PIMCalendarDAO#getTwinItems(Calendar) */ public List getTwins(Calendar 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 serverCalendarID, Calendar clientCalendar, Timestamp t) throws EntityException { log.info("\n\n=> PIMCalendarManager mergeItems begin"); log.info("\n\n=> Merging server item " + serverCalendarID + " with its client counterpart."); try { CalendarWrapper serverCalendarWrapper = this.dao .getItem(serverCalendarID); Calendar serverCalendar = serverCalendarWrapper.getCalendar(); MergeResult mergeResult = clientCalendar.merge(serverCalendar); if (mergeResult.isSetBRequired()) { updateItem(serverCalendarID, serverCalendar, t); } log.info("MergeResult: " + mergeResult); return mergeResult.isSetARequired(); } catch (Exception e) { log.error("Error merging items.", e); throw new EntityException("Error merging items. " + e); } } // ---------------------------------------------------------- Private // methods private CalendarWrapper createCalendarWrapper(Calendar c) { log.info("Created a new CalendarWrapper (UID not yet generated)."); return new CalendarWrapper(null, this.dao.getUserId(), c); } }