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