1 package org.kuali.ole.batch.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.apache.velocity.Template;
6 import org.apache.velocity.VelocityContext;
7 import org.apache.velocity.app.VelocityEngine;
8 import org.kuali.ole.OLEConstants;
9 import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
10 import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
11 import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
12 import org.kuali.ole.batch.helper.OLEBatchProcessDataHelper;
13 import org.kuali.ole.deliver.batch.OleMailer;
14 import org.kuali.ole.sys.batch.AbstractStep;
15 import org.kuali.ole.sys.batch.service.SchedulerService;
16 import org.kuali.rice.core.api.mail.EmailBody;
17 import org.kuali.rice.core.api.mail.EmailFrom;
18 import org.kuali.rice.core.api.mail.EmailSubject;
19 import org.kuali.rice.core.api.mail.EmailToList;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.krad.service.KRADServiceLocator;
22
23 import java.io.StringWriter;
24 import java.util.*;
25
26
27
28
29
30
31
32
33 public class OLEBatchProcessEmailStep extends AbstractStep {
34 private static final Logger LOG = Logger.getLogger(OLEBatchProcessEmailStep.class);
35 private static final String FROMEMAIL = "BATCHJOBADMIN@KUALI.ORG";
36
37 private OleMailer oleMailer;
38
39 private OleMailer getOleMailer() {
40 if (oleMailer == null) {
41 oleMailer = GlobalResourceLoader.getService("oleMailer");
42 }
43 return oleMailer;
44 }
45
46 private OLEBatchProcessDataHelper oleBatchProcessDataHelper;
47
48 private OLEBatchProcessDataHelper getOLEBatchProcessDataHelper() {
49
50 if (oleBatchProcessDataHelper == null) {
51 oleBatchProcessDataHelper = OLEBatchProcessDataHelper.getInstance();
52 }
53 return oleBatchProcessDataHelper;
54 }
55
56 @Override
57 public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
58 buildJobReport(jobName);
59 return true;
60 }
61
62 private void buildJobReport(String jobName) {
63 try {
64 String jobId = StringUtils.substringAfter(jobName, SchedulerService.SCHEDULED_GROUP + "." + OLEConstants.OLEBatchProcess.ADHOC_BATCH_JOB);
65 String scheduleId = StringUtils.substringAfter(jobName, SchedulerService.SCHEDULED_GROUP + "." + OLEConstants.OLEBatchProcess.BATCH_JOB);
66 if (StringUtils.isNotEmpty(jobId)) {
67 OLEBatchProcessJobDetailsBo jobDetailsBo = readJobRecord(jobId);
68 OLEBatchProcessDefinitionDocument processDefDoc = readOLEBatchProcessDefinitionDocument(jobDetailsBo.getBatchProcessId());
69 sendJobReportMail(processDefDoc, jobDetailsBo);
70 } else if (StringUtils.isNotEmpty(scheduleId)) {
71 OLEBatchProcessScheduleBo scheduleBo = readScheduleRecord(scheduleId);
72 OLEBatchProcessJobDetailsBo jobDetailsBo = readJobRecordForSch(scheduleBo.getBatchProcessId());
73 if(jobDetailsBo==null){
74 LOG.error("Error while Running step OLEBatchProcessEmailStep:: Unable to get job details for process id:: "+scheduleBo.getBatchProcessId());
75 }else{
76 OLEBatchProcessDefinitionDocument processDefDoc = readOLEBatchProcessDefinitionDocument(jobDetailsBo.getBatchProcessId());
77 sendJobReportMail(processDefDoc, jobDetailsBo);
78 }
79 }
80 } catch (Exception ex) {
81 LOG.error("Error while Running step OLEBatchProcessEmailStep:: ", ex);
82 }
83 }
84
85
86
87
88
89
90
91 private OLEBatchProcessJobDetailsBo readJobRecordForSch(String processId) {
92 Map<String, String> jobMap = new HashMap<String, String>();
93 jobMap.put("bat_prcs_id", processId);
94 List<OLEBatchProcessJobDetailsBo> jobDetailsBoList = (List<OLEBatchProcessJobDetailsBo>) KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(OLEBatchProcessJobDetailsBo.class, jobMap, "JOB_ID", false);
95 if (jobDetailsBoList.isEmpty()) {
96 return null;
97 } else {
98 return jobDetailsBoList.get(0);
99 }
100 }
101
102
103
104
105
106
107
108 private OLEBatchProcessJobDetailsBo readJobRecord(String jobId) {
109 Map<String, String> jobMap = new HashMap<String, String>();
110 jobMap.put("job_id", jobId);
111 OLEBatchProcessJobDetailsBo jobDetailsBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessJobDetailsBo.class, jobMap);
112 return jobDetailsBo;
113 }
114
115
116
117
118
119
120
121 private OLEBatchProcessScheduleBo readScheduleRecord(String scheduleId) {
122 Map<String, String> schMap = new HashMap<String, String>();
123 schMap.put("ole_bat_prcs_schdule_id", scheduleId);
124 OLEBatchProcessScheduleBo scheduleBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessScheduleBo.class, schMap);
125 return scheduleBo;
126 }
127
128
129
130
131
132
133
134 private OLEBatchProcessDefinitionDocument readOLEBatchProcessDefinitionDocument(String batchProcessId) {
135 Map<String, String> jobMap = new HashMap<String, String>();
136 jobMap.put("bat_prcs_id", batchProcessId);
137 OLEBatchProcessDefinitionDocument processDefDoc = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEBatchProcessDefinitionDocument.class, jobMap);
138 return processDefDoc;
139 }
140
141
142
143
144
145
146
147 private void sendJobReportMail(OLEBatchProcessDefinitionDocument processDefDoc, OLEBatchProcessJobDetailsBo job) throws Exception {
148 if (StringUtils.isNotBlank(processDefDoc.getEmailIds())) {
149 String[] emailsIdsArray = processDefDoc.getEmailIds().split(",");
150 if (Integer.parseInt(job.getNoOfSuccessRecords()) <= 0) {
151 if (processDefDoc.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.BATCH_DELETE)){
152 getOleMailer().sendEmail(new EmailFrom(FROMEMAIL), new EmailToList(Arrays.asList(emailsIdsArray)),
153 new EmailSubject(OLEConstants.OLEBatchProcess.BATCH_DELETION_JOB + (StringUtils.isEmpty(processDefDoc.getBatchProcessName()) ? "" : processDefDoc.getBatchProcessName()) + OLEConstants.OLEBatchProcess.FAILED),
154 new EmailBody(getReportByVM(job,processDefDoc)), null, null, true);
155 }
156 else {
157 getOleMailer().sendEmail(new EmailFrom(FROMEMAIL), new EmailToList(Arrays.asList(emailsIdsArray)),
158 new EmailSubject((StringUtils.isEmpty(processDefDoc.getBatchProcessName()) ? "" : processDefDoc.getBatchProcessName() + "-") + processDefDoc.getBatchProcessType() + OLEConstants.OLEBatchProcess.FAILED),
159 new EmailBody(getReportByVM(job,processDefDoc)), null, null, true);
160 }
161 } else {
162 if (processDefDoc.getBatchProcessType().equals(OLEConstants.OLEBatchProcess.BATCH_DELETE)){
163 getOleMailer().sendEmail(new EmailFrom(FROMEMAIL), new EmailToList(Arrays.asList(emailsIdsArray)),
164 new EmailSubject(OLEConstants.OLEBatchProcess.BATCH_DELETION_JOB + (StringUtils.isEmpty(processDefDoc.getBatchProcessName()) ? "" : processDefDoc.getBatchProcessName()) + OLEConstants.OLEBatchProcess.COMPLETE),
165 new EmailBody(getReportByVM(job,processDefDoc)), null, null, true);
166 }
167 else {
168 getOleMailer().sendEmail(new EmailFrom(FROMEMAIL), new EmailToList(Arrays.asList(emailsIdsArray)),
169 new EmailSubject((StringUtils.isEmpty(processDefDoc.getBatchProcessName()) ? "" : processDefDoc.getBatchProcessName() + "-") + processDefDoc.getBatchProcessType() + OLEConstants.OLEBatchProcess.COMPLETE),
170 new EmailBody(getReportByVM(job,processDefDoc)), null, null, true);
171 }
172 }
173 }
174 }
175
176
177
178
179
180
181 private String getReportByVM(OLEBatchProcessJobDetailsBo job,OLEBatchProcessDefinitionDocument processDefDoc) throws Exception {
182 VelocityEngine ve = GlobalResourceLoader.getService("velocityEngine");
183 ve.init();
184 VelocityContext context = new VelocityContext();
185 context.put("job", job);
186 context.put("processDefDoc", processDefDoc);
187 context.put("dataHelper", OLEBatchProcessDataHelper.getInstance());
188 context.put("Integer", Integer.class);
189 context.put("StringUtils", StringUtils.class);
190 Template t = ve.getTemplate("jobreport.vm");
191 StringWriter writer = new StringWriter();
192 t.merge(context, writer);
193 return writer.toString();
194 }
195
196
197 }