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 public class DocumentHelperServiceImpl implements DocumentHelperService {
43
44 private DataDictionaryService dataDictionaryService;
45
46
47
48
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
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
108
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
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
167
168 public DataDictionaryService getDataDictionaryService() {
169 if (dataDictionaryService == null) {
170 this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
171 }
172 return this.dataDictionaryService;
173 }
174
175
176
177
178 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
179 this.dataDictionaryService = dataDictionaryService;
180 }
181
182 }