Coverage Report - org.kuali.rice.kew.document.DocumentTypeMaintainable
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeMaintainable
0%
0/57
0%
0/26
4
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.doctype.bo.DocumentType;
 27  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 28  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 29  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 30  
 import org.kuali.rice.kew.util.KEWPropertyConstants;
 31  
 import org.kuali.rice.kew.xml.DocumentTypeXmlParser;
 32  
 import org.kuali.rice.kns.bo.DocumentHeader;
 33  
 import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
 34  
 import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
 35  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 36  
 import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
 37  
 import org.kuali.rice.kns.maintenance.Maintainable;
 38  
 import org.kuali.rice.kns.util.ObjectUtils;
 39  
 import org.kuali.rice.kns.web.ui.Field;
 40  
 import org.kuali.rice.kns.web.ui.Row;
 41  
 import org.kuali.rice.kns.web.ui.Section;
 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  
      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#processAfterCopy(org.kuali.rice.kns.document.MaintenanceDocument, java.util.Map)
 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  
     /**
 91  
      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#doRouteStatusChange(org.kuali.rice.kns.bo.DocumentHeader)
 92  
      */
 93  
     @Override
 94  
     public void doRouteStatusChange(DocumentHeader documentHeader) {
 95  0
         super.doRouteStatusChange(documentHeader);
 96  0
     }
 97  
     
 98  
     private Set<String> constructUserInterfaceEditablePropertyNamesList() {
 99  0
         Set<String> propertyNames = new HashSet<String>();
 100  0
         List<MaintainableSectionDefinition> sectionDefinitions = getMaintenanceDocumentDictionaryService().getMaintainableSections(docTypeName);
 101  0
         for (MaintainableSectionDefinition maintainableSectionDefinition : sectionDefinitions) {
 102  0
             for (MaintainableItemDefinition maintainableItemDefinition : maintainableSectionDefinition.getMaintainableItems()) {
 103  0
                 propertyNames.add(maintainableItemDefinition.getName());
 104  
             }
 105  
         }
 106  0
         return propertyNames;
 107  
     }
 108  
 
 109  
     /**
 110  
      * This is a complete override which does not call into
 111  
      * {@link KualiMaintainableImpl}. This method calls
 112  
      * {@link DocumentTypeService#versionAndSave(DocumentType)}.
 113  
      * 
 114  
      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
 115  
      */
 116  
     @Override
 117  
     public void saveBusinessObject() {
 118  0
         DocumentTypeService docTypeService = KEWServiceLocator.getDocumentTypeService();
 119  0
         DocumentType newDocumentType = (DocumentType) getBusinessObject();
 120  0
         String documentTypeName = newDocumentType.getName();
 121  0
         DocumentType docTypeFromDatabase = docTypeService.findByName(documentTypeName);
 122  0
         if (ObjectUtils.isNull(docTypeFromDatabase)) {
 123  0
             docTypeService.versionAndSave(newDocumentType);
 124  
         }
 125  
         else {
 126  0
             Boolean applyRetroactively = newDocumentType.getApplyRetroactively();
 127  
                 DocumentType newDocumentTypeFromDatabase;
 128  0
             DocumentTypeXmlParser parser = new DocumentTypeXmlParser();
 129  
             try {
 130  0
                 newDocumentTypeFromDatabase = parser.generateNewDocumentTypeFromExisting(documentTypeName);
 131  0
             } catch (Exception e) {
 132  0
                 throw new WorkflowRuntimeException("Error while attempting to generate new document type from existing " +
 133  
                                 "database document type with name '" + documentTypeName + "'", e);
 134  0
             }
 135  0
             newDocumentTypeFromDatabase.populateDataDictionaryEditableFields(constructUserInterfaceEditablePropertyNamesList(), newDocumentType);
 136  0
             docTypeService.versionAndSave(newDocumentTypeFromDatabase);
 137  0
             if (ObjectUtils.isNotNull(applyRetroactively) && applyRetroactively.booleanValue()) {
 138  
                 // save all previous instances of document type with the same name
 139  
                 // fields: label, description, unresolvedHelpDefinitionUrl
 140  0
                 List<DocumentType> previousDocTypeInstances = docTypeService.findPreviousInstances(documentTypeName);
 141  0
                 for (DocumentType prevDocType : previousDocTypeInstances) {
 142  
                     // set up fields
 143  0
                     prevDocType.setLabel(newDocumentType.getLabel());
 144  0
                     prevDocType.setDescription(newDocumentType.getDescription());
 145  0
                     prevDocType.setUnresolvedHelpDefinitionUrl(newDocumentType.getUnresolvedHelpDefinitionUrl());
 146  0
                     prevDocType.setUnresolvedDocSearchHelpUrl(newDocumentType.getUnresolvedDocSearchHelpUrl());
 147  0
                     docTypeService.save(prevDocType, false);
 148  
                 }
 149  
                 // TODO: delyea - do we need to call this multiple times as in "DocumentTypeServiceImpl.save(DocumentType, boolean)"
 150  0
                 docTypeService.flushCache();
 151  
                 // save all former/current action items matching document type name
 152  
                 // fields: docLabel
 153  0
                 ActionListService actionListService = KEWServiceLocator.getActionListService(); 
 154  0
                 Collection<ActionItem> items = actionListService.findByDocumentTypeName(documentTypeName);
 155  0
                 for (ActionItem actionItem : items) {
 156  0
                     actionItem.setDocLabel(newDocumentType.getLabel());
 157  0
                     actionListService.saveActionItem(actionItem);
 158  
                 }
 159  
                 // save all former/current outbox action items matching document type name
 160  
                 // fields: docLabel
 161  0
                 Collection<ActionItem> outboxItems = actionListService.getOutboxItemsByDocumentType(documentTypeName);
 162  0
                 for (ActionItem outboxItem : outboxItems) {
 163  0
                     outboxItem.setDocLabel(newDocumentType.getLabel());
 164  0
                     actionListService.saveActionItem(outboxItem);
 165  
                 }
 166  
             }
 167  
         }
 168  0
     }
 169  
 
 170  
 }