1 package org.kuali.ole.ingest.controller;
2
3
4
5
6
7
8
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
34
35 @Controller
36 @RequestMapping(value = "/staffuploadcontroller")
37 public class StaffUploadController
38 extends UifControllerBase {
39
40
41
42 private static final Logger LOG = Logger.getLogger(StaffUploadController.class);
43
44
45
46
47
48
49
50 @Override
51 protected StaffUploadForm createInitialForm(HttpServletRequest request) {
52 return new StaffUploadForm();
53 }
54
55
56
57
58
59
60
61
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
74
75
76
77
78
79
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
115 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
116 }
117 } else {
118
119 staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
120 }
121 setLoadReportURL(staffUploadForm);
122
123
124
125 return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
126 }
127
128
129
130
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
140
141
142
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
153
154
155
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
163
164
165 private IngestProcessor getIngestProcessor() {
166 return new IngestProcessor();
167 }
168
169
170 }
171