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.PropertyUtil;
7 import org.kuali.ole.patron.bo.OlePatronIngestSummaryRecord;
8 import org.kuali.ole.service.OlePatronConverterService;
9 import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
10 import org.kuali.ole.ingest.form.OlePatronRecordForm;
11 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12 import org.kuali.rice.krad.service.KRADServiceLocator;
13 import org.kuali.rice.krad.web.controller.UifControllerBase;
14 import org.kuali.rice.krad.web.form.UifFormBase;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.context.ApplicationContext;
17 import org.springframework.stereotype.Controller;
18 import org.springframework.validation.BindingResult;
19 import org.springframework.web.bind.annotation.ModelAttribute;
20 import org.springframework.web.bind.annotation.RequestMapping;
21 import org.springframework.web.multipart.MultipartFile;
22 import org.springframework.web.servlet.ModelAndView;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29
30
31
32
33 @Controller
34 @RequestMapping(value = "/patronrecordcontroller")
35 public class OlePatronRecordController extends UifControllerBase {
36
37
38
39 private static final Logger LOG = Logger.getLogger(OlePatronRecordController.class);
40
41
42
43
44
45
46 @Override
47 protected OlePatronRecordForm createInitialForm(HttpServletRequest request) {
48 return new OlePatronRecordForm();
49 }
50
51
52
53
54
55
56
57
58
59 @Override
60 @RequestMapping(params = "methodToCall=start")
61 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
62 HttpServletRequest request, HttpServletResponse response) {
63 OlePatronRecordForm olePatronRecordForm = (OlePatronRecordForm) form;
64 return super.start(olePatronRecordForm, result, request, response);
65 }
66
67
68
69
70
71
72
73
74
75
76 @RequestMapping(params = "methodToCall=upload")
77 public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
78 HttpServletRequest request, HttpServletResponse response) throws Exception {
79 OlePatronRecordForm olePatronRecordForm = (OlePatronRecordForm) form;
80 OlePatronConverterService olePatronRecordService = GlobalResourceLoader.getService(OLEConstants.PATRON_CONVERTER_SERVICE);
81 OlePatronXMLSchemaValidator olePatronXMLSchemaValidator = new OlePatronXMLSchemaValidator();
82 OlePatronIngestSummaryRecord olePatronIngestSummaryRecord = new OlePatronIngestSummaryRecord();
83 MultipartFile multipartFile = olePatronRecordForm.getPatronFile();
84 String fileName = multipartFile.getOriginalFilename();
85 if (validateFile(multipartFile.getOriginalFilename())) {
86 String fileContent = new String(multipartFile.getBytes());
87 try {
88 boolean schemaFlag = olePatronXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream());
89 if(!schemaFlag){
90 olePatronRecordForm.setMessage(OLEConstants.PATRON_RECORD_INVALID_SCHEMA);
91 return super.start(olePatronRecordForm, result, request, response);
92 }
93 boolean addUnMatchedPatronFlag = olePatronRecordForm.isAddUnmatchedPatron();
94 boolean deletePatron = olePatronRecordForm.isDeletePatron();
95 olePatronRecordService.persistPatronFromFileContent(fileContent,addUnMatchedPatronFlag,deletePatron,fileName,olePatronIngestSummaryRecord);
96 olePatronRecordForm.setMessage(olePatronRecordService.getUploadProcessMessage(olePatronIngestSummaryRecord));
97 } catch (Exception patronRecordIngestException) {
98 olePatronRecordForm.setMessage(OLEConstants.PATRON_RECORD_FAILURE);
99 LOG.error("Failed to upload Patron record.", patronRecordIngestException);
100 }
101 } else {
102 olePatronRecordForm.setMessage(OLEConstants.PATRON_RECORD_SELECT_FILE);
103 }
104 return super.start(olePatronRecordForm, result, request, response);
105 }
106
107
108
109
110
111
112 public boolean validateFile(String inputFile) {
113 return (inputFile.contains(".xml") ? true:false);
114 }
115
116
117
118
119
120
121
122
123
124
125 @RequestMapping(params = "methodToCall=downloadAttachment")
126 public ModelAndView downloadAttachment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
127 HttpServletRequest request, HttpServletResponse response) throws Exception {
128 LOG.info("Start -- DownLoad Method of OlePatronRecordForm");
129 String olePatronSummaryId=request.getParameter(OLEConstants.PATRON_SUMMARY_REPORT_ID);
130 OlePatronRecordForm olePatronRecordForm = (OlePatronRecordForm) form;
131 olePatronRecordForm.setOlePatronSummaryId(olePatronSummaryId);
132 String directory = PropertyUtil.getPropertyUtil().getProperty(OLEConstants.STAGING_DIRECTORY)+
133 OLEConstants.PATRON_FILE_DIRECTORY;
134 String homeDirectory = System.getProperty(OLEConstants.USER_HOME_DIRECTORY);
135 File file=new File(homeDirectory+directory+olePatronSummaryId+OLEConstants.FAILED_PATRON_RECORD_NAME);
136 response.setContentType("application/octet-stream");
137 response.setHeader("Content-disposition", "attachment; filename=" + olePatronSummaryId+OLEConstants.FAILED_PATRON_RECORD_NAME);
138 response.setHeader("Expires", "0");
139 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
140 response.setHeader("Pragma", "public");
141 response.setContentLength((int) file.length());
142 InputStream fis = new FileInputStream(file);
143 IOUtils.copy(fis, response.getOutputStream());
144 response.getOutputStream().flush();
145
146 LOG.info("End -- DownLoad Method of AcquisitionBatchInputFileAction");
147 return null;
148 }
149
150
151
152
153 }