1 package org.kuali.ole.batch.controller;
2
3 import org.apache.commons.io.IOUtils;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
6 import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
7 import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
8 import org.kuali.ole.batch.form.OLEBatchProcessJobDetailsForm;
9 import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
10 import org.kuali.ole.batch.service.OLEBatchSchedulerService;
11 import org.kuali.ole.sys.batch.BatchFile;
12 import org.kuali.ole.sys.batch.BatchFileUtils;
13 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
14 import org.kuali.rice.krad.service.BusinessObjectService;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16 import org.kuali.rice.krad.util.GlobalVariables;
17 import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
18 import org.kuali.rice.krad.web.form.UifFormBase;
19 import org.quartz.CronExpression;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.validation.BindingResult;
22 import org.springframework.web.bind.annotation.ModelAttribute;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.servlet.ModelAndView;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.sql.Timestamp;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.*;
35
36
37
38
39
40
41
42
43
44
45 @Controller
46 @RequestMapping(value = "/oleBatchProcessJobController")
47 public class OLEBatchProcessJobDetailsController extends TransactionalDocumentControllerBase {
48
49 private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
50 private BusinessObjectService businessObjectService;
51 private OLEBatchSchedulerService schedulerService;
52
53 public OLEBatchSchedulerService getSchedulerService() {
54 if(schedulerService == null ) {
55 schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
56 }
57 return schedulerService;
58 }
59
60
61
62 public BusinessObjectService getBusinessObjectService() {
63 if(businessObjectService==null)
64 businessObjectService = KRADServiceLocator.getBusinessObjectService();
65 return businessObjectService;
66 }
67
68 private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper(){
69
70 if(oleBatchProcessDataHelper==null){
71 oleBatchProcessDataHelper= OLEBatchProcessDataHelper.getInstance();
72 }
73 return oleBatchProcessDataHelper;
74 }
75
76 @Override
77 protected OLEBatchProcessJobDetailsForm createInitialForm(HttpServletRequest request) {
78 OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = new OLEBatchProcessJobDetailsForm();
79 GlobalVariables.getUserSession().addObject("formId", oleBatchProcessJobDetailsForm.getFormKey());
80 List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
81 oleBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
82 return oleBatchProcessJobDetailsForm;
83 }
84
85 @RequestMapping(params = "methodToCall=stopButton")
86 public ModelAndView stopButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
87 HttpServletRequest request, HttpServletResponse response) throws Exception {
88 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
89 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
90 if(jobDetailsBo != null ) {
91 jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
92 getBusinessObjectService().save(jobDetailsBo);
93 }
94 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
95 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
96 getSchedulerService().stopJob(jobDetailsBo.getJobId());
97 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
98 }
99
100 @RequestMapping(params = "methodToCall=startButton")
101 public ModelAndView restartButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
102 HttpServletRequest request, HttpServletResponse response) throws Exception{
103 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
104 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
105 jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
106 getBusinessObjectService().save(jobDetailsBo);
107 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
108 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
109 getSchedulerService().startJob(jobDetailsBo.getJobId());
110 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
111 }
112
113 @RequestMapping(params = "methodToCall=resumeButton")
114 public ModelAndView resumeButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
115 HttpServletRequest request, HttpServletResponse response) throws Exception {
116 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
117 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
118 jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
119 getBusinessObjectService().save(jobDetailsBo);
120 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
121 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
122 getSchedulerService().resumeJob(jobDetailsBo.getJobId());
123 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
124 }
125
126 @RequestMapping(params = "methodToCall=removeButton")
127 public ModelAndView removeButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
128 HttpServletRequest request, HttpServletResponse response) throws Exception {
129 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
130 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
131 getOLEBatchProcessDataHelper().deleteBatchFailureFile(jobDetailsBo.getBatchProcessType(),jobDetailsBo.getJobId() + "_FailureRecord" + "_" + jobDetailsBo.getUploadFileName());
132 jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_CANCELLED);
133 getBusinessObjectService().save(jobDetailsBo);
134 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
135 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
136 getSchedulerService().deleteJob(jobDetailsBo.getJobId());
137 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
138 }
139
140
141 @RequestMapping(params = "methodToCall=pauseButton")
142 public ModelAndView pauseButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
143 HttpServletRequest request, HttpServletResponse response) throws Exception{
144 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
145 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
146 jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_PAUSED);
147 getBusinessObjectService().save(jobDetailsBo);
148 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
149 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
150 getSchedulerService().pauseJob(jobDetailsBo.getJobId());
151 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
152 }
153
154
155 @RequestMapping(params = "methodToCall=openViewReport")
156 public ModelAndView openViewReport(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
157 HttpServletRequest request, HttpServletResponse response) {
158 OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
159 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
160 if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
161 jobDetailsBo.setUploadFileName(getOLEBatchProcessDataHelper().getExportPathUrl(jobDetailsBo));
162 }
163 else if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT)) {
164 jobDetailsBo.setBibErrorPath(getOLEBatchProcessDataHelper().getBibPathUrl(jobDetailsBo));
165 } else if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_DELETE)) {
166 jobDetailsBo.setBatchDeletePath(getOLEBatchProcessDataHelper().getDeletePathUrl(jobDetailsBo));
167
168 }
169 List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo> )getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
170 oLEBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
171 oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBo(jobDetailsBo);
172
173 return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
174 }
175
176 @RequestMapping(params = "methodToCall=downLoadFile")
177 public ModelAndView downLoadFile(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
178 HttpServletRequest request, HttpServletResponse response) throws Exception {
179
180 OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
181
182 OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oleBatchProcessJobDetailsForm);
183 if (jobDetailsBo != null) {
184 Map map= new HashMap();
185 map.put("batchProcessId",jobDetailsBo.getBatchProcessId());
186 List<OLEBatchProcessDefinitionDocument> oleBatchProcessDefinitionDocumentList = (List<OLEBatchProcessDefinitionDocument>) getBusinessObjectService().findMatching(OLEBatchProcessDefinitionDocument.class, map);
187 if(oleBatchProcessDefinitionDocumentList!=null && oleBatchProcessDefinitionDocumentList.size()>0){
188 OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument=oleBatchProcessDefinitionDocumentList.get(0);
189 File file = new File(getBatchProcessFilePath(oleBatchProcessDefinitionDocument.getBatchProcessType()) + jobDetailsBo.getJobId() + "_FailureRecord" + "_" + jobDetailsBo.getUploadFileName());
190 if (!file.exists() || !file.isFile()) {
191 throw new RuntimeException("Error: non-existent file or directory provided");
192 }
193 File containingDirectory = file.getParentFile();
194 if (!BatchFileUtils.isDirectoryAccessible(containingDirectory.getAbsolutePath())) {
195 throw new RuntimeException("Error: inaccessible directory provided");
196 }
197
198 BatchFile batchFile = new BatchFile();
199 batchFile.setFile(file);
200 response.setContentType("application/octet-stream");
201 response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
202 response.setHeader("Expires", "0");
203 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
204 response.setHeader("Pragma", "public");
205 response.setContentLength((int) file.length());
206
207 InputStream fis = new FileInputStream(file);
208 IOUtils.copy(fis, response.getOutputStream());
209 response.getOutputStream().flush();
210 }
211 }
212 return null;
213
214
215 }
216
217 @RequestMapping(params = "methodToCall=jobDocHandler")
218 public ModelAndView jobDocHandler(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
219 HttpServletRequest request, HttpServletResponse response) throws Exception {
220 OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
221 ModelAndView modelAndView=super.docHandler(oleBatchProcessJobDetailsForm,result,request,response);
222 String documentClass= request.getParameter("documentClass");
223 if(documentClass!=null && OLEBatchProcessScheduleBo.class.toString().contains(documentClass)){
224 List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = (List<OLEBatchProcessScheduleBo>) getBusinessObjectService().findAll(OLEBatchProcessScheduleBo.class);
225 for (OLEBatchProcessScheduleBo oleBatchProcessScheduleBo : oleBatchProcessScheduleBoList) {
226 oleBatchProcessScheduleBo.setBatchProfileName(getBatchProcessDocument(oleBatchProcessScheduleBo.getBatchProcessId()).getBatchProcessProfileName());
227 }
228 oleBatchProcessJobDetailsForm.setOleBatchProcessScheduleBoList(oleBatchProcessScheduleBoList);
229 oneTimeDate(oleBatchProcessScheduleBoList);
230 }
231 else{
232 List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAll(OLEBatchProcessJobDetailsBo.class);
233 oleBatchProcessJobDetailsForm.setoLEBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
234 }
235 return modelAndView;
236 }
237
238 private OLEBatchProcessDefinitionDocument getBatchProcessDocument(String batchProcessId) {
239 Map map = new HashMap();
240 map.put("batchProcessId", batchProcessId);
241 OLEBatchProcessDefinitionDocument batchProcessDefinitionDocument = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessDefinitionDocument.class, map);
242 return batchProcessDefinitionDocument;
243 }
244
245 private String getBatchProcessFilePath(String batchProceesType) {
246 String batchProcessLocation = getOLEBatchProcessDataHelper().getBatchProcessFilePath(batchProceesType);
247 return batchProcessLocation;
248 }
249
250
251
252
253
254 private void runJobNow(String jobName){
255 OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
256 try {
257 schedulerService.startJob(jobName);
258 } catch (Exception e) {
259 LOG.error("Error while starting job with job id :: "+jobName);
260 }
261 }
262
263 private void oneTimeDate(List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList) {
264
265 try {
266 for (OLEBatchProcessScheduleBo oleBatchProcessScheduleBo : oleBatchProcessScheduleBoList) {
267 CronExpression exp = new CronExpression(oleBatchProcessScheduleBo.getCronExpression());
268 Date date = exp.getNextValidTimeAfter(new Date());
269 if (date != null) {
270 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
271 Timestamp nextRunTime = new Timestamp(date.getTime());
272 oleBatchProcessScheduleBo.setNextRunTime(nextRunTime);
273 }
274 }
275 } catch (ParseException e) {
276 LOG.error("Error while validating cron exp::" + oleBatchProcessScheduleBoList.get(0).getCronExpression(), e);
277 }
278
279
280 }
281
282 @RequestMapping(params = "methodToCall=removeScheduleJob")
283 public ModelAndView removeScheduleJob(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
284 HttpServletRequest request, HttpServletResponse response) throws Exception {
285 OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm)form;
286 Map map =new HashMap();
287 map.put("scheduleId",request.getParameter("scheduleId"));
288 OLEBatchProcessScheduleBo scheduleBo = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessScheduleBo.class, map);
289 getOLEBatchProcessDataHelper().deleteBatchFailureFile(scheduleBo.getBatchProcessType(),scheduleBo.getScheduleId() + "_Scheduled" + "_" + scheduleBo.getUploadFileName());
290 getBusinessObjectService().delete(scheduleBo);
291 removeJob(scheduleBo.getScheduleId());
292 List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = ( List<OLEBatchProcessScheduleBo> ) getBusinessObjectService().findAll(OLEBatchProcessScheduleBo.class);
293 oleBatchProcessJobDetailsForm.setOleBatchProcessScheduleBoList(oleBatchProcessScheduleBoList);
294 return getUIFModelAndView(oleBatchProcessJobDetailsForm);
295
296 }
297
298 private void removeJob(String jobName){
299 OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
300 try {
301 schedulerService.deleteJob(jobName);
302 } catch (Exception e) {
303 LOG.error("Error while removing job with schedule id :: "+jobName);
304 }
305 }
306
307 private OLEBatchProcessJobDetailsBo getJobBo(OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm) {
308 Map map = new HashMap();
309 map.put("jobId", oleBatchProcessJobDetailsForm.getJobId());
310 OLEBatchProcessJobDetailsBo jobDetailsBo = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessJobDetailsBo.class, map);
311 return jobDetailsBo;
312 }
313
314
315 }
316
317
318