View Javadoc
1   /**
2    * Copyright 2005-2016 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.util.KRADConstants;
24  import org.kuali.rice.krad.util.KRADUtils;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * Form class for <code>LookupView</code> screens
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class LookupForm extends UifFormBase {
37      private static final long serialVersionUID = -7323484966538685327L;
38  
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupForm.class);
40  
41      private String dataObjectClassName;
42      private String docNum;
43      private String referencesToRefresh;
44  
45      private boolean multipleValuesSelect;
46      private String lookupCollectionName;
47  
48      private Map<String, String> lookupCriteria;
49      private Map<String, String> fieldConversions;
50  
51      private Collection<?> lookupResults;
52  
53      private boolean atLeastOneRowReturnable;
54      private boolean atLeastOneRowHasActions;
55  
56      private boolean redirectedLookup;
57  
58      public LookupForm() {
59          super();
60  
61          setViewTypeName(ViewType.LOOKUP);
62          atLeastOneRowReturnable = false;
63          atLeastOneRowHasActions = false;
64          multipleValuesSelect = false;
65          redirectedLookup = false;
66  
67          lookupCriteria = 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 on the initial request
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                  // assume lookupable will be set by controller or a redirect will happen
83                  return;
84              }
85  
86              if (StringUtils.isBlank(getDataObjectClassName())) {
87                  setDataObjectClassName(((LookupView) getView()).getDataObjectClassName().getName());
88              }
89              Class<?> dataObjectClass = Class.forName(getDataObjectClassName());
90              lookupable.setDataObjectClass(dataObjectClass);
91  
92              if (request.getMethod().equals("GET")) {
93                  // populate field conversions list
94                  if (request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER) != null) {
95                      String conversionFields = request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER);
96                      setFieldConversions(KRADUtils.convertStringParameterToMap(conversionFields));
97                  }
98  
99                  // perform upper casing of lookup parameters
100                 Map<String, String> fieldValues = new HashMap<String, String>();
101                 if (getLookupCriteria() != null) {
102                     for (Map.Entry<String, String> entry : getLookupCriteria().entrySet()) {
103                         // check here to see if this field is a criteria element on the form
104                         fieldValues.put(entry.getKey(), LookupUtils.forceUppercase(dataObjectClass, entry.getKey(),
105                                 entry.getValue()));
106                     }
107                 }
108 
109                 if (StringUtils.isNotBlank(getDocNum())) {
110                     fieldValues.put(KRADConstants.DOC_NUM, getDocNum());
111                 }
112 
113                 this.setLookupCriteria(fieldValues);
114             }
115         } catch (ClassNotFoundException e) {
116             LOG.error("Object class " + getDataObjectClassName() + " not found");
117             throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e);
118         }
119     }
120 
121     public Lookupable getLookupable() {
122         if ((getView() != null) && (getView().getViewHelperService() != null) && Lookupable.class.isAssignableFrom(
123                 getView().getViewHelperService().getClass())) {
124             return (Lookupable) getView().getViewHelperService();
125         } else if ((getPostedView() != null) && (getPostedView().getViewHelperService() != null) && Lookupable.class
126                 .isAssignableFrom(getPostedView().getViewHelperService().getClass())) {
127             return (Lookupable) getPostedView().getViewHelperService();
128         }
129 
130         return null;
131     }
132 
133     public String getDataObjectClassName() {
134         return this.dataObjectClassName;
135     }
136 
137     public void setDataObjectClassName(String dataObjectClassName) {
138         this.dataObjectClassName = dataObjectClassName;
139     }
140 
141     public String getDocNum() {
142         return this.docNum;
143     }
144 
145     public void setDocNum(String docNum) {
146         this.docNum = docNum;
147     }
148 
149     public String getReferencesToRefresh() {
150         return referencesToRefresh;
151     }
152 
153     public void setReferencesToRefresh(String referencesToRefresh) {
154         this.referencesToRefresh = referencesToRefresh;
155     }
156 
157     /**
158      * Indicates whether multiple values select should be enabled for the lookup
159      *
160      * <p>
161      * When set to true, the select field is enabled for the lookup results group that allows the user
162      * to select one or more rows for returning
163      * </p>
164      *
165      * @return boolean true if multiple values should be enabled, false otherwise
166      */
167     public boolean isMultipleValuesSelect() {
168         return multipleValuesSelect;
169     }
170 
171     /**
172      * Setter for the multiple values select indicator
173      *
174      * @param multipleValuesSelect
175      */
176     public void setMultipleValuesSelect(boolean multipleValuesSelect) {
177         this.multipleValuesSelect = multipleValuesSelect;
178     }
179 
180     /**
181      * For the case of multi-value lookup, indicates the collection that should be populated with
182      * the return results
183      *
184      * @return String collection name (must be full binding path)
185      */
186     public String getLookupCollectionName() {
187         return lookupCollectionName;
188     }
189 
190     /**
191      * Setter for the name of the collection that should be populated with lookup results
192      *
193      * @param lookupCollectionName
194      */
195     public void setLookupCollectionName(String lookupCollectionName) {
196         this.lookupCollectionName = lookupCollectionName;
197     }
198 
199     public Map<String, String> getLookupCriteria() {
200         return this.lookupCriteria;
201     }
202 
203     public void setLookupCriteria(Map<String, String> lookupCriteria) {
204         this.lookupCriteria = lookupCriteria;
205     }
206 
207     public Map<String, String> getFieldConversions() {
208         return this.fieldConversions;
209     }
210 
211     public void setFieldConversions(Map<String, String> fieldConversions) {
212         this.fieldConversions = fieldConversions;
213     }
214 
215     public Collection<?> getLookupResults() {
216         return this.lookupResults;
217     }
218 
219     public void setLookupResults(Collection<?> lookupResults) {
220         this.lookupResults = lookupResults;
221     }
222 
223     public boolean isAtLeastOneRowReturnable() {
224         return atLeastOneRowReturnable;
225     }
226 
227     public void setAtLeastOneRowReturnable(boolean atLeastOneRowReturnable) {
228         this.atLeastOneRowReturnable = atLeastOneRowReturnable;
229     }
230 
231     public boolean isAtLeastOneRowHasActions() {
232         return atLeastOneRowHasActions;
233     }
234 
235     public void setAtLeastOneRowHasActions(boolean atLeastOneRowHasActions) {
236         this.atLeastOneRowHasActions = atLeastOneRowHasActions;
237     }
238 
239     /**
240      * Indicates whether the requested was redirected from the lookup framework due to an external object
241      * request. This prevents the framework from performing another redirect check
242      *
243      * @return boolean true if request was a redirect, false if not
244      */
245     public boolean isRedirectedLookup() {
246         return redirectedLookup;
247     }
248 
249     /**
250      * Setter for the redirected request indicator
251      *
252      * @param redirectedLookup
253      */
254     public void setRedirectedLookup(boolean redirectedLookup) {
255         this.redirectedLookup = redirectedLookup;
256     }
257 }