View Javadoc

1   package org.kuali.ole.ingest.controller;
2   
3   /**
4    * Created by IntelliJ IDEA.
5    * User: balakumaranm
6    * Date: 3/28/12
7    * Time: 3:24 PM
8    * To change this template use File | Settings | File Templates.
9    */
10  
11  import org.apache.log4j.Logger;
12  import org.kuali.ole.OLEConstants;
13  import org.kuali.ole.PropertyUtil;
14  import org.kuali.ole.ingest.IngestProcessor;
15  import org.kuali.ole.ingest.ProfileBuilder;
16  import org.kuali.ole.ingest.form.StaffUploadForm;
17  import org.kuali.ole.ingest.pojo.IngestRecord;
18  import org.kuali.rice.krad.util.GlobalVariables;
19  import org.kuali.rice.krad.web.controller.UifControllerBase;
20  import org.kuali.rice.krad.web.form.UifFormBase;
21  import org.springframework.stereotype.Controller;
22  import org.springframework.validation.BindingResult;
23  import org.springframework.web.bind.annotation.ModelAttribute;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.multipart.MultipartFile;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  
32  /**
33   * StaffUploadController is the controller class for Staff Upload Controller.
34   */
35  @Controller
36  @RequestMapping(value = "/staffuploadcontroller")
37  public class StaffUploadController
38          extends UifControllerBase {
39      /**
40       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
41       */
42      private static final Logger LOG = Logger.getLogger(StaffUploadController.class);
43  
44      /**
45       * This method will return instance of StaffUploadForm.
46       * @param request
47       * @return  StaffUploadForm
48       */
49  
50      @Override
51      protected StaffUploadForm createInitialForm(HttpServletRequest request) {
52          return new StaffUploadForm();
53      }
54  
55      /**
56       * This method will take the initial request when click on StaffUpload screen.
57       * @param form
58       * @param result
59       * @param request
60       * @param response
61       * @return  ModelAndView
62       */
63      @Override
64      @RequestMapping(params = "methodToCall=start")
65      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
66                                HttpServletRequest request, HttpServletResponse response) {
67          StaffUploadForm staffUploadForm = (StaffUploadForm) form;
68          setLoadReportURL(staffUploadForm);
69          return super.start(staffUploadForm, result, request, response);
70      }
71  
72      /**
73       * This method persist the data from StaffUploadXml file into database after uploading the StaffUploadXml..
74       * @param form
75       * @param result
76       * @param request
77       * @param response
78       * @return  ModelAndView
79       * @throws Exception
80       */
81      @RequestMapping(params = "methodToCall=upload")
82      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
83                                 HttpServletRequest request, HttpServletResponse response)throws Exception {
84          StaffUploadForm staffUploadForm = (StaffUploadForm) form;
85       
86          MultipartFile marcFile = staffUploadForm.getMarcFile();
87          MultipartFile ediFile = staffUploadForm.getEdiFile();
88          String agendaName=null;
89          String agendaDescription = null;
90          agendaName = staffUploadForm.getAgenda();
91          agendaDescription = staffUploadForm.getAgendaDescription();
92          if (null != agendaName && !agendaName.equals("")) {
93              String marcFileContent = new String(marcFile.getBytes());
94              String ediFileContent = new String(ediFile.getBytes());
95  
96              if(validateFile(marcFile.getOriginalFilename(),ediFile.getOriginalFilename())){
97                  IngestRecord ingestRecord = new IngestRecord();
98                  ingestRecord.setOriginalEdiFileName(ediFile.getOriginalFilename());
99                  ingestRecord.setOriginalMarcFileName(marcFile.getOriginalFilename());
100                 ingestRecord.setAgendaName(agendaName);
101                 ingestRecord.setAgendaDescription(agendaDescription);
102                 ingestRecord.setEdiFileContent(ediFileContent);
103                 ingestRecord.setMarcFileContent(marcFileContent);
104                 ingestRecord.setByPassPreProcessing(isPreProcessingRequired(marcFile, ediFile));
105                 try {
106                     getIngestProcessor().start(ingestRecord);
107                     staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_SUCCESS);
108                 } catch (Exception staffUploadException) {
109                     staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_FAILURE);
110                     LOG.error("Failed to perform Staff Upload:", staffUploadException);
111                 }
112 
113             } else {
114                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_MESSAGE_UPLOAD);
115                 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
116             }
117         } else {
118                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_AGENDA_NAME);
119             staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
120         }
121         setLoadReportURL(staffUploadForm);
122 
123 
124         //return super.start(staffUploadForm, result, request, response);
125         return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
126     }
127 
128     /**
129      * Sets the LoadReportUrl attribute value.
130      * @param staffUploadForm
131      */
132     private void setLoadReportURL(StaffUploadForm staffUploadForm) {
133         String LOADREPORT_URL="loadreport.url";
134         String loadreportURL = PropertyUtil.getPropertyUtil().getProperty(LOADREPORT_URL);
135         staffUploadForm.setLoadReportURL(loadreportURL);
136     }
137 
138     /**
139      * This method validate the xml fileType
140      * @param marcFile
141      * @param ediFile
142      * @return boolean
143      */
144     public boolean validateFile(String marcFile, String ediFile) {
145         return (marcFile.contains(".xml") && ediFile.contains(".edi") ||
146                 marcFile.contains(".mrc") && ediFile.contains(".xml") ||
147                 !(marcFile.contains(".mrc") && ediFile.contains(".edi")
148                         || (marcFile.contains(".xml") && ediFile.contains(".xml")))? false:true);
149     }
150 
151     /**
152      *  This method checks whether preProcessing is required or not based on the originalFileName.
153      * @param marcFile
154      * @param ediFile
155      * @return   boolean
156      */
157     private boolean isPreProcessingRequired(MultipartFile marcFile, MultipartFile ediFile) {
158         return (marcFile.getOriginalFilename().contains(".mrc") && ediFile.getOriginalFilename().contains(".edi") ? true : false);
159     }
160 
161     /**
162      *  This method returns the instance of Ingest Processor.
163      * @return IngestProcessor
164      */
165     private IngestProcessor getIngestProcessor() {
166         return new IngestProcessor();
167     }
168 
169 
170 }
171