View Javadoc

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