001package org.kuali.ole.ingest.controller;
002
003/**
004 * Created by IntelliJ IDEA.
005 * User: balakumaranm
006 * Date: 3/28/12
007 * Time: 3:24 PM
008 * To change this template use File | Settings | File Templates.
009 */
010
011import org.apache.log4j.Logger;
012import org.kuali.ole.OLEConstants;
013import org.kuali.ole.ingest.IngestProcessor;
014import org.kuali.ole.ingest.form.StaffUploadForm;
015import org.kuali.ole.ingest.pojo.IngestRecord;
016import org.kuali.rice.core.api.config.property.ConfigContext;
017import org.kuali.rice.krad.web.controller.UifControllerBase;
018import org.kuali.rice.krad.web.form.UifFormBase;
019import org.springframework.stereotype.Controller;
020import org.springframework.validation.BindingResult;
021import org.springframework.web.bind.annotation.ModelAttribute;
022import org.springframework.web.bind.annotation.RequestMapping;
023import org.springframework.web.multipart.MultipartFile;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029
030/**
031 * StaffUploadController is the controller class for Staff Upload Controller.
032 */
033@Controller
034@RequestMapping(value = "/staffuploadcontroller")
035public class StaffUploadController
036        extends UifControllerBase {
037    /**
038     * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
039     */
040    private static final Logger LOG = Logger.getLogger(StaffUploadController.class);
041
042    /**
043     * This method will return instance of StaffUploadForm.
044     * @param request
045     * @return  StaffUploadForm
046     */
047
048    @Override
049    protected StaffUploadForm createInitialForm(HttpServletRequest request) {
050        return new StaffUploadForm();
051    }
052
053    /**
054     * This method will take the initial request when click on StaffUpload screen.
055     * @param form
056     * @param result
057     * @param request
058     * @param response
059     * @return  ModelAndView
060     */
061    @Override
062    @RequestMapping(params = "methodToCall=start")
063    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
064                              HttpServletRequest request, HttpServletResponse response) {
065        StaffUploadForm staffUploadForm = (StaffUploadForm) form;
066        String user = request.getParameter(OLEConstants.LOGIN_USR);
067        staffUploadForm.setUser(user);
068        loadReportURL(staffUploadForm);
069        return super.start(staffUploadForm, result, request, response);
070    }
071
072    /**
073     * This method persist the data from StaffUploadXml file into database after uploading the StaffUploadXml..
074     * @param form
075     * @param result
076     * @param request
077     * @param response
078     * @return  ModelAndView
079     * @throws Exception
080     */
081    @RequestMapping(params = "methodToCall=upload")
082    public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
083                               HttpServletRequest request, HttpServletResponse response)throws Exception {
084        StaffUploadForm staffUploadForm = (StaffUploadForm) form;
085
086        MultipartFile marcFile = staffUploadForm.getMarcFile();
087        MultipartFile ediFile = staffUploadForm.getEdiFile();
088        String user = staffUploadForm.getUser();
089        String agendaName=null;
090        String agendaDescription = null;
091        agendaName = staffUploadForm.getAgenda();
092        agendaDescription = staffUploadForm.getAgendaDescription();
093        if (null != agendaName && !agendaName.equals("")) {
094            String marcFileContent = new String(marcFile.getBytes());
095            String ediFileContent = new String(ediFile.getBytes());
096
097            if(validateFile(marcFile.getOriginalFilename(),ediFile.getOriginalFilename())){
098                IngestRecord ingestRecord = new IngestRecord();
099                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