001    /*
002     * Copyright 2007-2009 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.DataDictionary;
020    import org.kuali.rice.kns.datadictionary.DocumentEntry;
021    import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
022    import org.kuali.rice.kns.datadictionary.TransactionalDocumentEntry;
023    import org.kuali.rice.kns.document.Document;
024    import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
025    import org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase;
026    import org.kuali.rice.kns.document.authorization.DocumentPresentationController;
027    import org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase;
028    import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizerBase;
029    import org.kuali.rice.kns.document.authorization.MaintenanceDocumentPresentationControllerBase;
030    import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizerBase;
031    import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
032    import org.kuali.rice.kns.service.DataDictionaryService;
033    import org.kuali.rice.kns.service.DocumentHelperService;
034    import org.kuali.rice.kns.service.KNSServiceLocator;
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         */
049        public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
050            DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
051    
052            if (StringUtils.isBlank(documentType)) {
053                throw new IllegalArgumentException("invalid (blank) documentType");
054            }
055    
056            DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
057            if (documentEntry == null) {
058                throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
059            }
060    
061            Class<? extends DocumentAuthorizer> documentAuthorizerClass = documentEntry.getDocumentAuthorizerClass();
062    
063            DocumentAuthorizer documentAuthorizer = null;
064            try {
065                if (documentAuthorizerClass != null) {
066                    documentAuthorizer = documentAuthorizerClass.newInstance();
067                }
068                else if (documentEntry instanceof MaintenanceDocumentEntry) {
069                    documentAuthorizer = new MaintenanceDocumentAuthorizerBase();
070                }
071                else if (documentEntry instanceof TransactionalDocumentEntry) {
072                    documentAuthorizer = new TransactionalDocumentAuthorizerBase();
073                }
074                else {
075                    documentAuthorizer = new DocumentAuthorizerBase();
076                }
077            }
078            catch (Exception e) {
079                throw new RuntimeException("unable to instantiate documentAuthorizer '" + documentAuthorizerClass.getName() + "' for doctype '" + documentType + "'", e);
080            }
081    
082            return documentAuthorizer;
083        }
084    
085        /**
086         * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentAuthorizer(org.kuali.rice.kns.document.Document)
087         */
088        public DocumentAuthorizer getDocumentAuthorizer(Document document) {
089            if (document == null) {
090                throw new IllegalArgumentException("invalid (null) document");
091            } else if (document.getDocumentHeader() == null) {
092                throw new IllegalArgumentException(
093                        "invalid (null) document.documentHeader");
094            } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
095                throw new IllegalArgumentException(
096                        "invalid (null) document.documentHeader.workflowDocument");
097            }
098    
099            String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentType();
100    
101            DocumentAuthorizer documentAuthorizer = getDocumentAuthorizer(documentType);
102            return documentAuthorizer;
103        }
104    
105        /**
106         * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentPresentationController(java.lang.String)
107         */
108        public DocumentPresentationController getDocumentPresentationController(String documentType) {
109            DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
110            DocumentPresentationController documentPresentationController = null;
111            
112            if (StringUtils.isBlank(documentType)) {
113                throw new IllegalArgumentException("invalid (blank) documentType");
114            }
115    
116            DocumentEntry documentEntry = dataDictionary.getDocumentEntry(documentType);
117            if (documentEntry == null) {
118                throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
119            }
120            Class<? extends DocumentPresentationController> documentPresentationControllerClass = null;
121            try{
122                documentPresentationControllerClass = documentEntry.getDocumentPresentationControllerClass();
123                if(documentPresentationControllerClass != null){
124                    documentPresentationController = documentPresentationControllerClass.newInstance();
125                } else {
126                    DocumentEntry doc = dataDictionary.getDocumentEntry(documentType);
127                    if ( doc instanceof TransactionalDocumentEntry ) {
128                        documentPresentationController = new TransactionalDocumentPresentationControllerBase();
129                    } else if(doc instanceof MaintenanceDocumentEntry) {
130                        documentPresentationController = new MaintenanceDocumentPresentationControllerBase();
131                    } else {
132                        documentPresentationController = new DocumentPresentationControllerBase();
133                    }
134                }
135            }
136            catch (Exception e) {
137                throw new RuntimeException("unable to instantiate documentPresentationController '" + documentPresentationControllerClass.getName() + "' for doctype '" + documentType + "'", e);
138            }
139    
140            return documentPresentationController;
141        }
142    
143        /**
144         * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentPresentationController(org.kuali.rice.kns.document.Document)
145         */
146        public DocumentPresentationController getDocumentPresentationController(Document document) {
147            if (document == null) {
148                throw new IllegalArgumentException("invalid (null) document");
149            }
150            else if (document.getDocumentHeader() == null) {
151                throw new IllegalArgumentException("invalid (null) document.documentHeader");
152            }
153            else if (!document.getDocumentHeader().hasWorkflowDocument()) {
154                throw new IllegalArgumentException("invalid (null) document.documentHeader.workflowDocument");
155            }
156    
157            String documentType = document.getDocumentHeader().getWorkflowDocument().getDocumentType();
158    
159            DocumentPresentationController documentPresentationController = getDocumentPresentationController(documentType);
160            return documentPresentationController;
161        }
162    
163        /**
164         * @return the dataDictionaryService
165         */
166        public DataDictionaryService getDataDictionaryService() {
167            if (dataDictionaryService == null) {
168                this.dataDictionaryService = KNSServiceLocator.getDataDictionaryService();
169            }
170            return this.dataDictionaryService;
171        }
172    
173        /**
174         * @param dataDictionaryService the dataDictionaryService to set
175         */
176        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
177            this.dataDictionaryService = dataDictionaryService;
178        }
179    
180    }