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