KickJava   Java API By Example, From Geeks To Geeks.

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


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.Sync4jSource;
28
29 /**
30  * This is the store for SyncSource information of the Sync4j engine.
31  * <ul>
32  * <li>sync4j.server.engine.Sync4jSource</li>
33  * </ul>
34  *
35  * @author Stefano Fornari @ Funambol
36  * @author Harrie @ Lisanza Net
37  */

38 public class SyncSourcePersistentStore
39 extends BasePersistentStore
40 implements PersistentStore, java.io.Serializable JavaDoc {
41
42     // --------------------------------------------------------------- Constants
43

44     public static final int SQL_GET_SOURCE = 0;
45     public static final int SQL_SELECT_ALL_SOURCES = 1;
46     public static final int SQL_DELETE_CLIENT_MAPPING = 2;
47     public static final int SQL_DELETE_LAST_SYNC = 3;
48     public static final int SQL_DELETE_SOURCE = 4;
49     public static final int SQL_INSERT_SYNCSOURCE = 5;
50     public static final int SQL_UPDATE_SYNCSOURCE = 6;
51     public static final int SQL_DELETE_SOURCE_CLIENT_MAPPING = 7;
52     public static final int SQL_DELETE_SOURCE_LAST_SYNC = 8;
53
54     // -------------------------------------------------------------- Properties
55
protected String JavaDoc[] sql = null;
56
57     public void setSql(String JavaDoc[] sql) {
58         this.sql = sql;
59     }
60
61     public String JavaDoc[] getSql() {
62         return this.sql;
63     }
64
65     // ------------------------------------------------------------ Private data
66
// ------------------------------------------------------------ Constructors
67
// ---------------------------------------------------------- Public methods
68

69     public boolean delete(Object JavaDoc o)
70     throws PersistentStoreException {
71         if (o instanceof Sync4jSource) {
72             Sync4jSource s = (Sync4jSource) o;
73
74             Connection conn = null;
75             PreparedStatement stmt = null;
76
77             try {
78                 conn = dataSource.getConnection();
79
80                 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_CLIENT_MAPPING]);
81                 stmt.setString(1, s.getUri());
82                 stmt.executeUpdate();
83                 stmt.close();
84
85                 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_LAST_SYNC]);
86                 stmt.setString(1, s.getUri());
87                 stmt.executeUpdate();
88                 stmt.close();
89
90                 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE]);
91                 stmt.setString(1, s.getUri());
92                 stmt.executeUpdate();
93                 stmt.close();
94
95             } catch (SQLException e) {
96                 throw new PersistentStoreException("Error deleting the source " + s, e);
97             } finally {
98                 DBTools.close(conn, stmt, null);
99             }
100             return true;
101         }
102         return false;
103     }
104     public boolean store(Object JavaDoc o) throws PersistentStoreException {
105         if (!(o instanceof Sync4jSource)) {
106             return false; // Not me
107
}
108         Sync4jSource ss = (Sync4jSource) o;
109
110         Connection conn = null;
111         PreparedStatement stmt = null;
112         int n = 0;
113
114         try {
115             conn = dataSource.getConnection();
116
117             stmt = conn.prepareStatement(sql[SQL_UPDATE_SYNCSOURCE]);
118
119             stmt.setString(1, ss.getConfig());
120             stmt.setString(2, ss.getSourceName());
121             stmt.setString(3, ss.getSourceTypeId());
122             stmt.setString(4, ss.getUri());
123
124             n = stmt.executeUpdate();
125             stmt.close();
126
127             if (n == 0) {
128                 stmt = conn.prepareStatement(sql[SQL_INSERT_SYNCSOURCE]);
129                 stmt.setString(1, ss.getUri());
130                 stmt.setString(2, ss.getConfig());
131                 stmt.setString(3, ss.getSourceName());
132                 stmt.setString(4, ss.getSourceTypeId());
133                 stmt.executeUpdate();
134                 stmt.close();
135             }
136
137         } catch (SQLException e) {
138             throw new PersistentStoreException("Error updating the syncsource " + ss, e);
139         } finally {
140             DBTools.close(conn, stmt, null);
141         }
142         return true;
143     }
144
145     /**
146      * Read the source from the data store.
147      *
148      * @param o the object that is an Sync4jSource
149      *
150      * @throws PersistentException in case of error reading the data store
151      */

152     public boolean read(Object JavaDoc o)
153     throws PersistentStoreException {
154         if (o instanceof Sync4jSource) {
155             Sync4jSource s = (Sync4jSource) o;
156
157             Connection conn = null;
158             PreparedStatement stmt = null;
159             ResultSet rs = null;
160
161             try {
162                 conn = dataSource.getConnection();
163
164                 stmt = conn.prepareStatement(sql[SQL_GET_SOURCE]);
165                 stmt.setString(1, s.getUri());
166                 rs = stmt.executeQuery();
167                 if (rs.next() == false) {
168                     throw new NotFoundException("Source not found for "
169                                                + s.getUri());
170                 }
171
172                 s.setUri (rs.getString(1));
173                 s.setConfig(rs.getString(2));
174             } catch (SQLException e) {
175                 throw new PersistentStoreException("Error reading the source " + s, e);
176             } finally {
177                 DBTools.close(conn, stmt, rs);
178             }
179             return true;
180         }
181         return false;
182     }
183
184    /**
185      * Read all sources
186      *
187      * @return an array with the sources (empty if no objects are found)
188      *
189      * @throws PersistentException in case of error reading the data store
190      */

191     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
192         if (objClass.getName().equals(Sync4jSource.class.getName())) {
193
194             Connection conn = null;
195             PreparedStatement stmt = null;
196             ResultSet rs = null;
197
198             ArrayList JavaDoc ret = new ArrayList JavaDoc();
199
200             try {
201                 conn = dataSource.getConnection();
202
203                 stmt = conn.prepareStatement(sql[SQL_SELECT_ALL_SOURCES]);
204                 rs = stmt.executeQuery();
205                 while (rs.next()) {
206                     ret.add(new Sync4jSource(rs.getString(1),
207                                              rs.getString(2)));
208                 }
209
210                 return ret.toArray(new Sync4jSource[ret.size()]);
211             } catch (SQLException e) {
212                 throw new PersistentStoreException("Error reading sources", e);
213             } finally {
214                 DBTools.close(conn, stmt, rs);
215             }
216         }
217         return null;
218     }
219
220     /**
221      * Read all informations
222      * @param o whichever object Sync
223      * @param clause condition where for select
224      *
225      * @return Object[] array of object
226      */

227     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException {
228         if (o instanceof Sync4jSource) {
229             Sync4jSource s = (Sync4jSource)o;
230
231             Connection conn = null;
232             PreparedStatement stmt = null;
233             ResultSet rs = null;
234
235             ArrayList JavaDoc ret = new ArrayList JavaDoc();
236
237             try {
238                 conn = dataSource.getConnection();
239
240                 PreparedWhere where = clause.getPreparedWhere();
241
242                 String JavaDoc query = sql[SQL_SELECT_ALL_SOURCES];
243                 if (where.sql.length() > 0) {
244                     query += " where " + where.sql;
245                 }
246
247                 stmt = conn.prepareStatement(query);
248
249                 for (int i=0; i<where.parameters.length; ++i) {
250                     stmt.setObject(i+1, where.parameters[i]);
251                 }
252
253                 rs = stmt.executeQuery();
254
255                 while (rs.next()) {
256                     ret.add(
257                         new Sync4jSource(
258                             rs.getString(1),
259                             rs.getString(2)
260                         )
261                     );
262                 }
263
264                 return ret.toArray(new Sync4jSource[ret.size()]);
265             } catch (SQLException e) {
266                 throw new PersistentStoreException("Error reading sources", e);
267             } finally {
268                 DBTools.close(conn, stmt, rs);
269             }
270         }
271         return null;
272     }
273
274     /**
275      * Insert a new SyncSource
276      */

277     protected void insertSyncSource(String JavaDoc sourcetypeid, Sync4jSource ss)
278     throws PersistentStoreException {
279         Connection conn = null;
280         PreparedStatement stmt = null;
281
282         try {
283             conn = dataSource.getConnection();
284
285             stmt = conn.prepareStatement(sql[SQL_INSERT_SYNCSOURCE]);
286
287             stmt.setString(1, ss.getUri());
288             stmt.setString(2, ss.getConfig());
289             stmt.setString(3, ss.getSourceName());
290             stmt.setString(4, sourcetypeid);
291
292             stmt.executeUpdate();
293             stmt.close();
294
295         } catch (SQLException e) {
296             throw new PersistentStoreException("Error storing the syncsource " + ss, e);
297         } finally {
298             DBTools.close(conn, stmt, null);
299         }
300     }
301
302     /**
303      * Update an existing SyncSource
304      */

305     protected void updateSyncSource(String JavaDoc sourcetypeid, Sync4jSource ss)
306     throws PersistentStoreException {
307         Connection conn = null;
308         PreparedStatement stmt = null;
309
310         try {
311             conn = dataSource.getConnection();
312
313             stmt = conn.prepareStatement(sql[SQL_UPDATE_SYNCSOURCE]);
314
315             stmt.setString(1, ss.getConfig());
316             stmt.setString(2, ss.getSourceName());
317             stmt.setString(3, ss.getSourceTypeId());
318             stmt.setString(4, ss.getUri());
319
320             stmt.executeUpdate();
321             stmt.close();
322
323         } catch (SQLException e) {
324             throw new PersistentStoreException("Error updating the syncsource " + ss, e);
325         } finally {
326             DBTools.close(conn, stmt, null);
327         }
328     }
329
330     public int count(Object JavaDoc o, Clause clause) {
331         return -1;
332     }
333 }
334
335
Popular Tags