001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kns.service.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.kns.datadictionary.KNSDocumentEntry;
020    import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
021    import org.kuali.rice.kns.datadictionary.TransactionalDocumentEntry;
022    import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
023    import org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase;
024    import org.kuali.rice.kns.document.authorization.DocumentPresentationController;
025    import org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase;
026    import org.kuali.rice.kns.document.authorization.MaintenanceDocumentPresentationControllerBase;
027    import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizerBase;
028    import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
029    import org.kuali.rice.kns.service.DocumentHelperService;
030    import org.kuali.rice.krad.datadictionary.DataDictionary;
031    import org.kuali.rice.krad.document.Document;
032    import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizerBase;
033    import org.kuali.rice.krad.service.DataDictionaryService;
034    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
035    
036    /**
037     * This class is a utility service intended to help retrieve objects related to particular documents.
038     * 
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     *
041     */
042    public class DocumentHelperServiceImpl implements DocumentHelperService {
043        
044        private DataDictionaryService dataDictionaryService;
045    
046        /**
047         * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentAuthorizer(java.lang.String)
048         * // TODO: in krad documents could have multiple views and multiple authorizer classes
049         */
050        public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
051            DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
052    
053            if (StringUtils.isBlank(documentType)) {
054                throw new IllegalArgumentException("invalid (blank) documentType");
055            }
056    
057            KNSDocumentEntry documentEntry = (KNSDocumentEntry) dataDictionary.getDocumentEntry(documentType);
058            if (documentEntry == null) {
059                throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
060            }
061    
062            Class<? extends DocumentAuthorizer> documentAuthorizerClass = documentEntry.getDocumentAuthorizerClass();
063    
064            DocumentAuthorizer documentAuthorizer = null;
065            try {
066                if (documentAuthorizerClass != null) {
067                    documentAuthorizer = documentAuthorizerClass.newInstance();
068                }
069                else if (documentEntry instanceof MaintenanceDocumentEntry) {
070                    documentAuthorizer = new MaintenanceDocumentAuthorizerBase();
071                }
072                else if (documentEntry instanceof TransactionalDocumentEntry) {
073                    documentAuthorizer = new TransactionalDocumentAuthorizerBase();
074                }
075                else {
076                    documentAuthorizer = new DocumentAuthorizerBase();
077                }
078            }
079            catch (Exception e) {
080                throw new RuntimeException("unable to instantiate documentAuthorizer '" + documentAuthorizerClass.getName() + "' for doctype '" + documentType + "'", e);
081            }
082    
083            return documentAuthorizer;
084        }
085    
086        /**
087         * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentAuthorizer(org.kuali.rice.krad.document.Document)
088         */
089        public DocumentAuthorizer getDocumentAuthorizer(Document document) {
090            if (document == null) {
091                throw new IllegalArgumentException("invalid (null) document");
092            } else if (document.getDocumentHeader() == null) {
093                throw new IllegalArgumentException(
094                        "invalid (null) document.documentHeader");
095            } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
096                throw new IllegalArgumentException(
097                        "invalid (null) document.documentHeader.workflowDocument");
098            }
099    
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    }