1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.action;
17
18 import org.apache.commons.beanutils.BeanComparator;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.kuali.rice.core.api.util.RiceConstants;
24 import org.kuali.rice.kns.lookup.HtmlData;
25 import org.kuali.rice.kns.lookup.LookupResultsService;
26 import org.kuali.rice.kns.lookup.LookupUtils;
27 import org.kuali.rice.kns.lookup.Lookupable;
28 import org.kuali.rice.kns.service.KNSServiceLocator;
29 import org.kuali.rice.kns.web.struts.form.MultipleValueLookupForm;
30 import org.kuali.rice.kns.web.ui.Column;
31 import org.kuali.rice.kns.web.ui.ResultRow;
32 import org.kuali.rice.krad.lookup.CollectionIncomplete;
33 import org.kuali.rice.krad.service.KRADServiceLocator;
34 import org.kuali.rice.krad.service.SequenceAccessorService;
35 import org.kuali.rice.krad.util.GlobalVariables;
36 import org.kuali.rice.krad.util.KRADConstants;
37 import org.kuali.rice.krad.util.UrlFactory;
38
39 import javax.servlet.ServletException;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Properties;
50 import java.util.Set;
51
52
53
54
55 public class KualiMultipleValueLookupAction extends KualiLookupAction implements KualiTableRenderAction {
56 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiMultipleValueLookupAction.class);
57
58
59
60
61
62
63
64 public static final int DEFAULT_MAX_ROWS_PER_PAGE = 50;
65
66
67
68
69
70
71
72
73
74
75
76
77 @Override
78 public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
79 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
80
81
82 String methodToCall = findMethodToCall(form, request);
83 if (methodToCall.equalsIgnoreCase("search")) {
84 GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
85 }
86
87 Lookupable kualiLookupable = multipleValueLookupForm.getLookupable();
88 if (kualiLookupable == null) {
89 LOG.error("Lookupable is null.");
90 throw new RuntimeException("Lookupable is null.");
91 }
92
93 Collection displayList = new ArrayList();
94 ArrayList<ResultRow> resultTable = new ArrayList<ResultRow>();
95
96
97 kualiLookupable.validateSearchParameters(multipleValueLookupForm.getFields());
98
99 boolean bounded = true;
100
101 displayList = performMultipleValueLookup(multipleValueLookupForm, resultTable, getMaxRowsPerPage(multipleValueLookupForm), bounded);
102 if (kualiLookupable.isSearchUsingOnlyPrimaryKeyValues()) {
103 multipleValueLookupForm.setSearchUsingOnlyPrimaryKeyValues(true);
104 multipleValueLookupForm.setPrimaryKeyFieldLabels(kualiLookupable.getPrimaryKeyFieldLabels());
105 }
106 else {
107 multipleValueLookupForm.setSearchUsingOnlyPrimaryKeyValues(false);
108 multipleValueLookupForm.setPrimaryKeyFieldLabels(KRADConstants.EMPTY_STRING);
109 }
110
111
112
113 if ( displayList instanceof CollectionIncomplete ){
114 request.setAttribute("reqSearchResultsActualSize", ((CollectionIncomplete) displayList).getActualSizeIfTruncated());
115 } else {
116 request.setAttribute("reqSearchResultsActualSize", displayList.size() );
117 }
118
119 request.setAttribute("reqSearchResults", resultTable);
120
121
122
123 if ( displayList instanceof CollectionIncomplete ){
124 multipleValueLookupForm.setResultsActualSize((int) ((CollectionIncomplete) displayList).getActualSizeIfTruncated().longValue());
125 } else {
126 multipleValueLookupForm.setResultsActualSize(displayList.size());
127 }
128
129
130 multipleValueLookupForm.setResultsLimitedSize(resultTable.size());
131
132 if (request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY) != null) {
133 GlobalVariables.getUserSession().removeObject(request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
134 }
135 request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable, KRADConstants.SEARCH_LIST_KEY_PREFIX));
136
137 request.getParameter(KRADConstants.REFRESH_CALLER);
138
139 return mapping.findForward(RiceConstants.MAPPING_BASIC);
140 }
141
142
143
144
145
146
147
148
149
150
151
152 @Override
153 public ActionForward switchToPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
154 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
155 List<ResultRow> resultTable = switchToPage(multipleValueLookupForm, getMaxRowsPerPage(multipleValueLookupForm));
156 request.setAttribute("reqSearchResults", resultTable);
157 return mapping.findForward(RiceConstants.MAPPING_BASIC);
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 @Override
176 public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
177 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
178 List<ResultRow> resultTable = sort(multipleValueLookupForm, getMaxRowsPerPage(multipleValueLookupForm));
179 request.setAttribute("reqSearchResults", resultTable);
180 return mapping.findForward(RiceConstants.MAPPING_BASIC);
181 }
182
183
184
185
186
187
188
189
190
191
192
193 public ActionForward prepareToReturnSelectedResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
194 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
195 if (StringUtils.isBlank(multipleValueLookupForm.getLookupResultsSequenceNumber())) {
196
197 return prepareToReturnNone(mapping, form, request, response);
198 }
199
200 prepareToReturnSelectedResultBOs(multipleValueLookupForm);
201
202
203 Properties parameters = new Properties();
204 parameters.put(KRADConstants.LOOKUP_RESULTS_BO_CLASS_NAME, multipleValueLookupForm.getBusinessObjectClassName());
205 parameters.put(KRADConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, multipleValueLookupForm.getLookupResultsSequenceNumber());
206 parameters.put(KRADConstants.DOC_FORM_KEY, multipleValueLookupForm.getFormKey());
207 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
208 parameters.put(KRADConstants.REFRESH_CALLER, KRADConstants.MULTIPLE_VALUE);
209 parameters.put(KRADConstants.ANCHOR, multipleValueLookupForm.getLookupAnchor());
210 parameters.put(KRADConstants.LOOKED_UP_COLLECTION_NAME, multipleValueLookupForm.getLookedUpCollectionName());
211 if(multipleValueLookupForm.getDocNum() != null){
212 parameters.put(KRADConstants.DOC_NUM, multipleValueLookupForm.getDocNum());
213 }
214
215
216 String backUrl = UrlFactory.parameterizeUrl(multipleValueLookupForm.getBackLocation(), parameters);
217 return new ActionForward(backUrl, true);
218 }
219
220
221
222
223
224
225
226
227
228
229 public ActionForward selectAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
230 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
231 List<ResultRow> resultTable = selectAll(multipleValueLookupForm, getMaxRowsPerPage(multipleValueLookupForm));
232 request.setAttribute("reqSearchResults", resultTable);
233 return mapping.findForward(RiceConstants.MAPPING_BASIC);
234 }
235
236
237
238
239
240
241
242
243
244
245
246 public ActionForward unselectAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
247 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
248 List<ResultRow> resultTable = unselectAll(multipleValueLookupForm, getMaxRowsPerPage(multipleValueLookupForm));
249 request.setAttribute("reqSearchResults", resultTable);
250 return mapping.findForward(RiceConstants.MAPPING_BASIC);
251 }
252
253
254
255
256
257
258 @Override
259 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
260 return prepareToReturnNone(mapping, form, request, response);
261 }
262
263
264
265
266
267
268
269
270
271
272
273 public ActionForward prepareToReturnNone(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
274 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
275 prepareToReturnNone(multipleValueLookupForm);
276
277
278 Properties parameters = new Properties();
279 parameters.put(KRADConstants.DOC_FORM_KEY, multipleValueLookupForm.getFormKey());
280 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
281 parameters.put(KRADConstants.REFRESH_CALLER, KRADConstants.MULTIPLE_VALUE);
282 parameters.put(KRADConstants.ANCHOR, multipleValueLookupForm.getLookupAnchor());
283 if(multipleValueLookupForm.getDocNum() != null){
284 parameters.put(KRADConstants.DOC_NUM, multipleValueLookupForm.getDocNum());
285 }
286 String backUrl = UrlFactory.parameterizeUrl(multipleValueLookupForm.getBackLocation(), parameters);
287 return new ActionForward(backUrl, true);
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301 public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
302 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
303 List<ResultRow> resultTable = prepareToExport(multipleValueLookupForm);
304 request.setAttribute("reqSearchResults", resultTable);
305 return mapping.findForward(RiceConstants.MAPPING_BASIC);
306 }
307
308
309
310
311
312
313
314
315
316
317
318 protected Collection performMultipleValueLookup(MultipleValueLookupForm multipleValueLookupForm, List<ResultRow> resultTable, int maxRowsPerPage, boolean bounded) {
319 Lookupable lookupable = multipleValueLookupForm.getLookupable();
320 Collection displayList = lookupable.performLookup(multipleValueLookupForm, resultTable, bounded);
321
322 List defaultSortColumns = lookupable.getDefaultSortColumns();
323 if (defaultSortColumns != null && !defaultSortColumns.isEmpty() && resultTable != null && !resultTable.isEmpty()) {
324
325 String firstSortColumn = (String) defaultSortColumns.get(0);
326
327
328 int firstSortColumnIdx = -1;
329 List<Column> columnsForFirstResultRow = resultTable.get(0).getColumns();
330 for (int i = 0; i < columnsForFirstResultRow.size(); i++) {
331 if (StringUtils.equals(firstSortColumn, columnsForFirstResultRow.get(i).getPropertyName())) {
332 firstSortColumnIdx = i;
333 break;
334 }
335 }
336 multipleValueLookupForm.setColumnToSortIndex(firstSortColumnIdx);
337 }
338 else {
339
340 multipleValueLookupForm.setColumnToSortIndex(-1);
341 }
342
343
344 multipleValueLookupForm.jumpToFirstPage(resultTable.size(), maxRowsPerPage);
345
346 SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
347 Long nextSeq = sas.getNextAvailableSequenceNumber(KRADConstants.LOOKUP_RESULTS_SEQUENCE);
348 String lookupResultsSequenceNumber = nextSeq.toString();
349 multipleValueLookupForm.setLookupResultsSequenceNumber(lookupResultsSequenceNumber);
350 try {
351 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
352 lookupResultsService.persistResultsTable(lookupResultsSequenceNumber, resultTable,
353 GlobalVariables.getUserSession().getPerson().getPrincipalId());
354 }
355 catch (Exception e) {
356 LOG.error("error occured trying to persist multiple lookup results", e);
357 throw new RuntimeException("error occured trying to persist multiple lookup results");
358 }
359
360
361 multipleValueLookupForm.setCompositeObjectIdMap(new HashMap<String, String>());
362
363 return displayList;
364 }
365
366
367
368
369
370
371
372 protected List<ResultRow> switchToPage(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
373 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
374
375 List<ResultRow> resultTable = null;
376 try {
377 resultTable = KNSServiceLocator.getLookupResultsService().retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
378 }
379 catch (Exception e) {
380 LOG.error("error occured trying to retrieve multiple lookup results", e);
381 throw new RuntimeException("error occured trying to retrieve multiple lookup results");
382 }
383
384 multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getSwitchToPageNumber(), resultTable.size(), maxRowsPerPage);
385
386 multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
387 multipleValueLookupForm.setCompositeObjectIdMap(LookupUtils.generateCompositeSelectedObjectIds(multipleValueLookupForm.getPreviouslySelectedObjectIdSet(),
388 multipleValueLookupForm.getDisplayedObjectIdSet(), multipleValueLookupForm.getSelectedObjectIdSet()));
389 return resultTable;
390 }
391
392
393
394
395
396
397
398
399 protected List<ResultRow> sort(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
400 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
401
402 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
403
404 List<ResultRow> resultTable = null;
405 try {
406 resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
407 }
408 catch (Exception e) {
409 LOG.error("error occured trying to retrieve multiple lookup results", e);
410 throw new RuntimeException("error occured trying to retrieve multiple lookup results");
411 }
412
413 int columnToSortOn = multipleValueLookupForm.getColumnToSortIndex();
414 int columnCurrentlySortedOn = Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex());
415
416
417
418
419 if (columnToSortOn == columnCurrentlySortedOn) {
420
421 Collections.reverse(resultTable);
422 }
423 else {
424
425
426
427
428 Collections.sort(resultTable, new BeanComparator("columns[" + columnToSortOn + "].propertyValue", LookupUtils.findBestValueComparatorForColumn(resultTable, columnToSortOn)));
429 }
430
431
432 try {
433 lookupResultsService.persistResultsTable(lookupResultsSequenceNumber, resultTable,
434 GlobalVariables.getUserSession().getPerson().getPrincipalId());
435 }
436 catch (Exception e) {
437 LOG.error("error occured trying to persist multiple lookup results", e);
438 throw new RuntimeException("error occured trying to persist multiple lookup results");
439 }
440
441
442 multipleValueLookupForm.jumpToFirstPage(resultTable.size(), maxRowsPerPage);
443
444 multipleValueLookupForm.setCompositeObjectIdMap(LookupUtils.generateCompositeSelectedObjectIds(multipleValueLookupForm.getPreviouslySelectedObjectIdSet(),
445 multipleValueLookupForm.getDisplayedObjectIdSet(), multipleValueLookupForm.getSelectedObjectIdSet()));
446 return resultTable;
447 }
448
449
450
451
452
453
454
455 protected void prepareToReturnSelectedResultBOs(MultipleValueLookupForm multipleValueLookupForm) {
456 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
457 if (StringUtils.isBlank(lookupResultsSequenceNumber)) {
458
459 return;
460 }
461 Map<String, String> compositeObjectIdMap = LookupUtils.generateCompositeSelectedObjectIds(multipleValueLookupForm.getPreviouslySelectedObjectIdSet(),
462 multipleValueLookupForm.getDisplayedObjectIdSet(), multipleValueLookupForm.getSelectedObjectIdSet());
463 Set<String> compositeObjectIds = compositeObjectIdMap.keySet();
464 try {
465 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
466 lookupResultsService.persistSelectedObjectIds(lookupResultsSequenceNumber, compositeObjectIds,
467 GlobalVariables.getUserSession().getPerson().getPrincipalId());
468 }
469 catch (Exception e) {
470 LOG.error("error occured trying to retrieve selected multiple lookup results", e);
471 throw new RuntimeException("error occured trying to retrieve selected multiple lookup results");
472 }
473 }
474
475
476
477
478
479
480 protected void prepareToReturnNone(MultipleValueLookupForm multipleValueLookupForm) {
481 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
482 try {
483 if (StringUtils.isNotBlank(lookupResultsSequenceNumber)) {
484
485 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
486 lookupResultsService.clearPersistedLookupResults(lookupResultsSequenceNumber);
487 multipleValueLookupForm.setLookupResultsSequenceNumber(null);
488 }
489 }
490 catch (Exception e) {
491
492 LOG.error("error occured trying to clear lookup results seq nbr " + lookupResultsSequenceNumber, e);
493 }
494 }
495
496
497
498
499
500
501
502
503
504
505 protected List<ResultRow> prepareToExport(MultipleValueLookupForm multipleValueLookupForm) {
506 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
507
508 List<ResultRow> resultTable = null;
509 try {
510 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
511 resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
512 }
513 catch (Exception e) {
514 LOG.error("error occured trying to export multiple lookup results", e);
515 throw new RuntimeException("error occured trying to export multiple lookup results");
516 }
517 return resultTable;
518 }
519
520
521
522
523
524
525
526
527 protected List<ResultRow> selectAll(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
528 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
529
530 List<ResultRow> resultTable = null;
531 try {
532 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
533 resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
534 }
535 catch (Exception e) {
536 LOG.error("error occured trying to export multiple lookup results", e);
537 throw new RuntimeException("error occured trying to export multiple lookup results");
538 }
539
540 Map<String, String> selectedObjectIds = new HashMap<String, String>();
541 for (ResultRow row : resultTable) {
542 String objId = row.getObjectId();
543 HtmlData.InputHtmlData returnUrl = (HtmlData.InputHtmlData) row.getReturnUrlHtmlData();
544 returnUrl.setChecked(HtmlData.InputHtmlData.CHECKBOX_CHECKED_VALUE);
545 row.setReturnUrl(returnUrl.constructCompleteHtmlTag());
546 if(objId != null){
547 selectedObjectIds.put(objId, objId);
548 }
549 }
550
551 multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getViewedPageNumber(), resultTable.size(), maxRowsPerPage);
552 multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
553 multipleValueLookupForm.setCompositeObjectIdMap(selectedObjectIds);
554
555 return resultTable;
556 }
557
558 @Override
559 public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
560 MultipleValueLookupForm multipleValueLookupForm = (MultipleValueLookupForm) form;
561
562
563 prepareToReturnNone(multipleValueLookupForm);
564
565 return super.clearValues(mapping, form, request, response);
566 }
567
568
569
570
571
572
573
574 protected List<ResultRow> unselectAll(MultipleValueLookupForm multipleValueLookupForm, int maxRowsPerPage) {
575 String lookupResultsSequenceNumber = multipleValueLookupForm.getLookupResultsSequenceNumber();
576
577 List<ResultRow> resultTable = null;
578 try {
579 LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
580 resultTable = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPerson().getPrincipalId());
581 }
582 catch (Exception e) {
583 LOG.error("error occured trying to export multiple lookup results", e);
584 throw new RuntimeException("error occured trying to export multiple lookup results");
585 }
586
587 Map<String, String> selectedObjectIds = new HashMap<String, String>();
588
589
590 multipleValueLookupForm.jumpToPage(multipleValueLookupForm.getViewedPageNumber(), resultTable.size(), maxRowsPerPage);
591 multipleValueLookupForm.setColumnToSortIndex(Integer.parseInt(multipleValueLookupForm.getPreviouslySortedColumnIndex()));
592 multipleValueLookupForm.setCompositeObjectIdMap(selectedObjectIds);
593
594 return resultTable;
595 }
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610 protected int getMaxRowsPerPage(MultipleValueLookupForm multipleValueLookupForm) {
611 Integer appMaxRowsPerPage = LookupUtils.getApplicationMaximumSearchResulsPerPageForMultipleValueLookups();
612 if (appMaxRowsPerPage == null) {
613 LOG.warn("Couldn't find application results per page for MV lookups. Using default of " + DEFAULT_MAX_ROWS_PER_PAGE);
614 appMaxRowsPerPage = new Integer(DEFAULT_MAX_ROWS_PER_PAGE);
615 }
616 return appMaxRowsPerPage;
617 }
618 }
619