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.List;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.kuali.rice.core.api.exception.RiceRuntimeException;
024import org.kuali.rice.krad.datadictionary.DataDictionary;
025import org.kuali.rice.krad.datadictionary.DataDictionaryException;
026import org.kuali.rice.krad.inquiry.Inquirable;
027import org.kuali.rice.krad.service.DataDictionaryService;
028import org.kuali.rice.krad.uif.UifConstants;
029import org.kuali.rice.krad.uif.UifParameters;
030import org.kuali.rice.krad.uif.util.ViewModelUtils;
031import org.kuali.rice.krad.uif.view.InquiryView;
032import org.kuali.rice.krad.uif.view.LookupView;
033import org.kuali.rice.krad.uif.view.MaintenanceView;
034import org.kuali.rice.krad.uif.view.View;
035import org.kuali.rice.krad.uif.service.ViewDictionaryService;
036import org.kuali.rice.krad.uif.UifConstants.ViewType;
037import org.kuali.rice.krad.util.ObjectUtils;
038import org.springframework.beans.PropertyValues;
039
040/**
041 * Implementation of <code>ViewDictionaryService</code>
042 *
043 * <p>
044 * Pulls view entries from the data dictionary to implement the various query
045 * methods
046 * </p>
047 *
048 * @author Kuali Rice Team (rice.collab@kuali.org)
049 */
050public class ViewDictionaryServiceImpl implements ViewDictionaryService {
051
052    private DataDictionaryService dataDictionaryService;
053
054    /**
055     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getInquirable(java.lang.Class,
056     *      java.lang.String)
057     */
058    public Inquirable getInquirable(Class<?> dataObjectClass, String viewName) {
059        Inquirable inquirable = null;
060
061        if (StringUtils.isBlank(viewName)) {
062            viewName = UifConstants.DEFAULT_VIEW_NAME;
063        }
064
065        Map<String, String> indexKey = new HashMap<String, String>();
066        indexKey.put(UifParameters.VIEW_NAME, viewName);
067        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
068
069        // get view properties
070        PropertyValues propertyValues = getDataDictionary().getViewPropertiesByType(ViewType.INQUIRY, indexKey);
071
072        String viewHelperServiceClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
073                "viewHelperServiceClassName");
074        if (StringUtils.isNotBlank(viewHelperServiceClassName)) {
075            try {
076                inquirable = (Inquirable) ObjectUtils.newInstance(Class.forName(viewHelperServiceClassName));
077            } catch (ClassNotFoundException e) {
078                throw new RiceRuntimeException(
079                        "Unable to find class for inquirable classname: " + viewHelperServiceClassName, e);
080            }
081        }
082
083        return inquirable;
084    }
085
086    /**
087     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isInquirable(java.lang.Class)
088     */
089    public boolean isInquirable(Class<?> dataObjectClass) {
090        Map<String, String> indexKey = new HashMap<String, String>();
091        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
092        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
093
094        boolean isInquirable = getDataDictionary().viewByTypeExist(ViewType.INQUIRY, indexKey);
095
096        return isInquirable;
097    }
098
099    /**
100     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isLookupable(java.lang.Class)
101     */
102    public boolean isLookupable(Class<?> dataObjectClass) {
103        Map<String, String> indexKey = new HashMap<String, String>();
104        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
105        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
106
107        boolean isLookupable = getDataDictionary().viewByTypeExist(ViewType.LOOKUP, indexKey);
108
109        return isLookupable;
110    }
111
112    /**
113     * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isMaintainable(java.lang.Class)
114     */
115    public boolean isMaintainable(Class<?> dataObjectClass) {
116        Map<String, String> indexKey = new HashMap<String, String>();
117        indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
118        indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
119
120        boolean isMaintainable = getDataDictionary().viewByTypeExist(ViewType.MAINTENANCE, indexKey);
121
122        return isMaintainable;
123    }
124
125    /**
126     * @see org.kuali.rice.krad.uif.service.impl.ViewDictionaryService#getResultSetLimitForLookup(java.lang.Class)
127     */
128    @Override
129    public Integer getResultSetLimitForLookup(Class<?> dataObjectClass) {
130        LookupView lookupView = null;
131
132        List<View> lookupViews = getDataDictionary().getViewsForType(UifConstants.ViewType.LOOKUP);
133        for (View view : lookupViews) {
134            LookupView lView = (LookupView) view;
135
136            if (StringUtils.equals(lView.getDataObjectClassName().getName(), dataObjectClass.getName())) {
137                // if we already found a lookup view, only override if this is the default
138                if (lookupView != null) {
139                    if (StringUtils.equals(lView.getViewName(), UifConstants.DEFAULT_VIEW_NAME)) {
140                        lookupView = lView;
141                    }
142                } else {
143                    lookupView = lView;
144                }
145            }
146        }
147
148        if (lookupView != null) {
149            return lookupView.getResultSetLimit();
150        }
151
152        return null;
153    }
154
155    protected DataDictionary getDataDictionary() {
156        return getDataDictionaryService().getDataDictionary();
157    }
158
159    protected DataDictionaryService getDataDictionaryService() {
160        return this.dataDictionaryService;
161    }
162
163    public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
164        this.dataDictionaryService = dataDictionaryService;
165    }
166}