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.ByteArrayOutputStream; |
19 | |
import java.io.IOException; |
20 | |
import java.lang.reflect.Method; |
21 | |
import java.util.Collections; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
import javax.servlet.http.HttpServletResponse; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.apache.struts.action.ActionForm; |
30 | |
import org.apache.struts.action.ActionForward; |
31 | |
import org.apache.struts.action.ActionMapping; |
32 | |
import org.apache.struts.action.RedirectingActionForward; |
33 | |
import org.kuali.rice.core.util.RiceConstants; |
34 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
35 | |
import org.kuali.rice.kim.util.KimCommonUtils; |
36 | |
import org.kuali.rice.kim.util.KimConstants; |
37 | |
import org.kuali.rice.kns.bo.Attachment; |
38 | |
import org.kuali.rice.kns.bo.BusinessObject; |
39 | |
import org.kuali.rice.kns.bo.Exporter; |
40 | |
import org.kuali.rice.kns.bo.Note; |
41 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
42 | |
import org.kuali.rice.kns.exception.AuthorizationException; |
43 | |
import org.kuali.rice.kns.inquiry.Inquirable; |
44 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
45 | |
import org.kuali.rice.kns.service.ModuleService; |
46 | |
import org.kuali.rice.kns.service.NoteService; |
47 | |
import org.kuali.rice.kns.util.GlobalVariables; |
48 | |
import org.kuali.rice.kns.util.KNSConstants; |
49 | |
import org.kuali.rice.kns.util.RiceKeyConstants; |
50 | |
import org.kuali.rice.kns.util.WebUtils; |
51 | |
import org.kuali.rice.kns.web.struts.form.InquiryForm; |
52 | |
import org.kuali.rice.kns.web.ui.Field; |
53 | |
import org.kuali.rice.kns.web.ui.Row; |
54 | |
import org.kuali.rice.kns.web.ui.Section; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public class KualiInquiryAction extends KualiAction { |
60 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiInquiryAction.class); |
61 | |
private NoteService noteService; |
62 | |
|
63 | |
@Override |
64 | |
protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException { |
65 | 0 | if (!(form instanceof InquiryForm)) { |
66 | 0 | super.checkAuthorization(form, methodToCall); |
67 | |
} else { |
68 | |
try { |
69 | 0 | if(!KNSConstants.DOWNLOAD_BO_ATTACHMENT_METHOD.equals(methodToCall)){ |
70 | 0 | Class businessObjectClass = Class.forName(((InquiryForm) form).getBusinessObjectClassName()); |
71 | 0 | if (!KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KNSConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.INQUIRE_INTO_RECORDS, KimCommonUtils.getNamespaceAndComponentSimpleName(businessObjectClass), null)) { |
72 | 0 | throw new AuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(), |
73 | |
"inquire", |
74 | |
businessObjectClass.getSimpleName()); |
75 | |
} |
76 | |
} |
77 | |
} |
78 | 0 | catch (ClassNotFoundException e) { |
79 | 0 | LOG.warn("Unable to load BusinessObject class: " + ((InquiryForm) form).getBusinessObjectClassName(), e); |
80 | 0 | super.checkAuthorization(form, methodToCall); |
81 | 0 | } |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
@Override |
86 | |
protected Map<String, String> getRoleQualification(ActionForm form, |
87 | |
String methodToCall) { |
88 | 0 | Map<String, String> roleQualification = super.getRoleQualification( |
89 | |
form, methodToCall); |
90 | 0 | if (form instanceof InquiryForm) { |
91 | 0 | Map<String, String> primaryKeys = ((InquiryForm) form) |
92 | |
.getInquiryPrimaryKeys(); |
93 | 0 | if (primaryKeys != null) { |
94 | 0 | for (String keyName : primaryKeys.keySet()) { |
95 | 0 | roleQualification.put(keyName, primaryKeys.get(keyName)); |
96 | |
} |
97 | |
} |
98 | |
} |
99 | 0 | return roleQualification; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
104 | 0 | request.setAttribute(KNSConstants.PARAM_MAINTENANCE_VIEW_MODE, KNSConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY); |
105 | 0 | return super.execute(mapping, form, request, response); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
113 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
114 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
115 | 0 | LOG.error("Business object name not given."); |
116 | 0 | throw new RuntimeException("Business object name not given."); |
117 | |
} |
118 | |
|
119 | 0 | Class boClass = Class.forName(inquiryForm.getBusinessObjectClassName()); |
120 | 0 | ModuleService responsibleModuleService = KNSServiceLocator.getKualiModuleService().getResponsibleModuleService(boClass); |
121 | 0 | if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(boClass)){ |
122 | 0 | String redirectUrl = responsibleModuleService.getExternalizableBusinessObjectInquiryUrl(boClass, (Map<String, String[]>) request.getParameterMap()); |
123 | 0 | ActionForward redirectingActionForward = new RedirectingActionForward(redirectUrl); |
124 | 0 | redirectingActionForward.setModule("/"); |
125 | 0 | return redirectingActionForward; |
126 | |
} |
127 | |
|
128 | 0 | return continueWithInquiry(mapping, form, request, response); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
public ActionForward downloadCustomBOAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
133 | 0 | String fileName = request.getParameter(KNSConstants.BO_ATTACHMENT_FILE_NAME); |
134 | 0 | String contentType = request.getParameter(KNSConstants.BO_ATTACHMENT_FILE_CONTENT_TYPE); |
135 | 0 | String fileContentBoField = request.getParameter(KNSConstants.BO_ATTACHMENT_FILE_CONTENT_FIELD); |
136 | |
|
137 | 0 | if (fileName != null |
138 | |
&& contentType != null |
139 | |
&& fileContentBoField != null) { |
140 | |
|
141 | 0 | checkAuthorization(form, findMethodToCall(form, request)); |
142 | |
|
143 | 0 | fileName = StringUtils.replace(fileName, " ", "_"); |
144 | |
|
145 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
146 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
147 | 0 | checkBO(bo); |
148 | |
|
149 | 0 | Class clazz = (bo.getClass()); |
150 | 0 | Method method = clazz.getMethod("get"+StringUtils.capitalize(fileContentBoField)); |
151 | 0 | byte[] fileContents = (byte[]) method.invoke(bo); |
152 | 0 | streamToResponse(fileContents, fileName, contentType,response); |
153 | 0 | } else { |
154 | 0 | if (fileName == null) { |
155 | 0 | LOG.error("Request Parameter \""+ KNSConstants.BO_ATTACHMENT_FILE_NAME + "\" not provided."); |
156 | |
} |
157 | 0 | if (contentType == null) { |
158 | 0 | LOG.error("Request Parameter \""+ KNSConstants.BO_ATTACHMENT_FILE_CONTENT_TYPE + "\" not provided."); |
159 | |
} |
160 | 0 | if (fileContentBoField == null) { |
161 | 0 | LOG.error("Request Parameter \""+ KNSConstants.BO_ATTACHMENT_FILE_CONTENT_FIELD + "\" not provided."); |
162 | |
} |
163 | |
} |
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public ActionForward downloadBOAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
179 | 0 | Long noteIdentifier = Long.valueOf(request.getParameter(KNSConstants.NOTE_IDENTIFIER)); |
180 | |
|
181 | 0 | Note note = this.getNoteService().getNoteByNoteId(noteIdentifier); |
182 | 0 | if(note != null){ |
183 | 0 | Attachment attachment = note.getAttachment(); |
184 | 0 | if(attachment != null){ |
185 | |
|
186 | 0 | attachment.setNote(note); |
187 | 0 | WebUtils.saveMimeInputStreamAsFile(response, attachment.getAttachmentMimeTypeCode(), attachment.getAttachmentContents(), attachment.getAttachmentFileName(), attachment.getAttachmentFileSize().intValue()); |
188 | |
} |
189 | 0 | return null; |
190 | |
} |
191 | |
|
192 | 0 | return mapping.findForward(RiceConstants.MAPPING_BASIC); |
193 | |
} |
194 | |
|
195 | |
public ActionForward continueWithInquiry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
196 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
197 | |
|
198 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
199 | 0 | LOG.error("Business object name not given."); |
200 | 0 | throw new RuntimeException("Business object name not given."); |
201 | |
} |
202 | |
|
203 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
204 | 0 | checkBO(bo); |
205 | |
|
206 | 0 | populateSections(mapping, request, inquiryForm, bo); |
207 | |
|
208 | 0 | return mapping.findForward(RiceConstants.MAPPING_BASIC); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public ActionForward toggleInactiveRecordDisplay(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
215 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
216 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
217 | 0 | LOG.error("Business object name not given."); |
218 | 0 | throw new RuntimeException("Business object name not given."); |
219 | |
} |
220 | |
|
221 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
222 | 0 | checkBO(bo); |
223 | |
|
224 | 0 | Inquirable kualiInquirable = inquiryForm.getInquirable(); |
225 | |
|
226 | 0 | String collectionName = extractCollectionName(request, KNSConstants.TOGGLE_INACTIVE_METHOD); |
227 | 0 | if (collectionName == null) { |
228 | 0 | LOG.error("Unable to get find collection name in request."); |
229 | 0 | throw new RuntimeException("Unable to get find collection class in request."); |
230 | |
} |
231 | 0 | String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE); |
232 | 0 | boolean showInactive = Boolean.parseBoolean(StringUtils.substringBetween(parameterName, KNSConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL, ".")); |
233 | 0 | kualiInquirable.setShowInactiveRecords(collectionName, showInactive); |
234 | |
|
235 | |
|
236 | 0 | populateSections(mapping, request, inquiryForm, bo); |
237 | |
|
238 | 0 | if (showInactive) { |
239 | 0 | reopenInactiveRecords(inquiryForm, collectionName); |
240 | |
} |
241 | |
|
242 | 0 | return mapping.findForward(RiceConstants.MAPPING_BASIC); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
protected void reopenInactiveRecords(InquiryForm inquiryForm, String collectionName) { |
252 | 0 | for (Object sectionAsObject : inquiryForm.getSections()) { |
253 | 0 | final Section section = (Section)sectionAsObject; |
254 | 0 | for (Row row: section.getRows()) { |
255 | 0 | for (Field field : row.getFields()) { |
256 | 0 | if (field.getFieldType().equals(Field.CONTAINER) && field.getContainerName().startsWith(collectionName)) { |
257 | 0 | final String tabKey = WebUtils.generateTabKey(generateCollectionSubTabName(field)); |
258 | 0 | inquiryForm.getTabStates().put(tabKey, "OPEN"); |
259 | 0 | } |
260 | |
} |
261 | |
} |
262 | 0 | } |
263 | 0 | } |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
protected String generateCollectionSubTabName(Field field) { |
272 | 0 | final String containerName = field.getContainerElementName(); |
273 | 0 | final String cleanedContainerName = |
274 | |
(containerName == null) ? |
275 | |
"" : |
276 | |
containerName.replaceAll("\\d+", ""); |
277 | 0 | StringBuilder subTabName = new StringBuilder(cleanedContainerName); |
278 | 0 | for (Field containerField : field.getContainerDisplayFields()) { |
279 | 0 | subTabName.append(containerField.getPropertyValue()); |
280 | |
} |
281 | 0 | return subTabName.toString(); |
282 | |
} |
283 | |
|
284 | |
@Override |
285 | |
public ActionForward toggleTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
286 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
287 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
288 | 0 | LOG.error("Business object name not given."); |
289 | 0 | throw new RuntimeException("Business object name not given."); |
290 | |
} |
291 | |
|
292 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
293 | 0 | checkBO(bo); |
294 | |
|
295 | 0 | populateSections(mapping, request, inquiryForm, bo); |
296 | |
|
297 | 0 | Inquirable kualiInquirable = inquiryForm.getInquirable(); |
298 | |
|
299 | 0 | return super.toggleTab(mapping, form, request, response); |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
@Override |
308 | |
public ActionForward hideAllTabs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
309 | |
|
310 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
311 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
312 | 0 | LOG.error("Business object name not given."); |
313 | 0 | throw new RuntimeException("Business object name not given."); |
314 | |
} |
315 | |
|
316 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
317 | 0 | checkBO(bo); |
318 | |
|
319 | 0 | populateSections(mapping, request, inquiryForm, bo); |
320 | |
|
321 | 0 | return super.hideAllTabs(mapping, form, request, response); |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
@Override |
328 | |
public ActionForward showAllTabs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
329 | |
|
330 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
331 | 0 | if (inquiryForm.getBusinessObjectClassName() == null) { |
332 | 0 | LOG.error("Business object name not given."); |
333 | 0 | throw new RuntimeException("Business object name not given."); |
334 | |
} |
335 | |
|
336 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
337 | 0 | checkBO(bo); |
338 | |
|
339 | 0 | populateSections(mapping, request, inquiryForm, bo); |
340 | |
|
341 | 0 | return super.showAllTabs(mapping, form, request, response); |
342 | |
} |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public ActionForward export(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { |
348 | 0 | InquiryForm inquiryForm = (InquiryForm) form; |
349 | 0 | if (inquiryForm.isCanExport()) { |
350 | 0 | BusinessObject bo = retrieveBOFromInquirable(inquiryForm); |
351 | 0 | checkBO(bo); |
352 | 0 | if (bo != null) { |
353 | 0 | BusinessObjectEntry businessObjectEntry = KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(inquiryForm.getBusinessObjectClassName()); |
354 | 0 | Class<? extends Exporter> exporterClass = businessObjectEntry.getExporterClass(); |
355 | 0 | if (exporterClass != null) { |
356 | 0 | Exporter exporter = exporterClass.newInstance(); |
357 | 0 | response.setContentType(KNSConstants.XML_MIME_TYPE); |
358 | 0 | response.setHeader("Content-disposition", "attachment; filename=export.xml"); |
359 | 0 | exporter.export(businessObjectEntry.getBusinessObjectClass(), Collections.singletonList(bo), KNSConstants.XML_FORMAT, response.getOutputStream()); |
360 | |
} |
361 | 0 | } else { |
362 | |
|
363 | 0 | populateSections(mapping, request, inquiryForm, bo); |
364 | 0 | return mapping.findForward(RiceConstants.MAPPING_BASIC); |
365 | |
} |
366 | |
} |
367 | |
|
368 | 0 | return null; |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
protected String extractCollectionName(HttpServletRequest request, String methodToCall) { |
377 | |
|
378 | 0 | String parameterName = (String) request.getAttribute(KNSConstants.METHOD_TO_CALL_ATTRIBUTE); |
379 | 0 | String collectionName = null; |
380 | 0 | if (StringUtils.isNotBlank(parameterName)) { |
381 | 0 | collectionName = StringUtils.substringBetween(parameterName, methodToCall + ".", ".("); |
382 | |
} |
383 | 0 | return collectionName; |
384 | |
} |
385 | |
|
386 | |
protected BusinessObject retrieveBOFromInquirable(InquiryForm inquiryForm) { |
387 | 0 | Inquirable kualiInquirable = inquiryForm.getInquirable(); |
388 | |
|
389 | 0 | BusinessObject bo = kualiInquirable.getBusinessObject(inquiryForm.retrieveInquiryDecryptedPrimaryKeys()); |
390 | 0 | if (bo == null) { |
391 | 0 | LOG.error("No records found in inquiry action."); |
392 | 0 | GlobalVariables.getMessageMap().putError(KNSConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INQUIRY); |
393 | |
} |
394 | 0 | return bo; |
395 | |
} |
396 | |
|
397 | |
protected void populateSections(ActionMapping mapping, HttpServletRequest request, InquiryForm inquiryForm, BusinessObject bo) { |
398 | 0 | Inquirable kualiInquirable = inquiryForm.getInquirable(); |
399 | |
|
400 | 0 | if (bo != null) { |
401 | |
|
402 | 0 | List sections = kualiInquirable.getSections(bo); |
403 | 0 | inquiryForm.setSections(sections); |
404 | 0 | kualiInquirable.addAdditionalSections(sections, bo); |
405 | 0 | } else { |
406 | 0 | inquiryForm.setSections(getEmptySections(kualiInquirable.getTitle())); |
407 | |
} |
408 | |
|
409 | 0 | request.setAttribute(KNSConstants.INQUIRABLE_ATTRIBUTE_NAME, kualiInquirable); |
410 | 0 | } |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
protected void streamToResponse(byte[] fileContents, String fileName, String fileContentType,HttpServletResponse response) throws Exception{ |
420 | 0 | ByteArrayOutputStream baos = null; |
421 | |
try{ |
422 | 0 | baos = new ByteArrayOutputStream(fileContents.length); |
423 | 0 | baos.write(fileContents); |
424 | 0 | WebUtils.saveMimeOutputStreamAsFile(response, fileContentType, baos, fileName); |
425 | 0 | }finally{ |
426 | 0 | try{ |
427 | 0 | if(baos!=null){ |
428 | 0 | baos.close(); |
429 | 0 | baos = null; |
430 | |
} |
431 | 0 | }catch(IOException ioEx){ |
432 | 0 | LOG.error("Error while downloading attachment"); |
433 | 0 | throw new RuntimeException("IOException occurred while downloading attachment", ioEx); |
434 | 0 | } |
435 | 0 | } |
436 | 0 | } |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
private List<Section> getEmptySections(String title) { |
443 | 0 | final Row row = new Row(Collections.<Field>emptyList()); |
444 | |
|
445 | 0 | final Section section = new Section(Collections.singletonList(row)); |
446 | 0 | section.setErrorKey("*"); |
447 | 0 | section.setSectionTitle(title != null ? title : ""); |
448 | 0 | section.setNumberOfColumns(0); |
449 | |
|
450 | 0 | return Collections.singletonList(section); |
451 | |
} |
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
private void checkBO(BusinessObject bo) { |
459 | 0 | if (bo == null && GlobalVariables.getMessageMap().hasNoMessages()) { |
460 | 0 | throw new UnsupportedOperationException("The record you have inquired on does not exist."); |
461 | |
} |
462 | 0 | } |
463 | |
|
464 | |
protected NoteService getNoteService() { |
465 | 0 | if ( noteService == null ) { |
466 | 0 | noteService = KNSServiceLocator.getNoteService(); |
467 | |
} |
468 | 0 | return this.noteService; |
469 | |
} |
470 | |
} |