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
64
65
66
67
68
69
70
71 @RequestMapping(params = "methodToCall=start")
72 @Override
73 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
74 HttpServletRequest request, HttpServletResponse response) {
75 LookupForm lookupForm = (LookupForm) form;
76
77 Lookupable lookupable = lookupForm.getLookupable();
78 if (lookupable == null) {
79 LOG.error("Lookupable is null.");
80 throw new RuntimeException("Lookupable is null.");
81 }
82 lookupable.initSuppressAction(lookupForm);
83
84
85 if (!lookupForm.isRedirectedLookup()) {
86 Class lookupObjectClass = null;
87 try {
88 lookupObjectClass = Class.forName(lookupForm.getDataObjectClassName());
89 } catch (ClassNotFoundException e) {
90 throw new RiceRuntimeException("Unable to get class for name: " + lookupForm.getDataObjectClassName());
91 }
92
93 ModuleService responsibleModuleService =
94 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(lookupObjectClass);
95 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(lookupObjectClass)) {
96 String lookupUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(lookupObjectClass,
97 KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
98
99 Properties redirectUrlProps = new Properties();
100 redirectUrlProps.put(UifParameters.REDIRECTED_LOOKUP, "true");
101
102
103 GlobalVariables.getUifFormManager().removeForm(form);
104
105 return performRedirect(form, lookupUrl, redirectUrlProps);
106 }
107 }
108
109 return super.start(lookupForm, result, request, response);
110 }
111
112
113
114
115 @Override
116 @RequestMapping(params = "methodToCall=cancel")
117 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
118 HttpServletRequest request, HttpServletResponse response) {
119 LookupForm lookupForm = (LookupForm) form;
120
121 Lookupable lookupable = lookupForm.getLookupable();
122 if (lookupable == null) {
123 LOG.error("Lookupable is null.");
124 throw new RuntimeException("Lookupable is null.");
125 }
126 lookupable.initSuppressAction(lookupForm);
127
128 Properties props = new Properties();
129 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
130 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
131 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
132 }
133 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
134 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
135 }
136
137
138 GlobalVariables.getUifFormManager().removeForm(form);
139
140 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
141 }
142
143
144
145
146 @RequestMapping(params = "methodToCall=clearValues")
147 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
148 HttpServletRequest request, HttpServletResponse response) {
149
150 Lookupable lookupable = lookupForm.getLookupable();
151 if (lookupable == null) {
152 LOG.error("Lookupable is null.");
153 throw new RuntimeException("Lookupable is null.");
154 }
155 lookupable.initSuppressAction(lookupForm);
156 lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
157
158 return getUIFModelAndView(lookupForm);
159 }
160
161
162
163
164
165 @RequestMapping(params = "methodToCall=search")
166 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
167 HttpServletRequest request, HttpServletResponse response) {
168
169 Lookupable lookupable = lookupForm.getLookupable();
170 if (lookupable == null) {
171 LOG.error("Lookupable is null.");
172 throw new RuntimeException("Lookupable is null.");
173 }
174 lookupable.initSuppressAction(lookupForm);
175
176
177 lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
178
179 Collection<?> displayList =
180 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
181
182 if (displayList instanceof CollectionIncomplete<?>) {
183 request.setAttribute("reqSearchResultsActualSize",
184 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
185 } else {
186 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
187 }
188
189 lookupForm.setSearchResults(displayList);
190
191 return getUIFModelAndView(lookupForm);
192 }
193
194
195
196
197
198
199
200 @RequestMapping(params = "methodToCall=returnSelected")
201 public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
202 HttpServletRequest request, HttpServletResponse response) {
203 Properties parameters = new Properties();
204 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
205
206 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
207 parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
208 }
209
210 parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
211 parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
212 parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
213
214 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
215 parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
216 }
217
218 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
219 parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
220 }
221
222 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
223 parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
224 }
225
226
227 String selectedLineValues = "";
228 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
229 if (selectedLines != null) {
230 for (String selectedLine : selectedLines) {
231 selectedLineValues += selectedLine + ",";
232 }
233 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
234 }
235
236 parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
237
238
239 GlobalVariables.getUifFormManager().removeForm(lookupForm);
240
241 return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
242 }
243 }