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