View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.service.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.core.api.exception.RiceRuntimeException;
23  import org.kuali.rice.krad.datadictionary.DataDictionary;
24  import org.kuali.rice.krad.inquiry.Inquirable;
25  import org.kuali.rice.krad.service.DataDictionaryService;
26  import org.kuali.rice.krad.uif.UifConstants;
27  import org.kuali.rice.krad.uif.UifParameters;
28  import org.kuali.rice.krad.uif.UifPropertyPaths;
29  import org.kuali.rice.krad.uif.util.ViewModelUtils;
30  import org.kuali.rice.krad.uif.view.LookupView;
31  import org.kuali.rice.krad.uif.service.ViewDictionaryService;
32  import org.kuali.rice.krad.uif.UifConstants.ViewType;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  import org.kuali.rice.krad.web.form.LookupForm;
35  import org.springframework.beans.PropertyValues;
36  
37  /**
38   * Implementation of <code>ViewDictionaryService</code>
39   *
40   * <p>
41   * Pulls view entries from the data dictionary to implement the various query
42   * methods
43   * </p>
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class ViewDictionaryServiceImpl implements ViewDictionaryService {
48      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
49              ViewDictionaryServiceImpl.class);
50  
51      private DataDictionaryService dataDictionaryService;
52  
53      /**
54       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getInquirable(java.lang.Class,
55       *      java.lang.String)
56       */
57      public Inquirable getInquirable(Class<?> dataObjectClass, String viewName) {
58          Inquirable inquirable = null;
59  
60          if (StringUtils.isBlank(viewName)) {
61              viewName = UifConstants.DEFAULT_VIEW_NAME;
62          }
63  
64          Map<String, String> indexKey = new HashMap<String, String>();
65          indexKey.put(UifParameters.VIEW_NAME, viewName);
66          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
67  
68          // get view properties
69          PropertyValues propertyValues = getDataDictionary().getViewPropertiesByType(ViewType.INQUIRY, indexKey);
70  
71          String viewHelperServiceClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
72                  UifPropertyPaths.VIEW_HELPER_SERVICE_CLASS);
73          if (StringUtils.isNotBlank(viewHelperServiceClassName)) {
74              try {
75                  inquirable = (Inquirable) ObjectUtils.newInstance(Class.forName(viewHelperServiceClassName));
76              } catch (ClassNotFoundException e) {
77                  throw new RiceRuntimeException(
78                          "Unable to find class for inquirable classname: " + viewHelperServiceClassName, e);
79              }
80          }
81  
82          return inquirable;
83      }
84  
85      /**
86       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isInquirable(java.lang.Class)
87       */
88      public boolean isInquirable(Class<?> dataObjectClass) {
89          Map<String, String> indexKey = new HashMap<String, String>();
90          indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
91          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
92  
93          boolean isInquirable = getDataDictionary().viewByTypeExist(ViewType.INQUIRY, indexKey);
94  
95          return isInquirable;
96      }
97  
98      /**
99       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isLookupable(java.lang.Class)
100      */
101     public boolean isLookupable(Class<?> dataObjectClass) {
102         Map<String, String> indexKey = new HashMap<String, String>();
103         indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
104         indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
105 
106         boolean isLookupable = getDataDictionary().viewByTypeExist(ViewType.LOOKUP, indexKey);
107 
108         return isLookupable;
109     }
110 
111     /**
112      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isMaintainable(java.lang.Class)
113      */
114     public boolean isMaintainable(Class<?> dataObjectClass) {
115         Map<String, String> indexKey = new HashMap<String, String>();
116         indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
117         indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
118 
119         boolean isMaintainable = getDataDictionary().viewByTypeExist(ViewType.MAINTENANCE, indexKey);
120 
121         return isMaintainable;
122     }
123 
124     /**
125      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getResultSetLimitForLookup(java.lang.Class,
126      *      org.kuali.rice.krad.web.form.LookupForm)
127      *
128      *      If the form is null, only the dataObjectClass will be used to find the LookupView and corresponding
129      *      results set limit.  Since the viewID is not known, the default view will be used.
130      */
131     @Override
132     public Integer getResultSetLimitForLookup(Class<?> dataObjectClass, LookupForm lookupForm) {
133         LookupView lookupView = null;
134         boolean multipleValueSelectSpecifiedOnURL = false;
135 
136         if (ObjectUtils.isNotNull(lookupForm)) {
137             if (lookupForm.isMultipleValuesSelect()) {
138                 multipleValueSelectSpecifiedOnURL = true;
139             }
140 
141             if (!multipleValueSelectSpecifiedOnURL && lookupForm.getViewRequestParameters().containsKey(UifParameters.MULTIPLE_VALUES_SELECT)) {
142                 String multiValueSelect = lookupForm.getViewRequestParameters().get(
143                         UifParameters.MULTIPLE_VALUES_SELECT);
144                 if (multiValueSelect.equalsIgnoreCase("true")) {
145                     multipleValueSelectSpecifiedOnURL = true;
146                 }
147             }
148             lookupView = (LookupView)lookupForm.getView();
149         } else {
150             Map<String, String> indexKey = new HashMap<String, String>();
151             indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
152             indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
153             lookupView = (LookupView)getDataDictionary().getViewByTypeIndex(ViewType.LOOKUP, indexKey);
154         }
155 
156         if (lookupView != null) {
157             if (lookupView.isMultipleValuesSelect() || multipleValueSelectSpecifiedOnURL) {
158                 return lookupView.getMultipleValuesSelectResultSetLimit();
159             } else {
160                 return lookupView.getResultSetLimit();
161             }
162         }
163         return null;
164     }
165 
166     protected DataDictionary getDataDictionary() {
167         return getDataDictionaryService().getDataDictionary();
168     }
169 
170     protected DataDictionaryService getDataDictionaryService() {
171         return this.dataDictionaryService;
172     }
173 
174     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
175         this.dataDictionaryService = dataDictionaryService;
176     }
177 }