1 17 package org.alfresco.repo.webservice.authoring; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.InputStream ; 21 import java.io.Serializable ; 22 import java.rmi.RemoteException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.transaction.TransactionUtil; 31 import org.alfresco.repo.webservice.AbstractWebService; 32 import org.alfresco.repo.webservice.Utils; 33 import org.alfresco.repo.webservice.types.ContentFormat; 34 import org.alfresco.repo.webservice.types.NamedValue; 35 import org.alfresco.repo.webservice.types.ParentReference; 36 import org.alfresco.repo.webservice.types.Predicate; 37 import org.alfresco.repo.webservice.types.Reference; 38 import org.alfresco.repo.webservice.types.VersionHistory; 39 import org.alfresco.service.cmr.coci.CheckOutCheckInService; 40 import org.alfresco.service.cmr.lock.LockService; 41 import org.alfresco.service.cmr.lock.LockType; 42 import org.alfresco.service.cmr.repository.ContentWriter; 43 import org.alfresco.service.cmr.repository.NodeRef; 44 import org.alfresco.service.cmr.version.Version; 45 import org.alfresco.service.cmr.version.VersionService; 46 import org.alfresco.service.namespace.QName; 47 import org.alfresco.service.transaction.TransactionService; 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 51 58 public class AuthoringWebService extends AbstractWebService implements 59 AuthoringServiceSoapPort 60 { 61 64 private static Log logger = LogFactory.getLog(AuthoringWebService.class); 65 66 69 private CheckOutCheckInService cociService; 70 71 74 private LockService lockService; 75 76 79 private VersionService versionService; 80 81 84 private TransactionService transactionService; 85 86 92 public void setCheckOutCheckinService(CheckOutCheckInService cociService) 93 { 94 this.cociService = cociService; 95 } 96 97 102 public void setLockService(LockService lockService) 103 { 104 this.lockService = lockService; 105 } 106 107 112 public void setVersionService(VersionService versionService) 113 { 114 this.versionService = versionService; 115 } 116 117 123 public void setTransactionService(TransactionService transactionService) 124 { 125 this.transactionService = transactionService; 126 } 127 128 132 public CheckoutResult checkout(final Predicate items, final ParentReference destination) throws RemoteException , 133 AuthoringFault 134 { 135 try 136 { 137 return TransactionUtil.executeInUserTransaction( 138 this.transactionService, 139 new TransactionUtil.TransactionWork<CheckoutResult>() 140 { 141 public CheckoutResult doWork() 142 { 143 List <NodeRef> nodes = Utils.resolvePredicate(items, 144 AuthoringWebService.this.nodeService, 145 AuthoringWebService.this.searchService, 146 AuthoringWebService.this.namespaceService); 147 CheckoutResult checkoutResult = new CheckoutResult(); 148 Reference[] originals = new Reference[nodes.size()]; 149 Reference[] workingCopies = new Reference[nodes.size()]; 150 151 NodeRef destinationRef = null; 154 if (destination != null) 155 { 156 destinationRef = Utils.convertToNodeRef( 157 destination, 158 AuthoringWebService.this.nodeService, 159 AuthoringWebService.this.searchService, 160 AuthoringWebService.this.namespaceService); 161 } 162 163 for (int x = 0; x < nodes.size(); x++) 164 { 165 NodeRef original = nodes.get(x); 167 168 NodeRef workingCopy = null; 171 if (destinationRef != null) 172 { 173 workingCopy = AuthoringWebService.this.cociService 174 .checkout( 175 original, 176 destinationRef, 177 QName.createQName(destination.getAssociationType()), 178 QName.createQName(destination.getChildName())); 179 } else 180 { 181 workingCopy = AuthoringWebService.this.cociService 182 .checkout(original); 183 } 184 185 originals[x] = Utils.convertToReference(original); 187 workingCopies[x] = Utils.convertToReference(workingCopy); 188 } 189 190 checkoutResult.setOriginals(originals); 192 checkoutResult.setWorkingCopies(workingCopies); 193 194 return checkoutResult; 195 } 196 }); 197 } 198 catch (Throwable e) 199 { 200 if (logger.isDebugEnabled()) 201 { 202 logger.error("Unexpected error occurred", e); 203 } 204 205 throw new AuthoringFault(0, e.getMessage()); 206 } 207 } 208 209 213 public CheckinResult checkin(final Predicate items, 214 final NamedValue[] comments, final boolean keepCheckedOut) 215 throws RemoteException , AuthoringFault 216 { 217 try 218 { 219 return TransactionUtil.executeInUserTransaction( 220 this.transactionService, 221 new TransactionUtil.TransactionWork<CheckinResult>() 222 { 223 public CheckinResult doWork() 224 { 225 List <NodeRef> nodes = Utils.resolvePredicate( 227 items, 228 AuthoringWebService.this.nodeService, 229 AuthoringWebService.this.searchService, 230 AuthoringWebService.this.namespaceService); 231 232 Map <String , Serializable > mapComments = new HashMap <String , Serializable >(comments.length); 234 for (NamedValue value : comments) 235 { 236 mapComments.put(value.getName(), value.getValue()); 237 } 238 239 Reference[] checkedIn = new Reference[nodes.size()]; 240 List <Reference> listWorkingCopies = new ArrayList <Reference>(nodes.size()); 241 int iIndex = 0; 242 243 for (NodeRef node : nodes) 246 { 247 NodeRef checkedInNode = AuthoringWebService.this.cociService.checkin(node, mapComments, null, keepCheckedOut); 249 250 checkedIn[iIndex] = Utils.convertToReference(checkedInNode); 252 253 if (keepCheckedOut == true) 255 { 256 listWorkingCopies.add(Utils.convertToReference(node)); 257 } 258 iIndex++; 259 } 260 261 Reference[] workingCopies = listWorkingCopies.toArray(new Reference[listWorkingCopies.size()]); 263 if (workingCopies == null) 264 { 265 workingCopies = new Reference[0]; 266 } 267 268 CheckinResult result = new CheckinResult(); 270 result.setCheckedIn(checkedIn); 271 result.setWorkingCopies(workingCopies); 272 273 return result; 274 } 275 }); 276 } 277 catch (Throwable e) 278 { 279 if (logger.isDebugEnabled()) 280 { 281 logger.error("Unexpected error occurred", e); 282 } 283 284 throw new AuthoringFault(0, e.getMessage()); 285 } 286 } 287 288 291 public Reference checkinExternal(final Reference node, final NamedValue[] comments, final boolean keepCheckedOut, final ContentFormat format, final byte[] content) 292 throws RemoteException , AuthoringFault 293 { 294 try 295 { 296 return TransactionUtil.executeInUserTransaction( 297 this.transactionService, 298 new TransactionUtil.TransactionWork<Reference>() 299 { 300 public Reference doWork() 301 { 302 NodeRef nodeRef = Utils.convertToNodeRef( 304 node, 305 AuthoringWebService.this.nodeService, 306 AuthoringWebService.this.searchService, 307 AuthoringWebService.this.namespaceService); 308 309 ContentWriter contentWriter = AuthoringWebService.this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, false); 312 if (contentWriter == null) 313 { 314 throw new RuntimeException ("Unable to write external content before checkin."); 315 } 316 InputStream is = new ByteArrayInputStream (content); 317 contentWriter.setEncoding(format.getEncoding()); 318 contentWriter.setMimetype(format.getMimetype()); 319 contentWriter.putContent(is); 320 String contentUrl = contentWriter.getContentUrl(); 321 322 Map <String , Serializable > versionProperties = new HashMap <String , Serializable >(comments.length); 324 for (NamedValue namedValue : comments) 325 { 326 versionProperties.put(namedValue.getName(), namedValue.getValue()); 327 } 328 329 NodeRef origNodeRef = AuthoringWebService.this.cociService.checkin( 331 nodeRef, 332 versionProperties, 333 contentUrl, 334 keepCheckedOut); 335 return Utils.convertToReference(origNodeRef); 337 } 338 }); 339 } 340 catch (Throwable e) 341 { 342 if (logger.isDebugEnabled()) 343 { 344 logger.error("Unexpected error occurred", e); 345 } 346 347 throw new AuthoringFault(0, e.getMessage()); 348 } 349 } 350 351 354 public CancelCheckoutResult cancelCheckout(final Predicate items) 355 throws RemoteException , AuthoringFault 356 { 357 try 358 { 359 return TransactionUtil.executeInUserTransaction( 360 this.transactionService, 361 new TransactionUtil.TransactionWork<CancelCheckoutResult>() 362 { 363 public CancelCheckoutResult doWork() 364 { 365 List <NodeRef> nodes = Utils.resolvePredicate( 367 items, 368 AuthoringWebService.this.nodeService, 369 AuthoringWebService.this.searchService, 370 AuthoringWebService.this.namespaceService); 371 372 Reference[] origNodes = new Reference[nodes.size()]; 374 Reference[] workingCopies = new Reference[nodes.size()]; 375 int iIndex = 0; 376 377 for (NodeRef node : nodes) 378 { 379 NodeRef origNode = AuthoringWebService.this.cociService.cancelCheckout(node); 381 382 origNodes[iIndex] = Utils.convertToReference(origNode); 384 workingCopies[iIndex] = Utils.convertToReference(node); 385 iIndex ++; 386 } 387 388 CancelCheckoutResult result = new CancelCheckoutResult(); 389 result.setOriginals(origNodes); 390 result.setWorkingCopies(workingCopies); 391 392 return result; 393 } 394 }); 395 } 396 catch (Throwable e) 397 { 398 if (logger.isDebugEnabled()) 399 { 400 logger.error("Unexpected error occurred", e); 401 } 402 403 throw new AuthoringFault(0, e.getMessage()); 404 } 405 } 406 407 411 public Reference[] lock(final Predicate items, final boolean lockChildren, final LockTypeEnum lockType) 412 throws RemoteException , AuthoringFault 413 { 414 try 415 { 416 return TransactionUtil.executeInUserTransaction( 417 this.transactionService, 418 new TransactionUtil.TransactionWork<Reference[]>() 419 { 420 public Reference[] doWork() 421 { 422 List <NodeRef> nodes = Utils.resolvePredicate( 424 items, 425 AuthoringWebService.this.nodeService, 426 AuthoringWebService.this.searchService, 427 AuthoringWebService.this.namespaceService); 428 429 Reference[] result = new Reference[nodes.size()]; 431 int iIndex = 0; 432 for (NodeRef node : nodes) 433 { 434 LockType convertedLockType = convertToLockType(lockType); 435 AuthoringWebService.this.lockService.lock(node, convertedLockType, 0, lockChildren); 436 result[iIndex] = Utils.convertToReference(node); 437 iIndex++; 438 } 439 440 return result; 441 } 442 }); 443 } 444 catch (Throwable e) 445 { 446 if (logger.isDebugEnabled()) 447 { 448 logger.error("Unexpected error occurred", e); 449 } 450 451 throw new AuthoringFault(0, e.getMessage()); 452 } 453 } 454 455 461 private LockType convertToLockType(LockTypeEnum lockTypeEnum) 462 { 463 LockType lockType = null; 464 if (lockTypeEnum.equals(LockTypeEnum.write) == true) 465 { 466 lockType = LockType.WRITE_LOCK; 467 } 468 else 469 { 470 lockType = LockType.READ_ONLY_LOCK; 471 } 472 return lockType; 473 } 474 475 479 public Reference[] unlock(final Predicate items, final boolean unlockChildren) 480 throws RemoteException , AuthoringFault 481 { 482 try 483 { 484 return TransactionUtil.executeInUserTransaction( 485 this.transactionService, 486 new TransactionUtil.TransactionWork<Reference[]>() 487 { 488 public Reference[] doWork() 489 { 490 List <NodeRef> nodes = Utils.resolvePredicate( 492 items, 493 AuthoringWebService.this.nodeService, 494 AuthoringWebService.this.searchService, 495 AuthoringWebService.this.namespaceService); 496 497 Reference[] result = new Reference[nodes.size()]; 499 int iIndex = 0; 500 for (NodeRef node : nodes) 501 { 502 AuthoringWebService.this.lockService.unlock(node, unlockChildren); 503 504 result[iIndex] = Utils.convertToReference(node); 505 iIndex++; 506 } 507 508 return result; 509 } 510 }); 511 } 512 catch (Throwable e) 513 { 514 if (logger.isDebugEnabled()) 515 { 516 logger.error("Unexpected error occurred", e); 517 } 518 519 throw new AuthoringFault(0, e.getMessage()); 520 } 521 } 522 523 526 public LockStatus[] getLockStatus(final Predicate items) 527 throws RemoteException , AuthoringFault 528 { 529 try 530 { 531 return TransactionUtil.executeInUserTransaction( 532 this.transactionService, 533 new TransactionUtil.TransactionWork<LockStatus[]>() 534 { 535 public LockStatus[] doWork() 536 { 537 List <NodeRef> nodes = Utils.resolvePredicate( 539 items, 540 AuthoringWebService.this.nodeService, 541 AuthoringWebService.this.searchService, 542 AuthoringWebService.this.namespaceService); 543 544 LockStatus[] result = new LockStatus[nodes.size()]; 546 int iIndex = 0; 547 for (NodeRef node : nodes) 548 { 549 String lockOwner = (String )AuthoringWebService.this.nodeService.getProperty(node, ContentModel.PROP_LOCK_OWNER); 551 552 LockTypeEnum lockTypeEnum = convertFromLockType(AuthoringWebService.this.lockService.getLockType(node)); 554 555 LockStatus lockStatus = new LockStatus(); 556 lockStatus.setLockOwner(lockOwner); 557 lockStatus.setLockType(lockTypeEnum); 558 lockStatus.setNode(Utils.convertToReference(node)); 559 560 result[iIndex] = lockStatus; 561 iIndex++; 562 } 563 564 return result; 565 } 566 }); 567 } 568 catch (Throwable e) 569 { 570 571 if (logger.isDebugEnabled()) 572 { 573 logger.error("Unexpected error occurred", e); 574 } 575 576 e.printStackTrace(); 577 throw new AuthoringFault(0, e.getMessage()); 578 } 579 } 580 581 private LockTypeEnum convertFromLockType(LockType lockType) 582 { 583 LockTypeEnum result = null; 584 if (lockType != null) 585 { 586 switch (lockType) 587 { 588 case WRITE_LOCK: 589 result = LockTypeEnum.write; 590 break; 591 case READ_ONLY_LOCK: 592 result = LockTypeEnum.read; 593 break; 594 } 595 } 596 return result; 597 } 598 599 603 public VersionResult createVersion(final Predicate items, final NamedValue[] comments, final boolean versionChildren) 604 throws RemoteException , AuthoringFault 605 { 606 try 607 { 608 return TransactionUtil.executeInUserTransaction( 609 this.transactionService, 610 new TransactionUtil.TransactionWork<VersionResult>() 611 { 612 public VersionResult doWork() 613 { 614 List <NodeRef> nodes = Utils.resolvePredicate( 616 items, 617 AuthoringWebService.this.nodeService, 618 AuthoringWebService.this.searchService, 619 AuthoringWebService.this.namespaceService); 620 621 Map <String , Serializable > mapComments = new HashMap <String , Serializable >(comments.length); 623 for (NamedValue value : comments) 624 { 625 mapComments.put(value.getName(), value.getValue()); 626 } 627 628 List <Reference> versionedReferences = new ArrayList <Reference>(nodes.size()); 629 List <org.alfresco.repo.webservice.types.Version> webServiceVersions = new ArrayList <org.alfresco.repo.webservice.types.Version>(nodes.size()); 630 631 for (NodeRef node : nodes) 633 { 634 Collection <Version> versions = AuthoringWebService.this.versionService.createVersion(node, mapComments, versionChildren); 635 for (Version version : versions) 636 { 637 versionedReferences.add(Utils.convertToReference(version.getVersionedNodeRef())); 638 webServiceVersions.add(Utils.convertToVersion(version)); 639 } 640 } 641 642 VersionResult result = new VersionResult(); 643 result.setNodes(versionedReferences.toArray(new Reference[versionedReferences.size()])); 644 result.setVersions(webServiceVersions.toArray(new org.alfresco.repo.webservice.types.Version[webServiceVersions.size()])); 645 return result; 646 } 647 }); 648 } 649 catch (Throwable e) 650 { 651 if (logger.isDebugEnabled()) 652 { 653 logger.error("Unexpected error occurred", e); 654 } 655 656 e.printStackTrace(); 657 658 throw new AuthoringFault(0, e.getMessage()); 659 } 660 } 661 662 665 public VersionHistory getVersionHistory(final Reference node) 666 throws RemoteException , AuthoringFault 667 { 668 try 669 { 670 return TransactionUtil.executeInUserTransaction( 671 this.transactionService, 672 new TransactionUtil.TransactionWork<VersionHistory>() 673 { 674 public VersionHistory doWork() 675 { 676 org.alfresco.service.cmr.version.VersionHistory versionHistory = 677 AuthoringWebService.this.versionService.getVersionHistory( 678 Utils.convertToNodeRef( 679 node, 680 AuthoringWebService.this.nodeService, 681 AuthoringWebService.this.searchService, 682 AuthoringWebService.this.namespaceService)); 683 684 VersionHistory webServiceVersionHistory = new VersionHistory(); 685 if (versionHistory != null) 686 { 687 Collection <Version> versions = versionHistory.getAllVersions(); 688 org.alfresco.repo.webservice.types.Version[] webServiceVersions = new org.alfresco.repo.webservice.types.Version[versions.size()]; 689 int iIndex = 0; 690 for (Version version : versions) 691 { 692 webServiceVersions[iIndex] = Utils.convertToVersion(version); 693 iIndex ++; 694 } 695 webServiceVersionHistory.setVersions(webServiceVersions); 696 } 697 698 return webServiceVersionHistory; 699 } 700 }); 701 } 702 catch (Throwable e) 703 { 704 if (logger.isDebugEnabled()) 705 { 706 logger.error("Unexpected error occurred", e); 707 } 708 709 throw new AuthoringFault(0, e.getMessage()); 710 } 711 } 712 713 717 @SuppressWarnings ("unchecked") 718 public void revertVersion(final Reference node, final String versionLabel) 719 throws RemoteException , AuthoringFault 720 { 721 try 722 { 723 TransactionUtil.executeInUserTransaction( 724 this.transactionService, 725 new TransactionUtil.TransactionWork() 726 { 727 public Object doWork() 728 { 729 NodeRef nodeRef = Utils.convertToNodeRef( 730 node, 731 AuthoringWebService.this.nodeService, 732 AuthoringWebService.this.searchService, 733 AuthoringWebService.this.namespaceService); 734 735 org.alfresco.service.cmr.version.VersionHistory versionHistory = AuthoringWebService.this.versionService.getVersionHistory(nodeRef); 736 if (versionHistory != null) 737 { 738 Version version = versionHistory.getVersion(versionLabel); 739 if (version != null) 740 { 741 AuthoringWebService.this.versionService.revert(nodeRef, version); 742 } 743 else 744 { 745 throw new RuntimeException ("The node could not be reverted because the version label is invalid."); 746 } 747 } 748 else 749 { 750 throw new RuntimeException ("A unversioned node cannot be reverted."); 751 } 752 753 return null; 754 } 755 }); 756 } 757 catch (Throwable e) 758 { 759 if (logger.isDebugEnabled()) 760 { 761 logger.error("Unexpected error occurred", e); 762 } 763 764 throw new AuthoringFault(0, e.getMessage()); 765 } 766 } 767 768 771 public VersionHistory deleteAllVersions(final Reference node) 772 throws RemoteException , AuthoringFault 773 { 774 try 775 { 776 return TransactionUtil.executeInUserTransaction( 777 this.transactionService, 778 new TransactionUtil.TransactionWork<VersionHistory>() 779 { 780 public VersionHistory doWork() 781 { 782 NodeRef nodeRef = Utils.convertToNodeRef( 783 node, 784 AuthoringWebService.this.nodeService, 785 AuthoringWebService.this.searchService, 786 AuthoringWebService.this.namespaceService); 787 788 AuthoringWebService.this.versionService.deleteVersionHistory(nodeRef); 789 790 return new VersionHistory(); 791 } 792 }); 793 } 794 catch (Throwable e) 795 { 796 if (logger.isDebugEnabled()) 797 { 798 logger.error("Unexpected error occurred", e); 799 } 800 801 throw new AuthoringFault(0, e.getMessage()); 802 } 803 } 804 } 805 | Popular Tags |