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 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
76
77
78
79
80
81
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
124 staffUploadForm.setMessage(OLEConstants.ERROR_MESSAGE_UPLOAD);
125 }
126 } else {
127
128 staffUploadForm.setMessage(OLEConstants.ERROR_AGENDA_NAME);
129 }
130 setLoadReportURL(staffUploadForm);
131
132
133
134 return getUIFModelAndView(staffUploadForm, "StaffUploadViewPage");
135 }
136
137
138
139
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
149
150
151
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
162
163
164
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
172
173
174 private IngestProcessor getIngestProcessor() {
175 return new IngestProcessor();
176 }
177
178
179 }
180