1 | /** |
---|
2 | * |
---|
3 | * @author Diorgenes Felipe Grzesiuk <diorgenes@prognus.com.br> |
---|
4 | * @copyright Copyright 2007-2008 Prognus |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License version 2 as published by |
---|
8 | * the Free Software Foundation. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with Foobar; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | package br.com.prognus.psync.items.manager; |
---|
20 | |
---|
21 | import java.sql.Timestamp; |
---|
22 | import java.util.List; |
---|
23 | |
---|
24 | import br.com.prognus.psync.exception.EntityException; |
---|
25 | import br.com.prognus.psync.exception.PIMDBAccessException; |
---|
26 | import br.com.prognus.psync.items.dao.PIMCalendarDAO; |
---|
27 | import br.com.prognus.psync.items.dao.PIMEntityDAO; |
---|
28 | import br.com.prognus.psync.items.model.CalendarWrapper; |
---|
29 | |
---|
30 | import com.funambol.common.pim.calendar.Calendar; |
---|
31 | import com.funambol.framework.security.Sync4jPrincipal; |
---|
32 | import com.funambol.framework.tools.merge.MergeResult; |
---|
33 | |
---|
34 | public class PIMCalendarManager extends PIMEntityManager { |
---|
35 | |
---|
36 | // ------------------------------------------------------------- Private |
---|
37 | // data |
---|
38 | |
---|
39 | private PIMCalendarDAO dao; |
---|
40 | |
---|
41 | // ------------------------------------------------------------- |
---|
42 | // Constructors |
---|
43 | |
---|
44 | /** |
---|
45 | * @see PIMEntityDAO#PIMEntityDAO(String, String) |
---|
46 | */ |
---|
47 | public PIMCalendarManager(String jndiDataSourceName, |
---|
48 | Sync4jPrincipal principal) { |
---|
49 | |
---|
50 | this.dao = new PIMCalendarDAO(jndiDataSourceName, principal); |
---|
51 | super.dao = this.dao; |
---|
52 | |
---|
53 | log.trace("Created new PIMCalendarManager for user ID " |
---|
54 | + principal.getUsername()); |
---|
55 | } |
---|
56 | |
---|
57 | // ----------------------------------------------------------- Public |
---|
58 | // methods |
---|
59 | |
---|
60 | /** |
---|
61 | * Updates a calendar. |
---|
62 | * |
---|
63 | * @param uid |
---|
64 | * the UID of the calendar |
---|
65 | * @param c |
---|
66 | * a Calendar object |
---|
67 | * @param t |
---|
68 | * the last update time that will be forced as the last update |
---|
69 | * time of the updated calendar |
---|
70 | * @return the UID of the calendar (it should be the same as the argument) |
---|
71 | * @throws EntityException |
---|
72 | */ |
---|
73 | public String updateItem(String uid, Calendar c, Timestamp t) |
---|
74 | throws EntityException { |
---|
75 | |
---|
76 | log.trace("Updating calendar with ID " + uid |
---|
77 | + " on the server, setting " + t |
---|
78 | + " as its last update timestamp."); |
---|
79 | |
---|
80 | CalendarWrapper cw = new CalendarWrapper(uid, this.dao.getUserId(), c); |
---|
81 | cw.setLastUpdate(t); |
---|
82 | |
---|
83 | try { |
---|
84 | this.dao.updateItem(cw); |
---|
85 | return cw.getId(); |
---|
86 | } catch (Exception e) { |
---|
87 | throw new EntityException("Error updating item.", e); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * @see PIMCalendarDAO#addItem(Calendar) |
---|
93 | */ |
---|
94 | public String addItem(Calendar c, Timestamp t) throws EntityException { |
---|
95 | |
---|
96 | try { |
---|
97 | CalendarWrapper cw = createCalendarWrapper(c); |
---|
98 | cw.setLastUpdate(t); |
---|
99 | this.dao.addItem(cw); |
---|
100 | return cw.getId(); |
---|
101 | } catch (PIMDBAccessException dbae) { |
---|
102 | throw new EntityException("Error adding item.", dbae); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * @see PIMCalendarDAO#getItem(String) |
---|
108 | */ |
---|
109 | public CalendarWrapper getItem(String id) throws EntityException { |
---|
110 | try { |
---|
111 | return this.dao.getItem(id); |
---|
112 | } catch (PIMDBAccessException dbae) { |
---|
113 | throw new EntityException("Error getting item. " + dbae); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * @see PIMCalendarDAO#getTwinItems(Calendar) |
---|
119 | */ |
---|
120 | public List getTwins(Calendar c) throws EntityException { |
---|
121 | try { |
---|
122 | return this.dao.getTwinItems(c); |
---|
123 | } catch (PIMDBAccessException dbae) { |
---|
124 | throw new EntityException("Error getting twins of an item.", dbae); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public boolean mergeItems(String serverCalendarID, Calendar clientCalendar, |
---|
129 | Timestamp t) throws EntityException { |
---|
130 | |
---|
131 | log.info("\n\n=> PIMCalendarManager mergeItems begin"); |
---|
132 | log.info("\n\n=> Merging server item " + serverCalendarID |
---|
133 | + " with its client counterpart."); |
---|
134 | |
---|
135 | try { |
---|
136 | CalendarWrapper serverCalendarWrapper = this.dao |
---|
137 | .getItem(serverCalendarID); |
---|
138 | Calendar serverCalendar = serverCalendarWrapper.getCalendar(); |
---|
139 | MergeResult mergeResult = clientCalendar.merge(serverCalendar); |
---|
140 | if (mergeResult.isSetBRequired()) { |
---|
141 | updateItem(serverCalendarID, serverCalendar, t); |
---|
142 | } |
---|
143 | |
---|
144 | log.info("MergeResult: " + mergeResult); |
---|
145 | |
---|
146 | return mergeResult.isSetARequired(); |
---|
147 | |
---|
148 | } catch (Exception e) { |
---|
149 | |
---|
150 | log.error("Error merging items.", e); |
---|
151 | throw new EntityException("Error merging items. " + e); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | // ---------------------------------------------------------- Private |
---|
156 | // methods |
---|
157 | |
---|
158 | private CalendarWrapper createCalendarWrapper(Calendar c) { |
---|
159 | |
---|
160 | log.info("Created a new CalendarWrapper (UID not yet generated)."); |
---|
161 | return new CalendarWrapper(null, this.dao.getUserId(), c); |
---|
162 | } |
---|
163 | } |
---|