1 17 package org.alfresco.web.bean.wizard; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.ResourceBundle ; 26 27 import javax.faces.application.FacesMessage; 28 import javax.faces.context.FacesContext; 29 import javax.faces.model.SelectItem; 30 import javax.transaction.UserTransaction ; 31 32 import org.alfresco.config.Config; 33 import org.alfresco.config.ConfigElement; 34 import org.alfresco.model.ContentModel; 35 import org.alfresco.service.cmr.dictionary.DictionaryService; 36 import org.alfresco.service.cmr.dictionary.TypeDefinition; 37 import org.alfresco.service.cmr.model.FileExistsException; 38 import org.alfresco.service.cmr.model.FileInfo; 39 import org.alfresco.service.cmr.repository.NodeRef; 40 import org.alfresco.service.cmr.search.SearchService; 41 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver; 42 import org.alfresco.service.namespace.NamespaceService; 43 import org.alfresco.service.namespace.QName; 44 import org.alfresco.web.app.Application; 45 import org.alfresco.web.bean.repository.Node; 46 import org.alfresco.web.bean.repository.Repository; 47 import org.alfresco.web.data.IDataContainer; 48 import org.alfresco.web.data.QuickSort; 49 import org.alfresco.web.ui.common.Utils; 50 import org.alfresco.web.ui.common.component.UIListItem; 51 import org.alfresco.web.ui.common.component.description.UIDescription; 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 60 public class NewSpaceWizard extends AbstractWizardBean 61 { 62 public static final String SPACE_ICON_DEFAULT = "space-icon-default"; 63 64 private static Log logger = LogFactory.getLog(NewSpaceWizard.class); 65 66 private static final String WIZARD_TITLE_ID = "new_space_title"; 68 private static final String WIZARD_DESC_ID = "new_space_desc"; 69 private static final String STEP1_TITLE_ID = "new_space_step1_title"; 70 private static final String STEP1_DESCRIPTION_ID = "new_space_step1_desc"; 71 private static final String STEP2_TITLE_ID = "new_space_step2_title"; 72 private static final String STEP2_DESCRIPTION_ID = "new_space_step2_desc"; 73 private static final String STEP3_TITLE_ID = "new_space_step3_title"; 74 private static final String STEP3_DESCRIPTION_ID = "new_space_step3_desc"; 75 private static final String FINISH_INSTRUCTION_ID = "new_space_finish_instruction"; 76 77 private static final String ERROR = "error_space"; 78 private static final String DEFAULT_SPACE_TYPE_ICON = "/images/icons/space.gif"; 79 private static final String ICONS_LOOKUP_KEY = " icons"; 80 81 protected SearchService searchService; 83 protected NamespaceService namespaceService; 84 protected DictionaryService dictionaryService; 85 86 protected String spaceType; 87 protected String icon; 88 protected String createFrom; 89 protected NodeRef existingSpaceId; 90 protected String templateSpaceId; 91 protected String copyPolicy; 92 protected String name; 93 protected String description; 94 protected String templateName; 95 protected boolean saveAsTemplate; 96 protected List <SelectItem> templates; 97 protected List <UIListItem> folderTypes; 98 protected List <UIDescription> folderTypeDescriptions; 99 100 protected NodeRef createdNode; 102 103 108 public String finish() 109 { 110 String outcome = FINISH_OUTCOME; 111 112 UserTransaction tx = null; 113 114 try 115 { 116 FacesContext context = FacesContext.getCurrentInstance(); 117 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 118 tx.begin(); 119 120 if (this.editMode) 121 { 122 Node currentSpace = this.browseBean.getActionSpace(); 124 NodeRef nodeRef = currentSpace.getNodeRef(); 125 126 fileFolderService.rename(nodeRef, this.name); 128 129 Map <QName, Serializable > properties = this.nodeService.getProperties(nodeRef); 131 properties.put(ContentModel.PROP_NAME, this.name); 132 properties.put(ContentModel.PROP_ICON, this.icon); 133 properties.put(ContentModel.PROP_DESCRIPTION, this.description); 134 135 this.nodeService.setProperties(nodeRef, properties); 137 } 138 else 139 { 140 String newSpaceId = null; 141 142 if (this.createFrom.equals("scratch")) 143 { 144 NodeRef parentNodeRef; 146 String nodeId = getNavigator().getCurrentNodeId(); 147 if (nodeId == null) 148 { 149 parentNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 150 } 151 else 152 { 153 parentNodeRef = new NodeRef(Repository.getStoreRef(), nodeId); 154 } 155 156 FileInfo fileInfo = fileFolderService.create( 157 parentNodeRef, 158 this.name, 159 Repository.resolveToQName(this.spaceType)); 160 NodeRef nodeRef = fileInfo.getNodeRef(); 161 newSpaceId = nodeRef.getId(); 162 163 if (logger.isDebugEnabled()) 164 logger.debug("Created folder node with name: " + this.name); 165 166 Map <QName, Serializable > uiFacetsProps = new HashMap <QName, Serializable >(5); 168 uiFacetsProps.put(ContentModel.PROP_ICON, this.icon); 169 uiFacetsProps.put(ContentModel.PROP_TITLE, this.name); 170 uiFacetsProps.put(ContentModel.PROP_DESCRIPTION, this.description); 171 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_UIFACETS, uiFacetsProps); 172 173 if (logger.isDebugEnabled()) 174 logger.debug("Added uifacets aspect with properties: " + uiFacetsProps); 175 176 this.createdNode = nodeRef; 178 } 179 else if (this.createFrom.equals("existing")) 180 { 181 NodeRef sourceNode = this.existingSpaceId; 183 NodeRef parentSpace = new NodeRef(Repository.getStoreRef(), getNavigator().getCurrentNodeId()); 184 185 NodeRef copiedNode = this.fileFolderService.copy(sourceNode, parentSpace, this.name).getNodeRef(); 187 188 this.nodeService.setProperty(copiedNode, ContentModel.PROP_DESCRIPTION, this.description); 190 this.nodeService.setProperty(copiedNode, ContentModel.PROP_ICON, this.icon); 191 192 newSpaceId = copiedNode.getId(); 193 194 if (logger.isDebugEnabled()) 195 logger.debug("Copied space with id of " + sourceNode.getId() + " to " + this.name); 196 197 this.createdNode = copiedNode; 199 } 200 else if (this.createFrom.equals("template")) 201 { 202 NodeRef sourceNode = new NodeRef(Repository.getStoreRef(), this.templateSpaceId); 204 NodeRef parentSpace = new NodeRef(Repository.getStoreRef(), getNavigator().getCurrentNodeId()); 205 NodeRef copiedNode = this.fileFolderService.copy(sourceNode, parentSpace, this.name).getNodeRef(); 207 this.nodeService.setProperty(copiedNode, ContentModel.PROP_DESCRIPTION, this.description); 209 this.nodeService.setProperty(copiedNode, ContentModel.PROP_ICON, this.icon); 210 211 newSpaceId = copiedNode.getId(); 212 213 if (logger.isDebugEnabled()) 214 logger.debug("Copied template space with id of " + sourceNode.getId() + " to " + this.name); 215 216 this.createdNode = copiedNode; 218 } 219 220 if (this.saveAsTemplate) 223 { 224 DynamicNamespacePrefixResolver namespacePrefixResolver = new DynamicNamespacePrefixResolver(null); 226 namespacePrefixResolver.registerNamespace(NamespaceService.APP_MODEL_PREFIX, NamespaceService.APP_MODEL_1_0_URI); 227 228 String xpath = Application.getRootPath(FacesContext.getCurrentInstance()) + "/" + 229 Application.getGlossaryFolderName(FacesContext.getCurrentInstance()) + "/" + 230 Application.getSpaceTemplatesFolderName(FacesContext.getCurrentInstance()); 231 232 NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 233 List <NodeRef> templateNodeList = this.searchService.selectNodes( 234 rootNodeRef, 235 xpath, null, namespacePrefixResolver, false); 236 if (templateNodeList.size() == 1) 237 { 238 NodeRef templateNode = templateNodeList.get(0); 240 NodeRef sourceNode = new NodeRef(Repository.getStoreRef(), newSpaceId); 241 fileFolderService.copy(sourceNode, templateNode, this.templateName); 243 } 244 } 245 } 246 247 performCustomProcessing(context); 249 250 tx.commit(); 252 253 String statusMsg = null; 256 if (this.editMode) 257 { 258 this.browseBean.getActionSpace().reset(); 259 statusMsg = MessageFormat.format(Application.getMessage(context, "status_space_updated"), 260 new Object []{this.name}); 261 } 262 else 263 { 264 statusMsg = MessageFormat.format(Application.getMessage(context, "status_space_created"), 266 new Object []{this.name}); 267 } 268 269 Utils.addStatusMessage(FacesMessage.SEVERITY_INFO, statusMsg); 271 } 272 catch (FileExistsException e) 273 { 274 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 276 String statusMsg = MessageFormat.format( 278 Application.getMessage( 279 FacesContext.getCurrentInstance(), "error_exists"), 280 e.getExisting().getName()); 281 Utils.addErrorMessage(statusMsg); 282 outcome = null; 284 } 285 catch (Throwable e) 286 { 287 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 289 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 290 FacesContext.getCurrentInstance(), ERROR), e.getMessage()), e); 291 outcome = null; 292 } 293 294 return outcome; 295 } 296 297 300 public String getWizardDescription() 301 { 302 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_DESC_ID); 303 } 304 305 308 public String getWizardTitle() 309 { 310 return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_TITLE_ID); 311 } 312 313 316 public String getStepDescription() 317 { 318 String stepDesc = null; 319 320 switch (this.currentStep) 321 { 322 case 1: 323 { 324 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_DESCRIPTION_ID); 325 break; 326 } 327 case 2: 328 { 329 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_DESCRIPTION_ID); 330 break; 331 } 332 case 3: 333 { 334 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP3_DESCRIPTION_ID); 335 break; 336 } 337 case 4: 338 { 339 stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_DESCRIPTION_ID); 340 break; 341 } 342 default: 343 { 344 stepDesc = ""; 345 } 346 } 347 348 return stepDesc; 349 } 350 351 354 public String getStepTitle() 355 { 356 String stepTitle = null; 357 358 switch (this.currentStep) 359 { 360 case 1: 361 { 362 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_TITLE_ID); 363 break; 364 } 365 case 2: 366 { 367 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_TITLE_ID); 368 break; 369 } 370 case 3: 371 { 372 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP3_TITLE_ID); 373 break; 374 } 375 case 4: 376 { 377 stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_TITLE_ID); 378 break; 379 } 380 default: 381 { 382 stepTitle = ""; 383 } 384 } 385 386 return stepTitle; 387 } 388 389 392 public String getStepInstructions() 393 { 394 String stepInstruction = null; 395 396 switch (this.currentStep) 397 { 398 case 4: 399 { 400 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), FINISH_INSTRUCTION_ID); 401 break; 402 } 403 default: 404 { 405 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), DEFAULT_INSTRUCTION_ID); 406 } 407 } 408 409 return stepInstruction; 410 } 411 412 415 public void init() 416 { 417 super.init(); 418 419 if (this.templates != null) 421 { 422 this.templates.clear(); 423 this.templates = null; 424 } 425 426 this.createFrom = "scratch"; 428 this.spaceType = ContentModel.TYPE_FOLDER.toString(); 429 this.icon = null; 430 this.copyPolicy = "contents"; 431 this.existingSpaceId = null; 432 this.templateSpaceId = null; 433 this.name = null; 434 this.description = ""; 435 this.templateName = null; 436 this.saveAsTemplate = false; 437 } 438 439 442 public void populate() 443 { 444 Node currentSpace = browseBean.getActionSpace(); 446 Map <String , Object > props = currentSpace.getProperties(); 447 448 this.name = (String )props.get("name"); 449 this.description = (String )props.get("description"); 450 this.icon = (String )props.get("app:icon"); 451 } 452 453 456 public String getSummary() 457 { 458 String summaryCreateType = null; 459 ResourceBundle bundle = Application.getBundle(FacesContext.getCurrentInstance()); 460 461 if (this.createFrom.equals("scratch")) 462 { 463 summaryCreateType = bundle.getString("scratch"); 464 } 465 else if (this.createFrom.equals("existing")) 466 { 467 summaryCreateType = bundle.getString("an_existing_space"); 468 } 469 else if (this.createFrom.equals("template")) 470 { 471 summaryCreateType = bundle.getString("a_template"); 472 } 473 474 478 String spaceTypeLabel = null; 479 for (UIListItem item : this.getFolderTypes()) 480 { 481 if (item.getValue().equals(this.spaceType)) 482 { 483 spaceTypeLabel = item.getLabel(); 484 break; 485 } 486 } 487 488 return buildSummary( 489 new String [] {bundle.getString("space_type"), bundle.getString("name"), 490 bundle.getString("description"), bundle.getString("creating_from")}, 491 new String [] {spaceTypeLabel, this.name, this.description, summaryCreateType}); 492 } 493 494 497 public List <SelectItem> getTemplateSpaces() 498 { 499 if (this.templates == null) 500 { 501 this.templates = new ArrayList <SelectItem>(); 502 503 FacesContext context = FacesContext.getCurrentInstance(); 504 String xpath = Application.getRootPath(context) + "/" + Application.getGlossaryFolderName(context) + 505 "/" + Application.getSpaceTemplatesFolderName(context) + "/*"; 506 NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 507 NamespaceService resolver = Repository.getServiceRegistry(context).getNamespaceService(); 508 List <NodeRef> results = this.searchService.selectNodes(rootNodeRef, xpath, null, resolver, false); 509 510 if (results.size() > 0) 511 { 512 for (NodeRef assocRef : results) 513 { 514 Node childNode = new Node(assocRef); 515 this.templates.add(new SelectItem(childNode.getId(), childNode.getName())); 516 } 517 518 QuickSort sorter = new QuickSort(this.templates, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 520 sorter.sort(); 521 } 522 523 this.templates.add(0, new SelectItem("none", Application.getMessage(FacesContext.getCurrentInstance(), "select_a_template"))); 525 } 526 527 return this.templates; 528 } 529 530 536 @SuppressWarnings ("unchecked") 537 public List <UIListItem> getFolderTypes() 538 { 539 if (this.folderTypes == null) 540 { 541 FacesContext context = FacesContext.getCurrentInstance(); 542 this.folderTypes = new ArrayList <UIListItem>(2); 543 this.folderTypeDescriptions = new ArrayList <UIDescription>(2); 544 545 UIListItem defaultItem = new UIListItem(); 547 String defaultLabel = Application.getMessage(context, "container"); 548 defaultItem.setValue(ContentModel.TYPE_FOLDER.toString()); 549 defaultItem.setLabel(defaultLabel); 550 defaultItem.setTooltip(defaultLabel); 551 defaultItem.getAttributes().put("image", DEFAULT_SPACE_TYPE_ICON); 552 this.folderTypes.add(defaultItem); 553 554 UIDescription defaultDesc = new UIDescription(); 555 defaultDesc.setControlValue(ContentModel.TYPE_FOLDER.toString()); 556 defaultDesc.setText(Application.getMessage(context, "container_desc")); 557 this.folderTypeDescriptions.add(defaultDesc); 558 559 Config wizardCfg = Application.getConfigService(FacesContext.getCurrentInstance()). 561 getConfig("Custom Folder Types"); 562 if (wizardCfg != null) 563 { 564 ConfigElement typesCfg = wizardCfg.getConfigElement("folder-types"); 565 if (typesCfg != null) 566 { 567 for (ConfigElement child : typesCfg.getChildren()) 568 { 569 QName idQName = Repository.resolveToQName(child.getAttribute("name")); 570 TypeDefinition typeDef = this.dictionaryService.getType(idQName); 571 572 if (typeDef != null && 573 this.dictionaryService.isSubClass(typeDef.getName(), ContentModel.TYPE_FOLDER)) 574 { 575 String label = null; 577 String msgId = child.getAttribute("displayLabelId"); 578 if (msgId != null) 579 { 580 label = Application.getMessage(context, msgId); 581 } 582 583 if (label == null) 585 { 586 label = child.getAttribute("displayLabel"); 587 } 588 589 if (label == null) 591 { 592 label = typeDef.getTitle(); 593 } 594 595 if (label == null) 597 { 598 label = idQName.getLocalName(); 599 } 600 601 String description = null; 603 msgId = child.getAttribute("descriptionMsgId"); 604 if (msgId != null) 605 { 606 description = Application.getMessage(context, msgId); 607 } 608 609 if (description == null) 610 { 611 description = child.getAttribute("description"); 612 } 613 614 if (description == null) 616 { 617 description = label; 618 } 619 620 String icon = child.getAttribute("icon"); 622 if (icon == null || icon.length() == 0) 623 { 624 icon = DEFAULT_SPACE_TYPE_ICON; 625 } 626 627 UIListItem item = new UIListItem(); 628 item.getAttributes().put("value", idQName.toString()); 629 item.getAttributes().put("label", label); 630 item.getAttributes().put("tooltip", label); 631 item.getAttributes().put("image", icon); 632 this.folderTypes.add(item); 633 634 UIDescription desc = new UIDescription(); 635 desc.setControlValue(idQName.toString()); 636 desc.setText(description); 637 this.folderTypeDescriptions.add(desc); 638 } 639 } 640 } 641 else 642 { 643 logger.warn("Could not find 'folder-types' configuration element"); 644 } 645 } 646 else 647 { 648 logger.warn("Could not find 'Custom Folder Types' configuration section"); 649 } 650 651 } 652 653 return this.folderTypes; 654 } 655 656 661 public List <UIDescription> getFolderTypeDescriptions() 662 { 663 if (this.folderTypeDescriptions == null) 664 { 665 getFolderTypes(); 667 } 668 669 return this.folderTypeDescriptions; 670 } 671 672 678 @SuppressWarnings ("unchecked") 679 public List <UIListItem> getIcons() 680 { 681 684 List <UIListItem> icons = null; 685 686 QName type = QName.createQName(this.spaceType); 687 String typePrefixForm = type.toPrefixString(this.namespaceService); 688 689 Config config = Application.getConfigService(FacesContext.getCurrentInstance()). 690 getConfig(typePrefixForm + ICONS_LOOKUP_KEY); 691 if (config != null) 692 { 693 ConfigElement iconsCfg = config.getConfigElement("icons"); 694 if (iconsCfg != null) 695 { 696 boolean first = true; 697 for (ConfigElement icon : iconsCfg.getChildren()) 698 { 699 String iconName = icon.getAttribute("name"); 700 String iconPath = icon.getAttribute("path"); 701 702 if (iconName != null && iconPath != null) 703 { 704 if (first) 705 { 706 709 icons = new ArrayList <UIListItem>(iconsCfg.getChildCount()); 710 if (this.icon == null) 711 { 712 this.icon = iconName; 714 } 715 first = false; 716 } 717 718 UIListItem item = new UIListItem(); 719 item.setValue(iconName); 720 item.getAttributes().put("image", iconPath); 721 icons.add(item); 722 } 723 } 724 } 725 } 726 727 if (icons == null) 729 { 730 icons = new ArrayList <UIListItem>(1); 731 this.icon = SPACE_ICON_DEFAULT; 732 733 UIListItem item = new UIListItem(); 734 item.setValue("space-icon-default"); 735 item.getAttributes().put("image", "/images/icons/space-icon-default.gif"); 736 icons.add(item); 737 } 738 739 return icons; 740 } 741 742 745 public SearchService getSearchService() 746 { 747 return searchService; 748 } 749 750 753 public void setSearchService(SearchService searchService) 754 { 755 this.searchService = searchService; 756 } 757 758 761 public void setNamespaceService(NamespaceService namespaceService) 762 { 763 this.namespaceService = namespaceService; 764 } 765 766 771 public void setDictionaryService(DictionaryService dictionaryService) 772 { 773 this.dictionaryService = dictionaryService; 774 } 775 776 779 public String getCopyPolicy() 780 { 781 return copyPolicy; 782 } 783 784 787 public void setCopyPolicy(String copyPolicy) 788 { 789 this.copyPolicy = copyPolicy; 790 } 791 792 795 public String getCreateFrom() 796 { 797 return createFrom; 798 } 799 800 803 public void setCreateFrom(String createFrom) 804 { 805 this.createFrom = createFrom; 806 } 807 808 811 public String getDescription() 812 { 813 return description; 814 } 815 816 819 public void setDescription(String description) 820 { 821 this.description = description; 822 } 823 824 827 public NodeRef getExistingSpaceId() 828 { 829 return existingSpaceId; 830 } 831 832 835 public void setExistingSpaceId(NodeRef existingSpaceId) 836 { 837 this.existingSpaceId = existingSpaceId; 838 } 839 840 843 public String getIcon() 844 { 845 return icon; 846 } 847 848 851 public void setIcon(String icon) 852 { 853 this.icon = icon; 854 } 855 856 859 public String getName() 860 { 861 return name; 862 } 863 864 867 public void setName(String name) 868 { 869 this.name = name; 870 } 871 872 875 public boolean isSaveAsTemplate() 876 { 877 return saveAsTemplate; 878 } 879 880 883 public void setSaveAsTemplate(boolean saveAsTemplate) 884 { 885 this.saveAsTemplate = saveAsTemplate; 886 } 887 888 891 public String getSpaceType() 892 { 893 return spaceType; 894 } 895 896 899 public void setSpaceType(String spaceType) 900 { 901 this.spaceType = spaceType; 902 } 903 904 907 public String getTemplateName() 908 { 909 return templateName; 910 } 911 912 915 public void setTemplateName(String templateName) 916 { 917 this.templateName = templateName; 918 } 919 920 923 public String getTemplateSpaceId() 924 { 925 return templateSpaceId; 926 } 927 928 931 public void setTemplateSpaceId(String templateSpaceId) 932 { 933 this.templateSpaceId = templateSpaceId; 934 } 935 936 939 protected String determineOutcomeForStep(int step) 940 { 941 String outcome = null; 942 943 switch(step) 944 { 945 case 1: 946 { 947 outcome = "create-from"; 948 break; 949 } 950 case 2: 951 { 952 if (createFrom.equalsIgnoreCase("scratch")) 953 { 954 outcome = "from-scratch"; 955 } 956 else if (createFrom.equalsIgnoreCase("existing")) 957 { 958 outcome = "from-existing"; 959 } 960 else if (createFrom.equalsIgnoreCase("template")) 961 { 962 outcome = "from-template"; 963 } 964 965 break; 966 } 967 case 3: 968 { 969 outcome = "details"; 970 break; 971 } 972 case 4: 973 { 974 outcome = "summary"; 975 break; 976 } 977 default: 978 { 979 outcome = CANCEL_OUTCOME; 980 } 981 } 982 983 return outcome; 984 } 985 986 991 protected void performCustomProcessing(FacesContext context) throws Exception 992 { 993 } 995 } 996 | Popular Tags |