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.bind.annotation.RequestMethod;
44 import org.springframework.web.servlet.ModelAndView;
45
46
47
48
49
50
51 @Controller
52 @RequestMapping(value = "/lookup")
53 public class LookupController extends UifControllerBase {
54 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class);
55
56
57
58
59 @Override
60 protected LookupForm createInitialForm(HttpServletRequest request) {
61 return new LookupForm();
62 }
63
64 protected void suppressActionsIfNeeded(LookupForm lookupForm) {
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
90 @RequestMapping(params = "methodToCall=start")
91 @Override
92 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
93 HttpServletRequest request, HttpServletResponse response) {
94 LookupForm lookupForm = (LookupForm) form;
95
96
97 if (!lookupForm.isRedirectedLookup()) {
98 Class lookupObjectClass = null;
99 try {
100 lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
101 } catch (ClassNotFoundException e) {
102 throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
103 }
104
105 ModuleService responsibleModuleService =
106 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
107 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
108 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
109 KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
110
111 Properties redirectUrlProps = new Properties();
112 redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
113
114
115 GlobalVariables.getUifFormManager().removeSessionForm(form);
116
117 return performRedirect(form, lookupUrl, redirectUrlProps);
118 }
119 }
120
121 return super.start(lookupForm, result, request, response);
122 }
123
124
125
126
127 @Override
128 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancel")
129 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
130 HttpServletRequest request, HttpServletResponse response) {
131 LookupForm lookupForm = (LookupForm) form;
132 suppressActionsIfNeeded(lookupForm);
133
134 Properties props = new Properties();
135 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
136 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
137 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
138 }
139 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
140 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
141 }
142
143
144 GlobalVariables.getUifFormManager().removeSessionForm(form);
145
146 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
147 }
148
149
150
151
152 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=clearValues")
153 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
154 HttpServletRequest request, HttpServletResponse response) {
155 suppressActionsIfNeeded(lookupForm);
156
157 Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
158 lookupForm.setLookupCriteria(lookupable.performClear(lookupForm, lookupForm.getLookupCriteria()));
159
160 return getUIFModelAndView(lookupForm);
161 }
162
163
164
165
166
167 @RequestMapping(params = "methodToCall=search")
168 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
169 HttpServletRequest request, HttpServletResponse response) {
170 suppressActionsIfNeeded(lookupForm);
171
172 Lookupable lookupable = lookupForm.getLookupable();
173 if (lookupable == null) {
174 LOG.error("Lookupable is null.");
175 throw new RuntimeException("Lookupable is null.");
176 }
177
178
179 boolean searchValid = lookupable.validateSearchParameters(lookupForm, lookupForm.getLookupCriteria());
180
181 if (searchValid) {
182 Collection<?> displayList = lookupable.performSearch(lookupForm, lookupForm.getLookupCriteria(), true);
183
184 if (displayList instanceof CollectionIncomplete<?>) {
185 request.setAttribute("reqSearchResultsActualSize",
186 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
187 } else {
188 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
189 }
190
191 lookupForm.setLookupResults(displayList);
192 }
193
194 return getUIFModelAndView(lookupForm);
195 }
196
197
198
199
200
201
202
203 @RequestMapping(method = RequestMethod.POST, params = "methodToCall=returnSelected")
204 public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
205 HttpServletRequest request, HttpServletResponse response) {
206 Properties parameters = new Properties();
207 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
208
209 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
210 parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
211 }
212
213 parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
214 parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
215 parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
216
217 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
218 parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
219 }
220
221 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
222 parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
223 }
224
225 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
226 parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
227 }
228
229
230 String selectedLineValues = "";
231 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
232 if (selectedLines != null) {
233 for (String selectedLine : selectedLines) {
234 selectedLineValues += selectedLine + ",";
235 }
236 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
237 }
238
239 parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
240
241
242 GlobalVariables.getUifFormManager().removeSessionForm(lookupForm);
243
244 return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
245 }
246 }