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 org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.krad.data.DataObjectUtils;
21  import org.kuali.rice.krad.datadictionary.DataDictionary;
22  import org.kuali.rice.krad.inquiry.Inquirable;
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.uif.view.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.web.form.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) DataObjectUtils.newInstance(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.web.form.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 = null;
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         if (lookupView != null) {
162             if (lookupView.isMultipleValuesSelect() || multipleValueSelectSpecifiedOnURL) {
163                 return lookupView.getMultipleValuesSelectResultSetLimit();
164             } else {
165                 return lookupView.getResultSetLimit();
166             }
167         }
168         return null;
169     }
170 
171     /**
172      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#getViewSessionPolicy(java.lang.String)
173      */
174     public ViewSessionPolicy getViewSessionPolicy(String viewId) {
175         Assert.hasLength(viewId, "view id is required for retrieving the view session policy");
176 
177         ViewSessionPolicy viewSessionPolicy = null;
178 
179         View view = getDataDictionary().getImmutableViewById(viewId);
180         if (view != null) {
181             viewSessionPolicy = view.getSessionPolicy();
182         }
183 
184         return viewSessionPolicy;
185     }
186 
187     /**
188      * @see org.kuali.rice.krad.uif.service.ViewDictionaryService#isSessionStorageEnabled(java.lang.String)
189      */
190     public boolean isSessionStorageEnabled(String viewId) {
191         Assert.hasLength(viewId, "view id is required for retrieving session indicator");
192 
193         boolean sessionStorageEnabled = false;
194 
195         View view = getDataDictionary().getImmutableViewById(viewId);
196         if (view != null) {
197             sessionStorageEnabled = view.isPersistFormToSession();
198         }
199 
200         return sessionStorageEnabled;
201     }
202 
203     protected DataDictionary getDataDictionary() {
204         return getDataDictionaryService().getDataDictionary();
205     }
206 
207     protected DataDictionaryService getDataDictionaryService() {
208         return this.dataDictionaryService;
209     }
210 
211     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
212         this.dataDictionaryService = dataDictionaryService;
213     }
214 }