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.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceRuntimeException;
020import org.kuali.rice.krad.datadictionary.DataDictionary;
021import org.kuali.rice.krad.inquiry.Inquirable;
022import org.kuali.rice.krad.lookup.LookupUtils;
023import org.kuali.rice.krad.service.DataDictionaryService;
024import org.kuali.rice.krad.uif.UifConstants;
025import org.kuali.rice.krad.uif.UifConstants.ViewType;
026import org.kuali.rice.krad.uif.UifParameters;
027import org.kuali.rice.krad.uif.UifPropertyPaths;
028import org.kuali.rice.krad.uif.service.ViewDictionaryService;
029import org.kuali.rice.krad.uif.util.ViewModelUtils;
030import org.kuali.rice.krad.lookup.LookupView;
031import org.kuali.rice.krad.uif.view.View;
032import org.kuali.rice.krad.uif.view.ViewSessionPolicy;
033import org.kuali.rice.krad.util.KRADUtils;
034import org.kuali.rice.krad.lookup.LookupForm;
035import org.springframework.beans.PropertyValues;
036import org.springframework.util.Assert;
037
038import java.util.HashMap;
039import java.util.Map;
040
041/**
042 * Implementation of <code>ViewDictionaryService</code>
043 *
044 * <p>
045 * Pulls view entries from the data dictionary to implement the various query
046 * methods
047 * </p>
048 *
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 */
051public class ViewDictionaryServiceImpl implements ViewDictionaryService {
052    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
053            ViewDictionaryServiceImpl.class);
054
055    private DataDictionaryService dataDictionaryService;
056
057    /**
058     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getInquirable(java.lang.Class,
059     *      java.lang.String)
060     */
061    public Inquirable getInquirable(Class<?> dataObjectClass, String viewName) {
062        Inquirable inquirable = null;
063
064        if (StringUtils.isBlank(viewName)) {
065            viewName = UifConstants.DEFAULT_VIEW_NAME;
066        }
067
068        Map<String, String> indexKey = new HashMap<String, String>();
069        indexKey.put(UifParameters.VIEW_NAME, viewName);
070        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
071
072        // get view properties
073        PropertyValues propertyValues = getDataDictionary().getViewPropertiesByType(ViewType.INQUIRY, indexKey);
074
075        String viewHelperServiceClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
076                UifPropertyPaths.VIEW_HELPER_SERVICE_CLASS);
077        if (StringUtils.isNotBlank(viewHelperServiceClassName)) {
078            try {
079                inquirable = (Inquirable) KRADUtils.createNewObjectFromClass(Class.forName(viewHelperServiceClassName));
080            } catch (ClassNotFoundException e) {
081                throw new RiceRuntimeException(
082                        "Unable to find class for inquirable classname: " + viewHelperServiceClassName, e);
083            }
084        }
085
086        return inquirable;
087    }
088
089    /**
090     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isInquirable(java.lang.Class)
091     */
092    public boolean isInquirable(Class<?> dataObjectClass) {
093        Map<String, String> indexKey = new HashMap<String, String>();
094        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
095        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
096
097        boolean isInquirable = getDataDictionary().viewByTypeExist(ViewType.INQUIRY, indexKey);
098
099        return isInquirable;
100    }
101
102    /**
103     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isLookupable(java.lang.Class)
104     */
105    public boolean isLookupable(Class<?> dataObjectClass) {
106        Map<String, String> indexKey = new HashMap<String, String>();
107        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
108        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
109
110        boolean isLookupable = getDataDictionary().viewByTypeExist(ViewType.LOOKUP, indexKey);
111
112        return isLookupable;
113    }
114
115    /**
116     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isMaintainable(java.lang.Class)
117     */
118    public boolean isMaintainable(Class<?> dataObjectClass) {
119        Map<String, String> indexKey = new HashMap<String, String>();
120        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
121        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
122
123        boolean isMaintainable = getDataDictionary().viewByTypeExist(ViewType.MAINTENANCE, indexKey);
124
125        return isMaintainable;
126    }
127
128    /**
129     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getResultSetLimitForLookup(java.lang.Class,
130     *      org.kuali.rice.krad.lookup.LookupForm)
131     *
132     *      If the form is null, only the dataObjectClass will be used to find the LookupView and corresponding
133     *      results set limit.  Since the viewID is not known, the default view will be used.
134     */
135    @Override
136    public Integer getResultSetLimitForLookup(Class<?> dataObjectClass, LookupForm lookupForm) {
137        LookupView lookupView;
138        boolean multipleValueSelectSpecifiedOnURL = false;
139
140        if (KRADUtils.isNotNull(lookupForm)) {
141            if (lookupForm.isMultipleValuesSelect()) {
142                multipleValueSelectSpecifiedOnURL = true;
143            }
144
145            if (!multipleValueSelectSpecifiedOnURL && lookupForm.getViewRequestParameters().containsKey(
146                    UifParameters.MULTIPLE_VALUES_SELECT)) {
147                String multiValueSelect = lookupForm.getViewRequestParameters().get(
148                        UifParameters.MULTIPLE_VALUES_SELECT);
149                if (multiValueSelect.equalsIgnoreCase("true")) {
150                    multipleValueSelectSpecifiedOnURL = true;
151                }
152            }
153            lookupView = (LookupView) lookupForm.getView();
154        } else {
155            Map<String, String> indexKey = new HashMap<String, String>();
156            indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
157            indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
158            lookupView = (LookupView) getDataDictionary().getViewByTypeIndex(ViewType.LOOKUP, indexKey);
159        }
160
161        Integer limit = null;
162
163        if (lookupView != null) {
164            if (lookupView.isMultipleValuesSelect() || multipleValueSelectSpecifiedOnURL) {
165                limit = lookupView.getMultipleValuesSelectResultSetLimit();
166
167                if (limit == null) {
168                    limit = LookupUtils.getApplicationMultipleValueSearchResultsLimit();
169                }
170            } else {
171                limit = lookupView.getResultSetLimit();
172
173                if (limit == null) {
174                    limit = LookupUtils.getApplicationSearchResultsLimit();
175                }
176            }
177        }
178
179        return limit;
180    }
181
182    /**
183     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getViewSessionPolicy(java.lang.String)
184     */
185    public ViewSessionPolicy getViewSessionPolicy(String viewId) {
186        Assert.hasLength(viewId, "view id is required for retrieving the view session policy");
187
188        ViewSessionPolicy viewSessionPolicy = null;
189
190        View view = getDataDictionary().getImmutableViewById(viewId);
191        if (view != null) {
192            viewSessionPolicy = view.getSessionPolicy();
193        }
194
195        return viewSessionPolicy;
196    }
197
198    /**
199     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isSessionStorageEnabled(java.lang.String)
200     */
201    public boolean isSessionStorageEnabled(String viewId) {
202        Assert.hasLength(viewId, "view id is required for retrieving session indicator");
203
204        boolean sessionStorageEnabled = false;
205
206        View view = getDataDictionary().getImmutableViewById(viewId);
207        if (view != null) {
208            sessionStorageEnabled = view.isPersistFormToSession();
209        }
210
211        return sessionStorageEnabled;
212    }
213
214    protected DataDictionary getDataDictionary() {
215        return getDataDictionaryService().getDataDictionary();
216    }
217
218    protected DataDictionaryService getDataDictionaryService() {
219        return this.dataDictionaryService;
220    }
221
222    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
223        this.dataDictionaryService = dataDictionaryService;
224    }
225}