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.Collections;
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.kim.api.KimConstants;
28 import org.kuali.rice.kim.api.identity.Person;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.krad.exception.AuthorizationException;
31 import org.kuali.rice.krad.lookup.CollectionIncomplete;
32 import org.kuali.rice.krad.lookup.Lookupable;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.uif.UifConstants;
35 import org.kuali.rice.krad.uif.UifParameters;
36 import org.kuali.rice.krad.uif.UifPropertyPaths;
37 import org.kuali.rice.krad.uif.view.LookupView;
38 import org.kuali.rice.krad.util.GlobalVariables;
39 import org.kuali.rice.krad.util.KRADConstants;
40 import org.kuali.rice.krad.util.KRADUtils;
41 import org.kuali.rice.krad.web.form.LookupForm;
42 import org.kuali.rice.krad.web.form.UifFormBase;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.validation.BindingResult;
45 import org.springframework.web.bind.annotation.ModelAttribute;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.servlet.ModelAndView;
48
49
50
51
52
53
54 @Controller
55 @RequestMapping(value = "/lookup")
56 public class LookupController extends UifControllerBase {
57 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupController.class);
58
59
60
61
62 @Override
63 protected LookupForm createInitialForm(HttpServletRequest request) {
64 return new LookupForm();
65 }
66
67 protected void suppressActionsIfNeeded(LookupForm lookupForm) {
68 try {
69 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
70 Person user = GlobalVariables.getUserSession().getPerson();
71
72 String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService()
73 .getMaintenanceDocumentTypeName(dataObjectClass);
74 if ((documentTypeName != null) &&
75 !KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName)
76 .canInitiate(documentTypeName, user)) {
77 ((LookupView) lookupForm.getView()).setSuppressActions(true);
78 }
79 } catch (ClassNotFoundException e) {
80 LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e);
81 }
82 }
83
84
85
86
87 @Override
88 public void checkAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
89 if (!(form instanceof LookupForm)) {
90 super.checkAuthorization(form, methodToCall);
91 } else {
92 LookupForm lookupForm = (LookupForm) form;
93 try {
94 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
95 Person user = GlobalVariables.getUserSession().getPerson();
96
97 if (!KimApiServiceLocator.getPermissionService()
98 .isAuthorizedByTemplateName(user.getPrincipalId(), KRADConstants.KRAD_NAMESPACE,
99 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
100 KRADUtils.getNamespaceAndComponentSimpleName(dataObjectClass),
101 Collections.<String, String>emptyMap())) {
102 throw new AuthorizationException(user.getPrincipalName(),
103 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, dataObjectClass.getSimpleName());
104 }
105 } catch (ClassNotFoundException e) {
106 LOG.warn("Unable to load Data Object Class class: " + lookupForm.getDataObjectClassName(), e);
107 super.checkAuthorization(lookupForm, methodToCall);
108 }
109 }
110 }
111
112 @RequestMapping(params = "methodToCall=start")
113 @Override
114 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
115 HttpServletRequest request, HttpServletResponse response) {
116 LookupForm lookupForm = (LookupForm) form;
117
118 suppressActionsIfNeeded(lookupForm);
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 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
143 }
144
145
146
147
148 @RequestMapping(params = "methodToCall=clearValues")
149 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
150 HttpServletRequest request, HttpServletResponse response) {
151 suppressActionsIfNeeded(lookupForm);
152
153 Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
154 lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
155
156 return getUIFModelAndView(lookupForm);
157 }
158
159
160
161
162
163 @RequestMapping(params = "methodToCall=search")
164 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
165 HttpServletRequest request, HttpServletResponse response) {
166 suppressActionsIfNeeded(lookupForm);
167
168 Lookupable lookupable = lookupForm.getLookupable();
169 if (lookupable == null) {
170 LOG.error("Lookupable is null.");
171 throw new RuntimeException("Lookupable is null.");
172 }
173
174
175 lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
176
177 Collection<?> displayList =
178 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
179
180 if (displayList instanceof CollectionIncomplete<?>) {
181 request.setAttribute("reqSearchResultsActualSize",
182 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
183 } else {
184 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
185 }
186
187 lookupForm.setSearchResults(displayList);
188
189 return getUIFModelAndView(lookupForm);
190 }
191
192
193
194
195
196
197
198 @RequestMapping(params = "methodToCall=returnSelected")
199 public ModelAndView returnSelected(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
200 HttpServletRequest request, HttpServletResponse response) {
201 Properties parameters = new Properties();
202 parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.RETURN_METHOD_TO_CALL);
203 parameters.put(UifParameters.SKIP_VIEW_INIT, "true");
204
205 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
206 parameters.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
207 }
208
209 parameters.put(KRADConstants.REFRESH_CALLER, lookupForm.getView().getId());
210 parameters.put(KRADConstants.REFRESH_CALLER_TYPE, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP);
211 parameters.put(KRADConstants.REFRESH_DATA_OBJECT_CLASS, lookupForm.getDataObjectClassName());
212
213 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
214 parameters.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
215 }
216
217 if (StringUtils.isNotBlank(lookupForm.getLookupCollectionName())) {
218 parameters.put(UifParameters.LOOKUP_COLLECTION_NAME, lookupForm.getLookupCollectionName());
219 }
220
221 if (StringUtils.isNotBlank(lookupForm.getReferencesToRefresh())) {
222 parameters.put(KRADConstants.REFERENCES_TO_REFRESH, lookupForm.getReferencesToRefresh());
223 }
224
225
226 String selectedLineValues = "";
227 Set<String> selectedLines = lookupForm.getSelectedCollectionLines().get(UifPropertyPaths.SEARCH_RESULTS);
228 if (selectedLines != null) {
229 for (String selectedLine : selectedLines) {
230 selectedLineValues += selectedLine + ",";
231 }
232 selectedLineValues = StringUtils.removeEnd(selectedLineValues, ",");
233 }
234
235 parameters.put(UifParameters.SELECTED_LINE_VALUES, selectedLineValues);
236
237 return performRedirect(lookupForm, lookupForm.getReturnLocation(), parameters);
238 }
239 }