KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24
25 import java.sql.*;
26
27 import sync4j.framework.tools.DBTools;
28
29 import sync4j.framework.server.LastTimestamp;
30 import sync4j.framework.server.store.Clause;
31 import sync4j.framework.server.store.PersistentStore;
32 import sync4j.framework.server.store.BasePersistentStore;
33 import sync4j.framework.server.store.NotFoundException;
34 import sync4j.framework.server.store.PersistentStoreException;
35 import sync4j.framework.server.ID;
36
37 /**
38  * This is the store for persistent information regarding ID
39  * It persists the following classes:
40  * <ul>
41  * <li>sync4j.framework.server.GUID</li>
42  * </ul>
43  *
44  * @version $Id: IDPersistentStore.java,v 1.1 2005/07/25 15:27:36 nichele Exp $
45  */

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

51     public static final int SQL_GET_ID = 0;
52     public static final int SQL_UPDATE_ID = 1;
53
54     // -------------------------------------------------------------- Properties
55

56     protected String JavaDoc[] sql = null;
57
58     public void setSql(String JavaDoc[] sql) {
59         this.sql = sql;
60     }
61
62     public String JavaDoc[] getSql() {
63         return this.sql;
64     }
65
66
67     // ------------------------------------------------------------ Constructors
68
// ---------------------------------------------------------- Public methods
69

70     public boolean delete(Object JavaDoc o) throws PersistentStoreException {
71         return false;
72     }
73
74     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException {
75         return false;
76     }
77
78     /**
79     * Store the given ID.
80     *
81     * @param o the Object that must be a ID object
82     * @throws PersistentStoreException
83     */

84     public boolean store(Object JavaDoc o) throws PersistentStoreException {
85         if (o instanceof ID) {
86
87             ID id = (ID) o;
88             String JavaDoc idSpace = id.getIdSpace();
89             int counter = id.getValue();
90
91             if (idSpace == null) {
92                 throw new PersistentStoreException("Id space must be not null");
93             }
94
95             Connection conn = null;
96             PreparedStatement stmt = null;
97             ResultSet rs = null;
98
99             try {
100                 conn = dataSource.getConnection();
101
102                 stmt = conn.prepareStatement(sql[SQL_UPDATE_ID]);
103                 stmt.setInt (1, counter);
104                 stmt.setString(2, idSpace);
105                 stmt.executeUpdate();
106
107             } catch (SQLException e) {
108                 e.printStackTrace();
109                 throw new PersistentStoreException("Error storing the counter " + idSpace, e);
110             } finally {
111                 DBTools.close(conn, stmt, rs);
112             }
113
114             return true;
115         }
116         return false;
117
118     }
119
120     /**
121      * Read the id from the data store.
122      * @param o The object that must be a ID for this method
123      * @throws PersistentException in case of error reading the data store
124      */

125     public boolean read(Object JavaDoc o) throws PersistentStoreException {
126         if (o instanceof ID) {
127
128             ID id = (ID) o;
129             String JavaDoc idSpace = id.getIdSpace();
130
131             if (idSpace == null) {
132                 throw new PersistentStoreException("Id space must be not null");
133             }
134
135             Connection conn = null;
136             PreparedStatement stmt = null;
137             ResultSet rs = null;
138
139             try {
140                 conn = dataSource.getConnection();
141
142                 int counter = 0;
143
144                 stmt = conn.prepareStatement(sql[SQL_GET_ID]);
145                 stmt.setString(1, id.getIdSpace());
146                 rs = stmt.executeQuery();
147
148                 if (rs.next() == false) {
149                     throw new NotFoundException("Counter not found for "
150                                                 + idSpace);
151                 }
152
153                 counter = rs.getInt(1) + 1;
154
155                 DBTools.close(null, stmt, rs);
156
157                 stmt = conn.prepareStatement(sql[SQL_UPDATE_ID]);
158                 stmt.setInt (1, counter);
159                 stmt.setString(2, idSpace);
160                 stmt.executeUpdate();
161
162                 id.setValue(counter);
163
164             } catch (SQLException e) {
165                 e.printStackTrace();
166                 throw new PersistentStoreException("Error reading the counter " + idSpace, e);
167             } finally {
168                 DBTools.close(conn, stmt, rs);
169             }
170
171             return true;
172         }
173         return false;
174     }
175
176     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
177         return null;
178     }
179
180     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException {
181         return new Object JavaDoc[0];
182     }
183
184     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException {
185         return 0;
186     }
187
188 }
189
190
Popular Tags