1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch.service.impl;
17
18 import java.io.File;
19 import java.io.InputStream;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.ole.gl.batch.EnterpriseFeedStep;
27 import org.kuali.ole.gl.batch.service.EnterpriseFeederNotificationService;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.OLEKeyConstants;
30 import org.kuali.ole.sys.Message;
31 import org.kuali.ole.sys.service.impl.OleParameterConstants;
32 import org.kuali.rice.core.api.config.property.ConfigurationService;
33 import org.kuali.rice.core.api.mail.MailMessage;
34 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35 import org.kuali.rice.krad.service.MailService;
36
37
38
39
40 public class EnterpriseFeederNotificationServiceImpl implements EnterpriseFeederNotificationService {
41 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EnterpriseFeederNotificationServiceImpl.class);
42
43 private ParameterService parameterService;
44 private ConfigurationService configurationService;
45 private MailService mailService;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Override
63 public void notifyFileFeedStatus(String feederProcessName, EnterpriseFeederStatus status, File doneFile, File dataFile, File reconFile, List<Message> errorMessages) {
64 String doneFileDescription = doneFile == null ? "Done file missing" : doneFile.getAbsolutePath();
65 String dataFileDescription = dataFile == null ? "Data file missing" : dataFile.getAbsolutePath();
66 String reconFileDescription = reconFile == null ? "Recon file missing" : reconFile.getAbsolutePath();
67
68
69 notifyFileFeedStatus(feederProcessName, status, doneFileDescription, null, dataFileDescription, null, reconFileDescription, null, errorMessages);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 @Override
91 public void notifyFileFeedStatus(String feederProcessName, EnterpriseFeederStatus status, String doneFileDescription, InputStream doneFileContents, String dataFileDescription, InputStream dataFileContents, String reconFileDescription, InputStream reconFileContents, List<Message> errorMessages) {
92 try {
93 if (isStatusNotifiable(feederProcessName, status, doneFileDescription, dataFileDescription, reconFileDescription, errorMessages)) {
94 Set<String> toEmailAddresses = generateToEmailAddresses(feederProcessName, status, doneFileDescription, dataFileDescription, reconFileDescription, errorMessages);
95
96 MailMessage mailMessage = new MailMessage();
97 String returnAddress = parameterService.getParameterValueAsString(OleParameterConstants.GENERAL_LEDGER_BATCH.class, OLEConstants.FROM_EMAIL_ADDRESS_PARM_NM);
98 if(StringUtils.isEmpty(returnAddress)) {
99 returnAddress = mailService.getBatchMailingList();
100 }
101 mailMessage.setFromAddress(returnAddress);
102 mailMessage.setToAddresses(toEmailAddresses);
103 mailMessage.setSubject(getSubjectLine(doneFileDescription, dataFileDescription, reconFileDescription, errorMessages, feederProcessName, status));
104 mailMessage.setMessage(buildFileFeedStatusMessage(doneFileDescription, dataFileDescription, reconFileDescription, errorMessages, feederProcessName, status));
105
106 mailService.sendMessage(mailMessage);
107 }
108 }
109 catch (Exception e) {
110
111
112 LOG.error("Error occured trying to send notifications.", e);
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 protected Set<String> generateToEmailAddresses(String feederProcessName, EnterpriseFeederStatus status, String doneFileDescription, String dataFileDescription, String reconFileDescription, List<Message> errorMessages) {
130 Set<String> addresses = new HashSet<String>();
131 Collection<String> addressesArray = parameterService.getParameterValuesAsString(EnterpriseFeedStep.class, OLEConstants.EnterpriseFeederApplicationParameterKeys.TO_ADDRESS);
132 for (String address : addressesArray) {
133 addresses.add(address);
134 }
135 return addresses;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 protected String generateFromEmailAddress(String feederProcessName, EnterpriseFeederStatus status, String doneFileDescription, String dataFileDescription, String reconFileDescription, List<Message> errorMessages) {
152 return mailService.getBatchMailingList();
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 @Override
170 public String getFileFeedStatusMessage(String feederProcessName, EnterpriseFeederStatus status, File doneFile, File dataFile, File reconFile, List<Message> errorMessages) {
171 String doneFileDescription = doneFile.getAbsolutePath();
172 String dataFileDescription = dataFile.getAbsolutePath();
173 String reconFileDescription = reconFile.getAbsolutePath();
174
175 return buildFileFeedStatusMessage(doneFileDescription, dataFileDescription, reconFileDescription, errorMessages, feederProcessName, status);
176 }
177
178
179
180
181
182
183 @Override
184 public String getFileFeedStatusMessage(String feederProcessName, EnterpriseFeederStatus status, String doneFileDescription, InputStream doneFileContents, String dataFileDescription, InputStream dataFileContents, String reconFileDescription, InputStream reconFileContents, List<Message> errorMessages) {
185 return buildFileFeedStatusMessage(doneFileDescription, dataFileDescription, reconFileDescription, errorMessages, feederProcessName, status);
186 }
187
188
189
190
191
192
193
194
195
196
197
198 protected String getSubjectLine(String doneFileDescription, String dataFileDescription, String reconFileDescription, List<Message> errorMessages, String feederProcessName, EnterpriseFeederStatus status) {
199 String subject = configurationService.getPropertyValueAsString(OLEKeyConstants.ERROR_ENTERPRISE_FEEDER_RECONCILIATION_OR_LOADING_ERROR);
200 if (subject == null) {
201 return "ERROR in reconciling or loading GL origin entries from file.";
202 }
203 String productionEnvironmentCode = configurationService.getPropertyValueAsString(OLEConstants.PROD_ENVIRONMENT_CODE_KEY);
204 String environmentCode = configurationService.getPropertyValueAsString(OLEConstants.ENVIRONMENT_KEY);
205 if (!StringUtils.equals(productionEnvironmentCode, environmentCode)) {
206 subject = environmentCode + ": " + subject;
207 }
208 return subject;
209 }
210
211
212
213
214
215
216
217
218
219
220
221 protected String buildFileFeedStatusMessage(String doneFileName, String dataFileName, String reconFileName, List<Message> errorMessages, String feederProcessName, EnterpriseFeederStatus status) {
222 StringBuilder buf = new StringBuilder();
223
224 buf.append("Data file: ").append(dataFileName).append("\n");
225 buf.append("Reconciliation File: ").append(reconFileName).append("\n");
226 buf.append("Done file: ").append(doneFileName).append("\n\n\n");
227
228 buf.append("Status: ").append(status.getStatusDescription()).append("\n\n\n");
229
230 if (status.isErrorEvent()) {
231 buf.append("The done file has been removed and ");
232 if (StringUtils.isNotBlank(feederProcessName)) {
233 buf.append(feederProcessName);
234 }
235 else {
236 buf.append("<process name unavailable>");
237 }
238 buf.append(" will continue without processing this set of files (see below).");
239
240 buf.append(" Please correct and resend the files for the next day's batch.");
241 }
242
243 buf.append("\n\n");
244
245 if (!errorMessages.isEmpty()) {
246 buf.append("Error/warning messages:\n");
247 for (Message message : errorMessages) {
248 if (message.getType() == Message.TYPE_FATAL) {
249 buf.append("ERROR: ");
250 }
251 if (message.getType() == Message.TYPE_WARNING) {
252 buf.append("WARNING: ");
253 }
254 buf.append(message.getMessage()).append("\n");
255 }
256 }
257
258 return buf.toString();
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 protected boolean isStatusNotifiable(String feederProcessName, EnterpriseFeederStatus status, String doneFileDescription, String dataFileDescription, String reconFileDescription, List<Message> errorMessages) {
273 if (status instanceof FileReconOkLoadOkStatus) {
274 return false;
275 }
276 return true;
277 }
278
279
280
281
282
283
284
285 public void setMailService(MailService mailService) {
286 this.mailService = mailService;
287 }
288
289 public void setConfigurationService(ConfigurationService configurationService) {
290 this.configurationService = configurationService;
291 }
292
293 public void setParameterService(ParameterService parameterService) {
294 this.parameterService = parameterService;
295 }
296 }