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