1 18 19 package sync4j.syncclient.sps; 20 21 import java.util.Enumeration; 22 import java.util.Vector; 23 24 import javax.microedition.rms.RecordStore; 25 26 import javax.microedition.pim.Contact; 27 import javax.microedition.pim.ContactList; 28 import javax.microedition.pim.PIM; 29 import javax.microedition.pim.PIMException; 30 import net.rim.device.api.system.PersistentObject; 31 import net.rim.device.api.system.PersistentStore; 32 import net.rim.device.api.ui.component.Dialog; 33 import net.rim.device.api.util.StringMatch; 34 import sync4j.syncclient.util.StaticDataHelper; 35 36 import sync4j.syncclient.blackberry.parser.ParserFactory; 37 import sync4j.syncclient.blackberry.parser.ContactParser; 38 39 46 public class ContactDataStore extends DataStore { 47 48 50 private static final String TIMESTAMP_RECORDSTORE = "TimeStampContactRS" ; 51 private static final long PERSISTENCE_KEY = 0x83cab9f064294a3aL ; 52 54 56 protected static PersistentObject store; 57 protected static Vector changes; 58 59 static { 60 61 store = PersistentStore.getPersistentObject(PERSISTENCE_KEY); 62 63 if(store.getContents() == null) { 64 store.setContents(new Vector()); 65 store.commit(); 66 } 67 68 changes = (Vector) store.getContents(); 69 70 } 71 72 77 public void setLastTimestamp(long lastTimestamp) 78 throws DataAccessException { 79 80 RecordStore recordStore = null; 81 82 try { 83 84 recordStore = RecordStore.openRecordStore(TIMESTAMP_RECORDSTORE, true); 85 86 int numRecords = recordStore.getNumRecords(); 87 88 String recordValue = String.valueOf(lastTimestamp); 89 90 if (numRecords == 1) { 91 recordStore.setRecord(1, recordValue.getBytes(), 0, (recordValue.getBytes()).length); 92 } else { 93 recordStore.addRecord(recordValue.getBytes(), 0, (recordValue.getBytes()).length); 94 } 95 96 97 } catch (Exception e) { 98 StaticDataHelper.log("error:" + e.getMessage()); 99 throw new DataAccessException(e.getMessage()); 100 } finally { 101 if (recordStore != null) { 102 try { 103 recordStore.closeRecordStore(); 104 recordStore = null; 105 } catch (Exception e) { 106 throw new DataAccessException(e.getMessage()); 107 } 108 } 109 } 110 111 } 112 113 117 public long getLastTimestamp() 118 throws DataAccessException { 119 120 RecordStore recordStore = null; 121 122 long lastTimestamp = 0; 123 124 try { 125 126 recordStore = RecordStore.openRecordStore(TIMESTAMP_RECORDSTORE, true); 127 128 int numRecords = recordStore.getNumRecords(); 129 130 if (numRecords == 0) { 131 lastTimestamp = 0; 132 } else { 133 lastTimestamp = Long.parseLong(new String(recordStore.getRecord(1))); 134 } 135 136 } catch (Exception e) { 137 StaticDataHelper.log("error:" + e.getMessage()); 138 throw new DataAccessException(e.getMessage()); 139 } finally { 140 if (recordStore != null) { 141 try { 142 recordStore.closeRecordStore(); 143 recordStore = null; 144 } catch (Exception e) { 145 throw new DataAccessException(e.getMessage()); 146 } 147 } 148 } 149 150 return lastTimestamp; 151 152 } 153 154 160 public Record setRecord(Record record, boolean modify) 161 throws DataAccessException{ 162 try { 163 164 ContactList list = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 165 166 Contact contact = getContact(record.getKey(), list, modify); 167 168 if(contact == null) { 169 Dialog.inform("Contact is null."); 170 return null; 171 } 172 String content = fixTag(record.getUid()); 173 174 ContactParser parser = ParserFactory.getParserInstance(list, contact, modify); 175 176 parser.parseContact(content); 177 178 contact.commit(); 179 180 String uid = contact.getString(Contact.UID, 0); 181 182 record.setKey(uid); 183 184 list.close(); 185 }catch(Exception e) { 186 e.printStackTrace(); 187 throw new DataAccessException(e); 188 } 189 return record; 190 191 } 192 193 203 private Contact getContact(String key, ContactList list, boolean modify) throws Exception{ 204 if(!modify) { 205 return list.createContact(); 206 }else { 207 Enumeration enum = list.items(key); 208 while(enum.hasMoreElements()) { 209 Contact contact = (Contact)enum.nextElement(); 210 if(contact.getString(Contact.UID, 0).equals(key)) 211 return contact; 212 } 213 return null; 214 } 215 } 216 217 222 private String fixTag(String content) { 223 StringBuffer contentBuffer = new StringBuffer(content); 224 StringMatch matcher = new StringMatch("<"); 225 int matchOffset = matcher.indexOf(contentBuffer, 0); 226 while(matchOffset != -1){ 227 contentBuffer.delete(matchOffset,matchOffset+4); 228 contentBuffer.insert(matchOffset,"<"); 229 matchOffset = matcher.indexOf(contentBuffer, 0); 230 } 231 return contentBuffer.toString(); 232 233 } 234 235 240 public void deleteRecord(Record record) throws DataAccessException { 241 try { 242 ContactList list = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 243 Contact contact = getContact(record.getKey(), list, true); 244 if(contact != null) 245 list.removeContact(contact); 246 list.close(); 247 }catch(Exception expn) { 248 throw new DataAccessException(expn); 249 } 250 } 251 252 259 public void addRecord(String uid, char state, Contact contact) throws Exception{ 260 String value = uid + "|" + state; 261 262 267 if(state == RECORD_STATE_DELETED) { 268 ContactList list = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 269 270 String data = getContactString(list, contact); 271 272 list.close(); 273 274 value += "|" + data; 275 276 } 277 278 int size = changes.size(); 279 String data = ""; 280 boolean dataSet = false; 281 if(changes.contains(value)) 282 return; 283 284 for(int i=size-1;i >= 0; --i) { 285 data = (String)changes.elementAt(i); 286 String dataUID = data.substring(0, data.indexOf("|")); 287 char dataState = data.substring(data.indexOf("|")+1).charAt(0); 288 if(!dataUID.equals(uid)) { 289 continue; 290 } else if(dataState == RECORD_STATE_NEW && state == RECORD_STATE_UPDATED) { 291 dataSet = true; 292 } else if (dataState == RECORD_STATE_NEW && state == RECORD_STATE_DELETED) { 293 changes.removeElement(data); 294 i = i-1; 295 dataSet = true; 296 } 297 else if(dataState == RECORD_STATE_UPDATED && state == RECORD_STATE_DELETED) { 298 changes.setElementAt(value,i); 299 changes.removeElement(data); 300 dataSet = true; 301 } 302 } 303 304 if(!dataSet) { 305 changes.addElement(value); 306 } 307 store.commit(); 308 309 } 310 311 317 public Vector getNoDeletedRecords() 318 throws DataAccessException { 319 320 Enumeration enum = null ; 321 ContactList list = null ; 322 Vector noDeletedRecords = null ; 323 324 Contact contact = null ; 325 326 Record record = null ; 327 328 try { 329 330 noDeletedRecords = new Vector(); 331 332 list = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 333 334 enum = list.items(); 335 336 while(enum.hasMoreElements()) { 337 338 contact = (Contact)enum.nextElement(); 339 340 record = new Record(contact.getString(contact.UID,0), RECORD_STATE_UNSIGNED, getContactString(list, contact)); 341 342 noDeletedRecords.addElement(record); 343 344 } 345 346 list.close(); 347 348 return noDeletedRecords; 349 350 } catch(Exception e) { 351 e.printStackTrace(); 352 throw new DataAccessException(e); 353 } 354 355 } 356 357 364 public Vector getRecords(char state) 365 throws DataAccessException { 366 367 store = PersistentStore.getPersistentObject(PERSISTENCE_KEY); 368 if(store.getContents() == null) { 369 store.setContents(new Vector()); 370 store.commit(); 371 } 372 373 changes = (Vector) store.getContents(); 374 375 int size = changes.size(); 376 String checkString = "|"+state; 377 Vector changeVector = new Vector(); 378 Record rec = null; 379 380 for(int i=0;i < size; i++) { 381 String record = (String)changes.elementAt(i); 382 383 int checkStringIndex = record.indexOf(checkString); 384 if(checkStringIndex != -1) { 385 String uid = record.substring(0, checkStringIndex); 386 try { 387 388 if(state == RECORD_STATE_DELETED) 389 rec = new Record(record); 390 else 391 rec = new Record(uid,state, getContactAsString(uid)); 392 393 changeVector.addElement(rec); 394 } catch(DataAccessException e) { 395 e.printStackTrace(); 396 throw new DataAccessException(e); 397 } 398 } 399 } 400 401 return changeVector; 402 } 403 404 410 private String getContactAsString(String uid) throws DataAccessException{ 411 try { 412 413 String contanctAsString = null; 414 415 ContactList list = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 416 417 Contact contact = getContact(uid, list, true); 418 419 contanctAsString = getContactString(list, contact); 420 421 list.close(); 422 423 return contanctAsString; 424 425 }catch(Exception e) { 426 e.printStackTrace(); 427 throw new DataAccessException(e); 428 } 429 } 430 431 432 441 private String getContactString(ContactList list, Contact contact) throws Exception, PIMException { 442 if(contact == null) { 443 Dialog.inform("Contact is null."); 444 return null; 445 } 446 447 ContactParser parser = ParserFactory.getParserInstance(list, contact, true); 448 449 String data = parser.toString(contact); 450 451 return data; 452 } 453 454 457 public void startDSOperations() { 458 } 459 460 468 public void commitDSOperations() 469 throws DataAccessException { 470 changes.removeAllElements(); 471 store.commit(); 472 } 473 474 public long getNextKey() { 475 return 0; 477 } 478 } 479 | Popular Tags |