KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import java.sql.*;
28
29 import sync4j.framework.tools.DBTools;
30 import sync4j.framework.engine.source.SyncSource;
31 import sync4j.framework.server.Sync4jSource;
32 import sync4j.framework.server.Sync4jSourceType;
33 import sync4j.framework.server.store.*;
34 import sync4j.framework.server.store.PersistentStoreException;
35
36 import org.apache.commons.lang.StringUtils;
37
38 import sync4j.framework.server.Sync4jModule;
39 import sync4j.framework.server.Sync4jConnector;
40
41 /**
42  *
43  * @author Luigia Fassina @ Funambol
44  *
45  * @version $Id: ModulesPersistentStore.java,v 1.11 2005/04/16 15:33:21 stefano_fornari Exp $
46  *
47  */

48 public class ModulesPersistentStore
49 extends BasePersistentStore
50 implements PersistentStore, java.io.Serializable JavaDoc {
51
52     // --------------------------------------------------------------- Constants
53
public static final int SQL_SELECT_ALL_MODULES_NAME = 0;
54     public static final int SQL_GET_MODULE = 1;
55     public static final int SQL_SELECT_MODULE_CONNECTOR = 2;
56     public static final int SQL_SELECT_CONNECTOR_SYNCSOURCETYPE = 3;
57     public static final int SQL_SELECT_SYNCSOURCETYPE_SYNCSOURCE = 4;
58
59     // -------------------------------------------------------------- Properties
60

61     private String JavaDoc[] sql = null;
62
63     public void setSql(String JavaDoc[] sql) {
64         this.sql = sql;
65     }
66
67     public String JavaDoc[] getSql() {
68         return this.sql;
69     }
70
71     // ------------------------------------------------------------ Private data
72
// ------------------------------------------------------------ Constructors
73
// ---------------------------------------------------------- Public methods
74

75     public boolean delete(Object JavaDoc o)
76     throws PersistentStoreException {
77         return false;
78     }
79
80     public Object JavaDoc[] delete(Class JavaDoc objClass) throws PersistentStoreException
81     {
82         return new Object JavaDoc[0];
83     }
84
85     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException {
86         return false;
87     }
88
89     public boolean store(Object JavaDoc o)
90     throws PersistentStoreException {
91         return false;
92     }
93
94     public boolean read(Object JavaDoc o)
95     throws PersistentStoreException {
96         if (o instanceof Sync4jModule) {
97             readModule((Sync4jModule) o);
98             return true;
99         }
100
101         return false;
102     }
103
104     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
105         if (objClass.getName().equals(Sync4jModule.class.getName())) {
106             return readModulesName();
107         }
108         return null;
109     }
110
111     /**
112      * Read all informations
113      * @param o whichever object Sync
114      * @param clause condition where for select - NOT USED
115      *
116      * @return Object[] array of object
117      */

118     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException {
119         if (o instanceof Sync4jSourceType) {
120             return readSyncSource((Sync4jSourceType) o);
121         }
122
123         return null;
124     }
125
126     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException {
127         return -1;
128     }
129
130     // --------------------------------------------------------- Private methods
131
private Object JavaDoc[] readModulesName()
132     throws PersistentStoreException {
133         Connection conn = null;
134         PreparedStatement stmt = null;
135         ResultSet rs = null;
136
137         ArrayList JavaDoc ret = new ArrayList JavaDoc();
138
139         try {
140             conn = dataSource.getConnection();
141
142             stmt = conn.prepareStatement(sql[SQL_SELECT_ALL_MODULES_NAME]);
143
144             rs = stmt.executeQuery();
145
146             while (rs.next()) {
147                 ret.add(
148                     new Sync4jModule(
149                         rs.getString(1),
150                         rs.getString(2),
151                         rs.getString(3)
152                     )
153                 );
154             }
155
156             return ret.toArray(new Sync4jModule[ret.size()]);
157         } catch (SQLException e) {
158             throw new PersistentStoreException("Error reading modules name", e);
159         } finally {
160             DBTools.close(conn, stmt, rs);
161         }
162     }
163
164     private void readModule(Sync4jModule module)
165     throws PersistentStoreException {
166         Connection conn = null;
167         PreparedStatement stmt = null;
168         ResultSet rs = null;
169
170         try {
171             conn = dataSource.getConnection();
172
173             stmt = conn.prepareStatement(sql[SQL_GET_MODULE]);
174             stmt.setString(1, module.getModuleId());
175
176             rs = stmt.executeQuery();
177
178             if (rs.next() == false) {
179                 throw new NotFoundException("Module not found for "
180                 + module.getModuleId()
181                 );
182             }
183             module.setModuleId(rs.getString(1));
184             module.setModuleName(rs.getString(2));
185             module.setDescription(rs.getString(3));
186
187             //get all connectors for this module
188
Sync4jConnector[] syncConnectors = (Sync4jConnector[])this.readModuleConnectors(module);
189             module.setConnectors(syncConnectors);
190
191             //for each SyncConnector select the SyncSourceType
192
for (int i=0; (syncConnectors != null) && i<syncConnectors.length; i++) {
193                 Sync4jConnector sc = syncConnectors[i];
194                 Sync4jSourceType[] sst = (Sync4jSourceType[])this.readSyncSourceType(sc);
195
196                 sc.setSourceTypes(sst);
197             }
198
199         } catch (SQLException e) {
200             throw new PersistentStoreException("Error reading the module " + module, e);
201         } finally {
202             DBTools.close(conn, stmt, rs);
203         }
204     }
205
206     private Object JavaDoc[] readModuleConnectors(Sync4jModule module)
207     throws PersistentStoreException {
208         Connection conn = null;
209         PreparedStatement stmt = null;
210         ResultSet rs = null;
211         ArrayList JavaDoc ret = new ArrayList JavaDoc();
212
213         try {
214             conn = dataSource.getConnection();
215
216             stmt = conn.prepareStatement(sql[SQL_SELECT_MODULE_CONNECTOR]);
217             stmt.setString(1, module.getModuleId());
218
219             rs = stmt.executeQuery();
220
221             while (rs.next()) {
222                 ret.add(
223                     new Sync4jConnector(
224                         rs.getString(1),
225                         rs.getString(2),
226                         rs.getString(3),
227                         rs.getString(4)
228                     )
229                 );
230             }
231
232             return ret.toArray(new Sync4jConnector[ret.size()]);
233
234         } catch (SQLException e) {
235             throw new PersistentStoreException("Error reading the connectors ", e);
236         } finally {
237             DBTools.close(conn, stmt, rs);
238         }
239     }
240
241     private Object JavaDoc[] readSyncSourceType(Sync4jConnector sc)
242     throws PersistentStoreException {
243         Connection conn = null;
244         PreparedStatement stmt = null;
245         ResultSet rs = null;
246         ArrayList JavaDoc ret = new ArrayList JavaDoc();
247
248         try {
249             conn = dataSource.getConnection();
250
251             stmt = conn.prepareStatement(sql[SQL_SELECT_CONNECTOR_SYNCSOURCETYPE]);
252             stmt.setString(1, sc.getConnectorId());
253
254             rs = stmt.executeQuery();
255
256             while (rs.next()) {
257                 ret.add(
258                     new Sync4jSourceType(
259                         rs.getString(1),
260                         rs.getString(2),
261                         rs.getString(3),
262                         rs.getString(4)
263                     )
264                 );
265             }
266
267             return ret.toArray(new Sync4jSourceType[ret.size()]);
268
269         } catch (SQLException e) {
270             throw new PersistentStoreException("Error reading the SyncSourceType ", e);
271         } finally {
272             DBTools.close(conn, stmt, rs);
273         }
274     }
275
276     private Object JavaDoc[] readSyncSource(Sync4jSourceType sst)
277     throws PersistentStoreException {
278         Connection conn = null;
279         PreparedStatement stmt = null;
280         ResultSet rs = null;
281         ArrayList JavaDoc ret = new ArrayList JavaDoc();
282
283         try {
284             conn = dataSource.getConnection();
285
286             stmt = conn.prepareStatement(sql[SQL_SELECT_SYNCSOURCETYPE_SYNCSOURCE]);
287             stmt.setString(1, sst.getSourceTypeId());
288
289             rs = stmt.executeQuery();
290
291             while (rs.next()) {
292                 ret.add(
293                     new Sync4jSource(
294                         rs.getString(1),
295                         rs.getString(2),
296                         rs.getString(3),
297                         rs.getString(4)
298                     )
299                 );
300             }
301
302             return ret.toArray(new Sync4jSource[ret.size()]);
303
304         } catch (SQLException e) {
305             throw new PersistentStoreException("Error reading the SyncSource ", e);
306         } finally {
307             DBTools.close(conn, stmt, rs);
308         }
309     }
310 }
311
Popular Tags