1 18 19 20 21 package sync4j.syncclient.sps.pocketpc; 22 23 import java.io.IOException ; 24 import java.util.Date ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 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 45 46 public class PPCDataStore extends DataStore { 47 48 50 public final String PROPERTY_DATA_STORE_VOLUME = "dataStoreVolume"; 51 52 54 57 private Vector deleted = null; 58 59 62 private Vector updated = null; 63 64 66 public PPCDataStore(String dataStoreName, RecordMetadata recordMetadata) { 67 super(dataStoreName, recordMetadata); 68 } 69 70 72 78 public Record newRecord(String key) { 79 return new Record(this, key); 80 } 81 82 88 public Record storeRecord(Record record) 89 throws DataAccessException { 90 updated.addElement(record); 91 92 return record; 93 } 94 95 100 public void deleteRecord(Record record) 101 throws DataAccessException { 102 deleted.addElement(record); 103 } 104 105 110 public Record readRecord(Record record) 111 throws DataAccessException { 112 RapiDB db = null; 113 String 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 131 public Vector findAllRecords() 132 throws DataAccessException { 133 Vector records = new Vector (); 134 RapiDB db = null; 135 Object [][] data; 136 int keyPos = SyncRecordFunction.getKeyFieldPosition(getRecordMetadata()); 137 int statusPos = SyncRecordFunction.getModificationTypeFieldPosition(getRecordMetadata()); 138 139 try { 140 db = new RapiDB(getVolume(), getDataStoreName()); 141 142 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 )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 176 public Vector findRecords(char state, Date since) 177 throws DataAccessException { 178 RapiDB db = null; 179 Object [] data; 180 Vector records = new Vector (); 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 )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 212 public Vector findRecords(RecordFilter recordFilter) 213 throws DataAccessException { 214 Vector all = findAllRecords(); 218 Vector selected = new Vector (); 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 242 public synchronized long getNextKey() { 243 try { 247 Thread.sleep(1); 248 } catch (Exception e) {}; 249 return System.currentTimeMillis(); 250 } 251 252 253 257 public void startDBOperations() 258 throws DataAccessException { 259 deleted = new Vector (); 260 updated = new Vector (); 261 } 262 263 264 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 n = deleted.size(); 288 for (int i=0; i<n; ++i) { 289 db.deleteRecord(((Record)deleted.elementAt(i)).getKey()); 290 } 291 292 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 Vector records = new Vector (); 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 329 private String getVolume() { 330 return (String )getDataStoreProperties().get(PROPERTY_DATA_STORE_VOLUME); 331 } 332 333 341 private Object [] recordToObjects(Record record) { 342 int l = record.getLength(); 343 Object [] values = new Object [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 359 private Record objectsToRecord(String key, Object [] data) { 360 Record record = new Record(this, key); 361 for (int i=0; i<data.length; ++i) { 362 record.setField(i+1, (String ) data[i]); 363 } 364 365 return record; 366 } 367 } | Popular Tags |