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.PIMContactDAO; |
---|
27 | import br.com.prognus.psync.items.dao.PIMEntityDAO; |
---|
28 | import br.com.prognus.psync.items.model.ContactWrapper; |
---|
29 | |
---|
30 | import com.funambol.common.pim.contact.Contact; |
---|
31 | import com.funambol.framework.security.Sync4jPrincipal; |
---|
32 | import com.funambol.framework.tools.merge.MergeResult; |
---|
33 | |
---|
34 | public class PIMContactManager extends PIMEntityManager { |
---|
35 | |
---|
36 | // ------------------------------------------------------------- Private |
---|
37 | // data |
---|
38 | |
---|
39 | private PIMContactDAO dao; |
---|
40 | |
---|
41 | // ------------------------------------------------------------- |
---|
42 | // Constructors |
---|
43 | |
---|
44 | /** |
---|
45 | * @see PIMEntityDAO#PIMEntityDAO(String, String) |
---|
46 | */ |
---|
47 | public PIMContactManager(String jndiDataSourceName, |
---|
48 | Sync4jPrincipal principal) { |
---|
49 | |
---|
50 | this.dao = new PIMContactDAO(jndiDataSourceName, principal); |
---|
51 | super.dao = this.dao; |
---|
52 | |
---|
53 | if (log.isTraceEnabled()) { |
---|
54 | log.trace("Created new PIMContactManager for user ID " |
---|
55 | + principal.getUsername() + "\n"); |
---|
56 | } |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | // ----------------------------------------------------------- Public |
---|
61 | // methods |
---|
62 | |
---|
63 | /** |
---|
64 | * Updates a contact. |
---|
65 | * |
---|
66 | * @param uid |
---|
67 | * the UID of the contact |
---|
68 | * @param c |
---|
69 | * a Contact object |
---|
70 | * @param t |
---|
71 | * the last update time that will be forced as the last update |
---|
72 | * time of the updated contact |
---|
73 | * @return the UID of the contact (it should be the same as the argument) |
---|
74 | * @throws EntityException |
---|
75 | */ |
---|
76 | public String updateItem(String uid, Contact c, Timestamp t) |
---|
77 | throws EntityException { |
---|
78 | |
---|
79 | if (log.isTraceEnabled()) { |
---|
80 | log.trace("Updating contact with ID " + uid |
---|
81 | + " on the server, setting " + t |
---|
82 | + " as its last update timestamp."); |
---|
83 | } |
---|
84 | |
---|
85 | ContactWrapper cw = new ContactWrapper(uid, this.dao.getUserId(), c); |
---|
86 | cw.setLastUpdate(t); |
---|
87 | |
---|
88 | try { |
---|
89 | return this.dao.updateItem(cw); |
---|
90 | } catch (PIMDBAccessException dbae) { |
---|
91 | throw new EntityException("Error updating item.", dbae); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * @see PIMContactDAO#addItem(Contact) |
---|
97 | */ |
---|
98 | public String addItem(Contact c, Timestamp t) throws EntityException { |
---|
99 | |
---|
100 | try { |
---|
101 | ContactWrapper cw = createContactWrapper(c); |
---|
102 | cw.setLastUpdate(t); |
---|
103 | this.dao.addItem(cw); |
---|
104 | return cw.getId(); |
---|
105 | } catch (PIMDBAccessException dbae) { |
---|
106 | throw new EntityException("Error adding item.", dbae); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * @see PIMContactDAO#getItem(String) |
---|
112 | */ |
---|
113 | public ContactWrapper getItem(String id) throws EntityException { |
---|
114 | try { |
---|
115 | return this.dao.getItem(id); |
---|
116 | } catch (PIMDBAccessException dbae) { |
---|
117 | throw new EntityException("Error getting item. " + dbae); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @see PIMContactDAO#getTwinItems(Contact) |
---|
123 | */ |
---|
124 | public List getTwins(Contact c) throws EntityException { |
---|
125 | try { |
---|
126 | return this.dao.getTwinItems(c); |
---|
127 | } catch (PIMDBAccessException dbae) { |
---|
128 | throw new EntityException("Error getting twins of an item.", dbae); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | public boolean mergeItems(String serverContactID, Contact clientContact, |
---|
133 | Timestamp t) throws EntityException { |
---|
134 | |
---|
135 | if (log.isTraceEnabled()) { |
---|
136 | log.trace("PIMContactManager mergeItems begin"); |
---|
137 | log.trace("Merging server item " + serverContactID |
---|
138 | + " with its client counterpart."); |
---|
139 | } |
---|
140 | |
---|
141 | try { |
---|
142 | |
---|
143 | ContactWrapper serverContactWrapper = this.dao |
---|
144 | .getItem(serverContactID); |
---|
145 | Contact serverContact = serverContactWrapper.getContact(); |
---|
146 | MergeResult mergeResult = clientContact.merge(serverContact); |
---|
147 | |
---|
148 | if (mergeResult.isSetBRequired()) { |
---|
149 | updateItem(serverContactID, serverContact, t); |
---|
150 | } |
---|
151 | |
---|
152 | if (log.isTraceEnabled()) { |
---|
153 | log.trace("MergeResult: " + mergeResult); |
---|
154 | } |
---|
155 | |
---|
156 | return mergeResult.isSetARequired(); |
---|
157 | |
---|
158 | } catch (Exception e) { |
---|
159 | |
---|
160 | log.error("SyncSource error merging the items", e); |
---|
161 | throw new EntityException("Error merging items. " + e); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | // ---------------------------------------------------------- Private |
---|
166 | // methods |
---|
167 | |
---|
168 | private ContactWrapper createContactWrapper(Contact c) { |
---|
169 | |
---|
170 | if (log.isTraceEnabled()) { |
---|
171 | log.trace("Created a new ContactWrapper (UID not yet generated)."); |
---|
172 | } |
---|
173 | |
---|
174 | return new ContactWrapper(null, this.dao.getUserId(), c); |
---|
175 | } |
---|
176 | |
---|
177 | } |
---|