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.ingest.IngestProcessor;
14  import org.kuali.ole.ingest.form.StaffUploadForm;
15  import org.kuali.ole.ingest.pojo.IngestRecord;
16  import org.kuali.rice.core.api.config.property.ConfigContext;
17  import org.kuali.rice.krad.web.controller.UifControllerBase;
18  import org.kuali.rice.krad.web.form.UifFormBase;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.validation.BindingResult;
21  import org.springframework.web.bind.annotation.ModelAttribute;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.multipart.MultipartFile;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  
30  /**
31   * StaffUploadController is the controller class for Staff Upload Controller.
32   */
33  @Controller
34  @RequestMapping(value = "/staffuploadcontroller")
35  public class StaffUploadController
36          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(StaffUploadController.class);
41  
42      /**
43       * This method will return instance of StaffUploadForm.
44       * @param request
45       * @return  StaffUploadForm
46       */
47  
48      @Override
49      protected StaffUploadForm createInitialForm(HttpServletRequest request) {
50          return new StaffUploadForm();
51      }
52  
53      /**
54       * This method will take the initial request when click on StaffUpload 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          StaffUploadForm staffUploadForm = (StaffUploadForm) form;
66          String user = request.getParameter(OLEConstants.LOGIN_USR);
67          staffUploadForm.setUser(user);
68          loadReportURL(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 user = staffUploadForm.getUser();
89          String agendaName=null;
90          String agendaDescription = null;
91          agendaName = staffUploadForm.getAgenda();
92          agendaDescription = staffUploadForm.getAgendaDescription();
93          if (null != agendaName && !agendaName.equals("")) {
94              String marcFileContent = new String(marcFile.getBytes());
95              String ediFileContent = new String(ediFile.getBytes());
96  
97              if(validateFile(marcFile.getOriginalFilename(),ediFile.getOriginalFilename())){
98                  IngestRecord ingestRecord = new IngestRecord();
99                  ingestRecord.setOriginalEdiFileName(ediFile.getOriginalFilename());
100                 ingestRecord.setOriginalMarcFileName(marcFile.getOriginalFilename());
101                 ingestRecord.setAgendaName(agendaName);
102                 ingestRecord.setAgendaDescription(agendaDescription);
103                 ingestRecord.setEdiFileContent(ediFileContent);
104                 ingestRecord.setMarcFileContent(marcFileContent);
105                 ingestRecord.setByPassPreProcessing(isPreProcessingRequired(marcFile, ediFile));
106                 ingestRecord.setUser(user);
107                 try {
108                     boolean failure_flag=true;
109                     int recordCount=getIngestProcessor().start(ingestRecord , failure_flag, null, null);
110                     if(failure_flag)
111                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_SUCCESS);
112                     else if(ingestRecord.isUpdate())
113                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_UPDATE_SUCCESS);
114                     else
115                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_FAILURE);
116                 } catch (Exception staffUploadException) {
117                     staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_FAILURE);
118                     LOG.error("Failed to perform Staff Upload:", staffUploadException);
119                 }
120 
121             } else {
122                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_MESSAGE_UPLOAD);
123                 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
124             }
125         } else {
126                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_AGENDA_NAME);
127             staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
128         }
129         loadReportURL(staffUploadForm);
130 
131 
132         //return super.start(staffUploadForm, result, request, response);
133         return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
134     }
135 
136     /**
137      * Sets the LoadReportUrl attribute value.
138      * @param staffUploadForm
139      */
140     private void loadReportURL(StaffUploadForm staffUploadForm) {
141         String LOADREPORT_URL="loadreport.url";
142         String loadreportURL = ConfigContext.getCurrentContextConfig().getProperty(LOADREPORT_URL);
143         staffUploadForm.setLoadReportURL(loadreportURL);
144     }
145 
146     /**
147      * This method validate the xml fileType
148      * @param marcFile
149      * @param ediFile
150      * @return boolean
151      */
152     public boolean validateFile(String marcFile, String ediFile) {
153         return (marcFile.contains(".xml") && ediFile.contains(".edi") ||
154                 marcFile.contains(".mrc") && ediFile.contains(".xml") ||
155                 !(marcFile.contains(".mrc") && ediFile.contains(".edi")
156                         || (marcFile.contains(".xml") && ediFile.contains(".xml")))? false:true);
157     }
158 
159     /**
160      *  This method checks whether preProcessing is required or not based on the originalFileName.
161      * @param marcFile
162      * @param ediFile
163      * @return   boolean
164      */
165     private boolean isPreProcessingRequired(MultipartFile marcFile, MultipartFile ediFile) {
166         return (marcFile.getOriginalFilename().contains(".mrc") && ediFile.getOriginalFilename().contains(".edi") ? true : false);
167     }
168 
169     /**
170      *  This method returns the instance of Ingest Processor.
171      * @return IngestProcessor
172      */
173     private IngestProcessor getIngestProcessor() {
174         return new IngestProcessor();
175     }
176 
177 
178 }
179