KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 /**
37  * This is the store for persistent information regarding the last time
38  * stamp of the synchronization process.
39  * It persists the following classes:
40  * <ul>
41  * <li>sync4j.framework.server.LastTimestamp</li>
42  * </ul>
43  *
44  * @author Stefano Fornari @ Funambol
45  * @author Harrie Hazewinkel
46  * @version $Id: LastTimestampPersistentStore.java,v 1.4 2005/04/13 14:44:39 stefano_fornari Exp $
47  */

48 public class LastTimestampPersistentStore
49 extends BasePersistentStore
50 implements PersistentStore, java.io.Serializable JavaDoc {
51
52     // --------------------------------------------------------------- Constants
53

54     // -------------------------------------------------------------- Properties
55
// --------------------------------------------- configurable SQL properties
56

57     /**
58      * The SQL query to insert the last timestamp
59      */

60     private String JavaDoc sqlInsertLastTimestamp =
61     "insert into sync4j_last_sync (principal, sync_source, last_anchor_server, last_anchor_client, start_sync, end_sync) values(?, ?, ?, ?, ?, ?)";
62
63     /** Getter for property sqlInsertLastTimestamp.
64      * @return Value of property sqlInsertLastTimestamp.
65      *
66      */

67     public String JavaDoc getSqlInsertLastTimestamp() {
68         return sqlInsertLastTimestamp;
69     }
70
71     /** Setter for property sqlInsertLastTimestamp.
72      * @param sqlInsertLastTimestamp New value of property sqlInsertLastTimestamp.
73      *
74      */

75     public void setSqlInsertLastTimestamp(String JavaDoc sqlInsertLastTimestamp) {
76         this.sqlInsertLastTimestamp = sqlInsertLastTimestamp;
77     }
78
79     /**
80      * The SQL query to update the last timestamp
81      */

82     private String JavaDoc sqlUpdateLastTimestamp =
83     "update sync4j_last_sync set last_anchor_server=?,last_anchor_client=?,start_sync=?,end_sync=? where principal=? and sync_source=?";
84
85     /** Getter for property sqlUpdateLastTimestamp.
86      * @return Value of property sqlUpdateLastTimestamp.
87      *
88      */

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

97     public void setSqlUpdateLastTimestamp(String JavaDoc sqlUpdateLastTimestamp) {
98         this.sqlUpdateLastTimestamp = sqlUpdateLastTimestamp;
99     }
100
101     /**
102      * The SQL query to select the last timestamp
103      */

104     private String JavaDoc sqlSelectLastTimestamp =
105     "select last_anchor_server, last_anchor_client,start_sync,end_sync from sync4j_last_sync where principal=? and sync_source=?";
106
107     /** Getter for property sqlUpdateLastTimestamp.
108      * @return Value of property sqlUpdateLastTimestamp.
109      *
110      */

111     public String JavaDoc getSqlSelectLastTimestamp() {
112         return sqlSelectLastTimestamp;
113     }
114
115     /** Setter for property sqlUpdateLastTimestamp.
116      * @param sqlSelectLastTimestamp New value of property sqlUpdateLastTimestamp.
117      *
118      */

119     public void setSqlSelectLastTimestamp(String JavaDoc sqlSelectLastTimestamp) {
120         this.sqlSelectLastTimestamp = sqlSelectLastTimestamp;
121     }
122
123     // ------------------------------------------------------------ Private data
124

125     // ------------------------------------------------------------ Constructors
126

127     // ---------------------------------------------------------- Public methods
128

129     /**
130      * Store the last timestamp.
131      * Into tagServer there is the Next anchor sent by client: now this become
132      * a Last anchor of the server.
133      * Into tagClient there is the Next anchor sent by server: now this become
134      * a Last anchor of the client.
135      *
136      * @param o the Object that must be a LastTimestamp object with all
137      * information about start and end sync timestamp
138      *
139      * @throws PersistentStoreException
140      */

141     public boolean store(Object JavaDoc o)
142     throws PersistentStoreException {
143         if (o instanceof LastTimestamp) {
144             LastTimestamp l = (LastTimestamp) o;
145
146             Connection conn = null;
147             PreparedStatement stmt = null;
148
149             try {
150                 conn = dataSource.getConnection();
151
152                 stmt = conn.prepareStatement(sqlUpdateLastTimestamp);
153                 stmt.setString (1, l.tagServer );
154                 stmt.setString (2, l.tagClient );
155                 stmt.setTimestamp(3, new Timestamp(l.start) );
156                 stmt.setTimestamp(4, new Timestamp(l.end) );
157                 stmt.setLong (5, Long.parseLong(l.principal));
158                 stmt.setString (6, l.database );
159                 int n = stmt.executeUpdate();
160
161                 if (n == 0) {
162                     //
163
// The first time!!!
164
//
165
stmt.close();
166                     stmt = conn.prepareStatement(sqlInsertLastTimestamp);
167                     stmt.setLong (1, Long.parseLong(l.principal));
168                     stmt.setString (2, l.database );
169                     stmt.setString (3, l.tagServer );
170                     stmt.setString (4, l.tagClient );
171                     stmt.setTimestamp(5, new Timestamp(l.start) );
172                     stmt.setTimestamp(6, new Timestamp(l.end) );
173                     stmt.executeUpdate();
174                 }
175             } catch (SQLException e) {
176                 throw new PersistentStoreException("Error storing last timestamp", e);
177             } finally {
178                 DBTools.close(conn, stmt, null);
179             }
180             return true;
181         }
182         return false;
183     }
184
185     public boolean read(Object JavaDoc o)
186     throws PersistentStoreException {
187         if (o instanceof LastTimestamp) {
188             LastTimestamp l = (LastTimestamp) o;
189
190             Connection conn = null;
191             PreparedStatement stmt = null;
192             ResultSet rs = null;
193
194             try {
195                 conn = dataSource.getConnection();
196
197                 stmt = conn.prepareStatement(sqlSelectLastTimestamp);
198                 stmt.setLong(1, Long.parseLong(l.principal));
199                 stmt.setString(2, l.database);
200                 rs = stmt.executeQuery();
201
202                 if (rs.next() == false) {
203                     throw new NotFoundException("Last timestamp not found for "
204                     + l.toString()
205                     );
206                 }
207                 l.tagServer = rs.getString (1) ;
208                 l.tagClient = rs.getString (2) ;
209                 l.start = rs.getTimestamp(3).getTime();
210                 l.end = rs.getTimestamp(4).getTime();
211             } catch (SQLException e) {
212                 throw new PersistentStoreException("Error reading last timestamp", e);
213             } finally {
214                 DBTools.close(conn, stmt, rs);
215             }
216             return true;
217         }
218         return false;
219     }
220
221     /** Read all objects stored the persistent media.
222      *
223      * @param objClass the object class handled by the persistent store
224      *
225      * @return an array containing the objects read. If no objects are found an
226      * empty array is returned. If the persistent store has not
227      * processed the quest, null is returned.
228      *
229      * @throws PersistentStoreException
230      *
231      */

232     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
233         //
234
// TO DO (not used yet)
235
//
236
return null;
237     }
238
239
240     public boolean delete(Object JavaDoc o) throws PersistentStoreException
241     {
242         return false;
243     }
244
245     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException
246     {
247         return null;
248     }
249
250     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException
251     {
252         return -1;
253     }
254
255     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException
256     {
257         return false;
258     }
259
260 }
261
Popular Tags