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