001/**
002 * Copyright 2005-2016 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.krad.uif.service.impl;
017
018import org.kuali.rice.kew.api.exception.WorkflowException;
019import org.kuali.rice.krad.document.Document;
020import org.kuali.rice.krad.service.DocumentDictionaryService;
021import org.kuali.rice.krad.service.DocumentService;
022import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
023import org.kuali.rice.krad.uif.UifConstants;
024import org.kuali.rice.krad.uif.UifParameters;
025import org.kuali.rice.krad.uif.service.ViewTypeService;
026import org.kuali.rice.krad.uif.util.ViewModelUtils;
027import org.kuali.rice.krad.util.KRADConstants;
028import org.kuali.rice.krad.util.KRADPropertyConstants;
029import org.springframework.beans.PropertyValues;
030
031import java.util.HashMap;
032import java.util.Map;
033
034/**
035 * Type service implementation for transactional views.
036 *
037 * <p>
038 * Indexes views on document class and view name. Can retrieve views by document type,
039 * document type and view name, document class, document class and view name, or document id.
040 * </p>
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class TransactionalViewTypeServiceImpl implements ViewTypeService {
045    private DocumentService documentService;
046    private DocumentDictionaryService documentDictionaryService;
047
048    /**
049    * Determines the view type name.
050    *
051    * <p>
052    * The view type name is specific for each type of transactional
053    * document and manually set.
054    * </p>
055    *
056    * @return String
057    */
058    public UifConstants.ViewType getViewTypeName() {
059        return UifConstants.ViewType.TRANSACTIONAL;
060    }
061
062    /**
063    * Get the document class and view name.
064    *
065    * <p>
066    * Extracts the view name and document class name from
067    * the PropertyValues.
068    * </p>
069    *
070    * @param propertyValues - collection including document class and view name
071    * @return Map<String, String>
072    */
073    public Map<String, String> getParametersFromViewConfiguration(PropertyValues propertyValues) {
074        Map<String, String> parameters = new HashMap<String, String>();
075
076        String viewName = ViewModelUtils.getStringValFromPVs(propertyValues, UifParameters.VIEW_NAME);
077        String documentClass = ViewModelUtils.getStringValFromPVs(propertyValues,
078                UifParameters.DOCUMENT_CLASS);
079
080        parameters.put(UifParameters.VIEW_NAME, viewName);
081        parameters.put(UifParameters.DOCUMENT_CLASS, documentClass);
082
083        return parameters;
084    }
085
086    /**
087    * Determines the view type name.
088    *
089    * <p>
090    * Check for document id in request parameters, if given retrieve document
091    * instance to get the document class. Otherwise check for document class or
092    * document type parameters for creating a new document view.
093    # </p>
094    *
095    * @param requestParameters - collection including document class and view name
096    * @return Map<String, String> - document class name, view name
097    */
098    @Override
099    public Map<String, String> getParametersFromRequest(Map<String, String> requestParameters) {
100        Map<String, String> parameters = new HashMap<String, String>();
101
102        if (requestParameters.containsKey(KRADPropertyConstants.DOC_ID)) {
103            String documentNumber = requestParameters.get(KRADPropertyConstants.DOC_ID);
104
105            Class<?> documentClass = null;
106            try {
107                // determine object class based on the document type
108                Document document = documentService.getByDocumentHeaderId(documentNumber);
109
110                if (!documentService.documentExists(documentNumber)) {
111                    parameters = new HashMap<String, String>();
112                    parameters.put(UifParameters.VIEW_ID, KRADConstants.KRAD_INITIATED_DOCUMENT_VIEW_NAME);
113                    return parameters;
114                }
115
116                if (document != null) {
117                    String docTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
118                    documentClass = getDocumentDictionaryService().getDocumentClassByName(docTypeName);
119                    if (documentClass != null) {
120                        parameters.put(UifParameters.DOCUMENT_CLASS, documentClass.getName());
121                    }
122                }
123
124                if (documentClass == null) {
125                    throw new RuntimeException(
126                            "Could not determine document class for document with id: " + documentNumber);
127                }
128
129            } catch (WorkflowException e) {
130                throw new RuntimeException(
131                        "Encountered workflow exception while retrieving document with id: " + documentNumber, e);
132            }
133        }
134        else {
135
136            if (requestParameters.containsKey(UifParameters.DOCUMENT_CLASS)) {
137                parameters.put(UifParameters.DOCUMENT_CLASS, requestParameters.get(UifParameters.DOCUMENT_CLASS));
138            }
139            else if (requestParameters.containsKey(UifParameters.DOC_TYPE_NAME)) {
140                String docTypeName = requestParameters.get(UifParameters.DOC_TYPE_NAME);
141                Class<?> documentClass = getDocumentDictionaryService().getDocumentClassByName(docTypeName);
142
143                if (documentClass != null) {
144                    parameters.put(UifParameters.DOCUMENT_CLASS, documentClass.getName());
145                }
146            }
147
148        }
149
150        if (requestParameters.containsKey(UifParameters.VIEW_NAME)) {
151            parameters.put(UifParameters.VIEW_NAME, requestParameters.get(UifParameters.VIEW_NAME));
152        }
153        else {
154            parameters.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
155        }
156
157        return parameters;
158    }
159
160    /**
161    * Returns the document service.
162    *
163    * <p>
164    * Gets the document service.
165    * </p>
166    *
167    * @return DocumentService - document service
168    */
169    protected DocumentService getDocumentService() {
170
171        if (documentService == null) {
172            this.documentService = KRADServiceLocatorWeb.getDocumentService();
173        }
174
175        return this.documentService;
176    }
177
178    /**
179    * Initializes the document service .
180    *
181    * <p>
182    * Sets the document service.
183    * </p>
184    *
185    * @param documentService - document service
186    */
187    public void setDocumentService(DocumentService documentService) {
188        this.documentService = documentService;
189    }
190
191    /**
192    * Returns the document dictionary service.
193    *
194    * <p>
195    * Gets the document dictionary service.
196    * </p>
197    *
198    * @return DocumentDictionaryService - document dictionary service
199    */
200    public DocumentDictionaryService getDocumentDictionaryService() {
201
202        if (documentDictionaryService == null) {
203            this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
204        }
205
206        return documentDictionaryService;
207    }
208
209    /**
210     * Initializes the document dictionary service.
211     *
212     * <p>
213     * Sets the document dictionary service.
214     * </p>
215     *
216     * @param documentDictionaryService - document dictionary service
217     */
218    public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
219        this.documentDictionaryService = documentDictionaryService;
220    }
221}