1 18 19 package sync4j.syncclient.spds.source; 20 21 import java.security.Principal ; 22 import java.util.Date ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 27 import sync4j.framework.core.AlertCode; 28 29 import sync4j.syncclient.sps.common.*; 30 31 import sync4j.syncclient.spds.engine.*; 32 import sync4j.syncclient.spds.SyncRecordFunction; 33 import sync4j.syncclient.spds.SyncException; 34 35 44 public class SPSSyncSource implements SyncSource { 45 46 private String name = null; 47 private String type = null; 48 private String sourceClass = null; 49 private String sourceURI = null; 50 private String storeManager = null; 51 private String storeManagerPackage = null; 52 private String storeVolume = null; 53 54 private int syncMode; 55 56 57 private boolean sync = false; 58 59 60 61 private boolean syncSourceChanged = false; 62 63 private boolean sortDB = false; 64 65 66 private String creatorId = null; 67 68 69 private String dataStoreType = null; 70 71 private DataStoreManager dataStoreManager = null; 72 73 private DataStore dataStore = null; 74 75 private ClassLoader classLoader = null; 76 77 78 80 81 public SPSSyncSource() { 82 83 } 84 85 87 public String getName() { 88 return this.name; 89 } 90 91 public void setName(String name) { 92 this.name = name; 93 } 94 95 public String getType() { 96 return this.type; 97 } 98 99 public void setType(String type) { 100 this.type = type; 101 } 102 103 104 107 public String getCreatorId() { 108 return this.creatorId; 109 } 110 111 114 public void setCreatorId(String creatorId) { 115 this.creatorId = creatorId; 116 } 117 118 121 public String getdataStoreType() { 122 return this.dataStoreType; 123 } 124 125 128 public void setDataStoreType(String dataStoreType) { 129 this.dataStoreType = dataStoreType; 130 } 131 132 135 public String getSourceClass() { 136 return this.sourceClass; 137 } 138 139 142 public void setSourceClass(String sourceClass) { 143 this.sourceClass = sourceClass; 144 } 145 146 149 public String getSourceURI() { 150 return this.sourceURI; 151 } 152 153 156 public void setSourceURI(String sourceURI) { 157 158 this.sourceURI = sourceURI; 159 } 160 161 164 public boolean getSortDB() { 165 return this.sortDB; 166 } 167 168 171 public void setSortDB(String sortDB) { 172 173 if ((sortDB).equals("true")) { 174 this.sortDB = true; 175 } else { 176 this.sortDB = false; 177 } 178 179 } 180 181 184 public String getStoreManager() { 185 return this.storeManager; 186 } 187 188 191 public void setStoreManager(String storeManager) { 192 this.storeManager = storeManager; 193 } 194 195 198 public String getStoreManagerPackage() { 199 return this.storeManagerPackage; 200 } 201 202 205 public void setStoreManagerPackage(String storeManagerPackage) { 206 this.storeManagerPackage = storeManagerPackage; 207 } 208 209 210 213 public ClassLoader getClassLoader() { 214 return this.classLoader; 215 } 216 217 220 public void setClassLoader(ClassLoader classLoader) { 221 this.classLoader = classLoader; 222 } 223 224 227 public boolean getSync() { 228 return this.sync; 229 } 230 231 234 public void setSync(String sync) { 235 236 if ((sync).equals("true")) { 237 this.sync = true; 238 } else { 239 this.sync = false; 240 } 241 242 } 243 244 247 public String getStoreVolume() { 248 return this.storeVolume; 249 } 250 251 254 public void setStoreVolume(String storeVolume) { 255 this.storeVolume = storeVolume; 256 } 257 258 260 265 public String toString() { 266 StringBuffer sb = new StringBuffer (super.toString()); 267 268 sb.append(" - {name: ").append(getName() ); 269 sb.append(" type: " ).append(getType() ); 270 sb.append(" uri: " ).append(getSourceURI()); 271 sb.append("}" ); 272 273 return sb.toString(); 274 } 275 276 288 public SyncItem[] getAllSyncItems(Principal principal) throws SyncException { 289 Vector records = null; 290 291 try { 292 records = dataStore.findAllRecords(); 293 } catch (DataAccessException e) { 294 throw new SyncException(e.getMessage()); 295 } 296 297 return recordsToSyncItems(dataStore, records); 298 } 299 300 316 public SyncItem[] getDeletedSyncItems(Principal principal, 317 Date since ) throws SyncException { 318 Vector records = null; 319 320 try { 321 records = dataStore.findRecords('D', since); 322 } catch (DataAccessException e) { 323 throw new SyncException(e.getMessage()); 324 } 325 326 return recordsToSyncItems(dataStore, records); 327 328 } 329 330 345 public SyncItem[] getNewSyncItems(Principal principal, 346 Date since ) throws SyncException { 347 Vector records = null; 348 349 try { 350 records = dataStore.findRecords('N', since); 351 } catch (DataAccessException e) { 352 throw new SyncException(e.getMessage()); 353 } 354 355 return recordsToSyncItems(dataStore, records); 356 } 357 358 371 public SyncItem[] getUpdatedSyncItems(Principal principal, 372 Date since ) throws SyncException { 373 Vector records = null; 374 375 try { 376 records = dataStore.findRecords('U', since); 377 } catch (DataAccessException e) { 378 throw new SyncException(e.getMessage()); 379 } 380 381 return recordsToSyncItems(dataStore, records); 382 383 } 384 385 394 public void removeSyncItem(Principal principal, SyncItem syncItem) throws SyncException { 395 396 syncSourceChanged = true; 397 398 Record record = null; 399 400 record = syncItemToRecord (dataStore, syncItem); 401 402 try { 403 dataStore.deleteRecord(record); 404 } catch (DataAccessException e) { 405 throw new SyncException(e.getMessage()); 406 } 407 408 } 409 410 424 public SyncItem setSyncItem(Principal principal, SyncItem syncItem) throws SyncException { 425 426 syncSourceChanged = true; 427 428 Record record = null; 429 430 long keyBase = 0; 431 432 record = syncItemToRecord (dataStore, syncItem); 433 434 try { 435 record = dataStore.storeRecord(record); 436 } catch (DataAccessException e) { 437 throw new SyncException(e.getMessage()); 438 } 439 440 return recordToSyncItem(dataStore, record); 441 442 } 443 444 447 public void beginSync(int syncMode) throws SyncException { 448 449 this.syncMode = syncMode; 450 451 Hashtable dataStoreProperties = null; 452 453 dataStoreManager = 454 DataStoreManagerFactory.getDataStoreManager( 455 storeManager, 456 storeManagerPackage, 457 classLoader 458 ); 459 460 if (dataStoreManager == null) { 461 throw new SyncException( "Datastore manager " 462 + storeManagerPackage 463 + '.' 464 + storeManager 465 + DataStoreManagerFactory.DATASTORE_MANAGER_FILE_NAME_END 466 + " not found" 467 ); 468 } 469 470 String storeName = sourceURI; 475 int p = storeName.lastIndexOf('/'); 476 if ((p>=0) && (p<(storeName.length()-1))) { 477 storeName = sourceURI.substring(p+1); 478 } 479 480 dataStore = dataStoreManager.getDataStore(storeName); 481 482 if (dataStore == null) { 483 throw new SyncException("Datastore " + storeName + " not found!"); 484 } 485 486 dataStoreProperties = new Hashtable (); 487 488 if (this.creatorId != null) { 489 dataStoreProperties.put("creatorId", this.creatorId); 490 } 491 492 if (this.dataStoreType != null) { 493 dataStoreProperties.put("dataStoreType", this.dataStoreType); 494 } 495 496 if (this.storeVolume != null) { 497 dataStoreProperties.put("dataStoreVolume", this.storeVolume); 498 } 499 500 dataStore.setDataStoreProperties(dataStoreProperties); 501 502 try { 503 dataStore.startDBOperations(); 504 } catch (DataAccessException e) { 505 throw new SyncException(e.getMessage()); 506 } 507 508 if (syncMode == AlertCode.REFRESH_FROM_SERVER) { 513 try { 514 emptyDataStore(); 515 } catch (DataAccessException e) { 516 throw new SyncException(e.getMessage()); 517 } 518 } 519 } 520 521 524 public void commitSync() throws SyncException { 525 526 Vector records = null; 527 528 if (syncSourceChanged && sortDB) { 533 try { 534 records = (Vector ) dataStore.findAllRecords().clone(); 535 536 int l = records.size(); 537 538 for (int i=0; i < l; i++) { 539 dataStore.deleteRecord((Record) records.elementAt(i)); 540 } 541 542 records = sortRecords(records); 543 544 l = records.size(); 545 546 for (int i=0; i < l; i++) { 547 dataStore.storeRecord((Record) records.elementAt(i)); 548 } 549 550 records = null; 551 552 } catch (DataAccessException e) { 553 throw new SyncException(e.getMessage()); 554 } 555 } 556 557 try { 558 dataStore.commitDBOperations(); 559 } catch (DataAccessException e) { 560 throw new SyncException(e.getMessage()); 561 } 562 563 dataStore = null; 564 565 return; 566 567 } 568 569 571 580 private SyncItem[] recordsToSyncItems(DataStore dataStore, Vector records) { 581 SyncItem[] syncItems = null; 582 583 syncItems = new SyncItem[records.size()]; 584 585 int l = records.size(); 586 587 for (int i=0; i < l; i++) { 588 syncItems[i] = recordToSyncItem(dataStore, (Record) records.elementAt(i)); 589 } 590 591 return syncItems; 592 } 593 594 603 private SyncItem recordToSyncItem(DataStore dataStore, Record record) { 604 StringBuffer xml = new StringBuffer (); 605 606 int positionKey = 0; 607 int positionTimestamp = 0; 608 int positionState = 0; 609 610 int count = 0; 611 612 positionKey = record.getPositionKeyField(); 613 positionTimestamp = SyncRecordFunction.getPositionTimestampField(record); 614 positionState = SyncRecordFunction.getPositionModificationTypeField(record); 615 616 SyncItem syncItem = null; 621 622 syncItem = new SyncItemImpl( 623 this, 624 record.getKey(), 625 record.getString(positionState).charAt(0) 626 ); 627 628 xml.append("<record xmlns=\"xmlns://www.funambol.com/spds\">\n"); 632 633 634 int l = dataStore.getRecordMetadata().getFieldMetadata().length; 635 636 for (int i=1; i <= l; i++) { 637 638 if (i != positionTimestamp && i != positionState) { 639 640 if (record.getString(i)!=null && record.getString(i).length() > 0) { 641 xml.append("<field" + i + ">" ); 642 xml.append(record.getString(i)); 643 xml.append("</field" + i + ">\n" ); 644 } 645 646 } 647 648 } 649 650 xml.append("</record>\n"); 651 652 syncItem.setProperty( 653 new SyncItemProperty(SyncItem.PROPERTY_BINARY_CONTENT , 654 xml.toString().getBytes()) 655 ); 656 657 return syncItem; 658 659 } 660 661 670 private Record syncItemToRecord (DataStore dataStore, SyncItem syncItem) throws SyncException { 671 672 Record record = null; 673 674 Hashtable fields = null; 675 Enumeration fieldNumbers = null; 676 677 String xml = null; 678 String timestamp = null; 679 String fieldNumber = null; 680 String fieldValue = null; 681 682 int positionKey = 0; 683 int positionTimestamp = 0; 684 int positionState = 0; 685 int numberOperationFields = 0; 686 687 int j=0; 688 689 record = new Record (dataStore, syncItem.getKey().getKeyAsString()); 690 691 positionKey = record.getPositionKeyField(); 692 positionTimestamp = SyncRecordFunction.getPositionTimestampField(record); 693 positionState = SyncRecordFunction.getPositionModificationTypeField(record); 694 numberOperationFields = SyncRecordFunction.getNumberOperationFields(record); 695 696 if(syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT) != null) { 697 698 xml = new String ((byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT)); 699 700 fields = xmlToValues(xml); 701 702 fieldNumbers = fields.keys(); 703 704 while (fieldNumbers.hasMoreElements()) { 705 706 fieldNumber = (String ) fieldNumbers.nextElement(); 707 fieldValue = (String ) fields.get((Object )fieldNumber); 708 709 try { 710 record.setField(Integer.parseInt(fieldNumber), fieldValue); 711 } catch (NumberFormatException e) { 712 throw new SyncException("SPSSyncSource, error syncItemToRecord: " + e.getMessage()); 713 714 } 715 } 716 717 if (positionState !=-1 ) { 718 record.setField(positionState, String.valueOf(syncItem.getState())); 719 } 720 721 722 } 723 724 return record; 725 } 726 727 728 742 private Hashtable xmlToValues(String xml) { 743 744 Hashtable fields = null; 745 String fieldNumber = null; 746 String fieldValue = null; 747 748 int offset = 0, end, positionEndFieldTag, len; 749 750 fields = new Hashtable (); 751 752 len = xml.length(); 753 754 while (offset < len) { 755 756 offset = xml.indexOf("<field", offset); 757 758 if (offset < 0) { 759 break; 760 } 761 762 offset += 6; 763 764 positionEndFieldTag = xml.indexOf(">", offset); 765 766 if (xml.substring(positionEndFieldTag - 1, positionEndFieldTag).equals("/")) { 768 769 fieldNumber = xml.substring(offset, positionEndFieldTag - 1); 770 fieldValue = ""; 771 offset = positionEndFieldTag + 1; 772 773 } else { 775 776 fieldNumber = xml.substring(offset, positionEndFieldTag); 777 end = xml.indexOf("</field" + fieldNumber + ">", positionEndFieldTag + 1); 778 if (end < 0) { 779 break; 780 } 781 782 fieldValue = xml.substring(positionEndFieldTag + 1, end); 783 784 offset = end + fieldNumber.length() + 8; 785 786 } 787 788 fields.put(fieldNumber, fieldValue); 789 } 790 791 return fields; 792 } 793 794 801 private Vector sortRecords(Vector records) { 802 803 Record tmp1Record = null; 804 805 Record tmp2Record = null; 806 807 for(int i = records.size() - 1 ; i > 0; i-- ) { 808 809 int maxLoc = 0; 810 811 for (int j = 1; j <= i; j++) { 812 if (((Record) records.elementAt(j)).getKey().compareTo(((Record) records.elementAt(maxLoc)).getKey()) > 0) { 813 maxLoc = j; 814 } 815 } 816 817 tmp1Record = (Record) records.elementAt(maxLoc); 818 819 tmp2Record = (Record) records.elementAt(i); 820 821 records.setElementAt(tmp2Record, maxLoc); 822 823 records.setElementAt((Object ) tmp1Record, i); 824 825 } 826 827 return records; 828 } 829 830 835 private void emptyDataStore() throws DataAccessException { 836 837 Vector records = (Vector )dataStore.findAllRecords().clone(); 838 839 int l = records.size(); 840 841 for (int i=0; i < l; i++) { 842 dataStore.deleteRecord((Record) records.elementAt(i)); 843 } 844 845 records = null; 846 847 } 848 } | Popular Tags |