Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LookupForm |
|
| 1.8636363636363635;1.864 |
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.UifConstants.ViewType; | |
28 | import org.kuali.rice.kns.uif.UifParameters; | |
29 | import org.kuali.rice.kns.uif.container.LookupView; | |
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 | ||
51 | private Collection<?> searchResults; | |
52 | ||
53 | public LookupForm() { | |
54 | 0 | super(); |
55 | 0 | setViewTypeName(ViewType.LOOKUP); |
56 | 0 | } |
57 | ||
58 | public String getDataObjectClassName() { | |
59 | 0 | return this.dataObjectClassName; |
60 | } | |
61 | ||
62 | public void setDataObjectClassName(String dataObjectClassName) { | |
63 | 0 | this.dataObjectClassName = dataObjectClassName; |
64 | 0 | } |
65 | ||
66 | public String getDocNum() { | |
67 | 0 | return this.docNum; |
68 | } | |
69 | ||
70 | public void setDocNum(String docNum) { | |
71 | 0 | this.docNum = docNum; |
72 | 0 | } |
73 | ||
74 | public Map<String, String> getCriteriaFields() { | |
75 | 0 | return this.criteriaFields; |
76 | } | |
77 | ||
78 | public void setCriteriaFields(Map<String, String> criteriaFields) { | |
79 | 0 | this.criteriaFields = criteriaFields; |
80 | 0 | } |
81 | ||
82 | public Map<String, String> getCriteriaFieldsForLookup() { | |
83 | 0 | return this.criteriaFieldsForLookup; |
84 | } | |
85 | ||
86 | public void setCriteriaFieldsForLookup(Map<String, String> criteriaFieldsForLookup) { | |
87 | 0 | this.criteriaFieldsForLookup = criteriaFieldsForLookup; |
88 | 0 | } |
89 | ||
90 | public String getConversionFields() { | |
91 | 0 | return this.conversionFields; |
92 | } | |
93 | ||
94 | public void setConversionFields(String conversionFields) { | |
95 | 0 | this.conversionFields = conversionFields; |
96 | 0 | } |
97 | ||
98 | public Map<String, String> getFieldConversions() { | |
99 | 0 | return this.fieldConversions; |
100 | } | |
101 | ||
102 | public void setFieldConversions(Map<String, String> fieldConversions) { | |
103 | 0 | this.fieldConversions = fieldConversions; |
104 | 0 | } |
105 | ||
106 | public Collection<?> getSearchResults() { | |
107 | 0 | return this.searchResults; |
108 | } | |
109 | ||
110 | public void setSearchResults(Collection<?> searchResults) { | |
111 | 0 | this.searchResults = searchResults; |
112 | 0 | } |
113 | ||
114 | protected LookupViewHelperService getLookupViewHelperServiceFromModel() { | |
115 | 0 | ViewHelperService viewHelperService = getView().getViewHelperService(); |
116 | 0 | if (viewHelperService == null) { |
117 | 0 | LOG.error("ViewHelperService is null."); |
118 | 0 | throw new RuntimeException("ViewHelperService is null."); |
119 | } | |
120 | 0 | if (!LookupViewHelperService.class.isAssignableFrom(viewHelperService.getClass())) { |
121 | 0 | LOG.error("ViewHelperService class '" + viewHelperService.getClass().getName() + "' is not assignable from '" + LookupViewHelperService.class + "'"); |
122 | 0 | throw new RuntimeException("ViewHelperService class '" + viewHelperService.getClass().getName() + "' is not assignable from '" + LookupViewHelperService.class + "'"); |
123 | } | |
124 | 0 | return (LookupViewHelperService) viewHelperService; |
125 | } | |
126 | ||
127 | /** | |
128 | * Picks out business object name from the request to get retrieve a | |
129 | * lookupable and set properties. | |
130 | */ | |
131 | @Override | |
132 | public void postBind(HttpServletRequest request) { | |
133 | 0 | super.postBind(request); |
134 | ||
135 | try { | |
136 | 0 | LookupViewHelperService localLookupViewHelperService = getLookupViewHelperServiceFromModel(); |
137 | ||
138 | 0 | if (localLookupViewHelperService == null) { |
139 | 0 | LOG.error("LookupViewHelperService not found for view id " + getView().getId()); |
140 | 0 | throw new RuntimeException("LookupViewHelperService not found for view id " + getView().getId()); |
141 | } | |
142 | ||
143 | // check the doc form key is empty before setting so we don't | |
144 | // override a restored lookup form | |
145 | // if (request.getAttribute(KNSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) { | |
146 | // setFormKey((String) request.getAttribute(KNSConstants.DOC_FORM_KEY)); | |
147 | // } else if (request.getParameter(KNSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) { | |
148 | // setFormKey(request.getParameter(KNSConstants.DOC_FORM_KEY)); | |
149 | // } | |
150 | ||
151 | // 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) | |
152 | 0 | if (!((LookupView)getView()).isShowMaintenanceLinks()) { |
153 | // TODO delyea - is this the best way to decide whether to display the maintenance actions? | |
154 | 0 | if (StringUtils.contains(getReturnLocation(), "/"+KNSConstants.PORTAL_ACTION) |
155 | || StringUtils.contains(getReturnLocation(), "/index.html")) { | |
156 | 0 | ((LookupView)getView()).setShowMaintenanceLinks(true); |
157 | } | |
158 | } | |
159 | ||
160 | // this used to be in the Form as a property but has been moved for KRAD | |
161 | // String hideReturnLink = request.getParameter("hideReturnLink"); | |
162 | // Boolean hideReturnLinkValue = processBooleanParameter(hideReturnLink); | |
163 | // if (hideReturnLinkValue != null) { | |
164 | // localLookupViewHelperService.setHideReturnLink(hideReturnLinkValue.booleanValue()); | |
165 | // } | |
166 | ||
167 | // if (request.getParameter("conversionFields") != null) { | |
168 | // setConversionFields(request.getParameter("conversionFields")); | |
169 | // } | |
170 | // String value = request.getParameter("multipleValues"); | |
171 | // if (value != null) { | |
172 | // if ("YES".equals(value.toUpperCase())) { | |
173 | // setMultipleValues(true); | |
174 | // } else { | |
175 | // setMultipleValues(new Boolean(request.getParameter("multipleValues")).booleanValue()); | |
176 | // } | |
177 | // } | |
178 | // if (request.getParameter(KNSConstants.REFERENCES_TO_REFRESH) != null) { | |
179 | // setReferencesToRefresh(request.getParameter(KNSConstants.REFERENCES_TO_REFRESH)); | |
180 | // } | |
181 | ||
182 | 0 | if (request.getParameter(KNSConstants.LOOKUP_READ_ONLY_FIELDS) != null) { |
183 | 0 | setReadOnlyFields(request.getParameter(KNSConstants.LOOKUP_READ_ONLY_FIELDS)); |
184 | 0 | setReadOnlyFieldsList(LookupUtils.translateReadOnlyFieldsToList(getReadOnlyFields())); |
185 | 0 | localLookupViewHelperService.setReadOnlyFieldsList(getReadOnlyFieldsList()); |
186 | } | |
187 | ||
188 | // init lookupable with bo class | |
189 | 0 | Class<?> boClass = Class.forName(getDataObjectClassName()); |
190 | 0 | localLookupViewHelperService.setDataObjectClass(boClass); |
191 | 0 | Map<String, String> fieldValues = new HashMap<String, String>(); |
192 | 0 | Map<String, String> formFields = getCriteriaFields(); |
193 | ||
194 | 0 | if (formFields != null) { |
195 | 0 | for (Map.Entry<String, String> entry : formFields.entrySet()) { |
196 | // check here to see if this field is a criteria element on the form | |
197 | 0 | fieldValues.put(entry.getKey(), LookupUtils.forceUppercase(boClass, entry.getKey(), entry.getValue())); |
198 | } | |
199 | } | |
200 | ||
201 | // populate values into the localLookupable Field list | |
202 | // for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) { | |
203 | // Row row = (Row) iter.next(); | |
204 | // | |
205 | // for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { | |
206 | // Field field = (Field) iterator.next(); | |
207 | // | |
208 | // // check whether form already has value for field | |
209 | // if (formFields != null && formFields.containsKey(field.getPropertyName())) { | |
210 | // field.setPropertyValue(formFields.get(field.getPropertyName())); | |
211 | // } | |
212 | // | |
213 | // field.setPropertyValue(LookupUtils.forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue())); | |
214 | // fieldValues.put(field.getPropertyName(), field.getPropertyValue()); | |
215 | // localLookupable.applyFieldAuthorizationsFromNestedLookups(field); | |
216 | // } | |
217 | // } | |
218 | ||
219 | // check the lookupableImpl to see if there are additional fields | |
220 | // if (localLookupable.checkForAdditionalFields(fieldValues)) { | |
221 | // // populate values into the localLookupable Field list again because additional rows may have been added | |
222 | // for (Iterator iter = localLookupable.getRows().iterator(); iter.hasNext();) { | |
223 | // Row row = (Row) iter.next(); | |
224 | // | |
225 | // for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { | |
226 | // Field field = (Field) iterator.next(); | |
227 | // | |
228 | // // check whether form already has value for field | |
229 | // if (formFields != null && formFields.containsKey(field.getPropertyName())) { | |
230 | // field.setPropertyValue(formFields.get(field.getPropertyName())); | |
231 | // } | |
232 | // | |
233 | // // override values with request | |
234 | // if (request.getParameter(field.getPropertyName()) != null) { | |
235 | // if (!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) { | |
236 | // field.setPropertyValue(request.getParameter(field.getPropertyName()).trim()); | |
237 | // } else { | |
238 | // // multi value, set to values | |
239 | // field.setPropertyValues(request.getParameterValues(field.getPropertyName())); | |
240 | // } | |
241 | // } | |
242 | // fieldValues.put(field.getPropertyName(), field.getPropertyValue()); | |
243 | // } | |
244 | // } | |
245 | // | |
246 | // } | |
247 | 0 | fieldValues.put(UifParameters.RETURN_FORM_KEY, getReturnFormKey()); |
248 | 0 | fieldValues.put(UifParameters.RETURN_LOCATION, getReturnLocation()); |
249 | 0 | if (StringUtils.isNotBlank(getDocNum())) { |
250 | 0 | fieldValues.put(KNSConstants.DOC_NUM, getDocNum()); |
251 | } | |
252 | // if (StringUtils.isNotBlank(getReferencesToRefresh())) { | |
253 | // fieldValues.put(KNSConstants.REFERENCES_TO_REFRESH, this.getReferencesToRefresh()); | |
254 | // } | |
255 | ||
256 | 0 | this.setCriteriaFields(fieldValues); |
257 | ||
258 | 0 | setFieldConversions(LookupUtils.translateFieldConversions(getConversionFields())); |
259 | 0 | localLookupViewHelperService.setFieldConversions(getFieldConversions()); |
260 | 0 | localLookupViewHelperService.setDocNum(getDocNum()); |
261 | // setLookupViewHelperService(localLookupViewHelperService); | |
262 | 0 | setCriteriaFieldsForLookup(fieldValues); |
263 | ||
264 | // if showMaintenanceLinks is not already true, only show | |
265 | // maintenance links if the lookup was called from the portal (or | |
266 | // index.html for the generated applications) | |
267 | // TODO delyea - how to handle whether to show maintenance links or return links | |
268 | // if (!isShowMaintenanceLinks()) { | |
269 | // if (StringUtils.contains(getBackLocation(), "/" + KNSConstants.PORTAL_ACTION) || StringUtils.contains(getBackLocation(), "/index.html")) { | |
270 | // showMaintenanceLinks = true; | |
271 | // } | |
272 | // } | |
273 | 0 | } catch (ClassNotFoundException e) { |
274 | 0 | LOG.error("Object class " + getDataObjectClassName() + " not found"); |
275 | 0 | throw new RuntimeException("Object class " + getDataObjectClassName() + " not found", e); |
276 | 0 | } |
277 | 0 | } |
278 | ||
279 | protected Boolean processBooleanParameter(String parameterValue) { | |
280 | 0 | if (StringUtils.isNotBlank(parameterValue)) { |
281 | 0 | if ("YES".equals(parameterValue.toUpperCase())) { |
282 | 0 | return Boolean.TRUE; |
283 | } | |
284 | 0 | return new Boolean(parameterValue); |
285 | } | |
286 | 0 | return null; |
287 | } | |
288 | ||
289 | /** | |
290 | * BELOW COPIED FROM LookupForm | |
291 | */ | |
292 | ||
293 | private String readOnlyFields; | |
294 | private List<String> readOnlyFieldsList; | |
295 | ||
296 | public String getReadOnlyFields() { | |
297 | 0 | return this.readOnlyFields; |
298 | } | |
299 | ||
300 | public void setReadOnlyFields(String readOnlyFields) { | |
301 | 0 | this.readOnlyFields = readOnlyFields; |
302 | 0 | } |
303 | ||
304 | public List<String> getReadOnlyFieldsList() { | |
305 | 0 | return this.readOnlyFieldsList; |
306 | } | |
307 | ||
308 | public void setReadOnlyFieldsList(List<String> readOnlyFieldsList) { | |
309 | 0 | this.readOnlyFieldsList = readOnlyFieldsList; |
310 | 0 | } |
311 | ||
312 | } |