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.uif.view.View;
34  import org.kuali.rice.krad.uif.view.ViewSessionPolicy;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  import org.kuali.rice.krad.web.form.LookupForm;
37  import org.springframework.beans.PropertyValues;
38  import org.springframework.util.Assert;
39  
40  /**
41   * Implementation of <code>ViewDictionaryService</code>
42   *
43   * <p>
44   * Pulls view entries from the data dictionary to implement the various query
45   * methods
46   * </p>
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class ViewDictionaryServiceImpl implements ViewDictionaryService {
51      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
52              ViewDictionaryServiceImpl.class);
53  
54      private DataDictionaryService dataDictionaryService;
55  
56      /**
57       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getInquirable(java.lang.Class,
58       *      java.lang.String)
59       */
60      public Inquirable getInquirable(Class<?> dataObjectClass, String viewName) {
61          Inquirable inquirable = null;
62  
63          if (StringUtils.isBlank(viewName)) {
64              viewName = UifConstants.DEFAULT_VIEW_NAME;
65          }
66  
67          Map<String, String> indexKey = new HashMap<String, String>();
68          indexKey.put(UifParameters.VIEW_NAME, viewName);
69          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
70  
71          // get view properties
72          PropertyValues propertyValues = getDataDictionary().getViewPropertiesByType(ViewType.INQUIRY, indexKey);
73  
74          String viewHelperServiceClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
75                  UifPropertyPaths.VIEW_HELPER_SERVICE_CLASS);
76          if (StringUtils.isNotBlank(viewHelperServiceClassName)) {
77              try {
78                  inquirable = (Inquirable) ObjectUtils.newInstance(Class.forName(viewHelperServiceClassName));
79              } catch (ClassNotFoundException e) {
80                  throw new RiceRuntimeException(
81                          "Unable to find class for inquirable classname: " + viewHelperServiceClassName, e);
82              }
83          }
84  
85          return inquirable;
86      }
87  
88      /**
89       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isInquirable(java.lang.Class)
90       */
91      public boolean isInquirable(Class<?> dataObjectClass) {
92          Map<String, String> indexKey = new HashMap<String, String>();
93          indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
94          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
95  
96          boolean isInquirable = getDataDictionary().viewByTypeExist(ViewType.INQUIRY, indexKey);
97  
98          return isInquirable;
99      }
100 
101     /**
102      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isLookupable(java.lang.Class)
103      */
104     public boolean isLookupable(Class<?> dataObjectClass) {
105         Map<String, String> indexKey = new HashMap<String, String>();
106         indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
107         indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
108 
109         boolean isLookupable = getDataDictionary().viewByTypeExist(ViewType.LOOKUP, indexKey);
110 
111         return isLookupable;
112     }
113 
114     /**
115      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isMaintainable(java.lang.Class)
116      */
117     public boolean isMaintainable(Class<?> dataObjectClass) {
118         Map<String, String> indexKey = new HashMap<String, String>();
119         indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
120         indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
121 
122         boolean isMaintainable = getDataDictionary().viewByTypeExist(ViewType.MAINTENANCE, indexKey);
123 
124         return isMaintainable;
125     }
126 
127     /**
128      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getResultSetLimitForLookup(java.lang.Class,
129      *      org.kuali.rice.krad.web.form.LookupForm)
130      *
131      *      If the form is null, only the dataObjectClass will be used to find the LookupView and corresponding
132      *      results set limit.  Since the viewID is not known, the default view will be used.
133      */
134     @Override
135     public Integer getResultSetLimitForLookup(Class<?> dataObjectClass, LookupForm lookupForm) {
136         LookupView lookupView = null;
137         boolean multipleValueSelectSpecifiedOnURL = false;
138 
139         if (ObjectUtils.isNotNull(lookupForm)) {
140             if (lookupForm.isMultipleValuesSelect()) {
141                 multipleValueSelectSpecifiedOnURL = true;
142             }
143 
144             if (!multipleValueSelectSpecifiedOnURL && lookupForm.getViewRequestParameters().containsKey(
145                     UifParameters.MULTIPLE_VALUES_SELECT)) {
146                 String multiValueSelect = lookupForm.getViewRequestParameters().get(
147                         UifParameters.MULTIPLE_VALUES_SELECT);
148                 if (multiValueSelect.equalsIgnoreCase("true")) {
149                     multipleValueSelectSpecifiedOnURL = true;
150                 }
151             }
152             lookupView = (LookupView) lookupForm.getView();
153         } else {
154             Map<String, String> indexKey = new HashMap<String, String>();
155             indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
156             indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
157             lookupView = (LookupView) getDataDictionary().getViewByTypeIndex(ViewType.LOOKUP, indexKey);
158         }
159 
160         if (lookupView != null) {
161             if (lookupView.isMultipleValuesSelect() || multipleValueSelectSpecifiedOnURL) {
162                 return lookupView.getMultipleValuesSelectResultSetLimit();
163             } else {
164                 return lookupView.getResultSetLimit();
165             }
166         }
167         return null;
168     }
169 
170     /**
171      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getViewSessionPolicy(java.lang.String)
172      */
173     public ViewSessionPolicy getViewSessionPolicy(String viewId) {
174         Assert.hasLength(viewId, "view id is required for retrieving the view session policy");
175 
176         ViewSessionPolicy viewSessionPolicy = null;
177 
178         View view = getDataDictionary().getImmutableViewById(viewId);
179         if (view != null) {
180             viewSessionPolicy = view.getSessionPolicy();
181         }
182 
183         return viewSessionPolicy;
184     }
185 
186     /**
187      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isSessionStorageEnabled(java.lang.String)
188      */
189     public boolean isSessionStorageEnabled(String viewId) {
190         Assert.hasLength(viewId, "view id is required for retrieving session indicator");
191 
192         boolean sessionStorageEnabled = false;
193 
194         View view = getDataDictionary().getImmutableViewById(viewId);
195         if (view != null) {
196             sessionStorageEnabled = view.isPersistFormToSession();
197         }
198 
199         return sessionStorageEnabled;
200     }
201 
202     protected DataDictionary getDataDictionary() {
203         return getDataDictionaryService().getDataDictionary();
204     }
205 
206     protected DataDictionaryService getDataDictionaryService() {
207         return this.dataDictionaryService;
208     }
209 
210     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
211         this.dataDictionaryService = dataDictionaryService;
212     }
213 }