1 18 19 package sync4j.framework.protocol; 20 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import sync4j.framework.core.*; 25 import sync4j.framework.database.Database; 26 27 import sync4j.framework.protocol.ProtocolUtil; 28 import sync4j.framework.protocol.v11.SyncModificationsRequirements; 29 30 43 public class SyncModifications extends SyncPackage implements Flags { 44 45 47 56 public SyncModifications(final SyncHdr syncHeader, final SyncBody syncBody, Database[] syncDb) throws Sync4jException { 57 super(syncHeader, syncBody); 58 checkRequirements(); 59 databases = syncDb; 60 } 61 62 public SyncModifications(final SyncHdr syncHeader, final SyncBody syncBody) throws Sync4jException { 63 super(syncHeader, syncBody); 64 } 65 66 68 73 private CmdID serverCapabilitiesCmdId = null; 74 75 80 public CmdID getServerCapabilitiesCmdId() { 81 return this.serverCapabilitiesCmdId; 82 } 83 84 89 public void setServerCapabilitiesCmdId(CmdID serverCapabilitiesCmdId) { 90 this.serverCapabilitiesCmdId = serverCapabilitiesCmdId; 91 } 92 93 98 private CmdID clientCapabilitiesCmdId = null; 99 100 105 public CmdID getClientCapabilitiesCmdId() { 106 return this.clientCapabilitiesCmdId; 107 } 108 109 113 public void setClientCapabilitiesCmdId(CmdID clientCapabilitiesCmdId) { 114 this.clientCapabilitiesCmdId = clientCapabilitiesCmdId; 115 } 116 117 120 private Results clientCapabilitiesResults = null; 121 122 127 public Results getClientCapabilitiesResults() { 128 return this.clientCapabilitiesResults; 129 } 130 131 134 private Status serverCapabilitiesStatus = null; 135 136 141 public Status getServerCapabilitiesStatus() { 142 return this.serverCapabilitiesStatus; 143 } 144 145 148 private CmdID clientSyncCmdId = null; 149 150 155 public CmdID getClientSyncCmdId() { 156 return this.clientSyncCmdId; 157 } 158 159 164 public void setClientSyncCmdId(CmdID clientSyncCmdId) { 165 this.clientSyncCmdId = clientSyncCmdId; 166 } 167 168 171 private AbstractCommand[] serverModifications = null; 172 173 178 public AbstractCommand[] getServerModifications() { 179 return this.serverModifications; 180 } 181 182 187 public void setServerModifications(AbstractCommand[] serverModifications) { 188 this.serverModifications = serverModifications; 189 } 190 191 194 private Status[] clientModificationsStatus = null; 195 196 201 public Status[] getClientModificationsStatus() { 202 return this.clientModificationsStatus; 203 } 204 205 210 public void setClientModificationsStatus(Status[] clientModificationsStatus) { 211 this.clientModificationsStatus = clientModificationsStatus; 212 } 213 214 218 private AbstractCommand[] clientCommands = null; 219 220 225 public AbstractCommand[] getClientCommands() { 226 return clientCommands; 227 } 228 229 233 private Sync[] clientSyncCommands = null; 234 235 240 public Sync[] getClientSyncCommands() { 241 return this.clientSyncCommands; 242 } 243 244 247 private Database[] databases = null; 248 249 254 public void setDatabases(Database[] databases) { 255 this.databases = databases; 256 } 257 258 263 public Database[] getDatabases() { 264 return this.databases; 265 } 266 267 270 private Alert[] modificationsAlert = null; 271 272 277 public Alert[] getModificationsAlert() { 278 return this.modificationsAlert; 279 } 280 281 286 public void setModificationsAlert(Alert[] modificationsAlert) { 287 this.modificationsAlert = modificationsAlert; 288 } 289 290 292 296 public void checkHeaderRequirements() throws ProtocolException { 297 SyncModificationsRequirements.checkDTDVersion (syncHeader.getVerDTD() ); 298 SyncModificationsRequirements.checkProtocolVersion(syncHeader.getVerProto() ); 299 SyncModificationsRequirements.checkSessionId (syncHeader.getSessionID()); 300 SyncModificationsRequirements.checkMessageId (syncHeader.getMsgID() ); 301 SyncModificationsRequirements.checkTarget (syncHeader.getTarget() ); 302 SyncModificationsRequirements.checkSource (syncHeader.getSource() ); 303 } 304 305 311 public void checkBodyRequirements() throws ProtocolException { 312 clientCommands = (AbstractCommand[])syncBody.getCommands().toArray(new AbstractCommand[0]); 314 315 checkServerCapabilitiesStatus(); 322 323 checkClientCapabilitiesResult(); 329 330 checkSyncCommand(); 334 } 335 336 351 public SyncML getResponseMessage(String msgId) 352 throws ProtocolException { 353 SyncHdr responseHeader = getResponseHeader(msgId); 354 AbstractCommand[] commands = 355 (AbstractCommand[])getResponseCommands(msgId).toArray(new AbstractCommand[0]); 356 357 SyncBody responseBody = new SyncBody( 358 commands, 359 isFlag(FLAG_FINAL_MESSAGE) 360 ); 361 362 try { 363 return new SyncML(responseHeader, responseBody); 364 } catch (RepresentationException e) { 365 throw new ProtocolException("Unexpected error", e); 369 } 370 } 371 372 382 public List getResponseCommands(String msgId) throws ProtocolException { 383 ArrayList commandList = new ArrayList (); 384 385 assert(idGenerator != null); 386 387 if (syncHeader.isNoResp() == false) { 395 396 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 397 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 398 399 Status statusCommand = new Status( 400 idGenerator.next() , 401 syncHeader.getMsgID() , 402 "0" , 403 "SyncHdr" , 404 targetRefs , 405 sourceRefs , 406 null , 407 null , 408 new Data(StatusCode.OK) , 409 new Item[0] 410 ); 411 412 commandList.add(statusCommand); 413 414 for (int i=0; ( isFlag(FLAG_SYNC_STATUS_REQUIRED) 421 && (clientModificationsStatus != null) 422 && (i<clientModificationsStatus.length) ); ++i) { 423 commandList.add(clientModificationsStatus[i]); 424 } 425 426 for (int i=0; clientCommands != null && i<clientCommands.length; ++i) { 431 if (clientCommands[i] instanceof Alert) { 432 if (AlertCode.isInitializationCode(((Alert)clientCommands[i]).getData())) { 433 continue; 434 } 435 Item[] items = 436 (Item[])((ItemizedCommand)clientCommands[i]).getItems().toArray(new Item[0]); 437 438 ArrayList trefs = new ArrayList (); 439 ArrayList srefs = new ArrayList (); 440 Target t; 441 Source s; 442 for (int j=0; j<items.length; ++j) { 443 t = items[j].getTarget(); 444 s = items[j].getSource(); 445 446 if (t != null) { 447 trefs.add(new TargetRef(t)); 448 } 449 if (s != null) { 450 srefs.add(new SourceRef(s)); 451 } 452 } 454 if (trefs.size() > 0) { 455 targetRefs = (TargetRef[])trefs.toArray(new TargetRef[trefs.size()]); 456 } 457 if (srefs.size() > 0) { 458 sourceRefs = (SourceRef[])srefs.toArray(new SourceRef[srefs.size()]); 459 } 460 461 Status status = new Status( 462 idGenerator.next(), 463 syncHeader.getMsgID(), 464 clientCommands[i].getCmdID().getCmdID(), 465 clientCommands[i].getName(), 466 targetRefs, 467 sourceRefs, 468 null, 469 null, 470 new Data(StatusCode.OK), 471 new Item[0] 472 ); 473 commandList.add(status); 474 } } } 477 478 for (int i=0; ((modificationsAlert != null) && (i<modificationsAlert.length)); ++i) { 482 commandList.add(modificationsAlert[i]); 483 } 484 485 for (int i=0; ((serverModifications != null) && (i<serverModifications.length)); ++i) { 491 commandList.add(serverModifications[i]); 492 } 493 494 return commandList; 495 } 496 497 506 public Status createSyncStatusCommand() { 507 return new Status( idGenerator.next(), 508 "0" , 509 clientSyncCmdId.getCmdID() , 510 Sync.COMMAND_NAME , 511 (TargetRef[])null , 512 (SourceRef[])null , 513 null , 514 null , 515 null , 516 null 517 ); 518 519 } 520 521 552 public Sync createSyncCommand(Database db) 553 throws ProtocolException { 554 CmdID syncId = idGenerator.next(); 555 556 AbstractCommand[] commands = null; 557 558 561 if(db.getMethod() != AlertCode.ONE_WAY_FROM_CLIENT){ 562 commands = prepareCommands(db); 563 } 564 565 return new Sync( 566 syncId , 567 isFlag(FLAG_SYNC_RESPONSE_REQUIRED), 568 null , 569 db.getTarget() , 570 db.getSource() , 571 null , 572 new Long (0) , 573 commands 574 ); 575 576 } 577 578 586 public AbstractCommand[] prepareCommands(Database db) { 587 ArrayList commands = new ArrayList (); 588 589 Meta meta = new Meta(); 590 meta.setType(db.getType()); 591 592 Item[] items = null; 594 items = db.getAddItems(); 598 if (items != null) { 599 commands.add( 600 new Add( 601 idGenerator.next() , 602 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 603 null , 604 meta , 605 items ) 606 ); 607 } 608 609 items = db.getCopyItems(); 613 if (items != null) { 614 commands.add( 615 new Copy( 616 idGenerator.next() , 617 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 618 null , 619 meta , 620 items ) 621 ); 622 } 623 624 items = db.getDeleteItems(); 628 if (items != null) { 629 commands.add( 630 new Delete( 631 idGenerator.next() , 632 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 633 isFlag(FLAG_ARCHIVE_DATA) , 634 isFlag(FLAG_SOFT_DELETE) , 635 null , 636 meta , 637 items ) 638 ); 639 } 640 641 items = db.getExecItems(); 645 646 for (int i=0; ((items != null) && (i<items.length)); ++i) { 647 commands.add( 648 new Exec( 649 idGenerator.next() , 650 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 651 null , 652 items[i] ) 653 ); 654 } 655 656 items = db.getReplaceItems(); 660 if (items != null) { 661 commands.add( 662 new Replace( 663 idGenerator.next() , 664 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 665 null , 666 meta , 667 items ) 668 ); 669 } 670 671 int size = commands.size(); 672 AbstractCommand [] aCommands = new AbstractCommand[size]; 673 for (int i=0; i < size; i++) { 674 aCommands[i] = (AbstractCommand)commands.get(i); 675 } 676 return aCommands; 677 } 678 679 689 public SyncHdr getResponseHeader(String msgId) 690 throws ProtocolException { 691 Target target = new Target(syncHeader.getSource().getLocURI(), 692 syncHeader.getSource().getLocName()); 693 Source source = new Source(syncHeader.getTarget().getLocURI(), 694 syncHeader.getTarget().getLocName()); 695 return new SyncHdr ( 696 getDTDVersion() , 697 getProtocolVersion() , 698 syncHeader.getSessionID(), 699 msgId , 700 target , 701 source , 702 null , 703 false , 704 null , 705 null 706 ); 707 } 708 709 711 717 private void checkServerCapabilitiesStatus() 718 throws ProtocolException { 719 if (serverCapabilitiesCmdId == null) return; 723 724 List list = ProtocolUtil.filterCommands(clientCommands , 725 Status.class , 726 serverCapabilitiesCmdId); 727 728 if (list.size() == 0) { 729 Object [] args = new Object [] { serverCapabilitiesCmdId.getCmdID() }; 730 throw new ProtocolException(SyncModificationsRequirements.ERRMSG_MISSING_STATUS_COMMAND, args); 731 } 732 733 serverCapabilitiesStatus = (Status)list.get(0); 734 } 735 736 742 private void checkClientCapabilitiesResult() 743 throws ProtocolException { 744 if (clientCapabilitiesCmdId == null) return; 748 749 List list = ProtocolUtil.filterCommands(clientCommands , 750 Results.class , 751 clientCapabilitiesCmdId); 752 753 if (list.size() == 0) { 754 Object [] args = new Object [] { clientCapabilitiesCmdId.getCmdID() }; 755 throw new ProtocolException(SyncModificationsRequirements.ERRMSG_MISSING_RESULTS_COMMAND, args); 756 } 757 758 Results results = (Results)list.get(0); 759 760 SyncModificationsRequirements.checkCapabilities( 761 results, 762 SyncModificationsRequirements.CLIENT_CAPABILITIES 763 ); 764 765 clientCapabilitiesResults = results; 766 } 767 768 775 private void checkSyncCommand() 776 throws ProtocolException { 777 List list = ProtocolUtil.filterCommands(clientCommands , 778 Sync.class); 779 780 if (list.size() == 0) { 781 clientSyncCommands = new Sync[0]; 782 return; 783 } 784 785 clientSyncCommands = (Sync[])list.toArray(new Sync[list.size()]); 786 } 787 } 788 789 | Popular Tags |