1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
38  
39  
40  
41  
42  
43  @Deprecated
44  public class DocumentHelperServiceImpl implements DocumentHelperService {
45      
46      private DataDictionaryService dataDictionaryService;
47  
48      
49  
50  
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  
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 
110 
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 
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 
169 
170     public DataDictionaryService getDataDictionaryService() {
171         if (dataDictionaryService == null) {
172             this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
173         }
174         return this.dataDictionaryService;
175     }
176 
177     
178 
179 
180     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
181         this.dataDictionaryService = dataDictionaryService;
182     }
183 
184 }