View Javadoc
1   /**
2    * Copyright 2005-2015 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 org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.krad.datadictionary.DataDictionary;
21  import org.kuali.rice.krad.inquiry.Inquirable;
22  import org.kuali.rice.krad.lookup.LookupUtils;
23  import org.kuali.rice.krad.service.DataDictionaryService;
24  import org.kuali.rice.krad.uif.UifConstants;
25  import org.kuali.rice.krad.uif.UifConstants.ViewType;
26  import org.kuali.rice.krad.uif.UifParameters;
27  import org.kuali.rice.krad.uif.UifPropertyPaths;
28  import org.kuali.rice.krad.uif.service.ViewDictionaryService;
29  import org.kuali.rice.krad.uif.util.ViewModelUtils;
30  import org.kuali.rice.krad.lookup.LookupView;
31  import org.kuali.rice.krad.uif.view.View;
32  import org.kuali.rice.krad.uif.view.ViewSessionPolicy;
33  import org.kuali.rice.krad.util.KRADUtils;
34  import org.kuali.rice.krad.lookup.LookupForm;
35  import org.springframework.beans.PropertyValues;
36  import org.springframework.util.Assert;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  /**
42   * Implementation of <code>ViewDictionaryService</code>
43   *
44   * <p>
45   * Pulls view entries from the data dictionary to implement the various query
46   * methods
47   * </p>
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  public class ViewDictionaryServiceImpl implements ViewDictionaryService {
52      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(
53              ViewDictionaryServiceImpl.class);
54  
55      private DataDictionaryService dataDictionaryService;
56  
57      /**
58       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getInquirable(java.lang.Class,
59       *      java.lang.String)
60       */
61      public Inquirable getInquirable(Class<?> dataObjectClass, String viewName) {
62          Inquirable inquirable = null;
63  
64          if (StringUtils.isBlank(viewName)) {
65              viewName = UifConstants.DEFAULT_VIEW_NAME;
66          }
67  
68          Map<String, String> indexKey = new HashMap<String, String>();
69          indexKey.put(UifParameters.VIEW_NAME, viewName);
70          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
71  
72          // get view properties
73          PropertyValues propertyValues = getDataDictionary().getViewPropertiesByType(ViewType.INQUIRY, indexKey);
74  
75          String viewHelperServiceClassName = ViewModelUtils.getStringValFromPVs(propertyValues,
76                  UifPropertyPaths.VIEW_HELPER_SERVICE_CLASS);
77          if (StringUtils.isNotBlank(viewHelperServiceClassName)) {
78              try {
79                  inquirable = (Inquirable) KRADUtils.createNewObjectFromClass(Class.forName(viewHelperServiceClassName));
80              } catch (ClassNotFoundException e) {
81                  throw new RiceRuntimeException(
82                          "Unable to find class for inquirable classname: " + viewHelperServiceClassName, e);
83              }
84          }
85  
86          return inquirable;
87      }
88  
89      /**
90       * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isInquirable(java.lang.Class)
91       */
92      public boolean isInquirable(Class<?> dataObjectClass) {
93          Map<String, String> indexKey = new HashMap<String, String>();
94          indexKey.put(UifParameters.VIEW_NAME, UifConstants.DEFAULT_VIEW_NAME);
95          indexKey.put(UifParameters.DATA_OBJECT_CLASS_NAME, dataObjectClass.getName());
96  
97          boolean isInquirable = getDataDictionary().viewByTypeExist(ViewType.INQUIRY, indexKey);
98  
99          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 }