001/*
002 * Copyright 2007 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys.web.struts;
017
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.struts.action.ActionForm;
032import org.apache.struts.action.ActionForward;
033import org.apache.struts.action.ActionMapping;
034import org.apache.struts.upload.FormFile;
035import org.kuali.ole.select.businessobject.OleLoadFailureRecords;
036import org.kuali.ole.select.businessobject.OleLoadSumRecords;
037import org.kuali.ole.select.document.AcquisitionBatchInputFileDocument;
038import org.kuali.ole.select.service.BatchLoadService;
039import org.kuali.ole.select.service.impl.BatchLoadServiceImpl;
040import org.kuali.ole.sys.OLEConstants;
041import org.kuali.ole.sys.OLEKeyConstants;
042import org.kuali.ole.sys.batch.BatchInputFileType;
043import org.kuali.ole.sys.batch.BatchSpringContext;
044import org.kuali.ole.sys.businessobject.AcquisitionBatchUpload;
045import org.kuali.ole.sys.context.SpringContext;
046import org.kuali.ole.sys.exception.FileStorageException;
047import org.kuali.rice.kim.api.KimConstants;
048import org.kuali.rice.kim.api.services.IdentityManagementService;
049import org.kuali.rice.kns.question.ConfirmationQuestion;
050import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
051import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
052import org.kuali.rice.krad.exception.AuthorizationException;
053import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
054import org.kuali.rice.krad.service.ModuleService;
055import org.kuali.rice.krad.util.GlobalVariables;
056import org.kuali.rice.krad.util.KRADConstants;
057
058
059
060/**
061 * Handles actions from the batch upload screen.
062 */
063public class AcquisitionBatchInputFileAction extends KualiTransactionalDocumentActionBase {
064
065    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AcquisitionBatchInputFileAction.class);
066    private IdentityManagementService identityManagementService;
067
068    public AcquisitionBatchInputFileAction()
069    {
070        super();
071    }
072
073    /**
074     * @see org.kuali.rice.kns.web.struts.action.KualiAction#execute(org.apache.struts.action.ActionMapping,
075     *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
076     */
077    @Override
078    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
079        ActionForward forward = super.execute(mapping, form, request, response);
080        setupForm((AcquisitionBatchInputFileForm) form);
081        return forward;
082    }
083
084    @Override
085    protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
086        AcquisitionBatchUpload acquisitionBatchUpload = ((AcquisitionBatchInputFileForm) form).getAcquisitionBatchUpload();
087        acquisitionBatchUpload.setBatchInputTypeName(OLEConstants.ORD_FILE_TYPE_INDENTIFIER);
088        BatchInputFileType batchInputFileType = retrieveBatchInputFileTypeImpl(acquisitionBatchUpload.getBatchInputTypeName());
089        HashMap<String,String> permissionDetails = new HashMap<String,String>();
090        permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, getNamespaceCode(batchInputFileType.getClass()));
091        permissionDetails.put(KimConstants.AttributeConstants.BEAN_NAME, acquisitionBatchUpload.getBatchInputTypeName());
092        if (!getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, OLEConstants.PermissionTemplate.UPLOAD_BATCH_INPUT_FILES.name, permissionDetails, null)) {
093            throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), methodToCall, acquisitionBatchUpload.getBatchInputTypeName());
094        }
095        }
096
097    /**
098     * Forwards to the batch upload JSP. Initial request.
099     */
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     * Retrieves a BatchInputFileType implementation from Spring based on the given name.
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     * Builds list of filenames that the user has permission to manage, and populates the form member. Sets the title key from the
119     * batch input type.
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       /*ParameterService parmeterService =  SpringContext.getBean(ParameterService.class);
130        String url = parmeterService.getParameterValue(BatchUpload.class,OLEConstants.BATCH_UPLOAD_HELP_SYS_PARAM_NAME, batchInputFileType.getFileTypeIdentifer());
131        form.setUrl(url);*/
132          // set title key
133        form.setTitleKey(batchInputFileType.getTitleKey());
134    }
135
136    /**
137     *
138     * This method... Processing the Upload file
139     * @param mapping
140     * @param form
141     * @param request
142     * @param response
143     * @return
144     * @throws Exception
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     * This method... for clearing the data
202     * @param mapping
203     * @param form
204     * @param request
205     * @param response
206     * @return
207     * @throws Exception
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     * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#insertBONote(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
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     * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#deleteBONote(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
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     * This method uses for retrieve the load summary information uses the load summary id
240     * @param mapping
241     * @param form
242     * @param request
243     * @param response
244     * @return
245     * @throws Exception
246     */
247    public ActionForward getLoadSummaryReports(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
248        LOG.debug("Start -- getLoadSummaryReports Method of AcquisitionBatchInputFileAction");
249        // super.docHandler(mapping, form, request, response);
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    * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#docHandler(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
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    * This method uses for attaching the add note information to batch upload screen
281    * @param mapping
282    * @param form
283    * @param request
284    * @param response
285    * @param acqLoadSumId
286    * @throws Exception
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    * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#route(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
322    */
323   @Override
324public 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    * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#save(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
332    */
333   @Override
334public 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    * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#cancel(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
342    */
343   @Override
344public 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                        // ignore
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