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 java.util.HashMap;
019import java.util.Map;
020
021import org.kuali.rice.kew.api.exception.WorkflowException;
022import org.kuali.rice.krad.document.Document;
023import org.kuali.rice.krad.service.DocumentDictionaryService;
024import org.kuali.rice.krad.service.DocumentService;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026import org.kuali.rice.krad.uif.UifConstants;
027import org.kuali.rice.krad.uif.UifConstants.ViewType;
028import org.kuali.rice.krad.uif.UifParameters;
029import org.kuali.rice.krad.uif.util.ViewModelUtils;
030import org.kuali.rice.krad.uif.service.ViewTypeService;
031import org.kuali.rice.krad.util.KRADConstants;
032import org.kuali.rice.krad.util.KRADPropertyConstants;
033import org.springframework.beans.PropertyValues;
034
035/**
036 * Type service implementation for maintenance views
037 * 
038 * <p>
039 * Indexes views on object class and name. Can retrieve views by object class,
040 * object class and name, or document id
041 * </p>
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045public class MaintenanceViewTypeServiceImpl implements ViewTypeService {
046        private DocumentService documentService;
047    private DocumentDictionaryService documentDictionaryService;
048
049        /**
050         * @see org.kuali.rice.krad.uif.service.ViewTypeService#getViewTypeName()
051         */
052        public ViewType getViewTypeName() {
053                return ViewType.MAINTENANCE;
054        }
055
056    /**
057     * @see org.kuali.rice.krad.uif.service.ViewTypeService#getParametersFromViewConfiguration(org.springframework.beans.PropertyValues)
058     */
059    public Map<String, String> getParametersFromViewConfiguration(PropertyValues propertyValues) {
060        Map<String, String> parameters = new HashMap<String, String>();
061
062        String viewName = ViewModelUtils.getStringValFromPVs(propertyValues, UifParameters.VIEW_NAME);
063        String dataObjectClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
064                UifParameters.DATA_OBJECT_CLASS_NAME);
065
066        parameters.put(UifParameters.VIEW_NAME, viewName);
067        parameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClassName);
068
069        return parameters;
070    }
071
072        /**
073         * Check for document id in request parameters, if given retrieve document
074         * instance to get the object class and set the name parameter
075         * 
076         * @see org.kuali.rice.krad.uif.service.ViewTypeService#getParametersFromRequest(java.util.Map)
077         */
078        @Override
079        public Map<String, String> getParametersFromRequest(Map<String, String> requestParameters) {
080                Map<String, String> parameters = new HashMap<String, String>();
081
082                if (requestParameters.containsKey(UifParameters.VIEW_NAME)) {
083                        parameters.put(UifParameters.VIEW_NAME, requestParameters.get(UifParameters.VIEW_NAME));
084                }
085                else {
086                        parameters.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
087                }
088
089                if (requestParameters.containsKey(UifParameters.DATA_OBJECT_CLASS_NAME)) {
090                        parameters.put(UifParameters.DATA_OBJECT_CLASS_NAME,
091                                        requestParameters.get(UifParameters.DATA_OBJECT_CLASS_NAME));
092                }
093                else if (requestParameters.containsKey(KRADPropertyConstants.DOC_ID)) {
094                        String documentNumber = requestParameters.get(KRADPropertyConstants.DOC_ID);
095
096                        boolean objectClassFound = false;
097                        try {
098                                // determine object class based on the document type
099                                Document document = documentService.getByDocumentHeaderId(documentNumber);
100                if (!documentService.documentExists(documentNumber)) {
101                    parameters = new HashMap<String,String>();
102                    parameters.put(UifParameters.VIEW_ID, KRADConstants.KRAD_INITIATED_DOCUMENT_VIEW_NAME);
103                    return parameters;
104                }
105                                if (document != null) {
106                                        String docTypeName = document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName();
107                                        Class<?> objectClassName = getDocumentDictionaryService().getMaintenanceDataObjectClass(docTypeName);
108                                        if (objectClassName != null) {
109                                                objectClassFound = true;
110                                                parameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, objectClassName.getName());
111                                        }
112                                }
113
114                                if (!objectClassFound) {
115                                        throw new RuntimeException("Could not determine object class for maintenance document with id: "
116                                                        + documentNumber);
117                                }
118                        }
119                        catch (WorkflowException e) {
120                                throw new RuntimeException("Encountered workflow exception while retrieving document with id: "
121                                                + documentNumber, e);
122                        }
123                }
124
125                return parameters;
126        }
127
128        protected DocumentService getDocumentService() {
129        if (documentService == null) {
130            this.documentService = KRADServiceLocatorWeb.getDocumentService();
131        }
132                return this.documentService;
133        }
134
135        public void setDocumentService(DocumentService documentService) {
136                this.documentService = documentService;
137        }
138
139    public DocumentDictionaryService getDocumentDictionaryService() {
140        if (documentDictionaryService == null) {
141            this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
142        }
143        return documentDictionaryService;
144    }
145
146    public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
147        this.documentDictionaryService = documentDictionaryService;
148    }
149}