Coverage Report - org.kuali.rice.kew.api.WorkflowDocumentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowDocumentFactory
0%
0/73
0%
0/18
8.333
 
 1  
 /*
 2  
  * Copyright 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/ecl1.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.api;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.io.InputStreamReader;
 22  
 import java.lang.reflect.InvocationTargetException;
 23  
 import java.lang.reflect.Method;
 24  
 
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.kuali.rice.core.api.config.ConfigurationException;
 27  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 28  
 import org.kuali.rice.kew.api.action.InvalidActionTakenException;
 29  
 import org.kuali.rice.kew.api.doctype.IllegalDocumentTypeException;
 30  
 import org.kuali.rice.kew.api.document.DocumentContentUpdate;
 31  
 import org.kuali.rice.kew.api.document.DocumentUpdate;
 32  
 
 33  
 /**
 34  
  * TODO ..
 35  
  *
 36  
  */
 37  0
 public final class WorkflowDocumentFactory {
 38  
 
 39  
         private static final String CREATE_METHOD_NAME = "createDocument";
 40  
         private static final String LOAD_METHOD_NAME = "loadDocument"; 
 41  
         
 42  
     /**
 43  
      * TODO 
 44  
      * 
 45  
      * @param principalId TODO
 46  
      * @param documentTypeName TODO
 47  
      * 
 48  
      * @return TODO
 49  
      * 
 50  
      * @throws IllegalArgumentException if principalId is null or blank
 51  
      * @throws IllegalArgumentException if documentTypeName is null or blank
 52  
      * @throws IllegalDocumentTypeException if the document type does not allow for creation of a document,
 53  
      * this can occur when the given document type is used only as a parent and has no route path configured
 54  
      * @throws InvalidActionTakenException if the caller is not allowed to execute this action
 55  
      */
 56  
     public static WorkflowDocument createDocument(String principalId, String documentTypeName) {
 57  0
             return createDocument(principalId, documentTypeName, null, null);
 58  
     }
 59  
     
 60  
     /**
 61  
      * TODO
 62  
      * 
 63  
      * @param principalId TODO
 64  
      * @param documentTypeName TODO
 65  
      * @param title TODO
 66  
      * 
 67  
      * @return TODO
 68  
      * 
 69  
      * @throws IllegalArgumentException if principalId is null or blank
 70  
      * @throws IllegalArgumentException if documentTypeName is null or blank
 71  
      * @throws DocumentTypeNotFoundException if documentTypeName does not represent a valid document type
 72  
      */
 73  
     public static WorkflowDocument createDocument(String principalId, String documentTypeName, String title) {
 74  0
             DocumentUpdate.Builder builder = DocumentUpdate.Builder.create();
 75  0
             builder.setTitle(title);
 76  0
             return createDocument(principalId, documentTypeName, builder.build(), null);
 77  
     }
 78  
     
 79  
     /**
 80  
      * TODO
 81  
      * 
 82  
      * @param principalId TODO
 83  
      * @param documentTypeName TODO
 84  
      * @param documentUpdate TODO
 85  
      * @param documentContentUpdate TODO
 86  
      * 
 87  
      * @return TODO
 88  
      * 
 89  
      * @throws IllegalArgumentException if principalId is null or blank
 90  
      * @throws IllegalArgumentException if documentTypeName is null or blank
 91  
      * @throws DocumentTypeNotFoundException if documentTypeName does not represent a valid document type
 92  
      */
 93  
         public static WorkflowDocument createDocument(String principalId, String documentTypeName, DocumentUpdate documentUpdate, DocumentContentUpdate documentContentUpdate) {
 94  0
                 if (StringUtils.isBlank(principalId)) {
 95  0
                         throw new IllegalArgumentException("principalId was null or blank");
 96  
                 }
 97  0
                 if (StringUtils.isBlank(documentTypeName)) {
 98  0
                         throw new IllegalArgumentException("documentTypeName was null or blank");
 99  
                 }
 100  0
                 Object provider = loadProvider();
 101  0
                 Method createMethod = null;
 102  
                 try {
 103  0
                         createMethod = provider.getClass().getMethod(CREATE_METHOD_NAME, String.class, String.class, DocumentUpdate.class, DocumentContentUpdate.class);
 104  0
                 } catch (NoSuchMethodException e) {
 105  0
                         throw new ConfigurationException("Failed to locate valid createDocument method signature on provider class: " + provider.getClass().getName(), e);
 106  0
                 } catch (SecurityException e) {
 107  0
                         throw new ConfigurationException("Encountered security issue when attempting to access createDocument method on provider class: " + provider.getClass().getName(), e);
 108  0
                 }
 109  
                 
 110  0
                 Object workflowDocument = null;
 111  
                 
 112  
                 try {
 113  0
                         workflowDocument = createMethod.invoke(provider, principalId, documentTypeName, documentUpdate, documentContentUpdate);
 114  0
                 } catch (IllegalAccessException e) {
 115  0
                         throw new ConfigurationException("Failed to invoke " + CREATE_METHOD_NAME, e);
 116  0
                 } catch (InvocationTargetException e) {
 117  0
                         if (e.getCause() instanceof RuntimeException) {
 118  0
                                 throw (RuntimeException)e.getCause();
 119  
                         }
 120  0
                         throw new ConfigurationException("Failed to invoke " + CREATE_METHOD_NAME, e);
 121  0
                 }
 122  
 
 123  0
                 if (!(workflowDocument instanceof WorkflowDocument)) {
 124  0
                         throw new ConfigurationException("Created document is not a proper instance of " + WorkflowDocument.class + ", was instead " + workflowDocument.getClass());
 125  
                 }
 126  0
                 return (WorkflowDocument)workflowDocument;
 127  
         }
 128  
         
 129  
         public static WorkflowDocument loadDocument(String principalId, String documentId) {
 130  0
                 if (StringUtils.isBlank(principalId)) {
 131  0
                         throw new IllegalArgumentException("principalId was null or blank");
 132  
                 }
 133  0
                 if (StringUtils.isBlank(documentId)) {
 134  0
                         throw new IllegalArgumentException("documentId was null or blank");
 135  
                 }
 136  
                 
 137  0
                 Object provider = loadProvider();
 138  0
                 Method loadMethod = null;
 139  
                 try {
 140  0
                         loadMethod = provider.getClass().getMethod(LOAD_METHOD_NAME, String.class, String.class);
 141  0
                 } catch (NoSuchMethodException e) {
 142  0
                         throw new ConfigurationException("Failed to locate valid createDocument method signature on provider class: " + provider.getClass().getName(), e);
 143  0
                 } catch (SecurityException e) {
 144  0
                         throw new ConfigurationException("Encountered security issue when attempting to access createDocument method on provider class: " + provider.getClass().getName(), e);
 145  0
                 }
 146  
                 
 147  0
                 Object workflowDocument = null;
 148  
                 
 149  
                 try {
 150  0
                         workflowDocument = loadMethod.invoke(provider, principalId, documentId);
 151  0
                 } catch (IllegalAccessException e) {
 152  0
                         throw new ConfigurationException("Failed to invoke " + LOAD_METHOD_NAME, e);
 153  0
                 } catch (InvocationTargetException e) {
 154  0
                         if (e.getCause() instanceof RuntimeException) {
 155  0
                                 throw (RuntimeException)e.getCause();
 156  
                         }
 157  0
                         throw new ConfigurationException("Failed to invoke " + LOAD_METHOD_NAME, e);
 158  0
                 }
 159  
 
 160  0
                 if (!(workflowDocument instanceof WorkflowDocument)) {
 161  0
                         throw new ConfigurationException("Loaded document is not a proper instance of " + WorkflowDocument.class + ", was instead " + workflowDocument.getClass());
 162  
                 }
 163  0
                 return (WorkflowDocument)workflowDocument;
 164  
         }
 165  
         
 166  
         private static Object loadProvider() {
 167  0
                 String providerClassName = null;
 168  0
                 String resource = null;
 169  
                 try {
 170  0
             resource = new StringBuilder().append("META-INF/services/").append(WorkflowDocument.class.getName()).toString();
 171  0
             final InputStream resourceStream = ClassLoaderUtils.getDefaultClassLoader().getResourceAsStream(resource.toString());
 172  0
             if (resourceStream != null) {
 173  0
                 BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream, "UTF-8"));
 174  0
                 providerClassName = reader.readLine().trim();
 175  0
                 reader.close();
 176  0
                 Class<?> providerClass = Class.forName(providerClassName);
 177  0
                 return newInstance(providerClass);
 178  
             } else {
 179  0
                 throw new ConfigurationException("Failed to locate a services definition file at " + resource);
 180  
             }
 181  0
         } catch (IOException e) {
 182  0
             throw new ConfigurationException("Failure processing services definition file at " + resource, e);
 183  0
         } catch (ClassNotFoundException e) {
 184  0
                 throw new ConfigurationException("Failed to load provider class: " + providerClassName, e);
 185  
         }
 186  
         }
 187  
         
 188  
         private static Object newInstance(Class<?> providerClass) {
 189  
                 try {
 190  0
                         return providerClass.newInstance();
 191  0
                 } catch (InstantiationException e) {
 192  0
                         throw new ConfigurationException("Failed to instantiate provider class: " + providerClass.getName(), e);
 193  0
                 } catch (IllegalAccessException e) {
 194  0
                         throw new ConfigurationException("Failed to instantiate provider class: " + providerClass.getName(), e);
 195  
                 }
 196  
         }
 197  
 }