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.ClientModificationsRequirements; 29 30 43 public class ClientModifications 44 extends SyncPackage 45 implements Flags { 46 47 49 58 public ClientModifications(final SyncHdr syncHeader, 59 final SyncBody syncBody , 60 Database[] syncDb ) 61 throws ProtocolException { 62 super(syncHeader, syncBody); 63 checkRequirements(); 64 databases = syncDb; 65 } 66 67 public ClientModifications(final SyncHdr syncHeader, 68 final SyncBody syncBody ) 69 throws Sync4jException { 70 super(syncHeader, syncBody); 71 } 72 73 75 80 private CmdID serverCapabilitiesCmdId = null; 81 82 87 public CmdID getServerCapabilitiesCmdId() { 88 return this.serverCapabilitiesCmdId; 89 } 90 91 96 public void setServerCapabilitiesCmdId(CmdID serverCapabilitiesCmdId) { 97 this.serverCapabilitiesCmdId = serverCapabilitiesCmdId; 98 } 99 100 105 private CmdID clientCapabilitiesCmdId = null; 106 107 112 public CmdID getClientCapabilitiesCmdId() { 113 return this.clientCapabilitiesCmdId; 114 } 115 116 120 public void setClientCapabilitiesCmdId(CmdID clientCapabilitiesCmdId) { 121 this.clientCapabilitiesCmdId = clientCapabilitiesCmdId; 122 } 123 124 127 private Results clientCapabilitiesResults = null; 128 129 134 public Results getClientCapabilitiesResults() { 135 return this.clientCapabilitiesResults; 136 } 137 138 141 private Status serverCapabilitiesStatus = null; 142 143 148 public Status getServerCapabilitiesStatus() { 149 return this.serverCapabilitiesStatus; 150 } 151 152 155 private CmdID clientSyncCmdId = null; 156 157 162 public CmdID getClientSyncCmdId() { 163 return this.clientSyncCmdId; 164 } 165 166 171 public void setClientSyncCmdId(CmdID clientSyncCmdId) { 172 this.clientSyncCmdId = clientSyncCmdId; 173 } 174 175 178 private AbstractCommand[] serverModifications = null; 179 180 185 public AbstractCommand[] getServerModifications() { 186 return this.serverModifications; 187 } 188 189 194 public void setServerModifications(AbstractCommand[] serverModifications) { 195 this.serverModifications = serverModifications; 196 } 197 198 201 private Status[] clientModificationsStatus = null; 202 203 208 public Status[] getClientModificationsStatus() { 209 return this.clientModificationsStatus; 210 } 211 212 217 public void setClientModificationsStatus(Status[] clientModificationsStatus) { 218 this.clientModificationsStatus = clientModificationsStatus; 219 } 220 221 225 private AbstractCommand[] clientCommands = null; 226 227 232 public AbstractCommand[] getClientCommands() { 233 return clientCommands; 234 } 235 236 240 private Sync[] clientSyncCommands = null; 241 242 247 public Sync[] getClientSyncCommands() { 248 return this.clientSyncCommands; 249 } 250 251 254 private Database[] databases = null; 255 256 261 public void setDatabases(Database[] databases) { 262 this.databases = databases; 263 } 264 265 270 public Database[] getDatabases() { 271 return this.databases; 272 } 273 274 277 private Alert[] modificationsAlert = null; 278 279 284 public Alert[] getModificationsAlert() { 285 return this.modificationsAlert; 286 } 287 288 293 public void setModificationsAlert(Alert[] modificationsAlert) { 294 this.modificationsAlert = modificationsAlert; 295 } 296 297 299 303 public void checkHeaderRequirements() throws ProtocolException { 304 ClientModificationsRequirements.checkDTDVersion (syncHeader.getVerDTD() ); 305 ClientModificationsRequirements.checkProtocolVersion(syncHeader.getVerProto() ); 306 ClientModificationsRequirements.checkSessionId (syncHeader.getSessionID()); 307 ClientModificationsRequirements.checkMessageId (syncHeader.getMsgID() ); 308 ClientModificationsRequirements.checkTarget (syncHeader.getTarget() ); 309 ClientModificationsRequirements.checkSource (syncHeader.getSource() ); 310 } 311 312 318 public void checkBodyRequirements() throws ProtocolException { 319 clientCommands = (AbstractCommand[])syncBody.getCommands().toArray(new AbstractCommand[0]); 321 322 checkServerCapabilitiesStatus(); 329 330 checkClientCapabilitiesResult(); 336 337 checkSyncCommand(); 341 } 342 343 345 360 public SyncML getResponse(String msgId) throws ProtocolException { 361 ArrayList commandList = new ArrayList (); 362 363 if (idGenerator == null) { 364 throw new NullPointerException ("The id generator is null. Please set a value for idGenerator"); 365 } 366 367 if (syncHeader.isNoResp() == false) { 375 376 TargetRef[] targetRefs = new TargetRef[] { new TargetRef(syncHeader.getTarget().getLocURI()) }; 377 SourceRef[] sourceRefs = new SourceRef[] { new SourceRef(syncHeader.getSource().getLocURI()) }; 378 379 Status statusCommand = new Status( 380 idGenerator.next() , 381 syncHeader.getMsgID() , 382 "0" , 383 "SyncHdr" , 384 targetRefs , 385 sourceRefs , 386 null , 387 null , 388 new Data(StatusCode.OK) , 389 new Item[0] 390 ); 391 392 commandList.add(statusCommand); 393 394 for (int i=0; ( isFlag(FLAG_SYNC_STATUS_REQUIRED) 401 && (clientModificationsStatus != null) 402 && (i<clientModificationsStatus.length) ); ++i) { 403 commandList.add(clientModificationsStatus[i]); 404 } 405 } 406 407 for (int i=0; ((modificationsAlert != null) && (i<modificationsAlert.length)); ++i) { 411 commandList.add(modificationsAlert[i]); 412 } 413 414 for (int i=0; ((serverModifications != null) && (i<serverModifications.length)); ++i) { 420 commandList.add(serverModifications[i]); 421 } 422 423 Target target = new Target(syncHeader.getSource().getLocURI(), 427 syncHeader.getSource().getLocName()); 428 Source source = new Source(syncHeader.getTarget().getLocURI(), 429 syncHeader.getTarget().getLocName()); 430 SyncHdr responseHeader = new SyncHdr ( 431 getDTDVersion() , 432 getProtocolVersion() , 433 syncHeader.getSessionID(), 434 msgId , 435 target , 436 source , 437 null , 438 false , 439 null , 440 null 441 ); 442 443 AbstractCommand[] commands = null; 444 int size = commandList.size(); 445 if (size == 0) { 446 commands = new AbstractCommand[1]; 447 } else { 448 commands = new AbstractCommand[size]; 449 } 450 for (int i=0; i < size; i++) { 451 commands[i] = (AbstractCommand)commandList.get(i); 452 } 453 454 SyncBody responseBody = new SyncBody( 455 commands, 456 isFlag(FLAG_FINAL_MESSAGE) 457 ); 458 459 try { 460 return new SyncML(responseHeader, responseBody); 461 } catch (RepresentationException e) { 462 throw new ProtocolException("Unexpected error", e); 466 } 467 } 468 469 478 public Status createSyncStatusCommand() { 479 return new Status( 480 idGenerator.next() , 481 "0" , 482 clientSyncCmdId.getCmdID() , 483 Sync.COMMAND_NAME , 484 (TargetRef[])null , 485 (SourceRef[])null , 486 null , 487 null , 488 null , 489 null 490 ); 491 492 } 493 494 525 public Sync createSyncCommand(Database db) 526 throws ProtocolException { 527 CmdID syncId = idGenerator.next(); 528 529 AbstractCommand[] commands = null; 530 531 534 if(db.getMethod() != AlertCode.ONE_WAY_FROM_CLIENT){ 535 commands = prepareCommands(db); 536 } 537 538 return new Sync( 539 syncId , 540 isFlag(FLAG_SYNC_RESPONSE_REQUIRED), 541 null , 542 db.getTarget() , 543 db.getSource() , 544 null , 545 0, 546 commands 547 ); 548 549 } 550 551 559 public AbstractCommand[] prepareCommands(Database db) { 560 ArrayList commands = new ArrayList (); 561 562 Meta meta = new Meta(); 563 meta.setType(db.getType()); 564 565 Item[] items = null; 567 items = db.getAddItems(); 571 if (items != null) { 572 commands.add( 573 new Add( 574 idGenerator.next() , 575 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 576 null , 577 meta , 578 items ) 579 ); 580 } 581 582 items = db.getCopyItems(); 586 if (items != null) { 587 commands.add( 588 new Copy( 589 idGenerator.next() , 590 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 591 null , 592 meta , 593 items ) 594 ); 595 } 596 597 items = db.getDeleteItems(); 601 if (items != null) { 602 commands.add( 603 new Delete( 604 idGenerator.next() , 605 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 606 isFlag(FLAG_ARCHIVE_DATA) , 607 isFlag(FLAG_SOFT_DELETE) , 608 null , 609 meta , 610 items ) 611 ); 612 } 613 614 items = db.getExecItems(); 618 619 for (int i=0; ((items != null) && (i<items.length)); ++i) { 620 commands.add( 621 new Exec( 622 idGenerator.next() , 623 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 624 null , 625 items[i] ) 626 ); 627 } 628 629 items = db.getReplaceItems(); 633 if (items != null) { 634 commands.add( 635 new Replace( 636 idGenerator.next() , 637 isFlag(FLAG_MODIFICATIONS_RESPONSE_REQUIRED), 638 null , 639 meta , 640 items ) 641 ); 642 } 643 644 int size = commands.size(); 645 AbstractCommand [] aCommands = new AbstractCommand[size]; 646 for (int i=0; i < size; i++) { 647 aCommands[i] = (AbstractCommand)commands.get(i); 648 } 649 return aCommands; 650 } 651 652 654 660 private void checkServerCapabilitiesStatus() 661 throws ProtocolException { 662 if (serverCapabilitiesCmdId == null) return; 666 667 List list = ProtocolUtil.filterCommands(clientCommands , 668 Status.class , 669 serverCapabilitiesCmdId); 670 671 if (list.size() == 0) { 672 Object [] args = new Object [] { serverCapabilitiesCmdId.getCmdID() }; 673 throw new ProtocolException(ClientModificationsRequirements.ERRMSG_MISSING_STATUS_COMMAND, args); 674 } 675 676 serverCapabilitiesStatus = (Status)list.get(0); 677 } 678 679 685 private void checkClientCapabilitiesResult() 686 throws ProtocolException { 687 if (clientCapabilitiesCmdId == null) return; 691 692 List list = ProtocolUtil.filterCommands(clientCommands , 693 Results.class , 694 clientCapabilitiesCmdId); 695 696 if (list.size() == 0) { 697 Object [] args = new Object [] { clientCapabilitiesCmdId.getCmdID() }; 698 throw new ProtocolException(ClientModificationsRequirements.ERRMSG_MISSING_RESULTS_COMMAND, args); 699 } 700 701 Results results = (Results)list.get(0); 702 703 ClientModificationsRequirements.checkCapabilities( 704 results, 705 ClientModificationsRequirements.CLIENT_CAPABILITIES 706 ); 707 708 clientCapabilitiesResults = results; 709 } 710 711 718 private void checkSyncCommand() 719 throws ProtocolException { 720 List list = ProtocolUtil.filterCommands(clientCommands , 721 Sync.class); 722 723 if (list.size() == 0) { 724 clientSyncCommands = new Sync[0]; 725 return; 726 } 727 728 clientSyncCommands = (Sync[])list.toArray(new Sync[list.size()]); 729 } 730 731 } | Popular Tags |