View Javadoc

1   /**
2    * Copyright 2005-2011 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.web.form;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.lookup.LookupUtils;
20  import org.kuali.rice.krad.lookup.Lookupable;
21  import org.kuali.rice.krad.uif.UifConstants.ViewType;
22  import org.kuali.rice.krad.uif.view.LookupView;
23  import org.kuali.rice.krad.uif.service.ViewHelperService;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krad.util.KRADUtils;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Form class for <code>LookupView</code> screens
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class LookupForm extends UifFormBase {
40      private static final long serialVersionUID = -7323484966538685327L;
41  
42      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupForm.class);
43  
44      private String dataObjectClassName;
45      private String docNum;
46      private String referencesToRefresh;
47  
48      private boolean multipleValuesSelect;
49      private String lookupCollectionName;
50  
51      private Map<String, String> criteriaFields;
52      private Map<String, String> fieldConversions;
53  
54      private boolean atLeastOneRowReturnable;
55      private boolean atLeastOneRowHasActions;
56  
57      private Collection<?> searchResults;
58  
59      public LookupForm() {
60          super();
61  
62          setViewTypeName(ViewType.LOOKUP);
63          atLeastOneRowReturnable = false;
64          atLeastOneRowHasActions = false;
65          multipleValuesSelect = false;
66  
67          criteriaFields = new HashMap<String, String>();
68          fieldConversions = new HashMap<String, String>();
69      }
70  
71      /**
72       * Picks out business object name from the request to get retrieve a
73       * lookupable and set properties
74       */
75      @Override
76      public void postBind(HttpServletRequest request) {
77          super.postBind(request);
78  
79          try {
80              Lookupable lookupable = getLookupable();
81              if (lookupable == null) {
82                  LOG.error("Lookupable not found for view id " + getView().getId());
83                  throw new RuntimeException("Lookupable not found for view id " + getView().getId());
84              }
85  
86              if (StringUtils.isBlank(getDataObjectClassName())) {
87                  setDataObjectClassName(((LookupView) getView()).getDataObjectClassName().getName());
88              }
89  
90              // init lookupable with data object class
91              Class<?> dataObjectClass = Class.forName(getDataObjectClassName());
92              lookupable.setDataObjectClass(dataObjectClass);
93  
94              // if showMaintenanceLinks is not already true, only show maintenance links
95              // if the lookup was called from the home application view
96              if (!((LookupView) getView()).isShowMaintenanceLinks()) {
97                  // TODO replace with check to history
98                  if (StringUtils.contains(getReturnLocation(), "/" + KRADConstants.PORTAL_ACTION) ||
99                          StringUtils.contains(getReturnLocation(), "/index.html")) {
100                     ((LookupView) getView()).setShowMaintenanceLinks(true);
101                 }
102             }
103 
104             // populate lookup read only fields list on lookupable
105             lookupable.setReadOnlyFieldsList(getReadOnlyFieldsList());
106 
107             // populate field conversions list
108             if (request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER) != null) {
109                 String conversionFields = request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER);
110                 setFieldConversions(KRADUtils.convertStringParameterToMap(conversionFields));
111                 lookupable.setFieldConversions(getFieldConversions());
112             }
113 
114             // perform upper casing of lookup parameters
115             Map<String, String> fieldValues = new HashMap<String, String>();
116             Map<String, String> formFields = getCriteriaFields();
117 
118             if (formFields != null) {
119                 for (Map.Entry<String, String> entry : formFields.entrySet()) {
120                     // check here to see if this field is a criteria element on the form
121                     fieldValues.put(entry.getKey(),
122                             LookupUtils.forceUppercase(dataObjectClass, entry.getKey(), entry.getValue()));
123                 }
124             }
125 
126             // fieldValues.put(UifParameters.RETURN_FORM_KEY, getReturnFormKey());
127             // fieldValues.put(UifParameters.RETURN_LOCATION, getReturnLocation());
128             if (StringUtils.isNotBlank(getDocNum())) {
129                 fieldValues.put(KRADConstants.DOC_NUM, getDocNum());
130             }
131 
132             this.setCriteriaFields(fieldValues);
133         } catch (ClassNotFoundException e) {
134             LOG.error("Object class " + getDataObjectClassName() + " not found");
135             throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e);
136         }
137     }
138 
139     public Lookupable getLookupable() {
140         ViewHelperService viewHelperService = getView().getViewHelperService();
141         if (viewHelperService == null) {
142             LOG.error("ViewHelperService is null.");
143             throw new RuntimeException("ViewHelperService is null.");
144         }
145 
146         if (!Lookupable.class.isAssignableFrom(viewHelperService.getClass())) {
147             LOG.error("ViewHelperService class '" + viewHelperService.getClass().getName() +
148                     "' is not assignable from '" + Lookupable.class + "'");
149             throw new RuntimeException("ViewHelperService class '" + viewHelperService.getClass().getName() +
150                     "' is not assignable from '" + Lookupable.class + "'");
151         }
152 
153         return (Lookupable) viewHelperService;
154     }
155 
156     protected Boolean processBooleanParameter(String parameterValue) {
157         if (StringUtils.isNotBlank(parameterValue)) {
158             if ("YES".equals(parameterValue.toUpperCase())) {
159                 return Boolean.TRUE;
160             }
161             return new Boolean(parameterValue);
162         }
163         return null;
164     }
165 
166     public String getDataObjectClassName() {
167         return this.dataObjectClassName;
168     }
169 
170     public void setDataObjectClassName(String dataObjectClassName) {
171         this.dataObjectClassName = dataObjectClassName;
172     }
173 
174     public String getDocNum() {
175         return this.docNum;
176     }
177 
178     public void setDocNum(String docNum) {
179         this.docNum = docNum;
180     }
181 
182     public String getReferencesToRefresh() {
183         return referencesToRefresh;
184     }
185 
186     public void setReferencesToRefresh(String referencesToRefresh) {
187         this.referencesToRefresh = referencesToRefresh;
188     }
189 
190     /**
191      * Indicates whether multiple values select should be enabled for the lookup
192      *
193      * <p>
194      * When set to true, the select field is enabled for the lookup results group that allows the user
195      * to select one or more rows for returning
196      * </p>
197      *
198      * @return boolean true if multiple values should be enabled, false otherwise
199      */
200     public boolean isMultipleValuesSelect() {
201         return multipleValuesSelect;
202     }
203 
204     /**
205      * Setter for the multiple values select indicator
206      *
207      * @param multipleValuesSelect
208      */
209     public void setMultipleValuesSelect(boolean multipleValuesSelect) {
210         this.multipleValuesSelect = multipleValuesSelect;
211     }
212 
213     /**
214      * For the case of multi-value lookup, indicates the collection that should be populated with
215      * the return results
216      *
217      * @return String collection name (must be full binding path)
218      */
219     public String getLookupCollectionName() {
220         return lookupCollectionName;
221     }
222 
223     /**
224      * Setter for the name of the collection that should be populated with lookup results
225      *
226      * @param lookupCollectionName
227      */
228     public void setLookupCollectionName(String lookupCollectionName) {
229         this.lookupCollectionName = lookupCollectionName;
230     }
231 
232     public Map<String, String> getCriteriaFields() {
233         return this.criteriaFields;
234     }
235 
236     public void setCriteriaFields(Map<String, String> criteriaFields) {
237         this.criteriaFields = criteriaFields;
238     }
239 
240     public Map<String, String> getFieldConversions() {
241         return this.fieldConversions;
242     }
243 
244     public void setFieldConversions(Map<String, String> fieldConversions) {
245         this.fieldConversions = fieldConversions;
246     }
247 
248     public Collection<?> getSearchResults() {
249         return this.searchResults;
250     }
251 
252     public void setSearchResults(Collection<?> searchResults) {
253         this.searchResults = searchResults;
254     }
255 
256     public boolean isAtLeastOneRowReturnable() {
257         return atLeastOneRowReturnable;
258     }
259 
260     public void setAtLeastOneRowReturnable(boolean atLeastOneRowReturnable) {
261         this.atLeastOneRowReturnable = atLeastOneRowReturnable;
262     }
263 
264     public boolean isAtLeastOneRowHasActions() {
265         return atLeastOneRowHasActions;
266     }
267 
268     public void setAtLeastOneRowHasActions(boolean atLeastOneRowHasActions) {
269         this.atLeastOneRowHasActions = atLeastOneRowHasActions;
270     }
271 }