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 org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.kim.api.identity.Person;
20 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
21 import org.kuali.rice.kim.util.KimConstants;
22 import org.kuali.rice.krad.exception.AuthorizationException;
23 import org.kuali.rice.krad.lookup.CollectionIncomplete;
24 import org.kuali.rice.krad.lookup.Lookupable;
25 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26 import org.kuali.rice.krad.uif.UifConstants;
27 import org.kuali.rice.krad.uif.UifParameters;
28 import org.kuali.rice.krad.uif.container.LookupView;
29 import org.kuali.rice.krad.util.GlobalVariables;
30 import org.kuali.rice.krad.util.KRADConstants;
31 import org.kuali.rice.krad.util.KRADUtils;
32 import org.kuali.rice.krad.web.form.LookupForm;
33 import org.kuali.rice.krad.web.form.UifFormBase;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.validation.BindingResult;
36 import org.springframework.web.bind.annotation.ModelAttribute;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.servlet.ModelAndView;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import java.util.Collection;
43 import java.util.Properties;
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 supressActionsIfNeeded(LookupForm lookupForm) {
64 try {
65 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
66 Person user = GlobalVariables.getUserSession().getPerson();
67
68 String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService()
69 .getMaintenanceDocumentTypeName(dataObjectClass);
70 if ((documentTypeName != null) &&
71 !KRADServiceLocatorWeb.getDocumentHelperService().getDocumentAuthorizer(documentTypeName)
72 .canInitiate(documentTypeName, user)) {
73 ((LookupView) lookupForm.getView()).setSuppressActions(true);
74 }
75 } catch (ClassNotFoundException e) {
76 LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e);
77 }
78 }
79
80
81
82
83 @Override
84 public void checkAuthorization(UifFormBase form, String methodToCall) throws AuthorizationException {
85 if (!(form instanceof LookupForm)) {
86 super.checkAuthorization(form, methodToCall);
87 } else {
88 LookupForm lookupForm = (LookupForm) form;
89 try {
90 Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
91 Person user = GlobalVariables.getUserSession().getPerson();
92
93 if (!KimApiServiceLocator.getPermissionService()
94 .isAuthorizedByTemplateName(user.getPrincipalId(), KRADConstants.KRAD_NAMESPACE,
95 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
96 KRADUtils.getNamespaceAndComponentSimpleName(dataObjectClass),
97 null)) {
98 throw new AuthorizationException(user.getPrincipalName(),
99 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, dataObjectClass.getSimpleName());
100 }
101 } catch (ClassNotFoundException e) {
102 LOG.warn("Unable to load Data Object Class class: " + lookupForm.getDataObjectClassName(), e);
103 super.checkAuthorization(lookupForm, methodToCall);
104 }
105 }
106 }
107
108 @RequestMapping(params = "methodToCall=start")
109 @Override
110 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
111 HttpServletRequest request, HttpServletResponse response) {
112 LookupForm lookupForm = (LookupForm) form;
113
114 supressActionsIfNeeded(lookupForm);
115
116 return super.start(lookupForm, result, request, response);
117 }
118
119
120
121
122 @Override
123 @RequestMapping(params = "methodToCall=cancel")
124 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
125 HttpServletRequest request, HttpServletResponse response) {
126 LookupForm lookupForm = (LookupForm) form;
127 supressActionsIfNeeded(lookupForm);
128
129 Properties props = new Properties();
130 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
131 if (StringUtils.isNotBlank(lookupForm.getReturnFormKey())) {
132 props.put(UifParameters.FORM_KEY, lookupForm.getReturnFormKey());
133 }
134 if (StringUtils.isNotBlank(lookupForm.getDocNum())) {
135 props.put(UifParameters.DOC_NUM, lookupForm.getDocNum());
136 }
137
138 return performRedirect(lookupForm, lookupForm.getReturnLocation(), props);
139 }
140
141
142
143
144 @RequestMapping(params = "methodToCall=clearValues")
145 public ModelAndView clearValues(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
146 HttpServletRequest request, HttpServletResponse response) {
147 supressActionsIfNeeded(lookupForm);
148
149 Lookupable lookupable = (Lookupable) lookupForm.getLookupable();
150 lookupForm.setCriteriaFields(lookupable.performClear(lookupForm, lookupForm.getCriteriaFields()));
151
152 return getUIFModelAndView(lookupForm);
153 }
154
155
156
157
158
159 @RequestMapping(params = "methodToCall=search")
160 public ModelAndView search(@ModelAttribute("KualiForm") LookupForm lookupForm, BindingResult result,
161 HttpServletRequest request, HttpServletResponse response) {
162 supressActionsIfNeeded(lookupForm);
163 GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
164
165 Lookupable lookupable = lookupForm.getLookupable();
166 if (lookupable == null) {
167 LOG.error("Lookupable is null.");
168 throw new RuntimeException("Lookupable is null.");
169 }
170
171
172 lookupable.validateSearchParameters(lookupForm, lookupForm.getCriteriaFields());
173
174 Collection<?> displayList =
175 lookupable.performSearch(lookupForm, lookupForm.getCriteriaFields(), true);
176
177 if (displayList instanceof CollectionIncomplete<?>) {
178 request.setAttribute("reqSearchResultsActualSize",
179 ((CollectionIncomplete<?>) displayList).getActualSizeIfTruncated());
180 } else {
181 request.setAttribute("reqSearchResultsActualSize", new Integer(displayList.size()));
182 }
183
184 lookupForm.setSearchResults(displayList);
185
186 return getUIFModelAndView(lookupForm);
187 }
188 }