View Javadoc
1   /**
2    * Copyright 2005-2016 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 java.util.Collection;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.kew.api.KewApiServiceLocator;
23  import org.kuali.rice.kew.api.doctype.DocumentType;
24  import org.kuali.rice.krad.datadictionary.DataDictionary;
25  import org.kuali.rice.krad.datadictionary.DocumentEntry;
26  import org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry;
27  import org.kuali.rice.krad.document.Document;
28  import org.kuali.rice.krad.document.DocumentAuthorizer;
29  import org.kuali.rice.krad.document.DocumentAuthorizerBase;
30  import org.kuali.rice.krad.document.DocumentPresentationController;
31  import org.kuali.rice.krad.document.DocumentPresentationControllerBase;
32  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
33  import org.kuali.rice.krad.maintenance.Maintainable;
34  import org.kuali.rice.krad.maintenance.MaintenanceDocumentAuthorizerBase;
35  import org.kuali.rice.krad.maintenance.MaintenanceDocumentPresentationControllerBase;
36  import org.kuali.rice.krad.rules.rule.BusinessRule;
37  import org.kuali.rice.krad.service.DataDictionaryService;
38  import org.kuali.rice.krad.service.DocumentDictionaryService;
39  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40  
41  /**
42   * Implementation of <code>DocumentDictionaryService</code> which reads configuration
43   * from the data dictionary
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class DocumentDictionaryServiceImpl implements DocumentDictionaryService {
48      private DataDictionaryService dataDictionaryService;
49  
50      /**
51       * @see org.kuali.rice.krad.service.DocumentDictionaryService#getLabel
52       */
53      @Override
54      public String getLabel(String documentTypeName) {
55          String label = null;
56  
57          DocumentType docType = getDocumentType(documentTypeName);
58          if (docType != null) {
59              label = docType.getLabel();
60          }
61  
62          return label;
63      }
64  
65      /**
66       * @see org.kuali.rice.krad.service.DocumentDictionaryService#getMaintenanceDocumentTypeName
67       */
68      @Override
69      public String getMaintenanceDocumentTypeName(Class dataObjectClass) {
70          String documentTypeName = null;
71  
72          MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(dataObjectClass);
73          if (entry != null) {
74              documentTypeName = entry.getDocumentTypeName();
75          }
76  
77          return documentTypeName;
78      }
79  
80      /**
81       * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDescription
82       */
83      @Override
84      public String getDescription(String documentTypeName) {
85          String description = null;
86  
87          DocumentType docType = getDocumentType(documentTypeName);
88          if (docType != null) {
89              description = docType.getDescription();
90          }
91  
92          return description;
93      }
94  
95      /**
96       * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDefaultExistenceChecks
97       */
98      @Override
99      public Collection getDefaultExistenceChecks(Class dataObjectClass) {
100         return getDefaultExistenceChecks(getMaintenanceDocumentTypeName(dataObjectClass));
101     }
102 
103     /**
104      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDefaultExistenceChecks
105      */
106     @Override
107     public Collection getDefaultExistenceChecks(Document document) {
108         return getDefaultExistenceChecks(getDocumentEntry(document).getDocumentTypeName());
109     }
110 
111     /**
112      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDefaultExistenceChecks
113      */
114     @Override
115     public Collection getDefaultExistenceChecks(String docTypeName) {
116         Collection defaultExistenceChecks = null;
117 
118         DocumentEntry entry = getDocumentEntry(docTypeName);
119         if (entry != null) {
120             defaultExistenceChecks = entry.getDefaultExistenceChecks();
121         }
122 
123         return defaultExistenceChecks;
124     }
125 
126     /**
127      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getMaintenanceDataObjectClass
128      */
129     @Override
130     public Class<?> getMaintenanceDataObjectClass(String docTypeName) {
131         Class dataObjectClass = null;
132 
133         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
134         if (entry != null) {
135             dataObjectClass = entry.getDataObjectClass();
136         }
137 
138         return dataObjectClass;
139     }
140 
141     /**
142      * @see org.kuali.rice.krad.service.impl.DocumentDictionaryService#getMaintainableClass
143      */
144     @Override
145     public Class<? extends Maintainable> getMaintainableClass(String docTypeName) {
146         Class maintainableClass = null;
147 
148         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
149         if (entry != null) {
150             maintainableClass = entry.getMaintainableClass();
151         }
152 
153         return maintainableClass;
154     }
155 
156     /**
157      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getBusinessRulesClass
158      */
159     @Override
160     public Class<? extends BusinessRule> getBusinessRulesClass(Document document) {
161         Class<? extends BusinessRule> businessRulesClass = null;
162 
163         String docTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
164         DocumentEntry entry = getDocumentEntry(docTypeName);
165         if (entry != null) {
166             businessRulesClass = entry.getBusinessRulesClass();
167         }
168 
169         return businessRulesClass;
170     }
171 
172     /**
173      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getAllowsCopy
174      */
175     @Override
176     public Boolean getAllowsCopy(Document document) {
177         Boolean allowsCopy = Boolean.FALSE;
178 
179         if (document == null) {
180             return allowsCopy;
181         }
182 
183         DocumentEntry entry = null;
184         if (document instanceof MaintenanceDocument) {
185             MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
186             if (maintenanceDocument.getNewMaintainableObject() != null) {
187                 entry = getMaintenanceDocumentEntry(
188                         maintenanceDocument.getNewMaintainableObject().getDataObjectClass());
189             }
190         } else {
191             entry = getDocumentEntry(document);
192         }
193 
194         if (entry != null) {
195             allowsCopy = Boolean.valueOf(entry.getAllowsCopy());
196         }
197 
198         return allowsCopy;
199     }
200 
201     /**
202      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getAllowsNewOrCopy
203      */
204     @Override
205     public Boolean getAllowsNewOrCopy(String docTypeName) {
206         Boolean allowsNewOrCopy = Boolean.FALSE;
207 
208         if (docTypeName != null) {
209             MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
210             if (entry != null) {
211                 allowsNewOrCopy = Boolean.valueOf(entry.getAllowsNewOrCopy());
212             }
213         }
214 
215         return allowsNewOrCopy;
216     }
217 
218     /**
219      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentEntry(java.lang.String)
220      */
221     @Override
222     public DocumentEntry getDocumentEntry(String documentTypeName) {
223         if (documentTypeName == null) {
224             throw new IllegalArgumentException("invalid (null) document type name");
225         }
226 
227         DocumentEntry entry = getDataDictionary().getDocumentEntry(documentTypeName);
228 
229         return entry;
230     }
231 
232     /**
233      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentEntryByClass(java.lang.Class<? extends
234      *      org.kuali.rice.krad.document.Document>)
235      */
236     @Override
237     public DocumentEntry getDocumentEntryByClass(Class<? extends Document> documentClass) {
238         DocumentEntry entry = null;
239 
240         String documentTypeName = getDocumentTypeByClass(documentClass);
241         if (StringUtils.isNotBlank(documentTypeName)) {
242             entry = getDocumentEntry(documentTypeName);
243         }
244 
245         return entry;
246     }
247 
248     /**
249      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getMaintenanceDocumentEntry
250      */
251     @Override
252     public MaintenanceDocumentEntry getMaintenanceDocumentEntry(String docTypeName) {
253         if (StringUtils.isBlank(docTypeName)) {
254             throw new IllegalArgumentException("invalid (blank) docTypeName");
255         }
256 
257         MaintenanceDocumentEntry entry = (MaintenanceDocumentEntry) getDataDictionary().getDocumentEntry(docTypeName);
258         return entry;
259     }
260 
261     /**
262      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentClassByName
263      */
264     @Override
265     public Class<?> getDocumentClassByName(String documentTypeName) {
266         Class documentClass = null;
267 
268         DocumentEntry entry = getDocumentEntry(documentTypeName);
269         if (entry != null) {
270             documentClass = entry.getDocumentClass();
271         }
272 
273         return documentClass;
274     }
275 
276     /**
277      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentTypeByClass(java.lang.Class<? extends org.kuali.rice.krad.document.Document>)
278      */
279     @Override
280     public String getDocumentTypeByClass(Class<? extends Document> documentClass) {
281         if (documentClass == null) {
282             throw new IllegalArgumentException("invalid (null) document class");
283         }
284 
285         DocumentEntry entry = getDataDictionary().getDocumentEntry(documentClass.getName());
286         if (entry != null) {
287             return entry.getDocumentTypeName();
288         }
289 
290         return null;
291     }
292 
293     /**
294      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getAllowsRecordDeletion
295      */
296     @Override
297     public Boolean getAllowsRecordDeletion(Class dataObjectClass) {
298         Boolean allowsRecordDeletion = Boolean.FALSE;
299 
300         MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(dataObjectClass);
301 
302         if (docEntry != null) {
303             allowsRecordDeletion = Boolean.valueOf(docEntry.getAllowsRecordDeletion());
304         }
305 
306         return allowsRecordDeletion;
307     }
308 
309     /**
310      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getAllowsRecordDeletion
311      */
312     @Override
313     public Boolean getAllowsRecordDeletion(MaintenanceDocument document) {
314         return document != null ?
315                 this.getAllowsRecordDeletion(document.getNewMaintainableObject().getDataObjectClass()) : Boolean.FALSE;
316     }
317 
318     /**
319      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getLockingKeys
320      */
321     @Override
322     public List<String> getLockingKeys(String docTypeName) {
323         List lockingKeys = null;
324 
325         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
326         if (entry != null) {
327             lockingKeys = entry.getLockingKeyFieldNames();
328         }
329 
330         return lockingKeys;
331     }
332 
333     /**
334      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getPreserveLockingKeysOnCopy
335      */
336     @Override
337     public boolean getPreserveLockingKeysOnCopy(Class dataObjectClass) {
338         boolean preserveLockingKeysOnCopy = false;
339 
340         MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(dataObjectClass);
341 
342         if (docEntry != null) {
343             preserveLockingKeysOnCopy = docEntry.getPreserveLockingKeysOnCopy();
344         }
345 
346         return preserveLockingKeysOnCopy;
347     }
348 
349     /**
350      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getClearValueOnCopyPropertyNames
351      */
352     @Override
353     public List<String> getClearValueOnCopyPropertyNames(Class dataObjectClass) {
354         List clearValueOnCopyPropertyNames = null;
355 
356         MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(dataObjectClass);
357 
358         if (docEntry != null) {
359             clearValueOnCopyPropertyNames = docEntry.getClearValueOnCopyPropertyNames();
360         }
361 
362         return clearValueOnCopyPropertyNames;
363     }
364 
365     /**
366      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentAuthorizer(java.lang.String)
367      */
368     public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
369         DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
370 
371         if (StringUtils.isBlank(documentType)) {
372             throw new IllegalArgumentException("invalid (blank) documentType");
373         }
374 
375         DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
376         if (documentEntry == null) {
377             throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
378         }
379 
380         Class<? extends DocumentAuthorizer> documentAuthorizerClass = documentEntry.getDocumentAuthorizerClass();
381 
382         DocumentAuthorizer documentAuthorizer = null;
383         try {
384             if (documentAuthorizerClass != null) {
385                 documentAuthorizer = documentAuthorizerClass.newInstance();
386             } else if (documentEntry instanceof MaintenanceDocumentEntry) {
387                 documentAuthorizer = new MaintenanceDocumentAuthorizerBase();
388             } else {
389                 documentAuthorizer = new DocumentAuthorizerBase();
390             }
391         } catch (Exception e) {
392             throw new RuntimeException("unable to instantiate documentAuthorizer '"
393                     + documentAuthorizerClass.getName()
394                     + "' for doctype '"
395                     + documentType
396                     + "'", e);
397         }
398 
399         return documentAuthorizer;
400     }
401 
402     /**
403      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentAuthorizer(java.lang.String)
404      */
405     public DocumentAuthorizer getDocumentAuthorizer(Document document) {
406         if (document == null) {
407             throw new IllegalArgumentException("invalid (null) document");
408         } else if (document.getDocumentHeader() == null) {
409             throw new IllegalArgumentException("invalid (null) document.documentHeader");
410         } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
411             throw new IllegalArgumentException("invalid (null) document.documentHeader.workflowDocument");
412         }
413 
414         String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
415 
416         DocumentAuthorizer documentAuthorizer = getDocumentAuthorizer(documentType);
417 
418         return documentAuthorizer;
419     }
420 
421     /**
422      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentPresentationController(java.lang.String)
423      */
424     public DocumentPresentationController getDocumentPresentationController(String documentType) {
425         DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
426 
427         if (StringUtils.isBlank(documentType)) {
428             throw new IllegalArgumentException("invalid (blank) documentType");
429         }
430 
431         DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
432         if (documentEntry == null) {
433             throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
434         }
435 
436         Class<? extends DocumentPresentationController> documentPresentationControllerClass =
437                 documentEntry.getDocumentPresentationControllerClass();
438 
439         DocumentPresentationController documentPresentationController = null;
440         try {
441             if (documentPresentationControllerClass != null) {
442                 documentPresentationController = documentPresentationControllerClass.newInstance();
443             } else if (documentEntry instanceof MaintenanceDocumentEntry) {
444                 documentPresentationController = new MaintenanceDocumentPresentationControllerBase();
445             } else {
446                 documentPresentationController = new DocumentPresentationControllerBase();
447             }
448         } catch (Exception e) {
449             throw new RuntimeException("unable to instantiate documentAuthorizer '"
450                     + documentPresentationControllerClass.getName()
451                     + "' for doctype '"
452                     + documentType
453                     + "'", e);
454         }
455 
456         return documentPresentationController;
457     }
458 
459     /**
460      * @see org.kuali.rice.krad.service.DocumentDictionaryService#getDocumentPresentationController(java.lang.String)
461      */
462     public DocumentPresentationController getDocumentPresentationController(Document document) {
463         if (document == null) {
464             throw new IllegalArgumentException("invalid (null) document");
465         } else if (document.getDocumentHeader() == null) {
466             throw new IllegalArgumentException("invalid (null) document.documentHeader");
467         } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
468             throw new IllegalArgumentException("invalid (null) document.documentHeader.workflowDocument");
469         }
470 
471         String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
472 
473         DocumentPresentationController documentPresentationController = getDocumentPresentationController(documentType);
474 
475         return documentPresentationController;
476     }
477 
478     /**
479      * Retrieves the maintenance document entry associated with the given data object class
480      *
481      * @param dataObjectClass - data object class to retrieve maintenance document entry for
482      * @return MaintenanceDocumentEntry for associated data object class
483      */
484     protected MaintenanceDocumentEntry getMaintenanceDocumentEntry(Class dataObjectClass) {
485         if (dataObjectClass == null) {
486             throw new IllegalArgumentException("invalid (blank) dataObjectClass");
487         }
488 
489         MaintenanceDocumentEntry entry =
490                 getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(dataObjectClass);
491         return entry;
492     }
493 
494     /**
495      * Retrieves the document entry for the document type of the given document instance
496      *
497      * @param document - document instance to retrieve document entry for
498      * @return DocumentEntry instance found for document type
499      */
500     protected DocumentEntry getDocumentEntry(Document document) {
501         if (document == null) {
502             throw new IllegalArgumentException("invalid (null) document");
503         }
504 
505         DocumentEntry entry = getDataDictionary().getDocumentEntry(document.getClass().getName());
506 
507         return entry;
508     }
509 
510     /**
511      * Gets the workflow document type dto for the given documentTypeName
512      *
513      * @param documentTypeName - document type name to retrieve document type dto
514      * @return DocumentType for given document type name
515      */
516     protected DocumentType getDocumentType(String documentTypeName) {
517         return KewApiServiceLocator.getDocumentTypeService().getDocumentTypeByName(documentTypeName);
518     }
519 
520     protected DataDictionary getDataDictionary() {
521         return getDataDictionaryService().getDataDictionary();
522     }
523 
524     protected DataDictionaryService getDataDictionaryService() {
525         if (dataDictionaryService == null) {
526             this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
527         }
528         return dataDictionaryService;
529     }
530 
531     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
532         this.dataDictionaryService = dataDictionaryService;
533     }
534 }