KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package sync4j.server.store;
20
21 import java.util.ArrayList JavaDoc;
22 import java.sql.*;
23
24 import sync4j.framework.tools.Base64;
25 import sync4j.framework.tools.DBTools;
26 import sync4j.framework.server.store.*;
27 import sync4j.framework.server.Sync4jDevice;
28 import sync4j.framework.server.Capabilities;
29
30 import sync4j.server.tools.IdSpaceGenerator;
31
32 /**
33  * This is the store for device information in the Sync4j engine.
34  * Currently it persistes the following classes:
35  * <ul>
36  * <li>sync4j.server.engine.Sync4jDevice</li>
37  * </ul>
38  *
39  * @author Stefano Fornari @ Funambol
40  *
41  * @version $Id: DevicePersistentStore.java,v 1.15 2005/07/25 15:26:56 nichele Exp $
42  *
43  */

44 public class DevicePersistentStore
45 extends BasePersistentStore
46 implements PersistentStore, java.io.Serializable JavaDoc {
47
48     // --------------------------------------------------------------- Constants
49

50     private static final String JavaDoc DEFAULT_SERVER_PASSWORD = "sync4j";
51
52     private static final String JavaDoc OPT_INSERT = "INSERT";
53
54     public static final int SQL_SELECT_ALL_DEVICES = 0;
55     public static final int SQL_GET_DEVICE = 1;
56     public static final int SQL_INSERT_DEVICE = 2;
57     public static final int SQL_UPDATE_DEVICE = 3;
58     public static final int SQL_DELETE_DEVICE = 4;
59     public static final int SQL_COUNT_DEVICES = 5;
60
61     public static final String JavaDoc NS_DEVICE = "device" ;
62
63     // -------------------------------------------------------------- Properties
64

65     protected String JavaDoc[] sql = null;
66
67     public void setSql(String JavaDoc[] sql) {
68         this.sql = sql;
69     }
70
71     public String JavaDoc[] getSql() {
72         return this.sql;
73     }
74
75     // ------------------------------------------------------------ Private data
76
private static final String JavaDoc SEARCH_COUNT_DEVICES = "SCD";
77
78     // ------------------------------------------------------------ Constructors
79
// ---------------------------------------------------------- Public methods
80

81     /**
82      * Delete the device from the data store.
83      *
84      * @param o The object that must be a device for this method
85      *
86      * @throws PersistentException in case of error reading the data store
87      */

88     public boolean delete(Object JavaDoc o) throws PersistentStoreException {
89         if (o instanceof Sync4jDevice) {
90             Sync4jDevice d = (Sync4jDevice) o;
91
92             Connection conn = null;
93             PreparedStatement stmt = null;
94
95             try {
96                 conn = dataSource.getConnection();
97
98                 stmt = conn.prepareStatement(sql[SQL_DELETE_DEVICE]);
99                 stmt.setString(1, d.getDeviceId());
100                 stmt.executeUpdate();
101
102                 return true;
103             } catch (SQLException e) {
104                 throw new PersistentStoreException("Error deleting the device " + d, e);
105             } finally {
106                 DBTools.close(conn, stmt, null);
107             }
108         }
109         return false;
110     }
111
112     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException {
113         return false;
114     }
115
116     /**
117      * Insert or Update the device from the data store.
118      *
119      * @param o The object that must be a device for this method
120      *
121      * @throws PersistentException in case of error reading the data store
122      */

123     public boolean store(Object JavaDoc o) throws PersistentStoreException {
124         if (o instanceof Sync4jDevice) {
125             Sync4jDevice d = (Sync4jDevice) o;
126
127             Connection conn = null;
128             PreparedStatement stmt = null;
129             int n = 0;
130
131             try {
132                 conn = dataSource.getConnection();
133
134                 String JavaDoc serverPwd = DEFAULT_SERVER_PASSWORD;
135
136                 if (d.getServerPassword() != null) {
137                     serverPwd = d.getServerPassword();
138                 }
139
140                 if (d.getDeviceId() != null && !d.getDeviceId().equals("")) {
141                     stmt = conn.prepareStatement(sql[SQL_UPDATE_DEVICE]);
142
143                     stmt.setString(1, d.getDescription());
144                     stmt.setString(2, d.getType());
145                     byte[] nonce = d.getClientNonce();
146                     stmt.setString(3, ((nonce != null) ? new String JavaDoc(Base64.encode(nonce)) : null));
147                     nonce = d.getServerNonce();
148                     stmt.setString(4, ((nonce != null) ? new String JavaDoc(Base64.encode(nonce)) : null));
149                     stmt.setString(5, serverPwd);
150                     stmt.setString(6, d.getTimeZone());
151                     stmt.setString(7, (d.getConvertDate() ? "Y" : "N" ));
152                     stmt.setString(8, d.getCharset());
153                     stmt.setString(9, d.getDeviceId());
154
155                     n = stmt.executeUpdate();
156                     stmt.close(); stmt = null;
157                 }
158
159                 if (n == 0) {
160                     // The first time!!!
161
if (d.getDeviceId() == null || d.getDeviceId().equals("")) {
162                         int deviceId = getDeviceId();
163                         d.setDeviceId("" + deviceId);
164                     }
165
166                     stmt = conn.prepareStatement(sql[SQL_INSERT_DEVICE]);
167                     stmt.setString(1, d.getDeviceId());
168                     stmt.setString(2, d.getDescription());
169                     stmt.setString(3, d.getType());
170                     byte[] nonce = d.getClientNonce();
171                     stmt.setString(4, ((nonce != null) ? new String JavaDoc(Base64.encode(nonce)) : null));
172                     nonce = d.getServerNonce();
173                     stmt.setString(5, ((nonce != null) ? new String JavaDoc(Base64.encode(nonce)) : null));
174
175                     stmt.setString(6, serverPwd);
176
177                     //
178
// Set id_caps = 2 like default value
179
//
180
stmt.setInt(7, 2);
181                     stmt.setString(8, d.getTimeZone());
182                     stmt.setString(9, (d.getConvertDate() ? "Y" : "N" ));
183                     stmt.setString(10, d.getCharset());
184
185                     stmt.executeUpdate();
186                 }
187             } catch (SQLException e) {
188                 throw new PersistentStoreException("Error storing the device " + d, e);
189             } finally {
190                 DBTools.close(conn, stmt, null);
191             }
192             return true;
193         }
194         return false;
195     }
196
197     /**
198      * Read the device from the data store.
199      *
200      * @param o The object that must be a device for this method
201      *
202      * @throws PersistentException in case of error reading the data store
203      */

204     public boolean read(Object JavaDoc o) throws PersistentStoreException {
205         if (o instanceof Sync4jDevice) {
206             Sync4jDevice d = (Sync4jDevice) o;
207
208             Connection conn = null;
209             PreparedStatement stmt = null;
210             ResultSet rs = null;
211
212             try {
213                 conn = dataSource.getConnection();
214
215                 stmt = conn.prepareStatement(sql[SQL_GET_DEVICE]);
216                 stmt.setString(1, d.getDeviceId());
217                 rs = stmt.executeQuery();
218
219                 if (rs.next() == false) {
220                     throw new NotFoundException("Device not found for '"
221                     + d.getDeviceId() + "'");
222                 }
223
224                 d.setDescription(rs.getString(1));
225                 d.setType (rs.getString(2));
226                 String JavaDoc value = rs.getString(3);
227                 if (value == null) {
228                     d.setClientNonce(null);
229                 } else if (value.equals("")) {
230                     d.setClientNonce(new byte[0]);
231                 } else {
232                     d.setClientNonce(Base64.decode(value.getBytes()));
233                 }
234                 value = rs.getString(4);
235                 if (value == null) {
236                     d.setServerNonce(null);
237                 } else if (value.equals("")) {
238                     d.setServerNonce(new byte[0]);
239                 } else {
240                     d.setServerNonce(Base64.decode(value.getBytes()));
241                 }
242                 d.setServerPassword(rs.getString(5));
243                 d.setTimeZone(rs.getString(6));
244                 d.setConvertDate("Y".equals(rs.getString(7)));
245                 d.setCharset(rs.getString(8));
246
247
248                 Capabilities caps = new Capabilities();
249                 caps.setId (rs.getString(9));
250                 caps.setSupportLargeObject ("Y".equals(rs.getString(10)));
251                 caps.setSupportNumberOfChanges("Y".equals(rs.getString(11)));
252                 d.setCapabilities(caps);
253
254             } catch (SQLException e) {
255                 throw new PersistentStoreException("Error reading the device " + d, e);
256             } finally {
257                 DBTools.close(conn, stmt, rs);
258             }
259             return true;
260         }
261         return false;
262     }
263
264     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
265         return null;
266     }
267
268     /**
269      * Read all informations
270      * @param o whichever object Sync
271      * @param clause condition where for select
272      *
273      * @return Object[] array of object
274      */

275     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException {
276         if (o instanceof Sync4jDevice) {
277             Connection conn = null;
278             PreparedStatement stmt = null;
279             ResultSet rs = null;
280
281             ArrayList JavaDoc ret = new ArrayList JavaDoc();
282
283             try {
284                 conn = dataSource.getConnection();
285
286                 PreparedWhere where = clause.getPreparedWhere();
287                 String JavaDoc query = sql[SQL_SELECT_ALL_DEVICES];
288                 if (where.sql.length() > 0) {
289                     query += " where " + where.sql;
290                 }
291                 stmt = conn.prepareStatement(query);
292                 for (int i=0; i<where.parameters.length; ++i) {
293                     stmt.setObject(i+1, where.parameters[i]);
294                 }
295                 rs = stmt.executeQuery();
296
297                 while (rs.next()) {
298                     Sync4jDevice d = new Sync4jDevice();
299                     d.setDeviceId (rs.getString(1));
300                     d.setDescription (rs.getString(2));
301                     d.setType (rs.getString(3));
302
303                     String JavaDoc value = rs.getString(4);
304                     if (value == null) {
305                         d.setClientNonce(null);
306                     } else if (value.equals("")) {
307                         d.setClientNonce(new byte[0]);
308                     } else {
309                         d.setClientNonce(Base64.decode(value.getBytes()));
310                     }
311
312                     value = rs.getString(5);
313                     if (value == null) {
314                         d.setServerNonce(null);
315                     } else if (value.equals("")) {
316                         d.setServerNonce(new byte[0]);
317                     } else {
318                         d.setServerNonce(Base64.decode(value.getBytes()));
319                     }
320                     d.setServerPassword(rs.getString(6));
321                     d.setTimeZone(rs.getString(7));
322                     d.setConvertDate("Y".equals(rs.getString(8)));
323                     d.setCharset(rs.getString(9));
324
325                     ret.add(d);
326                 }
327                 return ret.toArray(new Sync4jDevice[ret.size()]);
328             } catch (SQLException e) {
329                 throw new PersistentStoreException("Error reading devices", e);
330             } finally {
331                 DBTools.close(conn, stmt, rs);
332             }
333         }
334         return null;
335     }
336
337
338     /**
339      * Select the number of devices that satisfy the conditions specified in input.
340      * @param clause The additional where clause.
341      * @return int number of devices
342      */

343     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException {
344         if (o instanceof Sync4jDevice) {
345             Connection conn = null;
346             PreparedStatement stmt = null;
347             ResultSet rs = null;
348             int n = 0;
349
350             try {
351                 conn = dataSource.getConnection();
352
353                 PreparedWhere where = clause.getPreparedWhere();
354                 String JavaDoc query = sql[SQL_COUNT_DEVICES];
355                 if (where.sql.length()>0) {
356                     query += " where " + where.sql;
357                 }
358                 stmt = conn.prepareStatement(query);
359                 for (int i=0; i<where.parameters.length; ++i) {
360                     stmt.setObject(i+1, where.parameters[i]);
361                 }
362                 rs = stmt.executeQuery();
363
364                 while (rs.next()) {
365                     n = rs.getInt(1);
366                 }
367                 return n;
368             } catch (SQLException e) {
369                 throw new PersistentStoreException("Error reading count devices ", e);
370             } finally {
371                 DBTools.close(conn, stmt, rs);
372             }
373         }
374         return -1;
375     }
376     //---------------------------------------------------------- Private Methods
377

378     /**
379      * Return the next device id reading it by IdSpaceGenerator
380      * @return int
381      */

382     private int getDeviceId() {
383         IdSpaceGenerator idGenerator = new IdSpaceGenerator(NS_DEVICE);
384         return Integer.parseInt(idGenerator.next());
385     }
386 }
387
388
Popular Tags