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