001package org.kuali.ole.ingest.controller; 002 003import org.apache.commons.io.IOUtils; 004import org.apache.log4j.Logger; 005import org.kuali.ole.OLEConstants; 006import org.kuali.ole.ingest.OleLocationXMLSchemaValidator; 007import org.kuali.ole.ingest.form.OleLocationForm; 008import org.kuali.ole.service.OleLocationConverterService; 009import org.kuali.rice.core.api.config.property.ConfigContext; 010import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 011import org.kuali.rice.krad.web.controller.UifControllerBase; 012import org.kuali.rice.krad.web.form.UifFormBase; 013import org.springframework.stereotype.Controller; 014import org.springframework.validation.BindingResult; 015import org.springframework.web.bind.annotation.ModelAttribute; 016import org.springframework.web.bind.annotation.RequestMapping; 017import org.springframework.web.multipart.MultipartFile; 018import org.springframework.web.servlet.ModelAndView; 019 020import javax.servlet.http.HttpServletRequest; 021import javax.servlet.http.HttpServletResponse; 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.InputStream; 025 026/** 027 * OleLocationController is the controller class for Location Controller 028 */ 029@Controller 030@RequestMapping(value = "/locationcontroller") 031public class OleLocationController extends UifControllerBase { 032 033 private static final Logger LOG = Logger.getLogger(OleLocationController.class); 034 035 /** 036 * This method will return instance of OleLocationForm. 037 * @param request 038 * @return OleLocationForm. 039 */ 040 @Override 041 protected OleLocationForm createInitialForm(HttpServletRequest request) { 042 return new OleLocationForm(); 043 } 044 045 /** 046 * This method takes the initial request when click on OleLocation Screen. 047 * @param form 048 * @param result 049 * @param request 050 * @param response 051 * @return ModelAndView. 052 */ 053 @Override 054 @RequestMapping(params = "methodToCall=start") 055 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 056 HttpServletRequest request, HttpServletResponse response) { 057 OleLocationForm oleLocationForm = (OleLocationForm) form; 058 return super.start(oleLocationForm, result, request, response); 059 } 060 061 /** 062 * This method persist the data from locationXml file into database after uploading the locationXml. 063 * @param form 064 * @param result 065 * @param request 066 * @param response 067 * @return ModelAndView. 068 * @throws Exception 069 */ 070 @RequestMapping(params = "methodToCall=upload") 071 public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 072 HttpServletRequest request, HttpServletResponse response) throws Exception { 073 OleLocationForm oleLocationForm = (OleLocationForm) form; 074 OleLocationConverterService oleLocationRecordService = GlobalResourceLoader.getService("oleLocationConverterService"); 075 OleLocationXMLSchemaValidator oleLocationXMLSchemaValidator = new OleLocationXMLSchemaValidator(); 076 MultipartFile multipartFile = oleLocationForm.getLocationFile(); 077 String locationFileName = multipartFile.getOriginalFilename(); 078 if (validateFile(multipartFile.getOriginalFilename())) { 079 String fileContent = new String(multipartFile.getBytes()); 080 try { 081 boolean schemaFlag = oleLocationXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream()); 082 if(!schemaFlag){ 083 oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_INVALID_SCHEMA); 084 return super.start(oleLocationForm, result, request, response); 085 } 086 String summary=oleLocationRecordService.persistLocationFromFileContent(fileContent,locationFileName); 087 oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_SUCCESS+" "+summary); 088 } catch (Exception locationIngestException) { 089 oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_FAILURE); 090 LOG.error("Failed to upload location.", locationIngestException); 091 } 092 } else { 093 oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_SELECT_FILE); 094 } 095 return super.start(oleLocationForm, result, request, response); 096 } 097 098 /** 099 * This method validate the xml file type. 100 * @param inputFile 101 * @return boolean. 102 */ 103 public boolean validateFile(String inputFile) { 104 return (inputFile.contains(".xml") ? true:false); 105 } 106 107 /** 108 * This method will download the Failure records as an attachment based on summaryId. 109 * @param form 110 * @param result 111 * @param request 112 * @param response 113 * @return null. 114 * @throws Exception 115 */ 116 @RequestMapping(params = "methodToCall=downloadAttachment") 117 public ModelAndView downloadAttachment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 118 HttpServletRequest request, HttpServletResponse response) throws Exception { 119 LOG.info("Start -- DownLoad Method of OleLocationRecordForm"); 120 String oleLocationSummaryId=request.getParameter(OLEConstants.LOCATION_SUMMARY_REPORT_ID); 121 OleLocationForm oleLocationForm = (OleLocationForm) form; 122 oleLocationForm.setOleLocationSummaryId(oleLocationSummaryId); 123 String directory =ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.LOCATION_ERROR_FILE_PATH); 124 String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY); 125 File file=new File(directory+oleLocationSummaryId+OLEConstants.FAILED_LOCATION_RECORD_NAME); 126 response.setContentType("application/octet-stream"); 127 response.setHeader("Content-disposition", "attachment; filename=" + oleLocationSummaryId+OLEConstants.FAILED_LOCATION_RECORD_NAME); 128 response.setHeader("Expires", "0"); 129 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 130 response.setHeader("Pragma", "public"); 131 response.setContentLength((int) file.length()); 132 133 InputStream fis = new FileInputStream(file); 134 IOUtils.copy(fis, response.getOutputStream()); 135 response.getOutputStream().flush(); 136 137 // } 138 LOG.info("End -- DownLoad Method of AcquisitionBatchInputFileAction"); 139 return null; 140 } 141}