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