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
114 GlobalVariables.getUifFormManager().removeForm(form);
115
116 return performRedirect(form, lookupUrl, redirectUrlProps);
117 }
118 }
119
120 return super.start(lookupForm, result, request, response);
121 }
122
123
124
125
126 @Override
127 @RequestMapping(params = "methodToCall=cancel")
128 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
129 HttpServletRequest request, HttpServletResponse response) {
130 LookupForm lookupForm = (LookupForm) form;
131 suppressActionsIfNeeded(lookupForm);
132
133 Properties props = new Properties();
134 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
135 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
136 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
137 }
138 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
139 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
140 }
141
142
143 GlobalVariables.getUifFormManager().removeForm(form);
144
145 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
146 }
147
148
149
150
151 @RequestMapping(params = "methodToCall=clearValues")
152 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
153 HttpServletRequest request, HttpServletResponse response) {
154 suppressActionsIfNeeded(lookupForm);
155
156 Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
157 lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
158
159 return getUIFModelAndView(lookupForm);
160 }
161
162
163
164
165
166 @RequestMapping(params = "methodToCall=search")
167 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
168 HttpServletRequest request, HttpServletResponse response) {
169 suppressActionsIfNeeded(lookupForm);
170
171 Lookupable lookupable = lookupForm.getLookupable();
172 if (lookupable == null) {
173 LOG.error("Lookupable is null.");
174 throw new RuntimeException("Lookupable is null.");
175 }
176
177
178 lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
179
180 Collection<?> displayList =
181 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
182
183 if (displayList instanceof CollectionIncomplete<?>) {
184 request.setAttribute("reqSearchResultsActualSize",
185 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
186 } else {
187 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
188 }
189
190 lookupForm.setSearchResults(displayList);
191
192 return getUIFModelAndView(lookupForm);
193 }
194
195
196
197
198
199
200
201 @RequestMapping(params = "methodToCall=returnSelected")
202 public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
203 HttpServletRequest request, HttpServletResponse response) {
204 Properties parameters = new Properties();
205 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
206
207 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
208 parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
209 }
210
211 parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
212 parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
213 parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
214
215 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
216 parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
217 }
218
219 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
220 parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
221 }
222
223 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
224 parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
225 }
226
227
228 String selectedLineValues = "";
229 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
230 if (selectedLines != null) {
231 for (String selectedLine : selectedLines) {
232 selectedLineValues += selectedLine + ",";
233 }
234 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
235 }
236
237 parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
238
239
240 GlobalVariables.getUifFormManager().removeForm(lookupForm);
241
242 return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
243 }
244 }