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.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   * OlePatronRecordController is the controller class for Patron Record Controller
32   */
33  @Controller
34  @RequestMapping(value = "/patronrecordcontroller")
35  public class OlePatronRecordController extends UifControllerBase {
36      /**
37       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
38       */
39      private static final Logger LOG = Logger.getLogger(OlePatronRecordController.class);
40  
41      /**
42       *   This method will return new Instance of OlePatronRecordForm.
43       * @param request
44       * @return   OlePatronRecordForm.
45       */
46      @Override
47      protected OlePatronRecordForm createInitialForm(HttpServletRequest request) {
48          return new OlePatronRecordForm();
49      }
50  
51      /**
52       *   This method takes the initial request when click on OlePatronRecord Screen.
53       * @param form
54       * @param result
55       * @param request
56       * @param response
57       * @return  ModelAndView
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       * This method persist the data from patronXml file into database after uploading the patronXml.
69       * @param form
70       * @param result
71       * @param request
72       * @param response
73       * @return  ModelAndView
74       * @throws Exception
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      *  This method validate the xml file type.
109      * @param inputFile
110      * @return boolean
111      */
112     public boolean validateFile(String inputFile) {
113         return (inputFile.contains(".xml") ? true:false);
114     }
115 
116     /**
117      *  This method will download the Failure records as an attachment based on summaryId.
118      * @param form
119      * @param result
120      * @param request
121      * @param response
122      * @return null
123      * @throws Exception
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 }