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          String user = request.getParameter(OLEConstants.LOGIN_USR);
69          staffUploadForm.setUser(user);
70          setLoadReportURL(staffUploadForm);
71          return super.start(staffUploadForm, result, request, response);
72      }
73  
74      /**
75       * This method persist the data from StaffUploadXml file into database after uploading the StaffUploadXml..
76       * @param form
77       * @param result
78       * @param request
79       * @param response
80       * @return  ModelAndView
81       * @throws Exception
82       */
83      @RequestMapping(params = "methodToCall=upload")
84      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
85                                 HttpServletRequest request, HttpServletResponse response)throws Exception {
86          StaffUploadForm staffUploadForm = (StaffUploadForm) form;
87       
88          MultipartFile marcFile = staffUploadForm.getMarcFile();
89          MultipartFile ediFile = staffUploadForm.getEdiFile();
90          String user = staffUploadForm.getUser();
91          String agendaName=null;
92          String agendaDescription = null;
93          agendaName = staffUploadForm.getAgenda();
94          agendaDescription = staffUploadForm.getAgendaDescription();
95          if (null != agendaName && !agendaName.equals("")) {
96              String marcFileContent = new String(marcFile.getBytes());
97              String ediFileContent = new String(ediFile.getBytes());
98  
99              if(validateFile(marcFile.getOriginalFilename(),ediFile.getOriginalFilename())){
100                 IngestRecord ingestRecord = new IngestRecord();
101                 ingestRecord.setOriginalEdiFileName(ediFile.getOriginalFilename());
102                 ingestRecord.setOriginalMarcFileName(marcFile.getOriginalFilename());
103                 ingestRecord.setAgendaName(agendaName);
104                 ingestRecord.setAgendaDescription(agendaDescription);
105                 ingestRecord.setEdiFileContent(ediFileContent);
106                 ingestRecord.setMarcFileContent(marcFileContent);
107                 ingestRecord.setByPassPreProcessing(isPreProcessingRequired(marcFile, ediFile));
108                 ingestRecord.setUser(user);
109                 try {
110                     boolean failure_flag=getIngestProcessor().start(ingestRecord);
111                     if(!failure_flag)
112                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_SUCCESS);
113                     else if(ingestRecord.isUpdate())
114                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_UPDATE_SUCCESS);
115                     else
116                         staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_FAILURE);
117                 } catch (Exception staffUploadException) {
118                     staffUploadForm.setMessage(OLEConstants.STAFF_UPLOAD_FAILURE);
119                     LOG.error("Failed to perform Staff Upload:", staffUploadException);
120                 }
121 
122             } else {
123                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_MESSAGE_UPLOAD);
124                 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
125             }
126         } else {
127                 //GlobalVariables.getMessageMap().putError("items", OLEConstants.ERROR_AGENDA_NAME);
128             staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
129         }
130         setLoadReportURL(staffUploadForm);
131 
132 
133         //return super.start(staffUploadForm, result, request, response);
134         return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
135     }
136 
137     /**
138      * Sets the LoadReportUrl attribute value.
139      * @param staffUploadForm
140      */
141     private void setLoadReportURL(StaffUploadForm staffUploadForm) {
142         String LOADREPORT_URL="loadreport.url";
143         String loadreportURL = PropertyUtil.getPropertyUtil().getProperty(LOADREPORT_URL);
144         staffUploadForm.setLoadReportURL(loadreportURL);
145     }
146 
147     /**
148      * This method validate the xml fileType
149      * @param marcFile
150      * @param ediFile
151      * @return boolean
152      */
153     public boolean validateFile(String marcFile, String ediFile) {
154         return (marcFile.contains(".xml") && ediFile.contains(".edi") ||
155                 marcFile.contains(".mrc") && ediFile.contains(".xml") ||
156                 !(marcFile.contains(".mrc") && ediFile.contains(".edi")
157                         || (marcFile.contains(".xml") && ediFile.contains(".xml")))? false:true);
158     }
159 
160     /**
161      *  This method checks whether preProcessing is required or not based on the originalFileName.
162      * @param marcFile
163      * @param ediFile
164      * @return   boolean
165      */
166     private boolean isPreProcessingRequired(MultipartFile marcFile, MultipartFile ediFile) {
167         return (marcFile.getOriginalFilename().contains(".mrc") && ediFile.getOriginalFilename().contains(".edi") ? true : false);
168     }
169 
170     /**
171      *  This method returns the instance of Ingest Processor.
172      * @return IngestProcessor
173      */
174     private IngestProcessor getIngestProcessor() {
175         return new IngestProcessor();
176     }
177 
178 
179 }
180