KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > sps > pocketpc > PPCDataStore


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
20
21 package sync4j.syncclient.sps.pocketpc;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import sync4j.syncclient.sps.common.DataAccessException;
29 import sync4j.syncclient.sps.common.DataStore;
30 import sync4j.syncclient.sps.common.Record;
31 import sync4j.syncclient.sps.common.RecordFilter;
32 import sync4j.syncclient.sps.common.RecordMetadata;
33 import sync4j.syncclient.spds.SyncRecordFunction;
34
35 import sync4j.syncclient.sps.common.util.StaticDataHelper;
36
37 /**
38  * This DataStore implementation makes use of JNI to access the ActiveSync
39  * RAPI subsystem to read/write the hendheld object store.
40  *
41  * @author Stefano Fornai
42
43  * $Id: PPCDataStore.java,v 1.3 2005/01/19 11:18:37 fabius Exp $
44  */

45
46 public class PPCDataStore extends DataStore {
47
48     // --------------------------------------------------------------- Constants
49

50     public final String JavaDoc PROPERTY_DATA_STORE_VOLUME = "dataStoreVolume";
51
52     // ------------------------------------------------------------- PrivateData
53

54     /**
55      * Elements that must be deleted at modifications committing.
56      */

57     private Vector JavaDoc deleted = null;
58
59     /**
60      * Elements that must be updated at modifications committing.
61      */

62     private Vector JavaDoc updated = null;
63
64     //-------------------------------------------------------------- Costructors
65

66     public PPCDataStore(String JavaDoc dataStoreName, RecordMetadata recordMetadata) {
67         super(dataStoreName, recordMetadata);
68     }
69
70     //----------------------------------------------------------- Public methods
71

72     /**
73      * create record
74      *
75      * @param key
76      * @return record
77      **/

78     public Record newRecord(String JavaDoc key) {
79         return new Record(this, key);
80     }
81
82     /**
83      * store record
84      * @param record
85      * @return stored Record
86      * @throws DataAccessException
87      **/

88     public Record storeRecord(Record record)
89     throws DataAccessException {
90         updated.addElement(record);
91
92         return record;
93     }
94
95     /**
96      * delete record
97      * @param record
98      * @throws DataAccessException
99      **/

100     public void deleteRecord(Record record)
101     throws DataAccessException {
102         deleted.addElement(record);
103     }
104
105     /**
106      * read record
107      * @param record
108      * @throws DataAccessException
109      **/

110     public Record readRecord(Record record)
111     throws DataAccessException {
112         RapiDB db = null;
113         String JavaDoc key = record.getKey();
114
115         try {
116             db = new RapiDB(getVolume(), getDataStoreName());
117
118             return objectsToRecord(key, db.readRecord(key));
119         } finally {
120             if (db != null) {
121                 db.closeDB(); db = null;
122             }
123         }
124     }
125
126     /**
127      * return all records of device stored in memory
128      *
129      * @return find records
130      **/

131     public Vector JavaDoc findAllRecords()
132     throws DataAccessException {
133         Vector JavaDoc records = new Vector JavaDoc();
134         RapiDB db = null;
135         Object JavaDoc[][] data;
136         int keyPos = SyncRecordFunction.getKeyFieldPosition(getRecordMetadata());
137         int statusPos = SyncRecordFunction.getModificationTypeFieldPosition(getRecordMetadata());
138
139         try {
140             db = new RapiDB(getVolume(), getDataStoreName());
141
142             //
143
// If the database does not exist, just return the empty vector
144
//
145
if (!db.openDB(keyPos, statusPos, keyPos)) {
146                 db = null;
147                 return records;
148             }
149
150             data = db.getAllRecords();
151
152             if (data != null) {
153                 for (int i=0; i<data.length; ++i) {
154                     records.addElement(objectsToRecord((String JavaDoc)data[i][keyPos], data[i]));
155                 }
156             }
157         } finally {
158             if (db != null) {
159                 db.closeDB(); db = null;
160             }
161         }
162
163         return records;
164     }
165
166
167     /**
168      * return a Vector of Records
169      * found by state
170      *
171      * @param state state of record
172      * @param since last timestamp
173      * @return find records
174      * @throws DataAccessException
175      **/

176     public Vector JavaDoc findRecords(char state, Date JavaDoc since)
177     throws DataAccessException {
178         RapiDB db = null;
179         Object JavaDoc[] data;
180         Vector JavaDoc records = new Vector JavaDoc();
181         int keyPos = SyncRecordFunction.getKeyFieldPosition(getRecordMetadata());
182         int statusPos = SyncRecordFunction.getModificationTypeFieldPosition(getRecordMetadata());
183
184         try {
185             db = new RapiDB(getVolume(), getDataStoreName());
186
187             if (!db.openDB(keyPos, statusPos, statusPos)){
188                 db = null;
189                 return records;
190             }
191
192             while ((data = db.getNextRecord(state)) != null) {
193                 records.addElement(objectsToRecord((String JavaDoc)data[keyPos], data));
194             }
195
196             return records;
197         } finally {
198             if (db != null) {
199                 db.closeDB(); db = null;
200             }
201         }
202     }
203
204
205     /**
206      * return records of dataStore find by RecordFilter
207      *
208      * @param recordFilter filter
209      * @return find records
210      * @throws DataAccessException
211      **/

212     public Vector JavaDoc findRecords(RecordFilter recordFilter)
213     throws DataAccessException {
214         //
215
// Not implemented here...
216
//
217
Vector JavaDoc all = findAllRecords();
218         Vector JavaDoc selected = new Vector JavaDoc();
219
220         Record record = null;
221
222         int l = all.size();
223
224         for(int i=0; i < l; i++) {
225
226             record = (Record) all.elementAt(i);
227
228             if(recordFilter.accept(record)) {
229                 selected.addElement(record);
230             }
231         }
232
233         return selected;
234     }
235
236     /**
237      * return next key on device
238      * 1000 if no record exist
239      *
240      * @return key
241      **/

242     public synchronized long getNextKey() {
243         //
244
// Sleeping for one millisecond ensures that everyone gets a new value
245
//
246
try {
247             Thread.sleep(1);
248         } catch (Exception JavaDoc e) {};
249         return System.currentTimeMillis();
250     }
251
252
253     /**
254      * Method define start DB operathions
255      * @throws DataAccessException
256      */

257     public void startDBOperations()
258     throws DataAccessException {
259         deleted = new Vector JavaDoc();
260         updated = new Vector JavaDoc();
261     }
262
263
264     /**
265      * Method define end DB operathions
266      * @throws DataAccessException
267      */

268     public void commitDBOperations()
269     throws DataAccessException {
270         RapiDB db = null;
271         int keyPos = SyncRecordFunction.getKeyFieldPosition(getRecordMetadata());
272         int statusPos = SyncRecordFunction.getModificationTypeFieldPosition(getRecordMetadata());
273         int n;
274         Record record = null;
275
276         try {
277             db = new RapiDB(getVolume(), getDataStoreName());
278
279             if (!db.openDB(keyPos, statusPos, keyPos)) {
280                 db.createDB(keyPos, statusPos);
281                 db.openDB(keyPos, statusPos, keyPos);
282             }
283
284             //
285
// Deletes deleted records
286
//
287
n = deleted.size();
288             for (int i=0; i<n; ++i) {
289                 db.deleteRecord(((Record)deleted.elementAt(i)).getKey());
290             }
291
292             //
293
// Updates new/updataed records
294
//
295
n = updated.size();
296             for (int i=0; i<n; ++i) {
297                 record = (Record)updated.elementAt(i);
298                 db.storeRecord(record.getKey(), recordToObjects(record));
299             }
300
301             db.closeDB(); db = null;
302
303             //
304
// Reset modification flag
305
//
306
Vector JavaDoc records = new Vector JavaDoc();
307             records.addAll(findRecords('N', null));
308             records.addAll(findRecords('U', null));
309
310             n = records.size();
311             if (n > 0) {
312                 db = new RapiDB(getVolume(), getDataStoreName());
313                 db.openDB(keyPos, statusPos, keyPos);
314
315                 for (int i=0; i<n; ++i) {
316                     record = (Record)records.elementAt(i);
317                     db.setRecordField(record.getKey(), statusPos, "S");
318                 }
319             }
320         } finally {
321             if (db != null) {
322                 db.closeDB(); db = null;
323             }
324         }
325     }
326
327     // --------------------------------------------------------- Private methods
328

329     private String JavaDoc getVolume() {
330         return (String JavaDoc)getDataStoreProperties().get(PROPERTY_DATA_STORE_VOLUME);
331     }
332
333     /**
334      * Converts a <i>Record</i> object to an <i>Object[]</i> array where each
335      * element contains the value of a field.
336      *
337      * @param record the record - NOT NULL
338      *
339      * @return the <i>Object[]</i> representation of the record
340      */

341     private Object JavaDoc[] recordToObjects(Record record) {
342         int l = record.getLength();
343         Object JavaDoc[] values = new Object JavaDoc[l];
344
345         for (int i=0; i<l; ++i) {
346             values[i] = record.getString(i+1);
347         }
348
349         return values;
350     }
351
352     /**
353      * Converts an <i>Object[]</i> into a <i>Record</i/.
354      *
355      * @param data the data - NOT NULL
356      *
357      * @return the newly created record
358      **/

359      private Record objectsToRecord(String JavaDoc key, Object JavaDoc[] data) {
360         Record record = new Record(this, key);
361         for (int i=0; i<data.length; ++i) {
362             record.setField(i+1, (String JavaDoc) data[i]);
363         }
364
365         return record;
366      }
367 }
Popular Tags