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