1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.action;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.kuali.rice.core.api.util.RiceConstants;
23 import org.kuali.rice.kim.api.KimConstants;
24 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25 import org.kuali.rice.kns.lookup.LookupUtils;
26 import org.kuali.rice.kns.lookup.Lookupable;
27 import org.kuali.rice.kns.service.DocumentHelperService;
28 import org.kuali.rice.kns.service.KNSServiceLocator;
29 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
30 import org.kuali.rice.kns.web.struts.form.LookupForm;
31 import org.kuali.rice.kns.web.ui.Field;
32 import org.kuali.rice.kns.web.ui.ResultRow;
33 import org.kuali.rice.kns.web.ui.Row;
34 import org.kuali.rice.krad.exception.AuthorizationException;
35 import org.kuali.rice.krad.lookup.CollectionIncomplete;
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.springframework.web.util.HtmlUtils;
40
41 import javax.servlet.ServletException;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.Map;
51
52
53
54
55
56
57
58 public class KualiLookupAction extends KualiAction {
59 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiLookupAction.class);
60
61 @Override
62 protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
63 if (!(form instanceof LookupForm)) {
64 super.checkAuthorization(form, methodToCall);
65 } else {
66 try {
67 Class businessObjectClass = Class.forName(((LookupForm) form).getBusinessObjectClassName());
68 if (!KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(
69 GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE,
70 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
71 KRADUtils.getNamespaceAndComponentSimpleName(businessObjectClass),
72 Collections.<String, String>emptyMap())) {
73 throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(),
74 KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS,
75 businessObjectClass.getSimpleName());
76 }
77 }
78 catch (ClassNotFoundException e) {
79 LOG.warn("Unable to load BusinessObject class: " + ((LookupForm) form).getBusinessObjectClassName(), e);
80 super.checkAuthorization(form, methodToCall);
81 }
82 }
83 }
84
85 private static MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
86 private static DocumentHelperService documentHelperService;
87 private static MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
88 if (maintenanceDocumentDictionaryService == null) {
89 maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService();
90 }
91 return maintenanceDocumentDictionaryService;
92 }
93 private static DocumentHelperService getDocumentHelperService() {
94 if (documentHelperService == null) {
95 documentHelperService = KNSServiceLocator.getDocumentHelperService();
96 }
97 return documentHelperService;
98 }
99
100
101
102
103
104
105
106 protected void supressActionsIfNeeded( ActionForm form ) throws ClassNotFoundException {
107 if ((form instanceof LookupForm) && ( ((LookupForm)form).getBusinessObjectClassName() != null )) {
108 Class businessObjectClass = Class.forName( ((LookupForm)form).getBusinessObjectClassName() );
109
110 String documentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(businessObjectClass);
111 if ((documentTypeName != null) && !getDocumentHelperService().getDocumentAuthorizer(documentTypeName).canInitiate(documentTypeName, GlobalVariables.getUserSession().getPerson())) {
112 ((LookupForm)form).setSuppressActions( true );
113 }
114 }
115 }
116
117
118
119
120
121
122 private void setCriteriaEnabled(ActionForm form) {
123 LookupForm lookupForm = (LookupForm) form;
124 if(lookupForm.isLookupCriteriaEnabled()) {
125
126 }
127 }
128
129
130
131
132
133 private void suppressNonMaintActionsIfNeeded(ActionForm form) {
134 LookupForm lookupForm = (LookupForm) form;
135 if(lookupForm.getLookupable()!=null) {
136 if(StringUtils.isNotEmpty(lookupForm.getLookupable().getSupplementalMenuBar())) {
137 lookupForm.setSupplementalActionsEnabled(true);
138 }
139
140 }
141 }
142
143 @Override
144 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
145 HttpServletResponse response) throws Exception {
146 LookupForm lookupForm = (LookupForm) form;
147
148 request.setAttribute(KRADConstants.PARAM_MAINTENANCE_VIEW_MODE, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_LOOKUP);
149 supressActionsIfNeeded(form);
150 suppressNonMaintActionsIfNeeded(form);
151 setCriteriaEnabled(form);
152
153 hideHeaderBarIfNeeded(form, request);
154
155 int numCols = KNSServiceLocator.getBusinessObjectDictionaryService().getLookupNumberOfColumns(
156 Class.forName(lookupForm.getBusinessObjectClassName()));
157 lookupForm.setNumColumns(numCols);
158
159 ActionForward forward = super.execute(mapping, form, request, response);
160
161
162 lookupForm.getLookupable().applyConditionalLogicForFieldDisplay();
163
164 return forward;
165 }
166
167 private void hideHeaderBarIfNeeded(ActionForm form, HttpServletRequest request) {
168 if (!((LookupForm) form).isHeaderBarEnabled()) {
169 ((LookupForm) form).setHeaderBarEnabled(false);
170 }
171 }
172
173
174
175
176
177 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
178 return mapping.findForward(RiceConstants.MAPPING_BASIC);
179 }
180
181
182
183
184 public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
185 LookupForm lookupForm = (LookupForm) form;
186
187
188 String methodToCall = findMethodToCall(form, request);
189 if (methodToCall.equalsIgnoreCase("search")) {
190 GlobalVariables.getUserSession().removeObjectsByPrefix(KRADConstants.SEARCH_METHOD);
191 }
192
193
194
195 Lookupable kualiLookupable = lookupForm.getLookupable();
196 if (kualiLookupable == null) {
197 LOG.error("Lookupable is null.");
198 throw new RuntimeException("Lookupable is null.");
199 }
200
201 Collection displayList = new ArrayList();
202 ArrayList<ResultRow> resultTable = new ArrayList<ResultRow>();
203
204
205 kualiLookupable.validateSearchParameters(lookupForm.getFields());
206
207 boolean bounded = true;
208
209 displayList = kualiLookupable.performLookup(lookupForm, resultTable, bounded);
210
211 if (kualiLookupable.isSearchUsingOnlyPrimaryKeyValues()) {
212 lookupForm.setSearchUsingOnlyPrimaryKeyValues(true);
213 lookupForm.setPrimaryKeyFieldLabels(kualiLookupable.getPrimaryKeyFieldLabels());
214 }
215 else {
216 lookupForm.setSearchUsingOnlyPrimaryKeyValues(false);
217 lookupForm.setPrimaryKeyFieldLabels(KRADConstants.EMPTY_STRING);
218 }
219
220 if ( displayList instanceof CollectionIncomplete ){
221 request.setAttribute("reqSearchResultsActualSize", ((CollectionIncomplete) displayList).getActualSizeIfTruncated());
222 } else {
223 request.setAttribute("reqSearchResultsActualSize", displayList.size() );
224 }
225
226 int resultsLimit = LookupUtils.getSearchResultsLimit(Class.forName(lookupForm.getBusinessObjectClassName()));
227 request.setAttribute("reqSearchResultsLimitedSize", resultsLimit);
228
229
230
231 boolean hasActionUrls = false;
232 for (Iterator<ResultRow> iterator = resultTable.iterator(); !hasActionUrls && iterator.hasNext();) {
233 if (StringUtils.isNotBlank(HtmlUtils.htmlUnescape(iterator.next().getActionUrls()).replace('\u00A0', '\u0020'))) {
234 hasActionUrls = true;
235 }
236 }
237 lookupForm.setActionUrlsExist(hasActionUrls);
238
239 request.setAttribute("reqSearchResults", resultTable);
240
241 if (request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY) != null) {
242 GlobalVariables.getUserSession().removeObject(request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
243 }
244
245 request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(resultTable, KRADConstants.SEARCH_LIST_KEY_PREFIX));
246
247 request.getParameter(KRADConstants.REFRESH_CALLER);
248
249 return mapping.findForward(RiceConstants.MAPPING_BASIC);
250 }
251
252
253
254
255
256 @Override
257 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
258 LookupForm lookupForm = (LookupForm) form;
259 Lookupable kualiLookupable = lookupForm.getLookupable();
260 if (kualiLookupable == null) {
261 LOG.error("Lookupable is null.");
262 throw new RuntimeException("Lookupable is null.");
263 }
264
265 if(StringUtils.equals(lookupForm.getRefreshCaller(),"customLookupAction")) {
266 return this.customLookupableMethodCall(mapping, lookupForm, request, response);
267 }
268
269 Map<String, String> fieldValues = new HashMap();
270 Map<String, String> values = lookupForm.getFields();
271
272 for (Row row: kualiLookupable.getRows()) {
273 for (Field field: row.getFields()) {
274 if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
275 if (request.getParameter(field.getPropertyName()) != null) {
276 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
277 field.setPropertyValue(request.getParameter(field.getPropertyName()));
278 } else {
279
280 field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
281 }
282 }
283 }
284 else if (values.get(field.getPropertyName()) != null) {
285 field.setPropertyValue(values.get(field.getPropertyName()));
286 }
287
288 kualiLookupable.applyFieldAuthorizationsFromNestedLookups(field);
289
290 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
291 }
292 }
293 fieldValues.put("docFormKey", lookupForm.getFormKey());
294 fieldValues.put("backLocation", lookupForm.getBackLocation());
295 fieldValues.put("docNum", lookupForm.getDocNum());
296
297 if (kualiLookupable.checkForAdditionalFields(fieldValues)) {
298 for (Row row: kualiLookupable.getRows()) {
299 for (Object element : row.getFields()) {
300 Field field = (Field) element;
301 if (field.getPropertyName() != null && !field.getPropertyName().equals("")) {
302 if (request.getParameter(field.getPropertyName()) != null) {
303
304 if(!Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())) {
305 field.setPropertyValue(request.getParameter(field.getPropertyName()));
306 } else {
307
308 field.setPropertyValues(request.getParameterValues(field.getPropertyName()));
309 }
310
311 fieldValues.put(field.getPropertyName(), request.getParameter(field.getPropertyName()));
312 }
313 else if (values.get(field.getPropertyName()) != null) {
314 field.setPropertyValue(values.get(field.getPropertyName()));
315 }
316 }
317 }
318 }
319 }
320 return mapping.findForward(RiceConstants.MAPPING_BASIC);
321 }
322
323
324
325
326 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
327 LookupForm lookupForm = (LookupForm) form;
328
329 String backUrl = lookupForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + lookupForm.getFormKey()+"&docNum="+lookupForm.getDocNum();
330 return new ActionForward(backUrl, true);
331 }
332
333
334
335
336
337 public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
338 LookupForm lookupForm = (LookupForm) form;
339 Lookupable kualiLookupable = lookupForm.getLookupable();
340 if (kualiLookupable == null) {
341 LOG.error("Lookupable is null.");
342 throw new RuntimeException("Lookupable is null.");
343 }
344
345 kualiLookupable.performClear(lookupForm);
346
347
348 return mapping.findForward(RiceConstants.MAPPING_BASIC);
349 }
350
351
352 public ActionForward viewResults(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
353 LookupForm lookupForm = (LookupForm) form;
354 if (lookupForm.isSearchUsingOnlyPrimaryKeyValues()) {
355 lookupForm.setPrimaryKeyFieldLabels(lookupForm.getLookupable().getPrimaryKeyFieldLabels());
356 }
357 request.setAttribute(KRADConstants.SEARCH_LIST_REQUEST_KEY, request.getParameter(KRADConstants.SEARCH_LIST_REQUEST_KEY));
358 request.setAttribute("reqSearchResults", GlobalVariables.getUserSession().retrieveObject(request.getParameter(
359 KRADConstants.SEARCH_LIST_REQUEST_KEY)));
360 request.setAttribute("reqSearchResultsActualSize", request.getParameter("reqSearchResultsActualSize"));
361 return mapping.findForward(RiceConstants.MAPPING_BASIC);
362 }
363
364 public ActionForward customLookupableMethodCall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
365
366 Lookupable kualiLookupable = ((LookupForm)form).getLookupable();
367 if (kualiLookupable == null) {
368 LOG.error("Lookupable is null.");
369 throw new RuntimeException("Lookupable is null.");
370 }
371
372 boolean ignoreErrors=false;
373 if(StringUtils.equals(((LookupForm)form).getRefreshCaller(),"customLookupAction")) {
374 ignoreErrors=true;
375 }
376
377 if(kualiLookupable.performCustomAction(ignoreErrors)) {
378
379 return search(mapping, form, request, response);
380 }
381 return mapping.findForward(RiceConstants.MAPPING_BASIC);
382
383 }
384
385 }