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