KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > store > ClientMappingPersistentStore


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.server.store;
19
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import java.sql.*;
25
26 import sync4j.framework.tools.DBTools;
27
28 import sync4j.framework.server.ClientMapping;
29 import sync4j.framework.server.store.Clause;
30 import sync4j.framework.server.store.PersistentStore;
31 import sync4j.framework.server.store.BasePersistentStore;
32 import sync4j.framework.server.store.NotFoundException;
33 import sync4j.framework.server.store.PersistentStoreException;
34
35 /**
36  * This is the store for information regarding the synchronization process and
37  * status. It persists the following classes:
38  *
39  * <ul>
40  * <li>sync4j.framework.server.ClientMapping</li>
41  * </ul>
42  *
43  * This <i>PersistentStore</i> is configured with the following map key:
44  * <ul>
45  * <li>jndiDataSourceName</li>
46  * </ul>
47  *
48  * @author Stefano Fornari @ Funambol
49  * @author Harrie Hazewinkel
50  * @version $Id: ClientMappingPersistentStore.java,v 1.6 2005/04/20 23:19:13 stefano_fornari Exp $
51  */

52 public class ClientMappingPersistentStore
53 extends BasePersistentStore
54 implements PersistentStore, java.io.Serializable JavaDoc {
55
56     // --------------------------------------------------------------- Constants
57

58     // -------------------------------------------------------------- Properties
59
// --------------------------------------------- configurable SQL properties
60
private String JavaDoc sqlInsertClientMapping =
61     "insert into sync4j_client_mapping (principal, sync_source, guid, luid) values(?, ?, ?, ?)";
62     private String JavaDoc sqlDeleteClientMapping =
63     "delete from sync4j_client_mapping where principal=? and sync_source=? and luid=?";
64     private String JavaDoc sqlUpdateClientMapping =
65     "update sync4j_client_mapping set luid=? where principal=? and sync_source=? and guid=?";
66     private String JavaDoc sqlSelectClientMapping =
67     "select luid,guid from sync4j_client_mapping where principal=? and sync_source=?";
68
69     /** Getter for property sqlInsertClientMapping.
70      * @return Value of property sqlInsertClientMapping.
71      *
72      */

73     public String JavaDoc getSqlInsertClientMapping() {
74         return sqlInsertClientMapping;
75     }
76
77     /** Setter for property sqlInsertClientMapping.
78      * @param sqlInsertClientMapping New value of property sqlInsertClientMapping.
79      *
80      */

81     public void setSqlInsertClientMapping(String JavaDoc sqlInsertClientMapping) {
82         this.sqlInsertClientMapping = sqlInsertClientMapping;
83     }
84
85     /** Getter for property sqlDeleteClientMapping.
86      * @return Value of property sqlDeleteClientMapping.
87      *
88      */

89     public String JavaDoc getSqlDeleteClientMapping() {
90         return sqlDeleteClientMapping;
91     }
92
93     /** Setter for property sqlDeleteClientMapping.
94      * @param sqlDeleteClientMapping New value of property sqlDeleteClientMapping.
95      *
96      */

97     public void setSqlDeleteClientMapping(String JavaDoc sqlDeleteClientMapping) {
98         this.sqlDeleteClientMapping = sqlDeleteClientMapping;
99     }
100
101     /** Getter for property sqlUpdateClientMapping.
102      * @return Value of property sqlUpdateClientMapping.
103      *
104      */

105     public String JavaDoc getSqlUpdateClientMapping() {
106         return sqlUpdateClientMapping;
107     }
108
109     /** Setter for property sqlUpdateClientMapping.
110      * @param sqlUpdateClientMapping New value of property sqlUpdateClientMapping.
111      *
112      */

113     public void setSqlUpdateClientMapping(String JavaDoc sqlUpdateClientMapping) {
114         this.sqlUpdateClientMapping = sqlUpdateClientMapping;
115     }
116
117     /** Getter for property sqlSelectClientMapping.
118      * @return Value of property sqlSelectClientMapping.
119      *
120      */

121     public String JavaDoc getSqlSelectClientMapping() {
122         return sqlSelectClientMapping;
123     }
124
125     /** Setter for property sqlSelectClientMapping.
126      * @param sqlSelectClientMapping New value of property sqlSelectClientMapping.
127      *
128      */

129     public void setSqlSelectClientMapping(String JavaDoc sqlSelectClientMapping) {
130         this.sqlSelectClientMapping = sqlSelectClientMapping;
131     }
132
133     // ------------------------------------------------------------ Private data
134

135     // ------------------------------------------------------------ Constructors
136

137     // ---------------------------------------------------------- Public methods
138

139     public boolean store(Object JavaDoc o)
140     throws PersistentStoreException {
141         if (o instanceof ClientMapping) {
142             ClientMapping clientMapping = (ClientMapping) o;
143
144             Connection conn = null;
145             PreparedStatement stmt = null, stmtIns = null;
146
147             long principal = -1;
148             String JavaDoc dbURI = clientMapping.getDbURI();
149             
150             principal = Long.parseLong(clientMapping.getPrincipal().getId());
151
152             assert ((principal >= 0) && (dbURI != null));
153
154             try {
155                 conn = dataSource.getConnection();
156
157                 if (clientMapping.isDeleted()) {
158                     stmt = conn.prepareStatement(sqlDeleteClientMapping);
159                     stmt.setLong(1, principal);
160                     stmt.setString(2, dbURI );
161                     String JavaDoc[] luids = clientMapping.getDeletedLuids();
162                     for (int i=0; i < luids.length; ++i) {
163                         stmt.setString(3, luids[i]);
164                         stmt.executeUpdate();
165                     }
166                     stmt.close(); stmt = null;
167                 }
168
169                 if (clientMapping.isModified()) {
170                     stmt = conn.prepareStatement(sqlUpdateClientMapping);
171                     stmt.setLong(2, principal);
172                     stmt.setString(3, dbURI );
173                     String JavaDoc[] luids = clientMapping.getModifiedLuids();
174
175                     String JavaDoc guid = null;
176                     for (int i=0; i < luids.length; ++i) {
177                         int n = -1;
178
179                         guid = clientMapping.getMappedValueForLuid(luids[i]);
180                         stmt.setString(1, luids[i]);
181                         stmt.setString(4, guid );
182                         n = stmt.executeUpdate();
183
184                         if (n == 0) {
185                             //
186
// insert new mapping
187
//
188
stmtIns = conn.prepareStatement(sqlInsertClientMapping);
189                             stmtIns.setLong(1, principal);
190                             stmtIns.setString(2, dbURI );
191                             stmtIns.setString(3, guid);
192                             stmtIns.setString(4, luids[i]);
193                             stmtIns.executeUpdate();
194                             stmtIns.close(); stmtIns = null;
195                         }
196                     }
197                     stmt.close(); stmt = null;
198                 }
199             } catch (SQLException e) {
200                 throw new PersistentStoreException("Error storing client mapping", e);
201             } finally {
202                 DBTools.close(conn, stmt, null);
203             }
204             return true;
205         }
206         return false;
207     }
208
209     public boolean read(Object JavaDoc o)
210     throws PersistentStoreException {
211         if (o instanceof ClientMapping) {
212             ClientMapping clientMapping = (ClientMapping) o;
213
214             Connection conn = null;
215             PreparedStatement stmt = null;
216             ResultSet rs = null;
217
218             try {
219                 conn = dataSource.getConnection();
220
221                 stmt = conn.prepareStatement(sqlSelectClientMapping);
222                 stmt.setLong(1, Long.parseLong(clientMapping.getPrincipal().getId()));
223                 stmt.setString(2, clientMapping.getDbURI() );
224                 rs = stmt.executeQuery();
225
226                 HashMap JavaDoc mapping = new HashMap JavaDoc();
227                 while (rs.next()) {
228                     mapping.put(rs.getString("guid"), rs.getString("luid"));
229                 }
230                 clientMapping.initializeFromMapping(mapping);
231             } catch (SQLException e) {
232                 throw new PersistentStoreException("Error reading mapping", e);
233             } finally {
234                 DBTools.close(conn, stmt, rs);
235             }
236             return true;
237         }
238         return false;
239     }
240
241     /** Read all objects stored the persistent media.
242      *
243      * @param objClass the object class handled by the persistent store
244      *
245      * @return an array containing the objects read. If no objects are found an
246      * empty array is returned. If the persistent store has not
247      * processed the quest, null is returned.
248      *
249      * @throws PersistentStoreException
250      *
251      */

252     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
253         //
254
// TO DO (not used yet)
255
//
256
return null;
257     }
258
259     public boolean delete(Object JavaDoc o) throws PersistentStoreException
260     {
261         return false;
262     }
263
264     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException
265     {
266         return null;
267     }
268
269     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException
270     {
271         return -1;
272     }
273
274     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException
275     {
276         return false;
277     }
278
279 }
280
Popular Tags