View Javadoc
1   package org.kuali.ole.batch.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.DataCarrierService;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.OLEPropertyConstants;
7   import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
8   import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
9   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
10  import org.kuali.ole.batch.form.OLEBatchProcessDefinitionForm;
11  import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
12  import org.kuali.ole.batch.service.OLEBatchSchedulerService;
13  import org.kuali.ole.batch.helper.OLESchedulerHelper;
14  import org.kuali.ole.batch.rule.OLEBatchProcessRule;
15  import org.kuali.ole.select.document.OLEInvoiceIngestLoadReport;
16  import org.kuali.rice.core.api.config.property.ConfigContext;
17  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
18  import org.kuali.rice.krad.service.KRADServiceLocator;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.UifParameters;
21  import org.kuali.rice.krad.util.GlobalVariables;
22  import org.kuali.rice.krad.util.KRADConstants;
23  import org.kuali.rice.krad.web.controller.TransactionalDocumentControllerBase;
24  import org.kuali.rice.krad.web.form.DocumentFormBase;
25  import org.kuali.rice.krad.web.form.UifFormBase;
26  import org.springframework.stereotype.Controller;
27  import org.springframework.validation.BindingResult;
28  import org.springframework.web.bind.annotation.ModelAttribute;
29  import org.springframework.web.bind.annotation.RequestMapping;
30  import org.springframework.web.multipart.MultipartFile;
31  import org.springframework.web.servlet.ModelAndView;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.sql.Timestamp;
36  import java.util.*;
37  
38  /**
39   * Created with IntelliJ IDEA.
40   * User: adityas
41   * Date: 7/12/13
42   * Time: 8:32 PM
43   * To change this template use File | Settings | File Templates.
44   */
45  @Controller
46  @RequestMapping(value = "/oleBatchProcessDefinitionController")
47  public class OLEBatchProcessDefinitionController extends TransactionalDocumentControllerBase {
48  
49      @Override
50      protected DocumentFormBase createInitialForm(HttpServletRequest request) {
51          OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = new OLEBatchProcessDefinitionForm();
52          return oleBatchProcessDefinitionForm;
53      }
54  
55      private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
56      private OLEBatchProcessRule oleBatchProcessRule;
57  
58      public OLEBatchProcessRule getOleBatchProcessRule() {
59          if (oleBatchProcessRule == null) {
60              oleBatchProcessRule = new OLEBatchProcessRule();
61          }
62          return oleBatchProcessRule;
63      }
64  
65      private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
66  
67          if (oleBatchProcessDataHelper == null) {
68              oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
69          }
70          return oleBatchProcessDataHelper;
71      }
72  
73      @RequestMapping(params = "methodToCall=startBatch")
74      public ModelAndView startBatch(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
75                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
76          ModelAndView modelAndView = super.docHandler(form,result,request,response);
77          boolean isValidated = getOleBatchProcessRule().canPerformBatchImport(GlobalVariables.getUserSession().getPrincipalId())
78                  || getOleBatchProcessRule().canPerformBatchExport(GlobalVariables.getUserSession().getPrincipalId())
79                  || getOleBatchProcessRule().canPerformBatchDelete(GlobalVariables.getUserSession().getPrincipalId());
80          if(!isValidated) {
81            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.ERROR_AUTHORIZATION);
82            ((OLEBatchProcessDefinitionForm) form).setPermissionFlag(false);
83          }
84          OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
85          OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
86          oleBatchProcessDefinitionDocument.setChunkSize(10000);
87          oleBatchProcessDefinitionDocument.setMaxRecordsInFile(100000);
88          oleBatchProcessDefinitionDocument.setMaxNumberOfThreads(10);
89          return modelAndView;
90      }
91  
92  
93      @RequestMapping(params = "methodToCall=runNowRoute")
94      public ModelAndView runNowRoute(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
95                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
96          OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
97          OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
98  
99  
100         boolean isValid = getOleBatchProcessRule().canPerformBatchImport(GlobalVariables.getUserSession().getPrincipalId())
101                 || getOleBatchProcessRule().canPerformBatchExport(GlobalVariables.getUserSession().getPrincipalId())
102                 || getOleBatchProcessRule().canPerformBatchDelete(GlobalVariables.getUserSession().getPrincipalId());
103         if (!isValid) {
104             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.ERROR_AUTHORIZATION);
105             return getUIFModelAndView(oleBatchProcessDefinitionForm);
106         }
107 
108         if (oleBatchProcessDefinitionDocument.getBatchProcessId() != null)
109             return getUIFModelAndView(oleBatchProcessDefinitionForm);
110         boolean isValidated = getOleBatchProcessRule().batchValidations(oleBatchProcessDefinitionForm);
111         if (!isValidated) {
112             return getUIFModelAndView(oleBatchProcessDefinitionForm);
113         }
114         if (oleBatchProcessDefinitionDocument.getBatchProcessType() != null && oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_INVOICE) && !oleBatchProcessDefinitionDocument.isContinueImportFlag()) {
115             String fileName = oleBatchProcessDefinitionDocument.getIngestedFile().getOriginalFilename();
116             List<OLEInvoiceIngestLoadReport> oleInvoiceIngestLoadReportList = (List<OLEInvoiceIngestLoadReport>) getBusinessObjectService().findAll(OLEInvoiceIngestLoadReport.class);
117             if (oleInvoiceIngestLoadReportList != null && oleInvoiceIngestLoadReportList.size() > 0) {
118                 List<String> fileNames = new ArrayList<>();
119                 for (OLEInvoiceIngestLoadReport oleInvoiceIngestLoadReport : oleInvoiceIngestLoadReportList) {
120                     fileNames.add(oleInvoiceIngestLoadReport.getFileName());
121                 }
122                 if (fileNames != null && fileNames.size() > 0) {
123                     if (fileNames.contains(fileName)) {
124                         oleBatchProcessDefinitionDocument.setFileFlag(true);
125                         return getUIFModelAndView(oleBatchProcessDefinitionForm);
126                     } else {
127 
128                     }
129                 }
130             }
131         }
132         oleBatchProcessDefinitionDocument.setUser(GlobalVariables.getUserSession().getPrincipalName());
133         oleBatchProcessDefinitionDocument.setLinkToJob(true);
134         oleBatchProcessDefinitionDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEBatchProcess.NEW_BATCH_PRCS_DOCUMENT + oleBatchProcessDefinitionDocument.getDocumentNumber());
135         saveJob(oleBatchProcessDefinitionForm);
136         ModelAndView modelAndView = super.save(oleBatchProcessDefinitionForm, result, request, response);
137         String jobName = null;
138         if (oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList() != null && oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().size() > 0) {
139             OLEBatchProcessJobDetailsBo jobDetailsBo = oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0);
140             jobName = jobDetailsBo.getJobId();
141             jobDetailsBo.setJobName(oleBatchProcessDefinitionDocument.getBatchProcessName());
142             KRADServiceLocator.getBusinessObjectService().save(jobDetailsBo);
143         }
144         if (!oleBatchProcessDefinitionDocument.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.CLAIM_REPORT) && oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList() != null && oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().size() > 0) {
145             // for order import
146             if (oleBatchProcessDefinitionDocument.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
147                 if (oleBatchProcessDefinitionDocument.getEdiFile() == null) {
148                     createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getMarcFile(), null, oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
149                 } else {
150                     createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getMarcFile(), oleBatchProcessDefinitionDocument.getEdiFile(), oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
151                 }
152             }
153             // for serial record import
154             else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)) {
155                 if (oleBatchProcessDefinitionDocument.getInputFormat().equalsIgnoreCase("xml")) {
156                     createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getIngestedFile(), null, oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
157                 } else {
158                     createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile(), oleBatchProcessDefinitionDocument.getSerialRecordTypeFile(), oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile(), oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
159                 }
160             } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
161                 if (oleBatchProcessDefinitionDocument.getLoadIdFromFile().equalsIgnoreCase("true")) {
162                     createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getIngestedFile(), null, oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
163                 }
164             }
165             // for other than   serial record import & Order Import
166             else {
167                 createBatchProcessJobFile(oleBatchProcessDefinitionDocument.getOleBatchProcessJobDetailsBoList().get(0), oleBatchProcessDefinitionDocument.getIngestedFile(), null, oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_JOB);
168             }
169 
170         }
171         if (jobName != null) {
172             runJobNow(jobName);
173         }
174         oleBatchProcessDefinitionDocument.setAfterSubmitFlag(true);
175         return modelAndView;
176     }
177 
178     /**
179      * This method saves the job
180      * @param
181      */
182     private OLEBatchProcessJobDetailsBo saveJob(DocumentFormBase form) {
183         Timestamp timestamp = new Timestamp(new Date().getTime());
184         OLEBatchProcessJobDetailsBo oleBatchProcessJobDetailsBo = new OLEBatchProcessJobDetailsBo();
185         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
186         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
187         String profileName = oleBatchProcessDefinitionDocument.getBatchProcessProfileName();
188         if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
189             if(oleBatchProcessDefinitionDocument.getEdiFile() != null) {
190                 MultipartFile marcFile = oleBatchProcessDefinitionDocument.getMarcFile();
191                 MultipartFile ediFile = oleBatchProcessDefinitionDocument.getEdiFile();
192                 oleBatchProcessJobDetailsBo.setUploadFileName(marcFile.getOriginalFilename() + "," + ediFile.getOriginalFilename());
193                 oleBatchProcessDefinitionDocument.setUploadFileName(marcFile.getOriginalFilename() + "," + ediFile.getOriginalFilename());
194                 oleBatchProcessDefinitionDocument.setMarcFileName(marcFile.getOriginalFilename());
195                 oleBatchProcessDefinitionDocument.setEdiFileName(ediFile.getOriginalFilename());
196             }
197             else{
198                 MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getMarcFile();
199                 oleBatchProcessJobDetailsBo.setUploadFileName(ingestFile.getOriginalFilename());
200                 oleBatchProcessDefinitionDocument.setUploadFileName(ingestFile.getOriginalFilename());
201                 oleBatchProcessDefinitionDocument.setMarcFileName(ingestFile.getOriginalFilename());
202             }
203         } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)) {
204             if (oleBatchProcessDefinitionDocument.getInputFormat().equalsIgnoreCase("xml")) {
205                 MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getIngestedFile();
206                 oleBatchProcessJobDetailsBo.setUploadFileName(ingestFile.getOriginalFilename());
207                 oleBatchProcessDefinitionDocument.setUploadFileName(ingestFile.getOriginalFilename());
208             } else {
209                 MultipartFile documentFile = oleBatchProcessDefinitionDocument.getSerialRecordDocumentFile();
210                 MultipartFile typeFile = oleBatchProcessDefinitionDocument.getSerialRecordTypeFile();
211                 MultipartFile historyFile = oleBatchProcessDefinitionDocument.getSerialRecordHistoryFile();
212                 oleBatchProcessJobDetailsBo.setUploadFileName((documentFile != null ? documentFile.getOriginalFilename() + "," : "")
213                         + (typeFile != null ? typeFile.getOriginalFilename() + "," : "")
214                         + (historyFile != null ? historyFile.getOriginalFilename() : ""));
215                 oleBatchProcessDefinitionDocument.setSerialRecordDocumentFileName(documentFile != null ? documentFile.getOriginalFilename() : null);
216                 oleBatchProcessDefinitionDocument.setSerialRecordTypeFileName(typeFile != null ? typeFile.getOriginalFilename() : null);
217                 oleBatchProcessDefinitionDocument.setSerialRecordHistoryFileName(historyFile != null ? historyFile.getOriginalFilename() : null);
218             }
219             oleBatchProcessDefinitionDocument.setOutputFormat(oleBatchProcessDefinitionDocument.getInputFormat());
220         } else if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT) && oleBatchProcessDefinitionDocument.getLoadIdFromFile().equalsIgnoreCase("true")) {
221             MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getIngestedFile();
222             oleBatchProcessJobDetailsBo.setUploadFileName(ingestFile.getOriginalFilename());
223             oleBatchProcessDefinitionDocument.setUploadFileName(ingestFile.getOriginalFilename());
224         } else if (!oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)
225                 && !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.CLAIM_REPORT)
226                 && !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.SERIAL_RECORD_IMPORT)) {
227             MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getIngestedFile();
228             oleBatchProcessJobDetailsBo.setUploadFileName(ingestFile.getOriginalFilename());
229             oleBatchProcessDefinitionDocument.setUploadFileName(ingestFile.getOriginalFilename());
230         }
231         oleBatchProcessJobDetailsBo.setCreateTime(timestamp);
232         if (oleBatchProcessDefinitionDocument.isScheduleFlag()) {
233             oleBatchProcessJobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_SCHEDULED);
234             //   oleBatchProcessJobDetailsBo.setStatus(profileName + " " + OLEConstants.OLEBatchProcess.JOB_STATUS_SCHEDULED);
235         } else {
236             oleBatchProcessJobDetailsBo.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
237             // oleBatchProcessJobDetailsBo.setStatus(profileName + " " + OLEConstants.OLEBatchProcess.JOB_STATUS_RUNNING);
238         }
239         String loginUser = GlobalVariables.getUserSession().getPrincipalName();
240         oleBatchProcessJobDetailsBo.setUserName(loginUser);
241         oleBatchProcessJobDetailsBo.setBatchProfileName(profileName);
242         oleBatchProcessJobDetailsBo.setBatchProcessType(oleBatchProcessDefinitionDocument.getBatchProcessType());
243         List<OLEBatchProcessJobDetailsBo> oleBatchProcessJobDetailsBos = new ArrayList<OLEBatchProcessJobDetailsBo>();
244         oleBatchProcessJobDetailsBos.add(oleBatchProcessJobDetailsBo);
245         oleBatchProcessDefinitionDocument.setOleBatchProcessJobDetailsBoList(oleBatchProcessJobDetailsBos);
246         oleBatchProcessJobDetailsBo.setStartTime(timestamp);
247         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
248         dataCarrierService.addData("oleBatchProcessJobDetailsBo",oleBatchProcessJobDetailsBo);
249         return oleBatchProcessJobDetailsBo;
250     }
251 
252     private void saveSchedule(OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm) {
253         // OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm)form;
254         Timestamp timestamp = new Timestamp(new Date().getTime());
255         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
256 
257         oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().setScheduleType(oleBatchProcessDefinitionDocument.getScheduleType());
258         oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().setOneTimeOrRecurring(oleBatchProcessDefinitionDocument.getOneTimeOrRecurring());
259         OLEBatchProcessScheduleBo oleBatchProcessScheduleBo = oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo();
260         if (oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
261             if(oleBatchProcessDefinitionDocument.getEdiFile() != null) {
262                 MultipartFile marcFile = oleBatchProcessDefinitionDocument.getMarcFile();
263                 MultipartFile ediFile = oleBatchProcessDefinitionDocument.getEdiFile();
264                 oleBatchProcessScheduleBo.setUploadFileName(marcFile.getOriginalFilename() + "," + ediFile.getOriginalFilename());
265                 oleBatchProcessDefinitionDocument.setUploadFileName(marcFile.getOriginalFilename() + "," + ediFile.getOriginalFilename());
266                 oleBatchProcessDefinitionDocument.setMarcFileName(marcFile.getOriginalFilename());
267                 oleBatchProcessDefinitionDocument.setEdiFileName(ediFile.getOriginalFilename());
268             }
269             else{
270                 MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getMarcFile();
271                 oleBatchProcessScheduleBo.setUploadFileName(ingestFile.getOriginalFilename());
272                 oleBatchProcessDefinitionDocument.setMarcFileName(ingestFile.getOriginalFilename());
273             }
274         } else if ((oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT) && oleBatchProcessDefinitionDocument.getLoadIdFromFile().equalsIgnoreCase(String.valueOf(Boolean.TRUE))) || !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
275             MultipartFile ingestFile = oleBatchProcessDefinitionDocument.getIngestedFile();
276             oleBatchProcessScheduleBo.setUploadFileName(ingestFile.getOriginalFilename());
277             oleBatchProcessDefinitionDocument.setUploadFileName(ingestFile.getOriginalFilename());
278         }
279         oleBatchProcessScheduleBo.setCreateTime(timestamp);
280         String loginUser = GlobalVariables.getUserSession().getPrincipalName();
281         oleBatchProcessScheduleBo.setUserName(loginUser);
282         String cronExpression = null;
283         if (!oleBatchProcessDefinitionDocument.getCronOrSchedule().equals(OLEConstants.OLEBatchProcess.PROVIDED_CRON)) {
284             OLESchedulerHelper oleSchedulerHelper = OLESchedulerHelper.getInstance();
285             cronExpression = oleSchedulerHelper.getCronExpression(oleBatchProcessScheduleBo);
286         } else {
287             cronExpression = oleBatchProcessDefinitionDocument.getEnteredCronExp();
288         }
289         boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExpression);
290         if (!validCronExpression) {
291             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OLEBatchProcess.ERROR_CRON_EXPRESSION);
292             return;
293         }
294         oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().setCronExpression(cronExpression);
295         oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().setWeekDays(oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().getWeekDays());
296         oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().setBatchProcessType(oleBatchProcessDefinitionDocument.getBatchProcessType());
297         List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBos = new ArrayList<OLEBatchProcessScheduleBo>();
298         oleBatchProcessScheduleBos.add(oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo());
299         oleBatchProcessDefinitionDocument.setOleBatchProcessScheduleBoList(oleBatchProcessScheduleBos);
300     }
301 
302     @Override
303     @RequestMapping(params = "methodToCall=route")
304     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
305                               HttpServletRequest request, HttpServletResponse response) {
306         ModelAndView modelAndView = null;
307         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
308         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
309         oleBatchProcessDefinitionDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEBatchProcess.NEW_BATCH_PRCS_DOCUMENT + oleBatchProcessDefinitionDocument.getDocumentNumber());
310         boolean isValidated = getOleBatchProcessRule().batchValidations(oleBatchProcessDefinitionForm);
311         if (!isValidated) {
312             return getUIFModelAndView(oleBatchProcessDefinitionForm);
313         }
314         oleBatchProcessDefinitionDocument.setUser(GlobalVariables.getUserSession().getPrincipalName());
315         if (!oleBatchProcessDefinitionDocument.isRunNowFlag()) {
316             saveSchedule(oleBatchProcessDefinitionForm);
317             boolean updateDocFlag = false;
318             if (oleBatchProcessDefinitionDocument.getBatchProcessId() != null) {
319                 updateDocFlag = true;
320                 if (oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo() != null) {
321                     rescheduleJob(oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().getScheduleId(), oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBo().getCronExpression());
322                 }
323             }
324 
325             try {
326                 modelAndView = super.save(oleBatchProcessDefinitionForm, result, request, response);
327                 if (oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBoList() != null && oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBoList().size() > 0 && !updateDocFlag) {
328                     if (oleBatchProcessDefinitionDocument.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT)) {
329                         createBatchProcessSchedulerFile(oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBoList().get(0), oleBatchProcessDefinitionDocument.getMarcFile(), oleBatchProcessDefinitionDocument.getEdiFile(), oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE);
330                     } else if ((oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT) && oleBatchProcessDefinitionDocument.getLoadIdFromFile().equalsIgnoreCase(String.valueOf(Boolean.TRUE))) || !oleBatchProcessDefinitionDocument.getBatchProcessType().equalsIgnoreCase(OLEConstants.OLEBatchProcess.BATCH_EXPORT)) {
331                         createBatchProcessSchedulerFile(oleBatchProcessDefinitionDocument.getOleBatchProcessScheduleBoList().get(0), oleBatchProcessDefinitionDocument.getIngestedFile(), null, oleBatchProcessDefinitionDocument.getBatchProcessType(), OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE);
332                     }
333                 }
334             } catch (Exception e) {
335                 LOG.error("Error when scheduling the job :" + e);
336             }
337             OLEBatchProcessScheduleBo scheduleBo = ((OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument()).getOleBatchProcessScheduleBo();
338             try {
339                 addToBatchScheduler(scheduleBo.getScheduleId(), scheduleBo.getCronExpression());
340             } catch (Exception e) {
341                 throw new RuntimeException(e);
342             }
343             oleBatchProcessDefinitionDocument.setRescheduleFlag(true);
344             oleBatchProcessDefinitionDocument.setAfterSubmitFlag(true);
345         }
346         return modelAndView;
347     }
348 
349     @Override
350     @RequestMapping(params = "methodToCall=save")
351     public ModelAndView save(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
352                              HttpServletRequest request, HttpServletResponse response) throws Exception {
353         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
354         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
355         oleBatchProcessDefinitionDocument.getDocumentHeader().setDocumentDescription(OLEConstants.OLEBatchProcess.NEW_BATCH_PRCS_DOCUMENT + oleBatchProcessDefinitionDocument.getDocumentNumber());
356         boolean isValidated = getOleBatchProcessRule().batchValidations(oleBatchProcessDefinitionForm);
357         if (!isValidated) {
358             return getUIFModelAndView(oleBatchProcessDefinitionForm);
359         }
360         return super.save(oleBatchProcessDefinitionForm, result, request, response);
361     }
362 
363     @RequestMapping(params = "methodToCall=clear")
364     public ModelAndView clear(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
365                               HttpServletRequest request, HttpServletResponse response) {
366         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
367         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
368         oleBatchProcessDefinitionDocument.setScheduleFlag(false);
369         oleBatchProcessDefinitionDocument.setBatchProcessName("");
370         oleBatchProcessDefinitionDocument.setBatchProcessProfileName("");
371         oleBatchProcessDefinitionDocument.setBatchProcessType("");
372         oleBatchProcessDefinitionDocument.setBatchProcessKRMSProfile("");
373         oleBatchProcessDefinitionDocument.setChunkSize(0);
374         return getUIFModelAndView(oleBatchProcessDefinitionForm);
375     }
376 
377     /**
378      * Ads the scheduled record id as the jobName and sets the trigger for the cronExpression supplied
379      * @param jobName
380      * @param cronExpression
381      */
382     private void addToBatchScheduler(String jobName, String cronExpression) throws Exception {
383         OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
384         schedulerService.initializeJobsForModule(jobName);
385         schedulerService.initializeTriggersForModule(jobName, cronExpression);
386     }
387 
388     /**
389      * Runs the process for the profile selected and takes the scheduled record id as the job name
390      *
391      * @param jobName
392      */
393     private void runJobNow(String jobName) {
394         OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
395         try {
396             schedulerService.startJob(jobName);
397         } catch (Exception e) {
398             LOG.error("Error while starting job with job id :: "+jobName);
399         }
400     }
401 
402    /* private String getBatchProcessFilePath(String batchProceesType) {
403 
404         String batchProcessLocation = getOLEBatchProcessDataHelper().getBatchProcessFilePath(batchProceesType);
405         return batchProcessLocation;
406     }*/
407 
408     private void createBatchProcessJobFile(OLEBatchProcessJobDetailsBo batchProcessJobDetailsBo, MultipartFile ingestFile1, MultipartFile ingestFile2, String batchProceesType, String jobType) throws Exception {
409         if (ingestFile2 == null) {
410             String ingestFileName = batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile1.getOriginalFilename();
411             getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, ingestFileName, new String(ingestFile1.getBytes()), batchProcessJobDetailsBo.getJobId());
412         } else {
413 
414             String mrcFileName = batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile1.getOriginalFilename();
415             String ediFileName = batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile2.getOriginalFilename();
416             if(ediFileName != null){
417                 getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, mrcFileName, ediFileName, new String(ingestFile1.getBytes()), new String(ingestFile2.getBytes()) ,batchProcessJobDetailsBo.getJobId() );
418             }
419            /* else {
420                 getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, mrcFileName, new String(ingestFile1.getBytes()), new String(ingestFile2.getBytes()));
421             }*/
422 
423         }
424 
425     }
426 
427     private void createBatchProcessJobFile(OLEBatchProcessJobDetailsBo batchProcessJobDetailsBo, MultipartFile ingestFile1, MultipartFile ingestFile2, MultipartFile ingestFile3, String batchProceesType, String jobType) throws Exception {
428         if (ingestFile1 != null || ingestFile2 != null || ingestFile3 != null) {
429             String documentFileName = ingestFile1 != null ? batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile1.getOriginalFilename() : null;
430             String typeFileName = ingestFile2 != null ? batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile2.getOriginalFilename() : null;
431             String historyFileName = ingestFile3 != null ? batchProcessJobDetailsBo.getJobId() + jobType + "_" + ingestFile3.getOriginalFilename() : null;
432             getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, documentFileName, typeFileName, historyFileName, ingestFile1 !=null ? new String(ingestFile1.getBytes()) : null, ingestFile2 !=null ? new String(ingestFile2.getBytes()) : null, ingestFile3 !=null ? new String(ingestFile3.getBytes()) : null ,batchProcessJobDetailsBo.getJobId());
433         }
434     }
435 
436     private void createBatchProcessSchedulerFile(OLEBatchProcessScheduleBo oleBatchProcessScheduleBo, MultipartFile ingestFile1, MultipartFile ingestFile2, String batchProceesType, String jobType) throws Exception {
437         if (ingestFile2 == null && ingestFile1!=null) {
438             String ingestFileName = oleBatchProcessScheduleBo.getScheduleId() + jobType + "_" + ingestFile1.getOriginalFilename();
439             getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, ingestFileName, new String(ingestFile1.getBytes()), oleBatchProcessScheduleBo.getScheduleId());
440         }
441         else  if (ingestFile2 != null && ingestFile1!=null) {
442             String mrcFileName = oleBatchProcessScheduleBo.getScheduleId() + jobType + "_" + ingestFile1.getOriginalFilename();
443             String ediFileName = oleBatchProcessScheduleBo.getScheduleId() + jobType + "_" + ingestFile2.getOriginalFilename();
444             getOLEBatchProcessDataHelper().createBatchProcessFile(batchProceesType, mrcFileName, ediFileName, new String(ingestFile1.getBytes()), new String(ingestFile2.getBytes()) , oleBatchProcessScheduleBo.getScheduleId());
445 
446         }
447 
448     }
449 
450     @RequestMapping(params = "methodToCall=docHandler")
451     public ModelAndView docHandler(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
452                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
453         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
454         String batchProcessId= request.getParameter("batchProcessId");
455         ModelAndView modelAndView = super.docHandler(oleBatchProcessDefinitionForm,result,request,response);
456         OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
457         batchProcessId= oleBatchProcessDefinitionDocument.getBatchProcessId();
458         if(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT.equals(oleBatchProcessDefinitionDocument.getBatchProcessType()) && oleBatchProcessDefinitionDocument.getUploadFileName()!=null) {
459             String[] fileNames = oleBatchProcessDefinitionDocument.getUploadFileName().split(",");
460             if(fileNames.length == 2) {
461                 oleBatchProcessDefinitionDocument.setMarcFileName(fileNames[0]);
462                 oleBatchProcessDefinitionDocument.setEdiFileName(fileNames[1]);
463             }
464             else {
465                 oleBatchProcessDefinitionDocument.setMarcFileName(fileNames[0]);
466             }
467         }
468 
469         if(oleBatchProcessDefinitionDocument.getCronOrSchedule()!=null) {
470             Map batchProcessMap = new HashMap();
471             batchProcessMap.put("batchProcessId", batchProcessId);
472             List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = (List<OLEBatchProcessScheduleBo>) getBusinessObjectService().findMatching(OLEBatchProcessScheduleBo.class, batchProcessMap);
473             if(oleBatchProcessScheduleBoList!=null && oleBatchProcessScheduleBoList.size() > 0){
474                 oleBatchProcessDefinitionDocument.setOleBatchProcessScheduleBo(oleBatchProcessScheduleBoList.get(0));
475                 oleBatchProcessDefinitionDocument.setScheduleFlag(true);
476             }
477         }
478         if (oleBatchProcessDefinitionDocument != null && oleBatchProcessDefinitionDocument.getDocumentHeader() != null
479                 && oleBatchProcessDefinitionDocument.getDocumentHeader().getWorkflowDocument() != null
480                 && oleBatchProcessDefinitionDocument.getDocumentHeader().getWorkflowDocument().isSaved()) {
481             oleBatchProcessDefinitionDocument.setAfterSubmitFlag(true);
482         }
483         return modelAndView;
484     }
485 
486     @RequestMapping(params = "methodToCall=reschedule")
487     public ModelAndView reschedule(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
488                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
489         OLEBatchProcessDefinitionForm oleBatchProcessDefinitionForm = (OLEBatchProcessDefinitionForm) form;
490         String batchProcessId = request.getParameter("batchProcessId");
491         ModelAndView modelAndView = null;
492         if (batchProcessId != null) {
493             Map batchProcessMap = new HashMap();
494             batchProcessMap.put("batchProcessId", batchProcessId);
495             List<OLEBatchProcessDefinitionDocument> oleBatchProcessDefinitionDocuments = (List<OLEBatchProcessDefinitionDocument>) getBusinessObjectService().findMatching(OLEBatchProcessDefinitionDocument.class, batchProcessMap);
496             if (oleBatchProcessDefinitionDocuments.size() > 0) {
497                 oleBatchProcessDefinitionForm.setDocId(oleBatchProcessDefinitionDocuments.get(0).getDocumentNumber());
498                 modelAndView = super.docHandler(oleBatchProcessDefinitionForm, result, request, response);
499                 OLEBatchProcessDefinitionDocument oleBatchProcessDefinitionDocument = (OLEBatchProcessDefinitionDocument) oleBatchProcessDefinitionForm.getDocument();
500                 oleBatchProcessDefinitionDocument.setRescheduleFlag(false);
501                 if(OLEConstants.OLEBatchProcess.ORDER_RECORD_IMPORT.equals(oleBatchProcessDefinitionDocument.getBatchProcessType()) && oleBatchProcessDefinitionDocument.getUploadFileName()!=null) {
502                     String[] fileNames = oleBatchProcessDefinitionDocument.getUploadFileName().split(",");
503                     if(fileNames.length == 2) {
504                         oleBatchProcessDefinitionDocument.setMarcFileName(fileNames[0]);
505                         oleBatchProcessDefinitionDocument.setEdiFileName(fileNames[1]);
506                     }
507                     else {
508                         oleBatchProcessDefinitionDocument.setMarcFileName(fileNames[0]);
509                     }
510                 }
511                 if (oleBatchProcessDefinitionDocument.getCronOrSchedule() != null) {
512                     List<OLEBatchProcessScheduleBo> oleBatchProcessScheduleBoList = (List<OLEBatchProcessScheduleBo>) getBusinessObjectService().findMatching(OLEBatchProcessScheduleBo.class, batchProcessMap);
513                     if (oleBatchProcessScheduleBoList != null && oleBatchProcessScheduleBoList.size() > 0) {
514                         oleBatchProcessDefinitionDocument.setOleBatchProcessScheduleBo(oleBatchProcessScheduleBoList.get(0));
515                         oleBatchProcessDefinitionDocument.setScheduleFlag(true);
516                     }
517                 }
518             }
519         }
520 
521         return modelAndView;
522     }
523 
524     private void rescheduleJob(String scheduleId, String cronExp) {
525         OLEBatchSchedulerService schedulerService = GlobalResourceLoader.getService("oleSchedulerService");
526         try {
527             schedulerService.rescheduleJob(scheduleId, cronExp);
528         } catch (Exception e) {
529             LOG.error("Error while removing job with schedule id :: " + scheduleId);
530         }
531     }
532 
533     @RequestMapping(params = "methodToCall=close")
534     public ModelAndView close(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
535                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
536         String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
537         String url = baseUrl + "/portal.do";
538         Properties props = new Properties();
539         props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
540         if (StringUtils.isNotBlank(form.getReturnFormKey())) {
541             props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
542         }
543         return performRedirect(form, url, props);
544     }
545 }