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
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
37
38
39
40 @Override
41 protected OleLocationForm createInitialForm(HttpServletRequest request) {
42 return new OleLocationForm();
43 }
44
45
46
47
48
49
50
51
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
63
64
65
66
67
68
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
100
101
102
103 public boolean validateFile(String inputFile) {
104 return (inputFile.contains(".xml") ? true:false);
105 }
106
107
108
109
110
111
112
113
114
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 }