1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.lookup;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.exception.RiceRuntimeException;
20 import org.kuali.rice.core.api.util.RiceConstants;
21 import org.kuali.rice.core.api.util.RiceKeyConstants;
22 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23 import org.kuali.rice.krad.service.ModuleService;
24 import org.kuali.rice.krad.uif.UifConstants;
25 import org.kuali.rice.krad.uif.UifParameters;
26 import org.kuali.rice.krad.uif.UifPropertyPaths;
27 import org.kuali.rice.krad.util.GlobalVariables;
28 import org.kuali.rice.krad.util.KRADConstants;
29 import org.kuali.rice.krad.util.KRADUtils;
30 import org.kuali.rice.krad.util.UrlFactory;
31 import org.kuali.rice.krad.web.form.UifFormBase;
32 import org.kuali.rice.krad.web.service.ModelAndViewService;
33 import org.kuali.rice.krad.web.service.impl.ControllerServiceImpl;
34 import org.springframework.web.servlet.ModelAndView;
35 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
36
37 import javax.servlet.http.HttpServletRequest;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Properties;
45 import java.util.Set;
46
47
48
49
50
51
52 public class LookupControllerServiceImpl extends ControllerServiceImpl implements LookupControllerService {
53
54 private ModelAndViewService modelAndViewService;
55
56
57
58
59 @Override
60 public ModelAndView start(UifFormBase form) {
61 LookupForm lookupForm = (LookupForm) form;
62
63 Lookupable lookupable = lookupForm.getLookupable();
64 if (lookupable == null) {
65 throw new RuntimeException("Lookupable is null");
66 }
67
68 HttpServletRequest request = form.getRequest();
69 if (request.getParameter(UifParameters.MESSAGE_TO_DISPLAY) != null) {
70 GlobalVariables.getMessageMap().putErrorForSectionId(UifConstants.MessageKeys.LOOKUP_RESULT_MESSAGES,
71 request.getParameter(UifParameters.MESSAGE_TO_DISPLAY));
72 }
73
74 if (!lookupForm.isRedirectedLookup()) {
75 ModelAndView redirectModelAndView = checkForModuleLookupRedirect(lookupForm, request);
76 if (redirectModelAndView != null) {
77 return redirectModelAndView;
78 }
79 }
80
81 String dialogId = request.getParameter(UifParameters.DIALOG_ID);
82 if (dialogId != null) {
83 lookupForm.setShowDialogId(dialogId);
84 }
85
86 return super.start(lookupForm);
87 }
88
89
90
91
92
93
94
95
96
97 protected ModelAndView checkForModuleLookupRedirect(LookupForm lookupForm, HttpServletRequest request) {
98 Class<?> lookupObjectClass;
99 try {
100 lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
101 } catch (ClassNotFoundException e) {
102 throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName(),
103 e);
104 }
105
106 ModuleService responsibleModuleService =
107 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
108 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
109 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
110 KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
111
112 Properties redirectUrlProps = new Properties();
113 redirectUrlProps.setProperty(UifParameters.REDIRECTED_LOOKUP, "true");
114
115
116 GlobalVariables.getUifFormManager().removeSessionForm(lookupForm);
117
118 return getModelAndViewService().performRedirect(lookupForm, lookupUrl, redirectUrlProps);
119 }
120
121 return null;
122 }
123
124
125
126
127
128
129
130 @Override
131 public ModelAndView search(LookupForm lookupForm) {
132 Lookupable lookupable = lookupForm.getLookupable();
133 if (lookupable == null) {
134 throw new RuntimeException("Lookupable is null.");
135 }
136
137 Collection<?> displayList = lookupable.performSearch(lookupForm, lookupForm.getLookupCriteria(), true);
138
139 lookupForm.setLookupResults(displayList);
140
141 return getModelAndViewService().getModelAndView(lookupForm);
142 }
143
144
145
146
147
148
149
150 @Override
151 public ModelAndView clearValues(LookupForm lookupForm) {
152 Lookupable lookupable = lookupForm.getLookupable();
153 if (lookupable == null) {
154 throw new RuntimeException("Lookupable is null.");
155 }
156
157 Map<String, String> resetLookupCriteria = lookupable.performClear(lookupForm, lookupForm.getLookupCriteria());
158
159 lookupForm.setLookupCriteria(resetLookupCriteria);
160
161 return getModelAndViewService().getModelAndView(lookupForm);
162 }
163
164
165
166
167
168
169
170
171 @Override
172 public ModelAndView selectAllPages(LookupForm lookupForm) {
173 List<? extends Object> lookupResults = (List<? extends Object>) lookupForm.getLookupResults();
174
175 List<String> fromFieldNames = new ArrayList<String>(lookupForm.getFieldConversions().keySet());
176
177
178 Set<String> selectedValues = new HashSet<String>();
179 for (Object lineItem : lookupResults) {
180 String lineIdentifier = LookupUtils.generateMultiValueKey(lineItem, fromFieldNames);
181
182 selectedValues.add(lineIdentifier);
183 }
184
185 lookupForm.setSelectedLookupResultsCache(selectedValues);
186
187 return getModelAndViewService().getModelAndView(lookupForm);
188 }
189
190
191
192
193
194
195
196 @Override
197 public ModelAndView deselectAllPages(LookupForm lookupForm) {
198 lookupForm.getSelectedLookupResultsCache().clear();
199
200 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.LOOKUP_RESULTS);
201 if (selectedLines != null) {
202 selectedLines.clear();
203 }
204
205 return getModelAndViewService().getModelAndView(lookupForm);
206 }
207
208
209
210
211
212
213
214
215
216
217
218 @Override
219 public String returnSelected(LookupForm lookupForm, RedirectAttributes redirectAttributes) {
220 LookupUtils.refreshLookupResultSelections(lookupForm);
221
222 Properties urlParams = buildReturnSelectedParameters(lookupForm);
223 String returnUrl = UrlFactory.parameterizeUrl(lookupForm.getReturnLocation(), urlParams);
224
225 boolean lookupCameFromDifferentServer = KRADUtils.areDifferentDomains(lookupForm.getReturnLocation(),
226 lookupForm.getRequestUrl());
227
228 boolean urlGreaterThanMaxLength = returnUrl.length() > RiceConstants.MAXIMUM_URL_LENGTH;
229 if (urlGreaterThanMaxLength) {
230
231 urlParams.remove(UifParameters.SELECTED_LINE_VALUES);
232
233
234
235 if (lookupCameFromDifferentServer) {
236 urlParams.setProperty(UifParameters.REFRESH_STATUS, UifConstants.RefreshStatus.ERROR);
237 urlParams.setProperty(UifParameters.MESSAGE_TO_DISPLAY,
238 RiceKeyConstants.INFO_LOOKUP_RESULTS_MV_RETURN_EXCEEDS_LIMIT);
239 } else {
240
241 String selectedLineValues = getSelectedLineValues(lookupForm);
242 redirectAttributes.addFlashAttribute(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
243 }
244 }
245
246 GlobalVariables.getUifFormManager().removeSessionForm(lookupForm);
247
248
249 returnUrl = UrlFactory.parameterizeUrl(lookupForm.getReturnLocation(), urlParams);
250
251 return UifConstants.REDIRECT_PREFIX + returnUrl;
252 }
253
254
255
256
257
258
259
260 protected Properties buildReturnSelectedParameters(LookupForm lookupForm) {
261 Properties parameters = new Properties();
262
263 parameters.setProperty(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
264 parameters.setProperty(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
265
266 if (StringUtils.isNotBlank(lookupForm.getView().getId())) {
267 parameters.setProperty(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
268 }
269
270 if (StringUtils.isNotBlank(lookupForm.getDataObjectClassName())) {
271 parameters.setProperty(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
272 }
273
274 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
275 parameters.setProperty(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
276 }
277
278 String multiValueReturnFieldsParam = getMultiValueReturnFields(lookupForm);
279 parameters.setProperty(UifParameters.MULIT_VALUE_RETURN_FILEDS, multiValueReturnFieldsParam);
280
281 String selectedLineValues = getSelectedLineValues(lookupForm);
282 parameters.setProperty(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
283
284 if (StringUtils.isNotBlank(lookupForm.getQuickfinderId())) {
285 parameters.setProperty(UifParameters.QUICKFINDER_ID, lookupForm.getQuickfinderId());
286 }
287
288 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
289 parameters.setProperty(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
290 }
291
292 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionId())) {
293 parameters.setProperty(UifParameters.LOOKUP_COLLECTION_ID, lookupForm.getLookupCollectionId());
294 }
295
296 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
297 parameters.setProperty(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
298 }
299
300 if (StringUtils.isNotBlank(lookupForm.getShowDialogId())) {
301 parameters.setProperty(UifParameters.DIALOG_ID, lookupForm.getShowDialogId());
302 }
303
304 return parameters;
305 }
306
307
308
309
310
311
312
313 protected String getMultiValueReturnFields(LookupForm lookupForm) {
314 String multiValueReturnFieldsParam = "";
315
316 List<String> multiValueReturnFields = lookupForm.getMultiValueReturnFields();
317 Collections.sort(multiValueReturnFields);
318 if (multiValueReturnFields != null && !multiValueReturnFields.isEmpty()) {
319 for (String field : multiValueReturnFields) {
320 multiValueReturnFieldsParam += field + ",";
321 }
322
323 multiValueReturnFieldsParam = StringUtils.removeEnd(multiValueReturnFieldsParam, ",");
324 }
325
326 return multiValueReturnFieldsParam;
327 }
328
329
330
331
332
333
334
335 protected String getSelectedLineValues(LookupForm lookupForm) {
336 String selectedLineValues = "";
337
338 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.LOOKUP_RESULTS);
339 if (selectedLines != null) {
340 for (String selectedLine : selectedLines) {
341 selectedLineValues += selectedLine + ",";
342 }
343
344 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
345 }
346
347 return selectedLineValues;
348 }
349
350
351
352
353
354
355 protected ModelAndViewService getModelAndViewService() {
356 return modelAndViewService;
357 }
358
359
360
361
362 public void setModelAndViewService(ModelAndViewService modelAndViewService) {
363 this.modelAndViewService = modelAndViewService;
364 }
365 }