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.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
32
33 @Controller
34 @RequestMapping(value = "/staffuploadcontroller")
35 public class StaffUploadController
36 extends UifControllerBase {
37
38
39
40 private static final Logger LOG = Logger.getLogger(StaffUploadController.class);
41
42
43
44
45
46
47
48 @Override
49 protected StaffUploadForm createInitialForm(HttpServletRequest request) {
50 return new StaffUploadForm();
51 }
52
53
54
55
56
57
58
59
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
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 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
123 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
124 }
125 } else {
126
127 staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
128 }
129 loadReportURL(staffUploadForm);
130
131
132
133 return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
134 }
135
136
137
138
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
148
149
150
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
161
162
163
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
171
172
173 private IngestProcessor getIngestProcessor() {
174 return new IngestProcessor();
175 }
176
177
178 }
179