View Javadoc
1   package org.kuali.ole.ingest.controller;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.ingest.OleLocationXMLSchemaValidator;
7   import org.kuali.ole.ingest.form.OleLocationForm;
8   import org.kuali.ole.service.OleLocationConverterService;
9   import org.kuali.rice.core.api.config.property.ConfigContext;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.krad.web.controller.UifControllerBase;
12  import org.kuali.rice.krad.web.form.UifFormBase;
13  import org.springframework.stereotype.Controller;
14  import org.springframework.validation.BindingResult;
15  import org.springframework.web.bind.annotation.ModelAttribute;
16  import org.springframework.web.bind.annotation.RequestMapping;
17  import org.springframework.web.multipart.MultipartFile;
18  import org.springframework.web.servlet.ModelAndView;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.InputStream;
25  
26  /**
27   * OleLocationController is the controller class for Location Controller
28   */
29  @Controller
30  @RequestMapping(value = "/locationcontroller")
31  public class OleLocationController extends UifControllerBase {
32  
33      private static final Logger LOG = Logger.getLogger(OleLocationController.class);
34  
35      /**
36       *  This method will return instance of OleLocationForm.
37       * @param request
38       * @return   OleLocationForm.
39       */
40       @Override
41      protected OleLocationForm createInitialForm(HttpServletRequest request) {
42          return new OleLocationForm();
43      }
44  
45      /**
46       *  This method takes the initial request when click on OleLocation Screen.
47       * @param form
48       * @param result
49       * @param request
50       * @param response
51       * @return  ModelAndView.
52       */
53      @Override
54      @RequestMapping(params = "methodToCall=start")
55      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
56                                HttpServletRequest request, HttpServletResponse response) {
57          OleLocationForm oleLocationForm = (OleLocationForm) form;
58          return super.start(oleLocationForm, result, request, response);
59      }
60  
61      /**
62       *   This method persist the data from locationXml file into database after uploading the locationXml.
63       * @param form
64       * @param result
65       * @param request
66       * @param response
67       * @return  ModelAndView.
68       * @throws Exception
69       */
70      @RequestMapping(params = "methodToCall=upload")
71      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
72                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
73          OleLocationForm oleLocationForm = (OleLocationForm) form;
74          OleLocationConverterService oleLocationRecordService = GlobalResourceLoader.getService("oleLocationConverterService");
75          OleLocationXMLSchemaValidator oleLocationXMLSchemaValidator = new OleLocationXMLSchemaValidator();
76          MultipartFile multipartFile = oleLocationForm.getLocationFile();
77          String locationFileName = multipartFile.getOriginalFilename();
78          if (validateFile(multipartFile.getOriginalFilename())) {
79              String fileContent = new String(multipartFile.getBytes());
80              try {
81                  boolean schemaFlag = oleLocationXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream());
82                  if(!schemaFlag){
83                      oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_INVALID_SCHEMA);
84                      return super.start(oleLocationForm, result, request, response);
85                  }
86                  String summary=oleLocationRecordService.persistLocationFromFileContent(fileContent,locationFileName);
87                  oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_SUCCESS+" "+summary);
88              } catch (Exception locationIngestException) {
89                  oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_FAILURE);
90                  LOG.error("Failed to upload location.", locationIngestException);
91              }
92          } else {
93              oleLocationForm.setMessage(OLEConstants.LOCATION_RECORD_SELECT_FILE);
94          }
95          return super.start(oleLocationForm, result, request, response);
96      }
97  
98      /**
99       *  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 }