View Javadoc

1   /**
2    * Copyright 2005-2014 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      private boolean redirectedLookup;
60  
61      public LookupForm() {
62          super();
63  
64          setViewTypeName(ViewType.LOOKUP);
65          atLeastOneRowReturnable = false;
66          atLeastOneRowHasActions = false;
67          multipleValuesSelect = false;
68          redirectedLookup = false;
69  
70          criteriaFields = new HashMap<String, String>();
71          fieldConversions = new HashMap<String, String>();
72      }
73  
74      /**
75       * Picks out business object name from the request to get retrieve a
76       * lookupable and set properties
77       */
78      @Override
79      public void postBind(HttpServletRequest request) {
80          super.postBind(request);
81  
82          try {
83              Lookupable lookupable = getLookupable();
84              if (lookupable == null) {
85                  // assume lookupable will be set by controller or a redirect will happen
86                  return;
87              }
88  
89              if (StringUtils.isBlank(getDataObjectClassName())) {
90                  setDataObjectClassName(((LookupView) getView()).getDataObjectClassName().getName());
91              }
92  
93              // init lookupable with data object class
94              Class<?> dataObjectClass = Class.forName(getDataObjectClassName());
95              lookupable.setDataObjectClass(dataObjectClass);
96  
97              // if showMaintenanceLinks is not already true, only show maintenance links
98              // if the lookup was called from the home application view
99              if (!((LookupView) getView()).isShowMaintenanceLinks()) {
100                 // TODO replace with check to history
101                 if (StringUtils.contains(getReturnLocation(), "/" + KRADConstants.PORTAL_ACTION) ||
102                         StringUtils.contains(getReturnLocation(), "/index.html")) {
103                     ((LookupView) getView()).setShowMaintenanceLinks(true);
104                 }
105             }
106 
107             // populate lookup read only fields list on lookupable
108             lookupable.setReadOnlyFieldsList(getReadOnlyFieldsList());
109 
110             // populate field conversions list
111             if (request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER) != null) {
112                 String conversionFields = request.getParameter(KRADConstants.CONVERSION_FIELDS_PARAMETER);
113                 setFieldConversions(KRADUtils.convertStringParameterToMap(conversionFields));
114                 lookupable.setFieldConversions(getFieldConversions());
115             }
116 
117             // perform upper casing of lookup parameters
118             Map<String, String> fieldValues = new HashMap<String, String>();
119             Map<String, String> formFields = getCriteriaFields();
120 
121             if (formFields != null) {
122                 for (Map.Entry<String, String> entry : formFields.entrySet()) {
123                     // check here to see if this field is a criteria element on the form
124                     fieldValues.put(entry.getKey(),
125                             LookupUtils.forceUppercase(dataObjectClass, entry.getKey(), entry.getValue()));
126                 }
127             }
128 
129             // fieldValues.put(UifParameters.RETURN_FORM_KEY, getReturnFormKey());
130             // fieldValues.put(UifParameters.RETURN_LOCATION, getReturnLocation());
131             if (StringUtils.isNotBlank(getDocNum())) {
132                 fieldValues.put(KRADConstants.DOC_NUM, getDocNum());
133             }
134 
135             this.setCriteriaFields(fieldValues);
136         } catch (ClassNotFoundException e) {
137             LOG.error("Object class " + getDataObjectClassName() + " not found");
138             throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e);
139         }
140     }
141 
142     public Lookupable getLookupable() {
143         if ((getView() != null) && Lookupable.class.isAssignableFrom(getView().getViewHelperService().getClass())) {
144             return (Lookupable) getView().getViewHelperService();
145         }
146 
147         return null;
148     }
149 
150     public String getDataObjectClassName() {
151         return this.dataObjectClassName;
152     }
153 
154     public void setDataObjectClassName(String dataObjectClassName) {
155         this.dataObjectClassName = dataObjectClassName;
156     }
157 
158     public String getDocNum() {
159         return this.docNum;
160     }
161 
162     public void setDocNum(String docNum) {
163         this.docNum = docNum;
164     }
165 
166     public String getReferencesToRefresh() {
167         return referencesToRefresh;
168     }
169 
170     public void setReferencesToRefresh(String referencesToRefresh) {
171         this.referencesToRefresh = referencesToRefresh;
172     }
173 
174     /**
175      * Indicates whether multiple values select should be enabled for the lookup
176      *
177      * <p>
178      * When set to true, the select field is enabled for the lookup results group that allows the user
179      * to select one or more rows for returning
180      * </p>
181      *
182      * @return boolean true if multiple values should be enabled, false otherwise
183      */
184     public boolean isMultipleValuesSelect() {
185         return multipleValuesSelect;
186     }
187 
188     /**
189      * Setter for the multiple values select indicator
190      *
191      * @param multipleValuesSelect
192      */
193     public void setMultipleValuesSelect(boolean multipleValuesSelect) {
194         this.multipleValuesSelect = multipleValuesSelect;
195     }
196 
197     /**
198      * For the case of multi-value lookup, indicates the collection that should be populated with
199      * the return results
200      *
201      * @return String collection name (must be full binding path)
202      */
203     public String getLookupCollectionName() {
204         return lookupCollectionName;
205     }
206 
207     /**
208      * Setter for the name of the collection that should be populated with lookup results
209      *
210      * @param lookupCollectionName
211      */
212     public void setLookupCollectionName(String lookupCollectionName) {
213         this.lookupCollectionName = lookupCollectionName;
214     }
215 
216     public Map<String, String> getCriteriaFields() {
217         return this.criteriaFields;
218     }
219 
220     public void setCriteriaFields(Map<String, String> criteriaFields) {
221         this.criteriaFields = criteriaFields;
222     }
223 
224     public Map<String, String> getFieldConversions() {
225         return this.fieldConversions;
226     }
227 
228     public void setFieldConversions(Map<String, String> fieldConversions) {
229         this.fieldConversions = fieldConversions;
230     }
231 
232     public Collection<?> getSearchResults() {
233         return this.searchResults;
234     }
235 
236     public void setSearchResults(Collection<?> searchResults) {
237         this.searchResults = searchResults;
238     }
239 
240     public boolean isAtLeastOneRowReturnable() {
241         return atLeastOneRowReturnable;
242     }
243 
244     public void setAtLeastOneRowReturnable(boolean atLeastOneRowReturnable) {
245         this.atLeastOneRowReturnable = atLeastOneRowReturnable;
246     }
247 
248     public boolean isAtLeastOneRowHasActions() {
249         return atLeastOneRowHasActions;
250     }
251 
252     public void setAtLeastOneRowHasActions(boolean atLeastOneRowHasActions) {
253         this.atLeastOneRowHasActions = atLeastOneRowHasActions;
254     }
255 
256     /**
257      * Indicates whether the requested was redirected from the lookup framework due to an external object
258      * request. This prevents the framework from performing another redirect check
259      *
260      * @return boolean true if request was a redirect, false if not
261      */
262     public boolean isRedirectedLookup() {
263         return redirectedLookup;
264     }
265 
266     /**
267      * Setter for the redirected request indicator
268      *
269      * @param redirectedLookup
270      */
271     public void setRedirectedLookup(boolean redirectedLookup) {
272         this.redirectedLookup = redirectedLookup;
273     }
274 }