1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.web.struts;
17
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.upload.FormFile;
35 import org.kuali.ole.select.businessobject.OleLoadFailureRecords;
36 import org.kuali.ole.select.businessobject.OleLoadSumRecords;
37 import org.kuali.ole.select.document.AcquisitionBatchInputFileDocument;
38 import org.kuali.ole.select.service.BatchLoadService;
39 import org.kuali.ole.select.service.impl.BatchLoadServiceImpl;
40 import org.kuali.ole.sys.OLEConstants;
41 import org.kuali.ole.sys.OLEKeyConstants;
42 import org.kuali.ole.sys.batch.BatchInputFileType;
43 import org.kuali.ole.sys.batch.BatchSpringContext;
44 import org.kuali.ole.sys.businessobject.AcquisitionBatchUpload;
45 import org.kuali.ole.sys.context.SpringContext;
46 import org.kuali.ole.sys.exception.FileStorageException;
47 import org.kuali.rice.kim.api.KimConstants;
48 import org.kuali.rice.kim.api.services.IdentityManagementService;
49 import org.kuali.rice.kns.question.ConfirmationQuestion;
50 import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
51 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
52 import org.kuali.rice.krad.exception.AuthorizationException;
53 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
54 import org.kuali.rice.krad.service.ModuleService;
55 import org.kuali.rice.krad.util.GlobalVariables;
56 import org.kuali.rice.krad.util.KRADConstants;
57
58
59
60
61
62
63 public class AcquisitionBatchInputFileAction extends KualiTransactionalDocumentActionBase {
64
65 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AcquisitionBatchInputFileAction.class);
66 private IdentityManagementService identityManagementService;
67
68 public AcquisitionBatchInputFileAction()
69 {
70 super();
71 }
72
73
74
75
76
77 @Override
78 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
79 ActionForward forward = super.execute(mapping, form, request, response);
80 setupForm((AcquisitionBatchInputFileForm) form);
81 return forward;
82 }
83
84 @Override
85 protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
86 AcquisitionBatchUpload acquisitionBatchUpload = ((AcquisitionBatchInputFileForm) form).getAcquisitionBatchUpload();
87 acquisitionBatchUpload.setBatchInputTypeName(OLEConstants.ORD_FILE_TYPE_INDENTIFIER);
88 BatchInputFileType batchInputFileType = retrieveBatchInputFileTypeImpl(acquisitionBatchUpload.getBatchInputTypeName());
89 HashMap<String,String> permissionDetails = new HashMap<String,String>();
90 permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, getNamespaceCode(batchInputFileType.getClass()));
91 permissionDetails.put(KimConstants.AttributeConstants.BEAN_NAME, acquisitionBatchUpload.getBatchInputTypeName());
92 if (!getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, OLEConstants.PermissionTemplate.UPLOAD_BATCH_INPUT_FILES.name, permissionDetails, null)) {
93 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), methodToCall, acquisitionBatchUpload.getBatchInputTypeName());
94 }
95 }
96
97
98
99
100 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
101 return super.docHandler(mapping, form, request, response);
102 }
103
104
105
106
107 private BatchInputFileType retrieveBatchInputFileTypeImpl(String batchInputTypeName) {
108 BatchInputFileType batchInputType = BatchSpringContext.getBatchInputFileType(batchInputTypeName);
109 if (batchInputType == null) {
110 LOG.error("Batch input type implementation not found for id " + batchInputTypeName);
111 throw new RuntimeException(("Batch input type implementation not found for id " + batchInputTypeName));
112 }
113
114 return batchInputType;
115 }
116
117
118
119
120
121
122 private void setupForm(AcquisitionBatchInputFileForm form) {
123 BatchInputFileType batchInputFileType = retrieveBatchInputFileTypeImpl(form.getAcquisitionBatchUpload().getBatchInputTypeName());
124
125 if (batchInputFileType == null) {
126 LOG.error("Batch input type implementation not found for id " + form.getAcquisitionBatchUpload().getBatchInputTypeName());
127 throw new RuntimeException(("Batch input type implementation not found for id " + form.getAcquisitionBatchUpload().getBatchInputTypeName()));
128 }
129
130
131
132
133 form.setTitleKey(batchInputFileType.getTitleKey());
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148
149 LOG.debug("Start-- upload Method of AcquisitionBatchInputFileAction");
150
151 try {
152 AcquisitionBatchInputFileForm acquisitionBatchInputFileForm=(AcquisitionBatchInputFileForm)form;
153 String docNumber=acquisitionBatchInputFileForm.getDocument().getDocumentNumber();
154 AcquisitionBatchInputFileDocument acquisitionBatchInputFileDocument=(AcquisitionBatchInputFileDocument)acquisitionBatchInputFileForm.getDocument();
155 AcquisitionBatchUpload acquisitionBatchUpload = acquisitionBatchInputFileForm.getAcquisitionBatchUpload();
156 FormFile uploadedFile = acquisitionBatchInputFileForm.getUploadFile();
157 BatchLoadService batchLoadService=(BatchLoadServiceImpl)SpringContext.getBean(BatchLoadServiceImpl.class);
158 long filesize=batchLoadService.getFileSize(uploadedFile);
159 if(acquisitionBatchUpload.getBatchLoadProfile()==null || "".equals(acquisitionBatchUpload.getBatchLoadProfile())){
160 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_LOAD_PROFILE, new String[] {});
161 return mapping.findForward(OLEConstants.MAPPING_BASIC);
162 }
163 if (uploadedFile == null || uploadedFile.getInputStream() == null || uploadedFile.getInputStream().available() == 0) {
164 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_SELECTED_SAVE, new String[] {});
165 return mapping.findForward(OLEConstants.MAPPING_BASIC);
166 }else if(batchLoadService.fileSizeValidation(filesize)){
167 return mapping.findForward(OLEConstants.MAPPING_BASIC);
168 }
169 acquisitionBatchInputFileForm.setFileName(uploadedFile.getFileName());
170 OleLoadSumRecords oleLoadSumRecords = new OleLoadSumRecords();
171 oleLoadSumRecords.setDocumentNumber(docNumber);
172 KRADServiceLocatorWeb.getDocumentService().saveDocument(acquisitionBatchInputFileDocument);
173 List<OleLoadFailureRecords> oleLoadFailureRecordsList=new ArrayList<OleLoadFailureRecords>(0);
174 boolean schemaFlag=acquisitionBatchInputFileDocument.prepareForLoadSummary(uploadedFile.getInputStream(),oleLoadFailureRecordsList,oleLoadSumRecords,uploadedFile,acquisitionBatchUpload);
175 if(!schemaFlag){
176 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_LOAD_SCHEMA, new String[] {});
177 return mapping.findForward(OLEConstants.MAPPING_BASIC);
178 }
179 acquisitionBatchInputFileForm.setOleLoadSumRecords(oleLoadSumRecords);
180 acquisitionBatchInputFileForm.setOleLoadFailureRecordsList(oleLoadFailureRecordsList);
181 if(oleLoadFailureRecordsList.size()>0){
182 FormFile formFile=getFormFileForAttachment(oleLoadSumRecords.getAcqLoadSumId());
183 acquisitionBatchInputFileForm.setAttachmentFile(formFile);
184 acquisitionBatchInputFileForm.getNewNote().setNoteText(OLEConstants.BATCH_FAILURE_ATCH_NAME);
185 super.insertBONote(mapping, form, request, response);
186 }
187 super.save(mapping, acquisitionBatchInputFileForm, request, response);
188 }
189
190 catch (FileStorageException e1) {
191 LOG.error("errors saving xml " + e1.getMessage(), e1);
192 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_SAVE, new String[] { e1.getMessage() });
193 }
194
195 LOG.debug("End -- upload Method of AcquisitionBatchInputFileAction");
196
197 return mapping.findForward(OLEConstants.MAPPING_VIEW);
198 }
199
200
201
202
203
204
205
206
207
208
209 public ActionForward clearValues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
210 LOG.debug("Start -- clearValues Method of AcquisitionBatchInputFileAction");
211 AcquisitionBatchInputFileForm acquisitionBatchInputFileForm = (AcquisitionBatchInputFileForm) form;
212 acquisitionBatchInputFileForm.getAcquisitionBatchUpload().setBatchLoadProfile(null);
213 acquisitionBatchInputFileForm.setUploadFile(null);
214 acquisitionBatchInputFileForm.getAcquisitionBatchUpload().setBatchDescription(null);
215 LOG.debug("End -- clearValues Method of AcquisitionBatchInputFileAction");
216 return mapping.findForward(OLEConstants.MAPPING_BASIC);
217 }
218
219
220
221
222 @Override
223 public ActionForward insertBONote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
224 {
225 super.insertBONote(mapping, form, request, response);
226 return mapping.findForward(OLEConstants.MAPPING_VIEW);
227 }
228
229
230
231
232 @Override
233 public ActionForward deleteBONote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
234 super.deleteBONote(mapping, form, request, response);
235 return mapping.findForward(OLEConstants.MAPPING_VIEW);
236 }
237
238
239
240
241
242
243
244
245
246
247 public ActionForward getLoadSummaryReports(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
248 LOG.debug("Start -- getLoadSummaryReports Method of AcquisitionBatchInputFileAction");
249
250 AcquisitionBatchInputFileForm acquisitionBatchInputFileForm = (AcquisitionBatchInputFileForm) form;
251 AcquisitionBatchInputFileDocument acquisitionBatchInputFileDocument = (AcquisitionBatchInputFileDocument) acquisitionBatchInputFileForm.getDocument();
252 OleLoadSumRecords oleLoadSumRecords = new OleLoadSumRecords();
253 List<OleLoadFailureRecords> oleLoadFailureRecordsList = new ArrayList<OleLoadFailureRecords>(0);
254 oleLoadSumRecords = acquisitionBatchInputFileDocument.getLoadSummaryAndFailureRecordsByLoadSummaryId(acquisitionBatchInputFileForm.getOleLoadSumRecords().getAcqLoadSumId(), oleLoadFailureRecordsList);
255 acquisitionBatchInputFileForm.setOleLoadSumRecords(oleLoadSumRecords);
256 acquisitionBatchInputFileForm.setOleLoadFailureRecordsList(oleLoadFailureRecordsList);
257 attachBONote(mapping, acquisitionBatchInputFileForm, request, response, oleLoadSumRecords.getAcqLoadSumId(),oleLoadFailureRecordsList.size());
258 return mapping.findForward(OLEConstants.MAPPING_VIEW);
259 }
260
261
262
263
264 @Override
265 public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
266 LOG.debug("Start -- docHandler Method of AcquisitionBatchInputFileAction");
267 super.docHandler(mapping, form, request, response);
268 AcquisitionBatchInputFileForm acquisitionBatchInputFileForm = (AcquisitionBatchInputFileForm) form;
269 AcquisitionBatchInputFileDocument acquisitionBatchInputFileDocument = (AcquisitionBatchInputFileDocument) acquisitionBatchInputFileForm.getDocument();
270 OleLoadSumRecords oleLoadSumRecords = new OleLoadSumRecords();
271 List<OleLoadFailureRecords> oleLoadFailureRecordsList = new ArrayList<OleLoadFailureRecords>(0);
272 oleLoadSumRecords = acquisitionBatchInputFileDocument.getLoadSummaryAndFailureRecordsByDocId(acquisitionBatchInputFileForm.getDocId(), oleLoadFailureRecordsList);
273 acquisitionBatchInputFileForm.setOleLoadSumRecords(oleLoadSumRecords);
274 acquisitionBatchInputFileForm.setOleLoadFailureRecordsList(oleLoadFailureRecordsList);
275 attachBONote(mapping, acquisitionBatchInputFileForm, request, response, oleLoadSumRecords.getAcqLoadSumId(),oleLoadFailureRecordsList.size());
276 return mapping.findForward(OLEConstants.MAPPING_VIEW);
277 }
278
279
280
281
282
283
284
285
286
287
288 public void attachBONote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response,int acqLoadSumId,int failureCount)throws Exception {
289 try {
290 AcquisitionBatchInputFileForm acquisitionBatchInputFileForm=(AcquisitionBatchInputFileForm)form;
291 if(acquisitionBatchInputFileForm.getDocument().getNotes().size()<=0)
292 {
293 FormFile formFile=getFormFileForAttachment(acqLoadSumId+OLEConstants.BATCH_FAILURE_BIB_FILE_ETN);
294 if(formFile!=null){
295 acquisitionBatchInputFileForm.setAttachmentFile(formFile);
296 if(failureCount==0)
297 acquisitionBatchInputFileForm.getNewNote().setNoteText(OLEConstants.BATCH_FAILURE_EXTRA_ATCH_NAME_BIB);
298 else
299 acquisitionBatchInputFileForm.getNewNote().setNoteText(OLEConstants.BATCH_FAILURE_ATCH_NAME_BIB);
300 super.insertBONote(mapping, form, request, response);
301 }
302 formFile=getFormFileForAttachment(acqLoadSumId+OLEConstants.BATCH_FAILURE_EDI_FILE_ETN);
303 if(formFile!=null){
304 acquisitionBatchInputFileForm.setAttachmentFile(formFile);
305 if(failureCount==0)
306 acquisitionBatchInputFileForm.getNewNote().setNoteText(OLEConstants.BATCH_FAILURE_EXTRA_ATCH_NAME_EDI);
307 else
308 acquisitionBatchInputFileForm.getNewNote().setNoteText(OLEConstants.BATCH_FAILURE_ATCH_NAME_EDI);
309 super.insertBONote(mapping, form, request, response);
310 }
311 }
312 }
313 catch (Exception ex) {
314 LOG.error("error while attaching BONote " + ex.getMessage(), ex);
315 throw new RuntimeException(ex);
316 }
317
318 }
319
320
321
322
323 @Override
324 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
325 {
326 super.route(mapping, form, request, response);
327 return mapping.findForward(OLEConstants.MAPPING_VIEW);
328 }
329
330
331
332
333 @Override
334 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
335 {
336 super.save(mapping, form, request, response);
337 return mapping.findForward(OLEConstants.MAPPING_VIEW);
338 }
339
340
341
342
343 @Override
344 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
345 {
346 Object question = request.getParameter(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME);
347 if (question == null) {
348 return this.performQuestionWithoutInput(mapping, form, request, response, KRADConstants.DOCUMENT_CANCEL_QUESTION, getKualiConfigurationService().getPropertyValueAsString("document.question.cancel.text"), KRADConstants.CONFIRMATION_QUESTION, KRADConstants.MAPPING_CANCEL, "");
349 }
350 else {
351 Object buttonClicked = request.getParameter(KRADConstants.QUESTION_CLICKED_BUTTON);
352 if ((KRADConstants.DOCUMENT_CANCEL_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
353 return mapping.findForward(OLEConstants.MAPPING_VIEW);
354 }
355 }
356 KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
357 doProcessingAfterPost( kualiDocumentFormBase, request );
358 if ( getDocumentService().documentExists(kualiDocumentFormBase.getDocId())){
359 getDocumentService().cancelDocument(kualiDocumentFormBase.getDocument(), kualiDocumentFormBase.getAnnotation());
360 }
361 return returnToSender(request, mapping, kualiDocumentFormBase);
362
363 }
364
365 @Override
366 public ActionForward close(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
367 KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
368 return returnToSender(request, mapping, kualiDocumentFormBase);
369 }
370
371 @Override
372 public ActionForward showAllTabs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
373 super.showAllTabs(mapping, form, request, response);
374 return mapping.findForward(OLEConstants.MAPPING_VIEW);
375 }
376 @Override
377 public ActionForward hideAllTabs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
378 super.hideAllTabs(mapping, form, request, response);
379 return mapping.findForward(OLEConstants.MAPPING_VIEW);
380 }
381
382 protected static String getNamespaceCode(Class<? extends Object> clazz) {
383 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
384 if (moduleService == null) {
385 return KimConstants.KIM_TYPE_DEFAULT_NAMESPACE;
386 }
387 return moduleService.getModuleConfiguration().getNamespaceCode();
388 }
389
390 protected IdentityManagementService getIdentityManagementService() {
391 if (identityManagementService == null) {
392 identityManagementService = SpringContext.getBean(IdentityManagementService.class);
393 }
394 return identityManagementService;
395 }
396
397
398 public FormFile getFormFileForAttachment(int acqLoadSumId) throws Exception {
399 try {
400 File f = new File(getBatchLoadService().getDestinationPath() + acqLoadSumId + OLEConstants.BATCH_FAILURE_FILE_MRK);
401 if (f.exists()) {
402 FormFile diskFile = new TempFormFile(f);
403 diskFile.setContentType("application/octet-stream");
404 diskFile.setFileName(acqLoadSumId + OLEConstants.BATCH_FAILURE_FILE_MRK);
405 diskFile.setFileSize((int) f.length());
406 return diskFile;
407 }
408 } catch (Exception ex) {
409 throw new RuntimeException( "Error attempting to get " + acqLoadSumId + " attachment", ex);
410 }
411 return null;
412 }
413
414 public FormFile getFormFileForAttachment(String fileName) throws Exception {
415 try {
416 File f = new File(getBatchLoadService().getDestinationPath() + fileName);
417 if (f.exists()) {
418 FormFile diskFile = new TempFormFile(f);
419 diskFile.setContentType("application/octet-stream");
420 diskFile.setFileName(fileName);
421 diskFile.setFileSize((int) f.length());
422 return diskFile;
423 }
424 } catch (Exception ex) {
425 throw new RuntimeException( "Error attempting to get " + fileName + " attachment", ex);
426 }
427 return null;
428 }
429
430 protected static class TempFormFile implements FormFile {
431
432 File f;
433 String contentType;
434 int fileSize;
435 String fileName;
436
437 public TempFormFile( File f ) {
438 this.f = f;
439 }
440
441 @Override
442 public void destroy() {}
443
444 @Override
445 public byte[] getFileData() throws FileNotFoundException, IOException {
446 byte[] fileData = new byte[getFileSize()];
447 FileInputStream fis = null;
448
449 try {
450 fis = new FileInputStream(f);
451 fis.read(fileData);
452 } catch (IOException e) {
453 fileData = null;
454 } finally {
455 if (fis != null) {
456 try {
457 fis.close();
458 } catch (IOException e) {
459
460 }
461 }
462 }
463
464 return fileData;
465 }
466
467 @Override
468 public InputStream getInputStream() throws FileNotFoundException, IOException {
469 return new FileInputStream(f);
470 }
471
472 @Override
473 public String getContentType() {
474 return contentType;
475 }
476
477 @Override
478 public void setContentType(String contentType) {
479 this.contentType = contentType;
480 }
481
482 @Override
483 public int getFileSize() {
484 return fileSize;
485 }
486
487 @Override
488 public void setFileSize(int fileSize) {
489 this.fileSize = fileSize;
490 }
491
492 @Override
493 public String getFileName() {
494 return fileName;
495 }
496
497 @Override
498 public void setFileName(String fileName) {
499 this.fileName = fileName;
500 }
501
502 }
503
504 protected BatchLoadService batchLoadService;
505
506 public BatchLoadService getBatchLoadService() {
507 if(batchLoadService==null) {
508 batchLoadService=SpringContext.getBean(BatchLoadServiceImpl.class);
509 }
510 return batchLoadService;
511 }
512
513
514 }
515