Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LookupForm |
|
| 1.8846153846153846;1.885 |
1 | /* | |
2 | * Copyright 2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.web.spring.form; | |
17 | ||
18 | import java.util.Collection; | |
19 | import java.util.HashMap; | |
20 | import java.util.List; | |
21 | import java.util.Map; | |
22 | ||
23 | import javax.servlet.http.HttpServletRequest; | |
24 | ||
25 | import org.apache.commons.lang.StringUtils; | |
26 | import org.kuali.rice.kns.lookup.LookupUtils; | |
27 | import org.kuali.rice.kns.uif.UifParameters; | |
28 | import org.kuali.rice.kns.uif.UifConstants.ViewType; | |
29 | import org.kuali.rice.kns.uif.container.View; | |
30 | import org.kuali.rice.kns.uif.service.LookupViewHelperService; | |
31 | import org.kuali.rice.kns.uif.service.ViewHelperService; | |
32 | import org.kuali.rice.kns.util.KNSConstants; | |
33 | ||
34 | /** | |
35 | * This class is the model for Lookups | |
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 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryForm.class); |
43 | ||
44 | private String dataObjectClassName; | |
45 | private String docNum; | |
46 | private Map<String, String> criteriaFields; | |
47 | private Map<String, String> criteriaFieldsForLookup; | |
48 | private String conversionFields; | |
49 | private Map<String, String> fieldConversions; | |
50 | 0 | private boolean suppressActions = false; |
51 | 0 | private boolean hideReturnLink = false; |
52 | ||
53 | private Collection<?> searchResults; | |
54 | ||
55 | public LookupForm() { | |
56 | 0 | super(); |
57 | 0 | setViewTypeName(ViewType.LOOKUP); |
58 | 0 | } |
59 | ||
60 | public String getDataObjectClassName() { | |
61 | 0 | return this.dataObjectClassName; |
62 | } | |
63 | ||
64 | public void setDataObjectClassName(String dataObjectClassName) { | |
65 | 0 | this.dataObjectClassName = dataObjectClassName; |
66 | 0 | } |
67 | ||
68 | public String getDocNum() { | |
69 | 0 | return this.docNum; |
70 | } | |
71 | ||
72 | public void setDocNum(String docNum) { | |
73 | 0 | this.docNum = docNum; |
74 | 0 | } |
75 | ||
76 | public Map<String, String> getCriteriaFields() { | |
77 | 0 | return this.criteriaFields; |
78 | } | |
79 | ||
80 | public void setCriteriaFields(Map<String, String> criteriaFields) { | |
81 | 0 | this.criteriaFields = criteriaFields; |
82 | 0 | } |
83 | ||
84 | public Map<String, String> getCriteriaFieldsForLookup() { | |
85 | 0 | return this.criteriaFieldsForLookup; |
86 | } | |
87 | ||
88 | public void setCriteriaFieldsForLookup(Map<String, String> criteriaFieldsForLookup) { | |
89 | 0 | this.criteriaFieldsForLookup = criteriaFieldsForLookup; |
90 | 0 | } |
91 | ||
92 | public String getConversionFields() { | |
93 | 0 | return this.conversionFields; |
94 | } | |
95 | ||
96 | public void setConversionFields(String conversionFields) { | |
97 | 0 | this.conversionFields = conversionFields; |
98 | 0 | } |
99 | ||
100 | public Map<String, String> getFieldConversions() { | |
101 | 0 | return this.fieldConversions; |
102 | } | |
103 | ||
104 | public void setFieldConversions(Map<String, String> fieldConversions) { | |
105 | 0 | this.fieldConversions = fieldConversions; |
106 | 0 | } |
107 | ||
108 | public Collection<?> getSearchResults() { | |
109 | 0 | return this.searchResults; |
110 | } | |
111 | ||
112 | public void setSearchResults(Collection<?> searchResults) { | |
113 | 0 | this.searchResults = searchResults; |
114 | 0 | } |
115 | ||
116 | /** | |
117 | * @param suppressActions The suppressActions to set. | |
118 | */ | |
119 | public void setSuppressActions(boolean suppressActions) { | |
120 | 0 | this.suppressActions = suppressActions; |
121 | 0 | } |
122 | ||
123 | /** | |
124 | * @return Returns the suppressActions. | |
125 | */ | |
126 | public boolean isSuppressActions() { | |
127 | 0 | return suppressActions; |
128 | } | |
129 | ||
130 | /** | |
131 | * @return Returns the hideReturnLink. | |
132 | */ | |
133 | public boolean isHideReturnLink() { | |
134 | 0 | return hideReturnLink; |
135 | } | |
136 | ||
137 | /** | |
138 | * @param hideReturnLink The hideReturnLink to set. | |
139 | */ | |
140 | public void setHideReturnLink(boolean hideReturnLink) { | |
141 | 0 | this.hideReturnLink = hideReturnLink; |
142 | 0 | } |
143 | ||
144 | protected LookupViewHelperService getLookupViewHelperServiceFromModel(View view) { | |
145 | 0 | ViewHelperService viewHelperService = view.getViewHelperService(); |
146 | 0 | if (viewHelperService == null) { |
147 | 0 | LOG.error("ViewHelperService is null."); |
148 | 0 | throw new RuntimeException("ViewHelperService is null."); |
149 | } | |
150 | 0 | if (!LookupViewHelperService.class.isAssignableFrom(viewHelperService.getClass())) { |
151 | 0 | LOG.error("ViewHelperService class '" + viewHelperService.getClass().getName() + "' is not assignable from '" + LookupViewHelperService.class + "'"); |
152 | 0 | throw new RuntimeException("ViewHelperService class '" + viewHelperService.getClass().getName() + "' is not assignable from '" + LookupViewHelperService.class + "'"); |
153 | } | |
154 | 0 | return (LookupViewHelperService) viewHelperService; |
155 | } | |
156 | ||
157 | /** | |
158 | * Picks out business object name from the request to get retrieve a | |
159 | * lookupable and set properties. | |
160 | */ | |
161 | @Override | |
162 | public void postBind(HttpServletRequest request) { | |
163 | 0 | super.postBind(request); |
164 | ||
165 | try { | |
166 | /* | |
167 | * TODO delyea - Investigate to make sure below retrieval takes into | |
168 | * account the following: 1) handle externalizable business objects | |
169 | * 2) allow for lookupableImpl bean id to be passed in via request | |
170 | * parameter KNSConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME | |
171 | */ | |
172 | // ViewHelperService localLookupable = getView().getViewHelperService(); | |
173 | 0 | LookupViewHelperService localLookupViewHelperService = getLookupViewHelperServiceFromModel(getView()); |
174 | ||
175 | 0 | if (localLookupViewHelperService == null) { |
176 | 0 | LOG.error("LookupViewHelperService not found for view id " + getView().getId()); |
177 | 0 | throw new RuntimeException("LookupViewHelperService not found for view id " + getView().getId()); |
178 | } | |
179 | // set parameters on lookupable | |
180 | /* | |
181 | * TODO: delyea - this setter used to get multipart form data | |
182 | * related to KNSConstants.UPLOADED_FILE_REQUEST_ATTRIBUTE_KEY | |
183 | * request attribute (see PojoFormBase.populate() method for more | |
184 | * info) | |
185 | */ | |
186 | // localLookupable.setParameters(request.getParameterMap()); | |
187 | ||
188 | // check the doc form key is empty before setting so we don't | |
189 | // override a restored lookup form | |
190 | 0 | if (request.getAttribute(KNSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) { |
191 | 0 | setFormKey((String) request.getAttribute(KNSConstants.DOC_FORM_KEY)); |
192 | 0 | } else if (request.getParameter(KNSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) { |
193 | 0 | setFormKey(request.getParameter(KNSConstants.DOC_FORM_KEY)); |
194 | } | |
195 | ||
196 | // if (request.getParameter(KNSConstants.DOC_NUM) != null) { | |
197 | // setDocNum(request.getParameter(KNSConstants.DOC_NUM)); | |
198 | // } | |
199 | ||
200 | // this used to be in the Form as a property but has been moved for KRAD | |
201 | 0 | Boolean showMaintenanceLinks = Boolean.valueOf(request.getParameter("showMaintenanceLinks")); |
202 | // if showMaintenanceLinks is not already true, only show maintenance links if the lookup was called from the portal (or index.html for the generated applications) | |
203 | 0 | if (!showMaintenanceLinks.booleanValue()) { |
204 | // TODO delyea - is this the best way to decide whether to display the maintenance actions? | |
205 | 0 | if (StringUtils.contains(getReturnLocation(), "/"+KNSConstants.PORTAL_ACTION) |
206 | || StringUtils.contains(getReturnLocation(), "/index.html")) { | |
207 | 0 | showMaintenanceLinks = Boolean.TRUE; |
208 | } | |
209 | } | |
210 | 0 | localLookupViewHelperService.setShowMaintenanceLinks(showMaintenanceLinks.booleanValue()); |
211 | ||
212 | // this used to be in the Form as a property but has been moved for KRAD | |
213 | // String hideReturnLink = request.getParameter("hideReturnLink"); | |
214 | // Boolean hideReturnLinkValue = processBooleanParameter(hideReturnLink); | |
215 | // if (hideReturnLinkValue != null) { | |
216 | // localLookupViewHelperService.setHideReturnLink(hideReturnLinkValue.booleanValue()); | |
217 | // } | |
218 | ||
219 | // if (request.getParameter("conversionFields") != null) { | |
220 | // setConversionFields(request.getParameter("conversionFields")); | |
221 | // } | |
222 | // String value = request.getParameter("multipleValues"); | |
223 | // if (value != null) { | |
224 | // if ("YES".equals(value.toUpperCase())) { | |
225 | // setMultipleValues(true); | |
226 | // } else { | |
227 | // setMultipleValues(new Boolean(request.getParameter("multipleValues")).booleanValue()); | |
228 | // } | |
229 | // } | |
230 | // if (request.getParameter(KNSConstants.REFERENCES_TO_REFRESH) != null) { | |
231 | // setReferencesToRefresh(request.getParameter(KNSConstants.REFERENCES_TO_REFRESH)); | |
232 | // } | |
233 | ||
234 | 0 | if (request.getParameter(KNSConstants.LOOKUP_READ_ONLY_FIELDS) != null) { |
235 | 0 | setReadOnlyFields(request.getParameter("readOnlyFields")); |
236 | 0 | setReadOnlyFieldsList(LookupUtils.translateReadOnlyFieldsToList(getReadOnlyFields())); |
237 | 0 | localLookupViewHelperService.setReadOnlyFieldsList(getReadOnlyFieldsList()); |
238 | } | |
239 | ||
240 | // init lookupable with bo class | |
241 | 0 | Class<?> boClass = Class.forName(getDataObjectClassName()); |
242 | 0 | localLookupViewHelperService.setDataObjectClass(boClass); |
243 | 0 | Map<String, String> fieldValues = new HashMap<String, String>(); |
244 | 0 | Map<String, String> formFields = getCriteriaFields(); |
245 | ||
246 | 0 | if (formFields != null) { |
247 | 0 | for (Map.Entry<String, String> entry : formFields.entrySet()) { |
248 | // check here to see if this field is a criteria element on the form | |
249 | 0 | fieldValues.put(entry.getKey(), LookupUtils.forceUppercase(boClass, entry.getKey(), entry.getValue())); |
250 | } | |
251 | } | |
252 | ||
253 | // populate values into the localLookupable Field list | |
254 | // for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) { | |
255 | // Row row = (Row) iter.next(); | |
256 | // | |
257 | // for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { | |
258 | // Field field = (Field) iterator.next(); | |
259 | // | |
260 | // // check whether form already has value for field | |
261 | // if (formFields != null && formFields.containsKey(field.getPropertyName())) { | |
262 | // field.setPropertyValue(formFields.get(field.getPropertyName())); | |
263 | // } | |
264 | // | |
265 | // field.setPropertyValue(LookupUtils.forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue())); | |
266 | // fieldValues.put(field.getPropertyName(), field.getPropertyValue()); | |
267 | // localLookupable.applyFieldAuthorizationsFromNestedLookups(field); | |
268 | // } | |
269 | // } | |
270 | ||
271 | // check the lookupableImpl to see if there are additional fields | |
272 | // if (localLookupable.checkForAdditionalFields(fieldValues)) { | |
273 | // // populate values into the localLookupable Field list again because additional rows may have been added | |
274 | // for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) { | |
275 | // Row row = (Row) iter.next(); | |
276 | // | |
277 | // for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { | |
278 | // Field field = (Field) iterator.next(); | |
279 | // | |
280 | // // check whether form already has value for field | |
281 | // if (formFields != null && formFields.containsKey(field.getPropertyName())) { | |
282 | // field.setPropertyValue(formFields.get(field.getPropertyName())); | |
283 | // } | |
284 | // | |
285 | // // override values with request | |
286 | // if (request.getParameter(field.getPropertyName()) != null) { | |
287 | // if (!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) { | |
288 | // field.setPropertyValue(request.getParameter(field.getPropertyName()).trim()); | |
289 | // } else { | |
290 | // // multi value, set to values | |
291 | // field.setPropertyValues(request.getParameterValues(field.getPropertyName())); | |
292 | // } | |
293 | // } | |
294 | // fieldValues.put(field.getPropertyName(), field.getPropertyValue()); | |
295 | // } | |
296 | // } | |
297 | // | |
298 | // } | |
299 | 0 | fieldValues.put(UifParameters.RETURN_FORM_KEY, getReturnFormKey()); |
300 | 0 | fieldValues.put(UifParameters.RETURN_LOCATION, getReturnLocation()); |
301 | 0 | if (StringUtils.isNotBlank(getDocNum())) { |
302 | 0 | fieldValues.put(KNSConstants.DOC_NUM, getDocNum()); |
303 | } | |
304 | // if (StringUtils.isNotBlank(getReferencesToRefresh())) { | |
305 | // fieldValues.put(KNSConstants.REFERENCES_TO_REFRESH, this.getReferencesToRefresh()); | |
306 | // } | |
307 | ||
308 | 0 | this.setCriteriaFields(fieldValues); |
309 | ||
310 | 0 | setFieldConversions(LookupUtils.translateFieldConversions(getConversionFields())); |
311 | 0 | localLookupViewHelperService.setFieldConversions(getFieldConversions()); |
312 | 0 | localLookupViewHelperService.setDocNum(getDocNum()); |
313 | 0 | localLookupViewHelperService.setSuppressActions(isSuppressActions()); |
314 | 0 | localLookupViewHelperService.setHideReturnLink(isHideReturnLink()); |
315 | // setLookupViewHelperService(localLookupViewHelperService); | |
316 | 0 | setCriteriaFieldsForLookup(fieldValues); |
317 | ||
318 | // if showMaintenanceLinks is not already true, only show | |
319 | // maintenance links if the lookup was called from the portal (or | |
320 | // index.html for the generated applications) | |
321 | // TODO delyea - how to handle whether to show maintenance links or return links | |
322 | // if (!isShowMaintenanceLinks()) { | |
323 | // if (StringUtils.contains(getBackLocation(), "/" + KNSConstants.PORTAL_ACTION) || StringUtils.contains(getBackLocation(), "/index.html")) { | |
324 | // showMaintenanceLinks = true; | |
325 | // } | |
326 | // } | |
327 | 0 | } catch (ClassNotFoundException e) { |
328 | 0 | LOG.error("Object class " + getDataObjectClassName() + " not found"); |
329 | 0 | throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e); |
330 | 0 | } |
331 | 0 | } |
332 | ||
333 | protected Boolean processBooleanParameter(String parameterValue) { | |
334 | 0 | if (StringUtils.isNotBlank(parameterValue)) { |
335 | 0 | if ("YES".equals(parameterValue.toUpperCase())) { |
336 | 0 | return Boolean.TRUE; |
337 | } | |
338 | 0 | return new Boolean(parameterValue); |
339 | } | |
340 | 0 | return null; |
341 | } | |
342 | ||
343 | /** | |
344 | * BELOW COPIED FROM LookupForm | |
345 | */ | |
346 | ||
347 | private String readOnlyFields; | |
348 | private List<String> readOnlyFieldsList; | |
349 | ||
350 | public String getReadOnlyFields() { | |
351 | 0 | return this.readOnlyFields; |
352 | } | |
353 | ||
354 | public void setReadOnlyFields(String readOnlyFields) { | |
355 | 0 | this.readOnlyFields = readOnlyFields; |
356 | 0 | } |
357 | ||
358 | public List<String> getReadOnlyFieldsList() { | |
359 | 0 | return this.readOnlyFieldsList; |
360 | } | |
361 | ||
362 | public void setReadOnlyFieldsList(List<String> readOnlyFieldsList) { | |
363 | 0 | this.readOnlyFieldsList = readOnlyFieldsList; |
364 | 0 | } |
365 | ||
366 | } |