1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.controller;
17
18 import java.util.Collection;
19 import java.util.Properties;
20 import java.util.Set;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.rice.core.api.exception.RiceRuntimeException;
27 import org.kuali.rice.krad.lookup.CollectionIncomplete;
28 import org.kuali.rice.krad.lookup.Lookupable;
29 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30 import org.kuali.rice.krad.service.ModuleService;
31 import org.kuali.rice.krad.uif.UifConstants;
32 import org.kuali.rice.krad.uif.UifParameters;
33 import org.kuali.rice.krad.uif.UifPropertyPaths;
34 import org.kuali.rice.krad.util.GlobalVariables;
35 import org.kuali.rice.krad.util.KRADConstants;
36 import org.kuali.rice.krad.util.KRADUtils;
37 import org.kuali.rice.krad.web.form.LookupForm;
38 import org.kuali.rice.krad.web.form.UifFormBase;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.validation.BindingResult;
41 import org.springframework.web.bind.annotation.ModelAttribute;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.servlet.ModelAndView;
44
45
46
47
48
49
50 @Controller
51 @RequestMapping(value = "/lookup")
52 public class LookupController extends UifControllerBase {
53 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class);
54
55
56
57
58 @Override
59 protected LookupForm createInitialForm(HttpServletRequest request) {
60 return new LookupForm();
61 }
62
63 protected void suppressActionsIfNeeded(LookupForm lookupForm) {
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 }
80
81
82
83
84
85
86
87
88
89 @RequestMapping(params = "methodToCall=start")
90 @Override
91 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
92 HttpServletRequest request, HttpServletResponse response) {
93 LookupForm lookupForm = (LookupForm) form;
94
95
96 if (!lookupForm.isRedirectedLookup()) {
97 Class lookupObjectClass = null;
98 try {
99 lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
100 } catch (ClassNotFoundException e) {
101 throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
102 }
103
104 ModuleService responsibleModuleService =
105 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
106 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
107 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
108 KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
109
110 Properties redirectUrlProps = new Properties();
111 redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
112
113 return performRedirect(form, lookupUrl, redirectUrlProps);
114 }
115 }
116
117 return super.start(lookupForm, result, request, response);
118 }
119
120
121
122
123 @Override
124 @RequestMapping(params = "methodToCall=cancel")
125 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
126 HttpServletRequest request, HttpServletResponse response) {
127 LookupForm lookupForm = (LookupForm) form;
128 suppressActionsIfNeeded(lookupForm);
129
130 Properties props = new Properties();
131 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
132 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
133 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
134 }
135 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
136 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
137 }
138
139 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
140 }
141
142
143
144
145 @RequestMapping(params = "methodToCall=clearValues")
146 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
147 HttpServletRequest request, HttpServletResponse response) {
148 suppressActionsIfNeeded(lookupForm);
149
150 Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
151 lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
152
153 return getUIFModelAndView(lookupForm);
154 }
155
156
157
158
159
160 @RequestMapping(params = "methodToCall=search")
161 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
162 HttpServletRequest request, HttpServletResponse response) {
163 suppressActionsIfNeeded(lookupForm);
164
165 Lookupable lookupable = lookupForm.getLookupable();
166 if (lookupable == null) {
167 LOG.error("Lookupable is null.");
168 throw new RuntimeException("Lookupable is null.");
169 }
170
171
172 lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
173
174 Collection<?> displayList =
175 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
176
177 if (displayList instanceof CollectionIncomplete<?>) {
178 request.setAttribute("reqSearchResultsActualSize",
179 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
180 } else {
181 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
182 }
183
184 lookupForm.setSearchResults(displayList);
185
186 return getUIFModelAndView(lookupForm);
187 }
188
189
190
191
192
193
194
195 @RequestMapping(params = "methodToCall=returnSelected")
196 public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
197 HttpServletRequest request, HttpServletResponse response) {
198 Properties parameters = new Properties();
199 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
200
201 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
202 parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
203 }
204
205 parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
206 parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
207 parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
208
209 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
210 parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
211 }
212
213 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
214 parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
215 }
216
217 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
218 parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
219 }
220
221
222 String selectedLineValues = "";
223 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
224 if (selectedLines != null) {
225 for (String selectedLine : selectedLines) {
226 selectedLineValues += selectedLine + ",";
227 }
228 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
229 }
230
231 parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
232
233
234 GlobalVariables.getUifFormManager().removeForm(lookupForm);
235
236 return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
237 }
238 }