Coverage Report - org.kuali.rice.krad.service.impl.DocumentHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentHelperServiceImpl
0%
0/64
0%
0/34
6.167
 
 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.krad.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.krad.datadictionary.DataDictionary;
 20  
 import org.kuali.rice.krad.datadictionary.DocumentEntry;
 21  
 import org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry;
 22  
 import org.kuali.rice.krad.datadictionary.TransactionalDocumentEntry;
 23  
 import org.kuali.rice.krad.document.Document;
 24  
 import org.kuali.rice.krad.document.authorization.DocumentAuthorizer;
 25  
 import org.kuali.rice.krad.document.authorization.DocumentAuthorizerBase;
 26  
 import org.kuali.rice.krad.document.authorization.DocumentPresentationController;
 27  
 import org.kuali.rice.krad.document.authorization.DocumentPresentationControllerBase;
 28  
 import org.kuali.rice.krad.document.authorization.MaintenanceDocumentAuthorizerBase;
 29  
 import org.kuali.rice.krad.document.authorization.MaintenanceDocumentPresentationControllerBase;
 30  
 import org.kuali.rice.krad.document.authorization.TransactionalDocumentAuthorizerBase;
 31  
 import org.kuali.rice.krad.document.authorization.TransactionalDocumentPresentationControllerBase;
 32  
 import org.kuali.rice.krad.service.DataDictionaryService;
 33  
 import org.kuali.rice.krad.service.DocumentHelperService;
 34  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 35  
 
 36  
 /**
 37  
  * This class is a utility service intended to help retrieve objects related to particular documents.
 38  
  * 
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  *
 41  
  */
 42  0
 public class DocumentHelperServiceImpl implements DocumentHelperService {
 43  
     
 44  
     private DataDictionaryService dataDictionaryService;
 45  
 
 46  
     /**
 47  
      * @see org.kuali.rice.krad.service.DocumentHelperService#getDocumentAuthorizer(java.lang.String)
 48  
      * // TODO: in krad documents could have multiple views and multiple authorizer classes
 49  
      */
 50  
     public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
 51  0
         DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
 52  
 
 53  0
         if (StringUtils.isBlank(documentType)) {
 54  0
             throw new IllegalArgumentException("invalid (blank) documentType");
 55  
         }
 56  
 
 57  0
         DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
 58  0
         if (documentEntry == null) {
 59  0
             throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
 60  
         }
 61  
 
 62  0
         Class<? extends DocumentAuthorizer> documentAuthorizerClass = documentEntry.getDocumentAuthorizerClass();
 63  
 
 64  0
         DocumentAuthorizer documentAuthorizer = null;
 65  
         try {
 66  0
             if (documentAuthorizerClass != null) {
 67  0
                 documentAuthorizer = documentAuthorizerClass.newInstance();
 68  
             }
 69  0
             else if (documentEntry instanceof MaintenanceDocumentEntry) {
 70  0
                 documentAuthorizer = new MaintenanceDocumentAuthorizerBase();
 71  
             }
 72  0
             else if (documentEntry instanceof TransactionalDocumentEntry) {
 73  0
                 documentAuthorizer = new TransactionalDocumentAuthorizerBase();
 74  
             }
 75  
             else {
 76  0
                 documentAuthorizer = new DocumentAuthorizerBase();
 77  
             }
 78  
         }
 79  0
         catch (Exception e) {
 80  0
             throw new RuntimeException("unable to instantiate documentAuthorizer '" + documentAuthorizerClass.getName() + "' for doctype '" + documentType + "'", e);
 81  0
         }
 82  
 
 83  0
         return documentAuthorizer;
 84  
     }
 85  
 
 86  
     /**
 87  
      * @see org.kuali.rice.krad.service.DocumentHelperService#getDocumentAuthorizer(org.kuali.rice.krad.document.Document)
 88  
      */
 89  
     public DocumentAuthorizer getDocumentAuthorizer(Document document) {
 90  0
         if (document == null) {
 91  0
             throw new IllegalArgumentException("invalid (null) document");
 92  0
         } else if (document.getDocumentHeader() == null) {
 93  0
             throw new IllegalArgumentException(
 94  
                     "invalid (null) document.documentHeader");
 95  0
         } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
 96  0
             throw new IllegalArgumentException(
 97  
                     "invalid (null) document.documentHeader.workflowDocument");
 98  
         }
 99  
 
 100  0
         String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
 101  
 
 102  0
         DocumentAuthorizer documentAuthorizer = getDocumentAuthorizer(documentType);
 103  0
         return documentAuthorizer;
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see org.kuali.rice.krad.service.DocumentHelperService#getDocumentPresentationController(java.lang.String)
 108  
      * // TODO: in krad documents could have multiple views and multiple presentation controller
 109  
      */
 110  
     public DocumentPresentationController getDocumentPresentationController(String documentType) {
 111  0
         DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
 112  0
         DocumentPresentationController documentPresentationController = null;
 113  
         
 114  0
         if (StringUtils.isBlank(documentType)) {
 115  0
             throw new IllegalArgumentException("invalid (blank) documentType");
 116  
         }
 117  
 
 118  0
         DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
 119  0
         if (documentEntry == null) {
 120  0
             throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
 121  
         }
 122  0
         Class<? extends DocumentPresentationController> documentPresentationControllerClass = null;
 123  
         try{
 124  0
             documentPresentationControllerClass = documentEntry.getDocumentPresentationControllerClass();
 125  0
             if(documentPresentationControllerClass != null){
 126  0
                 documentPresentationController = documentPresentationControllerClass.newInstance();
 127  
             } else {
 128  0
                 DocumentEntry doc = dataDictionary.getDocumentEntry(documentType);
 129  0
                 if ( doc instanceof TransactionalDocumentEntry ) {
 130  0
                     documentPresentationController = new TransactionalDocumentPresentationControllerBase();
 131  0
                 } else if(doc instanceof MaintenanceDocumentEntry) {
 132  0
                     documentPresentationController = new MaintenanceDocumentPresentationControllerBase();
 133  
                 } else {
 134  0
                     documentPresentationController = new DocumentPresentationControllerBase();
 135  
                 }
 136  
             }
 137  
         }
 138  0
         catch (Exception e) {
 139  0
             throw new RuntimeException("unable to instantiate documentPresentationController '" + documentPresentationControllerClass.getName() + "' for doctype '" + documentType + "'", e);
 140  0
         }
 141  
 
 142  0
         return documentPresentationController;
 143  
     }
 144  
 
 145  
     /**
 146  
      * @see org.kuali.rice.krad.service.DocumentHelperService#getDocumentPresentationController(org.kuali.rice.krad.document.Document)
 147  
      */
 148  
     public DocumentPresentationController getDocumentPresentationController(Document document) {
 149  0
         if (document == null) {
 150  0
             throw new IllegalArgumentException("invalid (null) document");
 151  
         }
 152  0
         else if (document.getDocumentHeader() == null) {
 153  0
             throw new IllegalArgumentException("invalid (null) document.documentHeader");
 154  
         }
 155  0
         else if (!document.getDocumentHeader().hasWorkflowDocument()) {
 156  0
             throw new IllegalArgumentException("invalid (null) document.documentHeader.workflowDocument");
 157  
         }
 158  
 
 159  0
         String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
 160  
 
 161  0
         DocumentPresentationController documentPresentationController = getDocumentPresentationController(documentType);
 162  0
         return documentPresentationController;
 163  
     }
 164  
 
 165  
     /**
 166  
      * @return the dataDictionaryService
 167  
      */
 168  
     public DataDictionaryService getDataDictionaryService() {
 169  0
         if (dataDictionaryService == null) {
 170  0
             this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
 171  
         }
 172  0
         return this.dataDictionaryService;
 173  
     }
 174  
 
 175  
     /**
 176  
      * @param dataDictionaryService the dataDictionaryService to set
 177  
      */
 178  
     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
 179  0
         this.dataDictionaryService = dataDictionaryService;
 180  0
     }
 181  
 
 182  
 }