001package org.kuali.ole.batch.controller;
002
003import org.apache.commons.io.IOUtils;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.OLEPropertyConstants;
006import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
007import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
008import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
009import org.kuali.ole.batch.form.OLEBatchProcessJobDetailsForm;
010import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
011import org.kuali.ole.batch.service.OLEBatchSchedulerService;
012import org.kuali.ole.sys.batch.BatchFile;
013import org.kuali.ole.sys.batch.BatchFileUtils;
014import org.kuali.rice.core.api.config.property.ConfigContext;
015import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
016import org.kuali.rice.krad.service.BusinessObjectService;
017import org.kuali.rice.krad.service.KRADServiceLocator;
018import org.kuali.rice.krad.uif.UifConstants;
019import org.kuali.rice.krad.uif.UifParameters;
020import org.kuali.rice.krad.util.GlobalVariables;
021import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
022import org.kuali.rice.krad.web.form.UifFormBase;
023import org.quartz.CronExpression;
024import org.springframework.stereotype.Controller;
025import org.springframework.validation.BindingResult;
026import org.springframework.web.bind.annotation.ModelAttribute;
027import org.springframework.web.bind.annotation.RequestMapping;
028import org.springframework.web.servlet.ModelAndView;
029
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032import java.io.File;
033import java.io.FileInputStream;
034import java.io.InputStream;
035import java.sql.Timestamp;
036import java.text.ParseException;
037import java.text.SimpleDateFormat;
038import java.util.*;
039
040/**
041 * Created with IntelliJ IDEA.
042 * User: krishnamohanv
043 * Date: 7/12/13
044 * Time: 5:06 PM
045 * To change this template use File | Settings | File Templates.
046 */
047
048
049@Controller
050@RequestMapping(value = "/oleBatchProcessJobController")
051public class OLEBatchProcessJobDetailsController extends TransactionalDocumentControllerBase {
052
053    private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
054    private BusinessObjectService businessObjectService;
055    private OLEBatchSchedulerService schedulerService;
056    private static HashMap<String, String> batchProcessJobStatusMap = new HashMap<String, String>();
057
058    public OLEBatchSchedulerService getSchedulerService() {
059        if (schedulerService == null) {
060            schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
061        }
062        return schedulerService;
063    }
064
065    public BusinessObjectService getBusinessObjectService() {
066        if (businessObjectService == null)
067            businessObjectService = KRADServiceLocator.getBusinessObjectService();
068        return businessObjectService;
069    }
070
071    private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
072
073        if (oleBatchProcessDataHelper == null) {
074            oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
075        }
076        return oleBatchProcessDataHelper;
077    }
078
079    @Override
080    protected OLEBatchProcessJobDetailsForm createInitialForm(HttpServletRequest request) {
081        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = new OLEBatchProcessJobDetailsForm();
082        GlobalVariables.getUserSession().addObject("formId", oleBatchProcessJobDetailsForm.getFormKey());
083        return oleBatchProcessJobDetailsForm;
084    }
085
086    @RequestMapping(params = "methodToCall=stopButton")
087    public ModelAndView stopButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
088                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
089        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
090        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
091        batchProcessJobStatusMap.put(jobDetailsBo.getJobId(), OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
092        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
093    }
094
095    @RequestMapping(params = "methodToCall=startButton")
096    public ModelAndView restartButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
097                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
098        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
099        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
100        jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
101        getBusinessObjectService().save(jobDetailsBo);
102        List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
103        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
104        getSchedulerService().startJob(jobDetailsBo.getJobId());
105        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
106    }
107
108    @RequestMapping(params = "methodToCall=resumeButton")
109    public ModelAndView resumeButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
110                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
111        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
112        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
113        if (jobDetailsBo.getPerCompleted().equalsIgnoreCase(String.format("%.2f", Float.valueOf(100)) + "%")) {
114            jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
115        } else {
116            jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
117            getSchedulerService().resumeJob(jobDetailsBo.getJobId());
118        }
119        getBusinessObjectService().save(jobDetailsBo);
120       /* List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
121        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);*/
122        for(OLEBatchProcessJobDetailsBo oleBatchProcessJobDetailsBo:oLEBatchProcessJobDetailsForm.getOleBatchProcessJobDetailsBoList()){
123            if(oleBatchProcessJobDetailsBo.getJobId().equals(jobDetailsBo.getJobId())){
124                oleBatchProcessJobDetailsBo.setStatus(jobDetailsBo.getStatus());
125                break;
126            }
127        }
128        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
129    }
130
131    @RequestMapping(params = "methodToCall=removeButton")
132    public ModelAndView removeButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
133                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
134        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
135        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
136        getOLEBatchProcessDataHelper().deleteBatchFailureFile(jobDetailsBo.getBatchProcessType(), jobDetailsBo.getJobId() + "_FailureRecord" + "_" + jobDetailsBo.getUploadFileName());
137        jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_CANCELLED);
138        getBusinessObjectService().save(jobDetailsBo);
139        /*List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
140        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);*/
141        for(OLEBatchProcessJobDetailsBo oleBatchProcessJobDetailsBo:oLEBatchProcessJobDetailsForm.getOleBatchProcessJobDetailsBoList()){
142            if(oleBatchProcessJobDetailsBo.getJobId().equals(jobDetailsBo.getJobId())){
143                oleBatchProcessJobDetailsBo.setStatus(jobDetailsBo.getStatus());
144                break;
145            }
146        }
147        getSchedulerService().deleteJob(jobDetailsBo.getJobId());
148        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
149    }
150
151    @RequestMapping(params = "methodToCall=pauseButton")
152    public ModelAndView pauseButton(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
153                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
154        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
155        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
156        if (jobDetailsBo.getPerCompleted().equalsIgnoreCase(String.format("%.2f", Float.valueOf(100)) + "%")) {
157            jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
158        } else {
159            jobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_PAUSED);
160            getSchedulerService().pauseJob(jobDetailsBo.getJobId());
161        }
162        getBusinessObjectService().save(jobDetailsBo);
163        /*List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
164        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);*/
165        for(OLEBatchProcessJobDetailsBo oleBatchProcessJobDetailsBo:oLEBatchProcessJobDetailsForm.getOleBatchProcessJobDetailsBoList()){
166            if(oleBatchProcessJobDetailsBo.getJobId().equals(jobDetailsBo.getJobId())){
167                oleBatchProcessJobDetailsBo.setStatus(jobDetailsBo.getStatus());
168                break;
169            }
170        }
171        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
172    }
173
174    @RequestMapping(params = "methodToCall=openViewReport")
175    public ModelAndView openViewReport(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
176                                       HttpServletRequest request, HttpServletResponse response) {
177        OLEBatchProcessJobDetailsForm oLEBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
178        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oLEBatchProcessJobDetailsForm);
179        if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
180            jobDetailsBo.setUploadFileName(getOLEBatchProcessDataHelper().getExportPathUrl(jobDetailsBo));
181        } else if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_BIB_IMPORT)) {
182            jobDetailsBo.setBibErrorPath(getOLEBatchProcessDataHelper().getBibPathUrl(jobDetailsBo));
183        } else if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_DELETE)) {
184            jobDetailsBo.setBatchDeletePath(getOLEBatchProcessDataHelper().getDeletePathUrl(jobDetailsBo));
185        } else if (jobDetailsBo.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)) {
186            jobDetailsBo.setSerialCSVErrorPath(getOLEBatchProcessDataHelper().getSerialCSVPathUrl(jobDetailsBo));
187        }
188        List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
189        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBoList);
190        oLEBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBo(jobDetailsBo);
191
192        return getUIFModelAndView(oLEBatchProcessJobDetailsForm);
193    }
194
195    @RequestMapping(params = "methodToCall=downLoadFile")
196    public ModelAndView downLoadFile(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
197                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
198
199        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
200
201        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oleBatchProcessJobDetailsForm);
202        if (jobDetailsBo != null) {
203            Map map = new HashMap();
204            map.put("batchProcessId", jobDetailsBo.getBatchProcessId());
205            List<OLEBatchProcessDefinitionDocument> oleBatchProcessDefinitionDocumentList = (List<OLEBatchProcessDefinitionDocument>) getBusinessObjectService().findMatching(OLEBatchProcessDefinitionDocument.class, map);
206            if (oleBatchProcessDefinitionDocumentList != null && oleBatchProcessDefinitionDocumentList.size() > 0) {
207                OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = oleBatchProcessDefinitionDocumentList.get(0);
208                File file = null;
209                if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
210                    file = new File(getBatchProcessFilePath(oleBatchProcessDefinitionDocument.getBatchProcessType() , jobDetailsBo.getJobId()) + jobDetailsBo.getJobId() + OLEConstants.OLEBatchProcess.DELETED_BIB_IDS_FILE_NAME);
211                } else {
212                    file = new File(getBatchProcessFilePath(oleBatchProcessDefinitionDocument.getBatchProcessType() , jobDetailsBo.getJobId()) + jobDetailsBo.getJobId() + "_FailureRecord" + "_" + jobDetailsBo.getUploadFileName());
213                }
214                if (!file.exists() || !file.isFile()) {
215                    throw new RuntimeException("Error: non-existent file or directory provided");
216                }
217                File containingDirectory = file.getParentFile();
218                if (!BatchFileUtils.isDirectoryAccessible(containingDirectory.getAbsolutePath())) {
219                    throw new RuntimeException("Error: inaccessible directory provided");
220                }
221
222                BatchFile batchFile = new BatchFile();
223                batchFile.setFile(file);
224                response.setContentType("application/octet-stream");
225                response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
226                response.setHeader("Expires", "0");
227                response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
228                response.setHeader("Pragma", "public");
229                response.setContentLength((int) file.length());
230
231                InputStream fis = new FileInputStream(file);
232                IOUtils.copy(fis, response.getOutputStream());
233                response.getOutputStream().flush();
234            }
235        }
236        return null;
237    }
238
239    @RequestMapping(params = "methodToCall=downLoadErrorAttachmentFile")
240    public ModelAndView downLoadErrorAttachmentFile(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
241                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
242        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
243        OLEBatchProcessJobDetailsBo jobDetailsBo = getJobBo(oleBatchProcessJobDetailsForm);
244        if (jobDetailsBo != null) {
245            Map map = new HashMap();
246            map.put("batchProcessId", jobDetailsBo.getBatchProcessId());
247            List<OLEBatchProcessDefinitionDocument> oleBatchProcessDefinitionDocumentList = (List<OLEBatchProcessDefinitionDocument>) getBusinessObjectService().findMatching(OLEBatchProcessDefinitionDocument.class, map);
248            if (oleBatchProcessDefinitionDocumentList != null && oleBatchProcessDefinitionDocumentList.size() > 0) {
249                OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = oleBatchProcessDefinitionDocumentList.get(0);
250                String uploadFileName = jobDetailsBo.getUploadFileName();
251                String errorFileName = null;
252                String[] fileNames = uploadFileName.split(",");
253                errorFileName = fileNames.length == 2 ? fileNames[0]:uploadFileName;
254                if(errorFileName.endsWith(".mrc")){
255                    errorFileName = errorFileName.replace(".mrc",".txt");
256                }
257                else if(errorFileName.endsWith(".INV")){
258                    errorFileName = errorFileName.replace(".INV",".txt");
259                }
260                else if(errorFileName.endsWith(".edi")){
261                    errorFileName = errorFileName.replace(".edi",".txt");
262                }
263                File file = new File(getBatchProcessFilePath(oleBatchProcessDefinitionDocument.getBatchProcessType() , jobDetailsBo.getJobId()) + jobDetailsBo.getJobId() + "_FailureRecord" + "_" + errorFileName);
264                if (!file.exists() || !file.isFile()) {
265                    throw new RuntimeException("Error: non-existent file or directory provided");
266                }
267                File containingDirectory = file.getParentFile();
268                if (!BatchFileUtils.isDirectoryAccessible(containingDirectory.getAbsolutePath())) {
269                    throw new RuntimeException("Error: inaccessible directory provided");
270                }
271                BatchFile batchFile = new BatchFile();
272                batchFile.setFile(file);
273                response.setContentType("application/octet-stream");
274                response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
275                response.setHeader("Expires", "0");
276                response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
277                response.setHeader("Pragma", "public");
278                response.setContentLength((int) file.length());
279
280                InputStream fis = new FileInputStream(file);
281                IOUtils.copy(fis, response.getOutputStream());
282                response.getOutputStream().flush();
283            }
284        }
285        return null;
286    }
287
288    @RequestMapping(params = "methodToCall=jobDocHandler")
289    public ModelAndView jobDocHandler(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
290                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
291        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
292        ModelAndView modelAndView = super.docHandler(oleBatchProcessJobDetailsForm, result, request, response);
293        String documentClass = request.getParameter("documentClass");
294        if (documentClass != null && OLEBatchProcessScheduleBo.class.toString().contains(documentClass)) {
295            List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = (List<OLEBatchProcessScheduleBo>) getBusinessObjectService().findAll(OLEBatchProcessScheduleBo.class);
296            for (OLEBatchProcessScheduleBo oleBatchProcessScheduleBo : oleBatchProcessScheduleBoList) {
297                oleBatchProcessScheduleBo.setBatchProfileName(getBatchProcessDocument(oleBatchProcessScheduleBo.getBatchProcessId()).getBatchProcessProfileName());
298            }
299            oleBatchProcessJobDetailsForm.setOleBatchProcessScheduleBoList(oleBatchProcessScheduleBoList);
300            oneTimeDate(oleBatchProcessScheduleBoList);
301        } else {
302            List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class,"jobId",false);
303            oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
304        }
305        return modelAndView;
306    }
307
308    private OLEBatchProcessDefinitionDocument getBatchProcessDocument(String batchProcessId) {
309        Map map = new HashMap();
310        map.put("batchProcessId", batchProcessId);
311        OLEBatchProcessDefinitionDocument batchProcessDefinitionDocument = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessDefinitionDocument.class, map);
312        return batchProcessDefinitionDocument;
313    }
314
315    private String getBatchProcessFilePath(String batchProceesType , String jobId) {
316        String batchProcessLocation = getOLEBatchProcessDataHelper().getBatchProcessFilePath(batchProceesType , jobId);
317        return batchProcessLocation;
318    }
319
320    /**
321     * Runs the process for the profile selected and takes the scheduled record id as the job name
322     * @param jobName
323     */
324    private void runJobNow(String jobName) {
325        OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
326        try {
327            schedulerService.startJob(jobName);
328        } catch (Exception e) {
329            LOG.error("Error while starting job with job id :: " + jobName);
330        }
331    }
332
333    private void oneTimeDate(List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList) {
334
335        try {
336            for (OLEBatchProcessScheduleBo oleBatchProcessScheduleBo : oleBatchProcessScheduleBoList) {
337                CronExpression exp = new CronExpression(oleBatchProcessScheduleBo.getCronExpression());
338                Date date = exp.getNextValidTimeAfter(new Date());
339                if (date != null) {
340                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
341                    Timestamp nextRunTime = new Timestamp(date.getTime());
342                    oleBatchProcessScheduleBo.setNextRunTime(nextRunTime);
343                }
344            }
345        } catch (ParseException e) {
346            LOG.error("Error while validating cron exp::" + oleBatchProcessScheduleBoList.get(0).getCronExpression(), e);
347        }
348
349
350    }
351
352    @RequestMapping(params = "methodToCall=removeScheduleJob")
353    public ModelAndView removeScheduleJob(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
354                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
355        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
356        Map map = new HashMap();
357        map.put("scheduleId", request.getParameter("scheduleId"));
358        OLEBatchProcessScheduleBo scheduleBo = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessScheduleBo.class, map);
359        getOLEBatchProcessDataHelper().deleteBatchFailureFile(scheduleBo.getBatchProcessType(), scheduleBo.getScheduleId() + "_Scheduled" + "_" + scheduleBo.getUploadFileName());
360        getBusinessObjectService().delete(scheduleBo);
361        removeJob(scheduleBo.getScheduleId());
362        List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = (List<OLEBatchProcessScheduleBo>) getBusinessObjectService().findAll(OLEBatchProcessScheduleBo.class);
363        oleBatchProcessJobDetailsForm.setOleBatchProcessScheduleBoList(oleBatchProcessScheduleBoList);
364        return getUIFModelAndView(oleBatchProcessJobDetailsForm);
365    }
366
367    private void removeJob(String jobName) {
368        OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
369        try {
370            schedulerService.deleteJob(jobName);
371        } catch (Exception e) {
372            LOG.error("Error while removing job with schedule id :: " + jobName);
373        }
374    }
375
376    private OLEBatchProcessJobDetailsBo getJobBo(OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm) {
377        Map map = new HashMap();
378        map.put("jobId", oleBatchProcessJobDetailsForm.getJobId());
379        OLEBatchProcessJobDetailsBo jobDetailsBo = getBusinessObjectService().findByPrimaryKey(OLEBatchProcessJobDetailsBo.class, map);
380        return jobDetailsBo;
381    }
382
383    public static HashMap<String, String> getBatchProcessJobStatusMap() {
384        return batchProcessJobStatusMap;
385    }
386
387    public static void setBatchProcessJobStatusMap(String jobId, String jobStatus) {
388        batchProcessJobStatusMap.put(jobId, jobStatus);
389    }
390
391    public static void removeStatusFromBatchProcess(String jobId) {
392        if (getBatchProcessJobStatusMap() != null)
393            batchProcessJobStatusMap.remove(jobId);
394    }
395
396    public static String getBatchProcessJobStatus(String jobId) {
397        String jobStatus = null;
398        if (getBatchProcessJobStatusMap() != null) {
399            jobStatus = batchProcessJobStatusMap.get(jobId);
400            if (jobStatus != null)
401                return jobStatus;
402            else {
403                return OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING;
404            }
405        } else {
406            return OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING;
407        }
408    }
409
410    @RequestMapping(params = "methodToCall=search")
411    public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
412                               HttpServletRequest request, HttpServletResponse response) throws Exception {
413        LOG.debug("Entering Batch process job details search");
414        boolean checkFromDate = false;
415        boolean checkToDate = false;
416        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
417        oleBatchProcessJobDetailsForm.setSingleJobView(false);
418        if (oleBatchProcessJobDetailsForm.getJobFromDate() != null) {
419            checkFromDate = true;
420        }
421        if (oleBatchProcessJobDetailsForm.getJobToDate() != null) {
422            checkToDate = true;
423        }
424        oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(null);
425        List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findAllOrderBy(OLEBatchProcessJobDetailsBo.class, "jobId", false);
426        if (!checkFromDate && !checkToDate) {
427            oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
428        } else {
429            List<OLEBatchProcessJobDetailsBo> resultList = new ArrayList<>();
430            for (OLEBatchProcessJobDetailsBo oleBatchProcessJobDetailsBo : oLEBatchProcessJobDetailsBoList) {
431                Date fromDate = oleBatchProcessJobDetailsBo.getCreateTime();
432                if (checkFromDate && checkToDate) {
433                    Date searchFromDate = oleBatchProcessJobDetailsForm.getJobFromDate();
434                    int subtract = 1;
435                    Calendar calendar = GregorianCalendar.getInstance();
436                    calendar.setTime(oleBatchProcessJobDetailsForm.getJobToDate());
437                    calendar.add(Calendar.DAY_OF_YEAR, subtract);
438                    Date searchToDate = calendar.getTime();
439                    if (fromDate.after(searchFromDate) && fromDate.before(searchToDate)) {
440                        resultList.add(oleBatchProcessJobDetailsBo);
441                    }
442                } else if (checkFromDate) {
443                    Date searchFromDate = oleBatchProcessJobDetailsForm.getJobFromDate();
444                    if (fromDate.after(searchFromDate)) {
445                        resultList.add(oleBatchProcessJobDetailsBo);
446                    }
447                } else if (checkToDate) {
448                    int subtract = 1;
449                    Calendar calendar = GregorianCalendar.getInstance();
450                    calendar.setTime(oleBatchProcessJobDetailsForm.getJobToDate());
451                    calendar.add(Calendar.DAY_OF_YEAR, subtract);
452                    Date searchToDate = calendar.getTime();
453                    if (fromDate.before(searchToDate)) {
454                        resultList.add(oleBatchProcessJobDetailsBo);
455                    }
456                }
457            }
458            oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(resultList);
459        }
460
461        LOG.debug("Leaving Batch process job details search");
462        return getUIFModelAndView(oleBatchProcessJobDetailsForm);
463    }
464
465    @RequestMapping(params = "methodToCall=clearSearch")
466    public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
467                                    HttpServletRequest request, HttpServletResponse response) {
468        LOG.debug("Entering Batch process job details search");
469        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
470        oleBatchProcessJobDetailsForm.setJobFromDate(null);
471        oleBatchProcessJobDetailsForm.setJobToDate(null);
472        LOG.debug("Leaving Batch process job details search");
473        return getUIFModelAndView(oleBatchProcessJobDetailsForm);
474    }
475
476    @RequestMapping(params = "methodToCall=cancel")
477    public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
478                               HttpServletRequest request, HttpServletResponse response) {
479        String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
480        String url = baseUrl + "/portal.do";
481        Properties props = new Properties();
482        props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
483        if (org.apache.commons.lang.StringUtils.isNotBlank(form.getReturnFormKey())) {
484            props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
485        }
486        return performRedirect(form, url, props);
487    }
488
489    @RequestMapping(params = "methodToCall=singleJobDetailView")
490    public ModelAndView singleJobDetailView(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
491                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
492        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
493        oleBatchProcessJobDetailsForm.setSingleJobView(true);
494        ModelAndView modelAndView = super.docHandler(oleBatchProcessJobDetailsForm, result, request, response);
495        Map<String,String> parameterMap = new HashMap<>();
496        parameterMap.put("batchProcessId",request.getParameter("batchProcessId"));
497        List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findMatching(OLEBatchProcessJobDetailsBo.class,parameterMap);
498        oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
499        return modelAndView;
500    }
501
502    @RequestMapping(params = "methodToCall=refreshSingleJobDetailView")
503    public ModelAndView refreshSingleJobDetailView(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
504                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
505        OLEBatchProcessJobDetailsForm oleBatchProcessJobDetailsForm = (OLEBatchProcessJobDetailsForm) form;
506        oleBatchProcessJobDetailsForm.setSingleJobView(true);
507        Map<String,String> parameterMap = new HashMap<>();
508        parameterMap.put("jobId",oleBatchProcessJobDetailsForm.getJobId());
509        List<OLEBatchProcessJobDetailsBo> oLEBatchProcessJobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) getBusinessObjectService().findMatching(OLEBatchProcessJobDetailsBo.class,parameterMap);
510        oleBatchProcessJobDetailsForm.setOleBatchProcessJobDetailsBoList(oLEBatchProcessJobDetailsBoList);
511        return getUIFModelAndView(oleBatchProcessJobDetailsForm);
512    }
513}