001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.kns.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kns.datadictionary.KNSDocumentEntry;
020import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
021import org.kuali.rice.kns.datadictionary.TransactionalDocumentEntry;
022import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
023import org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase;
024import org.kuali.rice.kns.document.authorization.DocumentPresentationController;
025import org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase;
026import org.kuali.rice.kns.document.authorization.MaintenanceDocumentPresentationControllerBase;
027import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizerBase;
028import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase;
029import org.kuali.rice.kns.service.DocumentHelperService;
030import org.kuali.rice.krad.datadictionary.DataDictionary;
031import org.kuali.rice.krad.document.Document;
032import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizerBase;
033import org.kuali.rice.krad.service.DataDictionaryService;
034import 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 * @deprecated Use {@link org.kuali.rice.krad.service.DocumentDictionaryService}.
042 */
043@Deprecated
044public class DocumentHelperServiceImpl implements DocumentHelperService {
045    
046    private DataDictionaryService dataDictionaryService;
047
048    /**
049     * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentAuthorizer(java.lang.String)
050     * // TODO: in krad documents could have multiple views and multiple authorizer classes
051     */
052    public DocumentAuthorizer getDocumentAuthorizer(String documentType) {
053        DataDictionary dataDictionary = getDataDictionaryService().getDataDictionary();
054
055        if (StringUtils.isBlank(documentType)) {
056            throw new IllegalArgumentException("invalid (blank) documentType");
057        }
058
059        KNSDocumentEntry documentEntry = (KNSDocumentEntry) dataDictionary.getDocumentEntry(documentType);
060        if (documentEntry == null) {
061            throw new IllegalArgumentException("unknown documentType '" + documentType + "'");
062        }
063
064        Class<? extends DocumentAuthorizer> documentAuthorizerClass = documentEntry.getDocumentAuthorizerClass();
065
066        DocumentAuthorizer documentAuthorizer = null;
067        try {
068            if (documentAuthorizerClass != null) {
069                documentAuthorizer = documentAuthorizerClass.newInstance();
070            }
071            else if (documentEntry instanceof MaintenanceDocumentEntry) {
072                documentAuthorizer = new MaintenanceDocumentAuthorizerBase();
073            }
074            else if (documentEntry instanceof TransactionalDocumentEntry) {
075                documentAuthorizer = new TransactionalDocumentAuthorizerBase();
076            }
077            else {
078                documentAuthorizer = new DocumentAuthorizerBase();
079            }
080        }
081        catch (Exception e) {
082            throw new RuntimeException("unable to instantiate documentAuthorizer '" + documentAuthorizerClass.getName() + "' for doctype '" + documentType + "'", e);
083        }
084
085        return documentAuthorizer;
086    }
087
088    /**
089     * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentAuthorizer(org.kuali.rice.krad.document.Document)
090     */
091    public DocumentAuthorizer getDocumentAuthorizer(Document document) {
092        if (document == null) {
093            throw new IllegalArgumentException("invalid (null) document");
094        } else if (document.getDocumentHeader() == null) {
095            throw new IllegalArgumentException(
096                    "invalid (null) document.documentHeader");
097        } else if (!document.getDocumentHeader().hasWorkflowDocument()) {
098            throw new IllegalArgumentException(
099                    "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     * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentPresentationController(java.lang.String)
110     * // TODO: in krad documents could have multiple views and multiple presentation controller
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     * @see org.kuali.rice.kns.service.DocumentHelperService#getDocumentPresentationController(org.kuali.rice.krad.document.Document)
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     * @return the dataDictionaryService
169     */
170    public DataDictionaryService getDataDictionaryService() {
171        if (dataDictionaryService == null) {
172            this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
173        }
174        return this.dataDictionaryService;
175    }
176
177    /**
178     * @param dataDictionaryService the dataDictionaryService to set
179     */
180    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
181        this.dataDictionaryService = dataDictionaryService;
182    }
183
184}