Coverage Report - org.kuali.rice.kew.doctype.service.impl.DocumentTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeServiceImpl
0%
0/80
0%
0/34
2.389
 
 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.doctype.service.impl;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.jdom.Element;
 20  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 21  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 22  
 import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
 23  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 24  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 25  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 26  
 import org.kuali.rice.kew.xml.DocumentTypeXmlParser;
 27  
 import org.kuali.rice.kew.xml.export.DocumentTypeXmlExporter;
 28  
 import org.kuali.rice.krad.util.ObjectUtils;
 29  
 
 30  
 import java.io.InputStream;
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 
 36  
 
 37  
 /**
 38  
  * The standard implementation of the DocumentTypeService.
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class DocumentTypeServiceImpl implements DocumentTypeService {
 43  
 
 44  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeServiceImpl.class);
 45  
     protected static final String XML_FILE_PARSE_ERROR = "general.error.parsexml";
 46  
 
 47  
     private DocumentTypeDAO documentTypeDAO;
 48  
 
 49  
     public Collection<DocumentType> find(DocumentType documentType, String docTypeParentName, boolean climbHierarchy) {
 50  0
        DocumentType docTypeParent = this.findByName(docTypeParentName);
 51  0
        return getDocumentTypeDAO().find(documentType, docTypeParent, climbHierarchy);
 52  
     }
 53  
 
 54  
     public DocumentType findById(String documentTypeId) {
 55  0
             if (documentTypeId == null) {
 56  0
                     return null;
 57  
             }
 58  
 
 59  0
                    return getDocumentTypeDAO().findById(documentTypeId);
 60  
     }
 61  
 
 62  
     public DocumentType findByDocumentId(String documentId) {
 63  0
             if (documentId == null) {
 64  0
                     return null;
 65  
             }
 66  0
             String documentTypeId = getDocumentTypeDAO().findDocumentTypeIdByDocumentId(documentId);
 67  0
             return findById(documentTypeId);
 68  
     }
 69  
 
 70  
     public DocumentType findByName(String name) {
 71  0
             return this.findByName(name, true);
 72  
     }
 73  
 
 74  
     public DocumentType findByNameCaseInsensitive(String name) {
 75  0
             return this.findByName(name, false);
 76  
     }
 77  
 
 78  
     /**
 79  
      * 
 80  
      * This method seaches for a DocumentType by document name.
 81  
      * 
 82  
      * @param name
 83  
      * @param caseSensitive
 84  
      * @return
 85  
      */
 86  
     private DocumentType findByName(String name, boolean caseSensitive) {
 87  0
             if (name == null) {
 88  0
                     return null;
 89  
             }
 90  0
         return getDocumentTypeDAO().findByName(name, caseSensitive);
 91  
     }
 92  
 
 93  
     public void versionAndSave(DocumentType documentType) {
 94  
         // at this point this save is designed to version the document type by creating an entire new record if this is going to be an update and
 95  
         // not a create just throw and exception to be on the safe side
 96  0
         if (documentType.getDocumentTypeId() != null && documentType.getVersionNumber() != null) {
 97  0
             throw new RuntimeException("DocumentType configured for update and not versioning which we support");
 98  
         }
 99  
 
 100  
         // grab the old document. Don't Use Cached Version!
 101  0
         DocumentType oldDocumentType = findByName(documentType.getName());
 102  
         // reset the children on the oldDocumentType
 103  
         //oldDocumentType.resetChildren();
 104  0
         String existingDocTypeId = null;
 105  0
         if (oldDocumentType != null) {
 106  0
             existingDocTypeId = oldDocumentType.getDocumentTypeId();
 107  
             // set version number on the new doc type using the max version from the database
 108  0
             Integer maxVersionNumber = documentTypeDAO.getMaxVersionNumber(documentType.getName());
 109  0
             documentType.setVersion((maxVersionNumber != null) ? new Integer(maxVersionNumber.intValue() + 1) : new Integer(0));
 110  0
             oldDocumentType.setCurrentInd(Boolean.FALSE);
 111  0
             if ( LOG.isInfoEnabled() ) {
 112  0
                 LOG.info("Saving old document type Id " + oldDocumentType.getDocumentTypeId() + " name '" + oldDocumentType.getName() + "' (current = " + oldDocumentType.getCurrentInd() + ")");
 113  
             }
 114  0
             save(oldDocumentType);
 115  
         }
 116  
         // check to see that no current documents exist in database
 117  0
         if (!CollectionUtils.isEmpty(documentTypeDAO.findAllCurrentByName(documentType.getName()))) {
 118  0
             String errorMsg = "Found invalid 'current' document with name '" + documentType.getName() + "'.  None should exist.";
 119  0
             LOG.error(errorMsg);
 120  0
             throw new RuntimeException(errorMsg);
 121  
         }
 122  
         // set up the previous current doc type on the new doc type
 123  0
         documentType.setPreviousVersionId(existingDocTypeId);
 124  0
         documentType.setCurrentInd(Boolean.TRUE);
 125  0
         save(documentType);
 126  0
         if ( LOG.isInfoEnabled() ) {
 127  0
             LOG.info("Saved current document type Id " + documentType.getDocumentTypeId() + " name '" + documentType.getName() + "' (current = " + documentType.getCurrentInd() + ")");
 128  
         }
 129  
         //attach the children to this new parent.  cloning the children would probably be a better way to go here...
 130  0
         if (ObjectUtils.isNotNull(existingDocTypeId)) {
 131  
             // documentType.getPreviousVersion() should not be null at this point
 132  0
             for (Iterator iterator = getChildDocumentTypes(existingDocTypeId).iterator(); iterator.hasNext();) {
 133  
 //                            for (Iterator iterator = oldDocumentType.getChildrenDocTypes().iterator(); iterator.hasNext();) {
 134  0
                 DocumentType child = (DocumentType) iterator.next();
 135  0
                 child.setDocTypeParentId(documentType.getDocumentTypeId());
 136  0
                 save(child);
 137  0
                 if ( LOG.isInfoEnabled() ) {
 138  0
                     LOG.info("Saved child document type Id " + child.getDocumentTypeId() + " name '" + child.getName() + "' (parent = " + child.getDocTypeParentId() + ", current = " + child.getCurrentInd() + ")");
 139  
                 }
 140  0
             }
 141  
         }
 142  
         // initiate a save of this document type's parent document type, this will force a
 143  
         // version check which should reveal (via an optimistic lock exception) whether or
 144  
         // not there is a concurrent transaction
 145  
         // which has modified the parent (and therefore made it non-current)
 146  
         // be sure to get the parent doc type directly from the db and not from the cache
 147  0
         if (documentType.getDocTypeParentId() != null) {
 148  0
             DocumentType parent = getDocumentTypeDAO().findById(documentType.getDocTypeParentId());
 149  0
             save(parent);
 150  0
             if ( LOG.isInfoEnabled() ) {
 151  0
                 LOG.info("Saved parent document type Id " + parent.getDocumentTypeId() + " name '" + parent.getName() + "' (current = " + parent.getCurrentInd() + ")");
 152  
             }
 153  
         }
 154  0
     }
 155  
 
 156  
     public void save(DocumentType documentType) {
 157  0
             getDocumentTypeDAO().save(documentType);
 158  0
     }
 159  
 
 160  
     public DocumentTypeDAO getDocumentTypeDAO() {
 161  0
         return documentTypeDAO;
 162  
     }
 163  
 
 164  
     public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) {
 165  0
         this.documentTypeDAO = documentTypeDAO;
 166  0
     }
 167  
 
 168  
     public synchronized List findAllCurrentRootDocuments() {
 169  0
                    return getDocumentTypeDAO().findAllCurrentRootDocuments();
 170  
     }
 171  
 
 172  
     public List findAllCurrent() {
 173  0
         return getDocumentTypeDAO().findAllCurrent();
 174  
     }
 175  
 
 176  
     public List<DocumentType> findPreviousInstances(String documentTypeName) {
 177  0
         return getDocumentTypeDAO().findPreviousInstances(documentTypeName);
 178  
     }
 179  
 
 180  
     public DocumentType findRootDocumentType(DocumentType docType) {
 181  0
         if (docType.getParentDocType() != null) {
 182  0
             return findRootDocumentType(docType.getParentDocType());
 183  
         } else {
 184  0
             return docType;
 185  
         }
 186  
     }
 187  
 
 188  
     public void loadXml(InputStream inputStream, String principalId) {
 189  0
         DocumentTypeXmlParser parser = new DocumentTypeXmlParser();
 190  
         try {
 191  0
             parser.parseDocumentTypes(inputStream);
 192  0
         } catch (Exception e) {
 193  0
             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error parsing documentType XML file", new WorkflowServiceErrorImpl("Error parsing documentType XML file", XML_FILE_PARSE_ERROR));
 194  0
             wsee.initCause(e);
 195  0
             throw wsee;
 196  0
         }
 197  0
     }
 198  
 
 199  
     public Element export(ExportDataSet dataSet) {
 200  0
         DocumentTypeXmlExporter exporter = new DocumentTypeXmlExporter();
 201  0
         return exporter.export(dataSet);
 202  
     }
 203  
     
 204  
     @Override
 205  
         public boolean supportPrettyPrint() {
 206  0
                 return true;
 207  
         }
 208  
 
 209  
     public List getChildDocumentTypes(String documentTypeId) {
 210  0
             List childDocumentTypes = new ArrayList();
 211  0
             List childIds = getDocumentTypeDAO().getChildDocumentTypeIds(documentTypeId);
 212  0
             for (Iterator iter = childIds.iterator(); iter.hasNext();) {
 213  0
                         String childDocumentTypeId = (String) iter.next();
 214  0
                         childDocumentTypes.add(findById(childDocumentTypeId));
 215  0
                 }
 216  0
             return childDocumentTypes;
 217  
     }
 218  
 
 219  
 }