Coverage Report - org.kuali.rice.kew.document.DocumentTypeMaintainable
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeMaintainable
0%
0/56
0%
0/26
4
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * This class is the maintainable implementation for the Workflow {@link DocumentType} 
 45  
  * 
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  *
 48  
  */
 49  0
 public class DocumentTypeMaintainable extends KualiMaintainableImpl {
 50  
     
 51  
     private static final long serialVersionUID = -5920808902137192662L;
 52  
 
 53  
     /**
 54  
      * Override the getSections method on this maintainable so that the document type name field
 55  
      * can be set to read-only for 
 56  
      */
 57  
     @Override
 58  
     public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
 59  0
         List<Section> sections = super.getSections(document, oldMaintainable);
 60  
         // if the document isn't new then we need to make the document type name field read-only
 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  
      * This overridden method resets the name
 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  
      * This is a complete override which does not call into
 108  
      * {@link KualiMaintainableImpl}. This method calls
 109  
      * {@link DocumentTypeService#versionAndSave(DocumentType)}.
 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  
                 // save all previous instances of document type with the same name
 135  
                 // fields: label, description, unresolvedHelpDefinitionUrl
 136  0
                 List<DocumentType> previousDocTypeInstances = docTypeService.findPreviousInstances(documentTypeName);
 137  0
                 for (DocumentType prevDocType : previousDocTypeInstances) {
 138  
                     // set up fields
 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  
                 // save all former/current action items matching document type name
 146  
                 // fields: docLabel
 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  
                 // save all former/current outbox action items matching document type name
 154  
                 // fields: docLabel
 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  
 }