| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.rice.kew.document; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.Collection; | 
  | 19 |  |  import java.util.HashSet; | 
  | 20 |  |  import java.util.List; | 
  | 21 |  |  import java.util.Map; | 
  | 22 |  |  import java.util.Set; | 
  | 23 |  |   | 
  | 24 |  |  import org.kuali.rice.kew.actionitem.ActionItem; | 
  | 25 |  |  import org.kuali.rice.kew.actionlist.service.ActionListService; | 
  | 26 |  |  import org.kuali.rice.kew.api.KEWPropertyConstants; | 
  | 27 |  |  import org.kuali.rice.kew.api.WorkflowRuntimeException; | 
  | 28 |  |  import org.kuali.rice.kew.doctype.bo.DocumentType; | 
  | 29 |  |  import org.kuali.rice.kew.doctype.service.DocumentTypeService; | 
  | 30 |  |  import org.kuali.rice.kew.service.KEWServiceLocator; | 
  | 31 |  |  import org.kuali.rice.kew.xml.DocumentTypeXmlParser; | 
  | 32 |  |  import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition; | 
  | 33 |  |  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition; | 
  | 34 |  |  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl; | 
  | 35 |  |  import org.kuali.rice.kns.web.ui.Field; | 
  | 36 |  |  import org.kuali.rice.kns.web.ui.Row; | 
  | 37 |  |  import org.kuali.rice.kns.web.ui.Section; | 
  | 38 |  |  import org.kuali.rice.krad.bo.DocumentHeader; | 
  | 39 |  |  import org.kuali.rice.kns.document.MaintenanceDocument; | 
  | 40 |  |  import org.kuali.rice.kns.maintenance.Maintainable; | 
  | 41 |  |  import org.kuali.rice.krad.util.ObjectUtils; | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 | 0 |  public class DocumentTypeMaintainable extends KualiMaintainableImpl { | 
  | 50 |  |       | 
  | 51 |  |      private static final long serialVersionUID = -5920808902137192662L; | 
  | 52 |  |   | 
  | 53 |  |       | 
  | 54 |  |   | 
  | 55 |  |   | 
  | 56 |  |   | 
  | 57 |  |      @Override | 
  | 58 |  |      public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) { | 
  | 59 | 0 |          List<Section> sections = super.getSections(document, oldMaintainable); | 
  | 60 |  |           | 
  | 61 | 0 |          if (!document.isNew()) { | 
  | 62 | 0 |              sectionLoop: for (Section section : sections) { | 
  | 63 | 0 |                  for (Row row : section.getRows()) { | 
  | 64 | 0 |                      for (Field field : row.getFields()) { | 
  | 65 | 0 |                          if (KEWPropertyConstants.NAME.equals(field.getPropertyName())) { | 
  | 66 | 0 |                              field.setReadOnly(true); | 
  | 67 | 0 |                              break sectionLoop; | 
  | 68 |  |                          } | 
  | 69 |  |                      } | 
  | 70 |  |                  } | 
  | 71 |  |              } | 
  | 72 |  |          } | 
  | 73 | 0 |          return sections; | 
  | 74 |  |      } | 
  | 75 |  |   | 
  | 76 |  |       | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 |  |   | 
  | 81 |  |      @Override | 
  | 82 |  |      public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) { | 
  | 83 | 0 |          super.processAfterCopy(document, parameters); | 
  | 84 | 0 |          DocumentType docType = ((DocumentType)getBusinessObject()); | 
  | 85 | 0 |          docType.setDocumentTypeId(null); | 
  | 86 | 0 |          docType.setName(""); | 
  | 87 | 0 |          docType.setPreviousVersionId(null); | 
  | 88 | 0 |      } | 
  | 89 |  |   | 
  | 90 |  |      @Override | 
  | 91 |  |      public void doRouteStatusChange(DocumentHeader documentHeader) { | 
  | 92 | 0 |          super.doRouteStatusChange(documentHeader); | 
  | 93 | 0 |      } | 
  | 94 |  |       | 
  | 95 |  |      private Set<String> constructUserInterfaceEditablePropertyNamesList() { | 
  | 96 | 0 |          Set<String> propertyNames = new HashSet<String>(); | 
  | 97 | 0 |          List<MaintainableSectionDefinition> sectionDefinitions = getMaintenanceDocumentDictionaryService().getMaintainableSections(getDocumentTypeName()); | 
  | 98 | 0 |          for (MaintainableSectionDefinition maintainableSectionDefinition : sectionDefinitions) { | 
  | 99 | 0 |              for (MaintainableItemDefinition maintainableItemDefinition : maintainableSectionDefinition.getMaintainableItems()) { | 
  | 100 | 0 |                  propertyNames.add(maintainableItemDefinition.getName()); | 
  | 101 |  |              } | 
  | 102 |  |          } | 
  | 103 | 0 |          return propertyNames; | 
  | 104 |  |      } | 
  | 105 |  |   | 
  | 106 |  |       | 
  | 107 |  |   | 
  | 108 |  |   | 
  | 109 |  |   | 
  | 110 |  |   | 
  | 111 |  |   | 
  | 112 |  |      @Override | 
  | 113 |  |      public void saveBusinessObject() { | 
  | 114 | 0 |          DocumentTypeService docTypeService = KEWServiceLocator.getDocumentTypeService(); | 
  | 115 | 0 |          DocumentType newDocumentType = (DocumentType) getBusinessObject(); | 
  | 116 | 0 |          String documentTypeName = newDocumentType.getName(); | 
  | 117 | 0 |          DocumentType docTypeFromDatabase = docTypeService.findByName(documentTypeName); | 
  | 118 | 0 |          if (ObjectUtils.isNull(docTypeFromDatabase)) { | 
  | 119 | 0 |              docTypeService.versionAndSave(newDocumentType); | 
  | 120 |  |          } | 
  | 121 |  |          else { | 
  | 122 | 0 |              Boolean applyRetroactively = newDocumentType.getApplyRetroactively(); | 
  | 123 |  |                  DocumentType newDocumentTypeFromDatabase; | 
  | 124 | 0 |              DocumentTypeXmlParser parser = new DocumentTypeXmlParser(); | 
  | 125 |  |              try { | 
  | 126 | 0 |                  newDocumentTypeFromDatabase = parser.generateNewDocumentTypeFromExisting(documentTypeName); | 
  | 127 | 0 |              } catch (Exception e) { | 
  | 128 | 0 |                  throw new WorkflowRuntimeException("Error while attempting to generate new document type from existing " + | 
  | 129 |  |                                  "database document type with name '" + documentTypeName + "'", e); | 
  | 130 | 0 |              } | 
  | 131 | 0 |              newDocumentTypeFromDatabase.populateDataDictionaryEditableFields(constructUserInterfaceEditablePropertyNamesList(), newDocumentType); | 
  | 132 | 0 |              docTypeService.versionAndSave(newDocumentTypeFromDatabase); | 
  | 133 | 0 |              if (ObjectUtils.isNotNull(applyRetroactively) && applyRetroactively.booleanValue()) { | 
  | 134 |  |                   | 
  | 135 |  |                   | 
  | 136 | 0 |                  List<DocumentType> previousDocTypeInstances = docTypeService.findPreviousInstances(documentTypeName); | 
  | 137 | 0 |                  for (DocumentType prevDocType : previousDocTypeInstances) { | 
  | 138 |  |                       | 
  | 139 | 0 |                      prevDocType.setLabel(newDocumentType.getLabel()); | 
  | 140 | 0 |                      prevDocType.setDescription(newDocumentType.getDescription()); | 
  | 141 | 0 |                      prevDocType.setUnresolvedHelpDefinitionUrl(newDocumentType.getUnresolvedHelpDefinitionUrl()); | 
  | 142 | 0 |                      prevDocType.setUnresolvedDocSearchHelpUrl(newDocumentType.getUnresolvedDocSearchHelpUrl()); | 
  | 143 | 0 |                      docTypeService.save(prevDocType); | 
  | 144 |  |                  } | 
  | 145 |  |                   | 
  | 146 |  |                   | 
  | 147 | 0 |                  ActionListService actionListService = KEWServiceLocator.getActionListService();  | 
  | 148 | 0 |                  Collection<ActionItem> items = actionListService.findByDocumentTypeName(documentTypeName); | 
  | 149 | 0 |                  for (ActionItem actionItem : items) { | 
  | 150 | 0 |                      actionItem.setDocLabel(newDocumentType.getLabel()); | 
  | 151 | 0 |                      actionListService.saveActionItem(actionItem); | 
  | 152 |  |                  } | 
  | 153 |  |                   | 
  | 154 |  |                   | 
  | 155 | 0 |                  Collection<ActionItem> outboxItems = actionListService.getOutboxItemsByDocumentType(documentTypeName); | 
  | 156 | 0 |                  for (ActionItem outboxItem : outboxItems) { | 
  | 157 | 0 |                      outboxItem.setDocLabel(newDocumentType.getLabel()); | 
  | 158 | 0 |                      actionListService.saveActionItem(outboxItem); | 
  | 159 |  |                  } | 
  | 160 |  |              } | 
  | 161 |  |          } | 
  | 162 | 0 |      } | 
  | 163 |  |   | 
  | 164 |  |  } |