1 17 package org.alfresco.web.bean; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.text.MessageFormat ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import javax.faces.context.FacesContext; 26 import javax.faces.event.ActionEvent; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.content.MimetypeMap; 31 import org.alfresco.repo.version.VersionModel; 32 import org.alfresco.service.cmr.coci.CheckOutCheckInService; 33 import org.alfresco.service.cmr.repository.ChildAssociationRef; 34 import org.alfresco.service.cmr.repository.ContentData; 35 import org.alfresco.service.cmr.repository.ContentReader; 36 import org.alfresco.service.cmr.repository.ContentService; 37 import org.alfresco.service.cmr.repository.ContentWriter; 38 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 39 import org.alfresco.service.cmr.repository.NodeRef; 40 import org.alfresco.service.cmr.repository.NodeService; 41 import org.alfresco.service.cmr.version.Version; 42 import org.alfresco.service.cmr.version.VersionType; 43 import org.alfresco.web.app.Application; 44 import org.alfresco.web.app.context.UIContextService; 45 import org.alfresco.web.app.servlet.DownloadContentServlet; 46 import org.alfresco.web.bean.repository.Node; 47 import org.alfresco.web.bean.repository.Repository; 48 import org.alfresco.web.ui.common.Utils; 49 import org.alfresco.web.ui.common.component.UIActionLink; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 56 public class CheckinCheckoutBean 57 { 58 61 64 public BrowseBean getBrowseBean() 65 { 66 return this.browseBean; 67 } 68 69 72 public void setBrowseBean(BrowseBean browseBean) 73 { 74 this.browseBean = browseBean; 75 } 76 77 80 public NodeService getNodeService() 81 { 82 return this.nodeService; 83 } 84 85 88 public void setNodeService(NodeService nodeService) 89 { 90 this.nodeService = nodeService; 91 } 92 93 96 public CheckOutCheckInService getVersionOperationsService() 97 { 98 return this.versionOperationsService; 99 } 100 101 104 public void setVersionOperationsService(CheckOutCheckInService versionOperationsService) 105 { 106 this.versionOperationsService = versionOperationsService; 107 } 108 109 112 public ContentService getContentService() 113 { 114 return this.contentService; 115 } 116 117 120 public void setContentService(ContentService contentService) 121 { 122 this.contentService = contentService; 123 } 124 125 128 public Node getDocument() 129 { 130 return this.document; 131 } 132 133 136 public void setDocument(Node document) 137 { 138 this.document = document; 139 } 140 141 144 public Node getWorkingDocument() 145 { 146 return this.workingDocument; 147 } 148 149 152 public void setWorkingDocument(Node workingDocument) 153 { 154 this.workingDocument = workingDocument; 155 } 156 157 163 public boolean isVersionable() 164 { 165 return getDocument().hasAspect(ContentModel.ASPECT_VERSIONABLE); 166 } 167 168 171 public void setKeepCheckedOut(boolean keepCheckedOut) 172 { 173 this.keepCheckedOut = keepCheckedOut; 174 } 175 176 179 public boolean getKeepCheckedOut() 180 { 181 return this.keepCheckedOut; 182 } 183 184 187 public void setMinorChange(boolean minorChange) 188 { 189 this.minorChange = minorChange; 190 } 191 192 195 public boolean getMinorChange() 196 { 197 return this.minorChange; 198 } 199 200 203 public String getVersionNotes() 204 { 205 return this.versionNotes; 206 } 207 208 211 public void setVersionNotes(String versionNotes) 212 { 213 this.versionNotes = versionNotes; 214 } 215 216 219 public NodeRef getSelectedSpaceId() 220 { 221 return this.selectedSpaceId; 222 } 223 224 227 public void setSelectedSpaceId(NodeRef selectedSpaceId) 228 { 229 this.selectedSpaceId = selectedSpaceId; 230 } 231 232 235 public String getCopyLocation() 236 { 237 if (this.fileName != null) 238 { 239 return CheckinCheckoutBean.COPYLOCATION_OTHER; 240 } 241 else 242 { 243 return this.copyLocation; 244 } 245 } 246 247 250 public void setCopyLocation(String copyLocation) 251 { 252 this.copyLocation = copyLocation; 253 } 254 255 258 public String getFileUploadSuccessMsg() 259 { 260 String msg = Application.getMessage(FacesContext.getCurrentInstance(), "file_upload_success"); 261 return MessageFormat.format(msg, new Object [] {getFileName()}); 262 } 263 264 267 public String getFileName() 268 { 269 FacesContext ctx = FacesContext.getCurrentInstance(); 272 FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap(). 273 get(FileUploadBean.FILE_UPLOAD_BEAN_NAME); 274 if (fileBean != null) 275 { 276 this.file = fileBean.getFile(); 277 this.fileName = fileBean.getFileName(); 278 } 279 280 return this.fileName; 281 } 282 283 286 public void setFileName(String fileName) 287 { 288 this.fileName = fileName; 289 290 FacesContext ctx = FacesContext.getCurrentInstance(); 292 FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap(). 293 get(FileUploadBean.FILE_UPLOAD_BEAN_NAME); 294 if (fileBean != null) 295 { 296 fileBean.setFileName(this.fileName); 297 } 298 } 299 300 303 public String getDocumentContent() 304 { 305 return this.documentContent; 306 } 307 308 311 public void setDocumentContent(String documentContent) 312 { 313 this.documentContent = documentContent; 314 } 315 316 319 public String getEditorOutput() 320 { 321 return this.editorOutput; 322 } 323 324 327 public void setEditorOutput(String editorOutput) 328 { 329 this.editorOutput = editorOutput; 330 } 331 332 333 336 343 public void setupContentAction(ActionEvent event) 344 { 345 UIActionLink link = (UIActionLink)event.getComponent(); 346 Map <String , String > params = link.getParameterMap(); 347 String id = params.get("id"); 348 if (id != null && id.length() != 0) 349 { 350 setupContentDocument(id); 351 } 352 else 353 { 354 setDocument(null); 355 } 356 357 clearUpload(); 358 } 359 360 367 private Node setupContentDocument(String id) 368 { 369 if (logger.isDebugEnabled()) 370 logger.debug("Setup for action, setting current document to: " + id); 371 372 Node node = null; 373 374 try 375 { 376 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 378 node = new Node(ref); 379 380 String url = DownloadContentServlet.generateDownloadURL(ref, node.getName()); 383 node.getProperties().put("url", url); 384 node.getProperties().put("workingCopy", node.hasAspect(ContentModel.ASPECT_WORKING_COPY)); 385 node.getProperties().put("fileType32", Utils.getFileTypeImage(node.getName(), false)); 386 387 setDocument(node); 389 390 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 393 } 394 catch (InvalidNodeRefException refErr) 395 { 396 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 397 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {id}) ); 398 } 399 400 return node; 401 } 402 403 406 public String checkoutFile() 407 { 408 String outcome = null; 409 410 UserTransaction tx = null; 411 412 Node node = getDocument(); 413 if (node != null) 414 { 415 try 416 { 417 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 418 tx.begin(); 419 420 if (logger.isDebugEnabled()) 421 logger.debug("Trying to checkout content node Id: " + node.getId()); 422 423 if (logger.isDebugEnabled()) 425 { 426 logger.debug("Checkout copy location: " + getCopyLocation()); 427 logger.debug("Selected Space Id: " + this.selectedSpaceId); 428 } 429 NodeRef workingCopyRef; 430 if (getCopyLocation().equals(COPYLOCATION_OTHER) && this.selectedSpaceId != null) 431 { 432 NodeRef destRef = this.selectedSpaceId; 434 435 ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(destRef); 436 workingCopyRef = this.versionOperationsService.checkout(node.getNodeRef(), 437 destRef, ContentModel.ASSOC_CONTAINS, childAssocRef.getQName()); 438 } 439 else 440 { 441 workingCopyRef = this.versionOperationsService.checkout(node.getNodeRef()); 442 } 443 444 Node workingCopy = new Node(workingCopyRef); 446 setWorkingDocument(workingCopy); 447 448 String url = DownloadContentServlet.generateDownloadURL(workingCopyRef, workingCopy.getName()); 451 452 workingCopy.getProperties().put("url", url); 453 workingCopy.getProperties().put("fileType32", Utils.getFileTypeImage(workingCopy.getName(), false)); 454 455 tx.commit(); 457 458 outcome = "checkoutFileLink"; 460 } 461 catch (Throwable err) 462 { 463 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 465 Utils.addErrorMessage(Application.getMessage( 466 FacesContext.getCurrentInstance(), MSG_ERROR_CHECKOUT) + err.getMessage(), err); 467 } 468 } 469 else 470 { 471 logger.warn("WARNING: checkoutFile called without a current Document!"); 472 } 473 474 return outcome; 475 } 476 477 480 public String checkoutFileOK() 481 { 482 String outcome = null; 483 484 Node node = getWorkingDocument(); 485 if (node != null) 486 { 487 clearUpload(); 489 setDocument(null); 490 setWorkingDocument(null); 491 492 outcome = "browse"; 493 } 494 else 495 { 496 logger.warn("WARNING: checkoutFileOK called without a current WorkingDocument!"); 497 } 498 499 return outcome; 500 } 501 502 505 public String editFileOK() 506 { 507 String outcome = null; 508 509 Node node = getDocument(); 510 if (node != null) 511 { 512 clearUpload(); 514 setDocument(null); 515 setWorkingDocument(null); 516 517 outcome = "browse"; 518 } 519 else 520 { 521 logger.warn("WARNING: editFileOK called without a current Document!"); 522 } 523 524 return outcome; 525 } 526 527 531 public void editFile(ActionEvent event) 532 { 533 UIActionLink link = (UIActionLink)event.getComponent(); 534 Map <String , String > params = link.getParameterMap(); 535 String id = params.get("id"); 536 if (id != null && id.length() != 0) 537 { 538 Node node = setupContentDocument(id); 539 540 if (node.hasAspect(ContentModel.ASPECT_INLINEEDITABLE) && 542 node.getProperties().get(ContentModel.PROP_EDITINLINE) != null && 543 ((Boolean )node.getProperties().get(ContentModel.PROP_EDITINLINE)).booleanValue() == true) 544 { 545 ContentReader reader = getContentService().getReader(node.getNodeRef(), ContentModel.PROP_CONTENT); 547 String mimetype = reader.getMimetype(); 548 549 if (MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(mimetype) || 551 MimetypeMap.MIMETYPE_XML.equals(mimetype) || 552 MimetypeMap.MIMETYPE_TEXT_CSS.equals(mimetype)) 553 { 554 if (reader != null) 556 { 557 setEditorOutput(reader.getContentString()); 558 } 559 else 560 { 561 setEditorOutput(""); 562 } 563 564 FacesContext fc = FacesContext.getCurrentInstance(); 566 fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editTextInline"); 567 } 568 else 569 { 570 if (reader != null) 572 { 573 setDocumentContent(reader.getContentString()); 574 } 575 else 576 { 577 setDocumentContent(""); 578 } 579 setEditorOutput(null); 580 581 FacesContext fc = FacesContext.getCurrentInstance(); 583 fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editHtmlInline"); 584 } 585 } 586 else 587 { 588 FacesContext fc = FacesContext.getCurrentInstance(); 590 fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "editFile"); 591 } 592 } 593 } 594 595 598 public String editInlineOK() 599 { 600 String outcome = null; 601 602 UserTransaction tx = null; 603 604 Node node = getDocument(); 605 if (node != null) 606 { 607 try 608 { 609 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 610 tx.begin(); 611 612 if (logger.isDebugEnabled()) 613 logger.debug("Trying to update content node Id: " + node.getId()); 614 615 ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true); 617 writer.putContent(this.editorOutput); 618 619 tx.commit(); 621 622 clearUpload(); 624 setDocument(null); 625 setDocumentContent(null); 626 setEditorOutput(null); 627 628 outcome = "browse"; 629 } 630 catch (Throwable err) 631 { 632 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 634 Utils.addErrorMessage(Application.getMessage( 635 FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage()); 636 } 637 } 638 else 639 { 640 logger.warn("WARNING: editInlineOK called without a current Document!"); 641 } 642 643 return outcome; 644 } 645 646 649 public String undoCheckout() 650 { 651 String outcome = null; 652 653 Node node = getWorkingDocument(); 654 if (node != null) 655 { 656 try 657 { 658 this.versionOperationsService.cancelCheckout(node.getNodeRef()); 660 661 clearUpload(); 662 663 outcome = "browse"; 664 } 665 catch (Throwable err) 666 { 667 Utils.addErrorMessage(Application.getMessage( 668 FacesContext.getCurrentInstance(), MSG_ERROR_CANCELCHECKOUT) + err.getMessage(), err); 669 } 670 } 671 else 672 { 673 logger.warn("WARNING: undoCheckout called without a current WorkingDocument!"); 674 } 675 676 return outcome; 677 } 678 679 685 public String undoCheckoutFile() 686 { 687 String outcome = null; 688 689 Node node = getDocument(); 690 if (node != null) 691 { 692 try 693 { 694 if (node.hasAspect(ContentModel.ASPECT_WORKING_COPY)) 695 { 696 this.versionOperationsService.cancelCheckout(node.getNodeRef()); 697 } 698 else if (node.hasAspect(ContentModel.ASPECT_LOCKABLE)) 699 { 700 throw new RuntimeException ("NOT IMPLEMENTED"); 705 } 706 else 707 { 708 throw new IllegalStateException ("Node supplied for undo checkout has neither Working Copy or Locked aspect!"); 709 } 710 711 clearUpload(); 712 713 outcome = "browse"; 714 } 715 catch (Throwable err) 716 { 717 Utils.addErrorMessage(MSG_ERROR_CANCELCHECKOUT + err.getMessage(), err); 718 } 719 } 720 else 721 { 722 logger.warn("WARNING: undoCheckout called without a current WorkingDocument!"); 723 } 724 725 return outcome; 726 } 727 728 731 public String checkinFileOK() 732 { 733 String outcome = null; 734 735 UserTransaction tx = null; 736 737 Node node = getDocument(); 739 if (node != null && (getCopyLocation().equals(COPYLOCATION_CURRENT) || this.getFileName() != null)) 740 { 741 try 742 { 743 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 744 tx.begin(); 745 746 if (logger.isDebugEnabled()) 747 logger.debug("Trying to checkin content node Id: " + node.getId()); 748 749 String contentUrl; 752 String mimetype; 753 if (getCopyLocation().equals(COPYLOCATION_CURRENT)) 754 { 755 ContentData contentData = (ContentData) node.getProperties().get(ContentModel.PROP_CONTENT); 756 contentUrl = (contentData == null ? null : contentData.getContentUrl()); 757 } 758 else 760 { 761 ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, false); 764 writer.putContent(this.file); 766 contentUrl = writer.getContentUrl(); 767 } 768 769 if (contentUrl == null || contentUrl.length() == 0) 770 { 771 throw new IllegalStateException ("Content URL is empty for specified working copy content node!"); 772 } 773 774 Map <String , Serializable > props = new HashMap <String , Serializable >(1, 1.0f); 776 props.put(Version.PROP_DESCRIPTION, this.versionNotes); 777 if (this.minorChange) 779 { 780 props.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR); 781 } 782 else 783 { 784 props.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR); 785 } 786 787 NodeRef originalDoc = this.versionOperationsService.checkin( 788 node.getNodeRef(), 789 props, 790 contentUrl, 791 this.keepCheckedOut); 792 793 tx.commit(); 795 796 setDocument(null); 798 clearUpload(); 799 800 outcome = "browse"; 801 } 802 catch (Throwable err) 803 { 804 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 806 Utils.addErrorMessage(Application.getMessage( 807 FacesContext.getCurrentInstance(), MSG_ERROR_CHECKIN) + err.getMessage(), err); 808 } 809 } 810 else 811 { 812 logger.warn("WARNING: checkinFileOK called without a current Document!"); 813 } 814 815 return outcome; 816 } 817 818 821 public String updateFileOK() 822 { 823 String outcome = null; 824 825 UserTransaction tx = null; 826 827 Node node = getDocument(); 829 if (node != null && this.getFileName() != null) 830 { 831 try 832 { 833 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 834 tx.begin(); 835 836 if (logger.isDebugEnabled()) 837 logger.debug("Trying to update content node Id: " + node.getId()); 838 839 ContentWriter writer = this.contentService.getWriter(node.getNodeRef(), ContentModel.PROP_CONTENT, true); 841 writer.putContent(this.file); 842 843 tx.commit(); 845 846 setDocument(null); 848 clearUpload(); 849 850 outcome = "browse"; 851 } 852 catch (Throwable err) 853 { 854 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 856 Utils.addErrorMessage(Application.getMessage( 857 FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE) + err.getMessage(), err); 858 } 859 } 860 else 861 { 862 logger.warn("WARNING: updateFileOK called without a current Document!"); 863 } 864 865 return outcome; 866 } 867 868 871 public String cancel() 872 { 873 clearUpload(); 875 876 return "browse"; 877 } 878 879 882 private void clearUpload() 883 { 884 if (this.file != null) 886 { 887 this.file.delete(); 888 } 889 890 this.file = null; 891 this.fileName = null; 892 this.keepCheckedOut = false; 893 this.minorChange = true; 894 this.copyLocation = COPYLOCATION_CURRENT; 895 this.versionNotes = ""; 896 this.selectedSpaceId = null; 897 898 FacesContext ctx = FacesContext.getCurrentInstance(); 900 ctx.getExternalContext().getSessionMap().remove(FileUploadBean.FILE_UPLOAD_BEAN_NAME); 901 } 902 903 904 907 private static Log logger = LogFactory.getLog(CheckinCheckoutBean.class); 908 909 910 private static final String MSG_ERROR_CHECKIN = "error_checkin"; 911 private static final String MSG_ERROR_CANCELCHECKOUT = "error_cancel_checkout"; 912 private static final String MSG_ERROR_UPDATE = "error_update"; 913 private static final String MSG_ERROR_CHECKOUT = "error_checkout"; 914 915 916 private static final String COPYLOCATION_CURRENT = "current"; 917 private static final String COPYLOCATION_OTHER = "other"; 918 919 920 private Node document; 921 922 923 private Node workingDocument; 924 925 926 private String documentContent; 927 928 929 private String editorOutput; 930 931 932 private File file; 933 private String fileName; 934 private boolean keepCheckedOut = false; 935 private boolean minorChange = true; 936 private String copyLocation = COPYLOCATION_CURRENT; 937 private String versionNotes = ""; 938 private NodeRef selectedSpaceId = null; 939 940 941 protected BrowseBean browseBean; 942 943 944 protected NodeService nodeService; 945 946 947 protected CheckOutCheckInService versionOperationsService; 948 949 950 protected ContentService contentService; 951 } 952 | Popular Tags |