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