1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.coa.batch;
17
18 import java.io.IOException;
19 import java.util.Collection;
20 import java.util.Date;
21
22 import org.apache.log4j.Logger;
23 import org.kuali.ole.coa.businessobject.CfdaUpdateResults;
24 import org.kuali.ole.coa.service.CfdaService;
25 import org.kuali.ole.sys.OLEConstants;
26 import org.kuali.ole.sys.OLEKeyConstants;
27 import org.kuali.ole.sys.batch.AbstractStep;
28 import org.kuali.rice.core.api.config.property.ConfigurationService;
29 import org.kuali.rice.core.api.mail.MailMessage;
30 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
31 import org.kuali.rice.krad.service.MailService;
32
33
34
35
36
37
38 public class CfdaBatchStep extends AbstractStep {
39
40 private static final Logger LOG = org.apache.log4j.Logger.getLogger(CfdaBatchStep.class);
41
42 protected CfdaService cfdaService;
43 protected MailService mailService;
44 protected ParameterService parameterService;
45 protected ConfigurationService configurationService;
46
47
48
49
50
51 @Override
52 public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
53 MailMessage message = new MailMessage();
54
55 try {
56 CfdaUpdateResults results = cfdaService.update();
57
58
59 StringBuilder builder = new StringBuilder();
60 builder.append("The CFDA batch script is complete.\n");
61 builder.append(" - ");
62 builder.append(results.getNumberOfRecordsDeactivatedBecauseNoLongerOnWebSite());
63 builder.append(" records were deactivated because they are no longer on the web site.\n");
64 builder.append(" - ");
65 builder.append(results.getNumberOfRecordsInKfsDatabase());
66 builder.append(" records were in the KFS database.\n");
67 builder.append(" - ");
68 builder.append(results.getNumberOfRecordsNewlyAddedFromWebSite());
69 builder.append(" records were newly added from the web site.\n");
70 builder.append(" - ");
71 builder.append(results.getNumberOfRecordsNotUpdatedBecauseManual());
72 builder.append(" records were not updated because they are manual.\n");
73 builder.append(" - ");
74 builder.append(results.getNumberOfRecordsReActivated());
75 builder.append(" records were re-activated.\n");
76 builder.append(" - ");
77 builder.append(results.getNumberOfRecordsRetrievedFromWebSite());
78 builder.append(" records were retrieved from the web site.\n");
79 builder.append(" - ");
80 builder.append(results.getNumberOfRecordsUpdatedBecauseAutomatic());
81 builder.append(" records were updated because they are automatic.\n");
82 builder.append(" - ");
83 builder.append(results.getNumberOfRecrodsNotUpdatedForHistoricalPurposes());
84 builder.append(" records were not updated for historical reasons.\n");
85 builder.append(" - Message\n");
86 builder.append(null != results.getMessage() ? results.getMessage() : "");
87
88 LOG.info(message.toString());
89
90 Collection<String> listservAddresses = parameterService.getParameterValuesAsString(CfdaBatchStep.class, OLEConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES);
91 if (listservAddresses.isEmpty()) {
92 LOG.fatal("No addresses for notification to in " + OLEConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES + " parameter. Aborting Email.");
93 return true;
94 }
95
96 for (String listserv : listservAddresses) {
97 if (LOG.isInfoEnabled()) {
98 LOG.info("Mailing to: "+listserv);
99 }
100 message.addToAddress(listserv);
101 }
102
103 message.setFromAddress(listservAddresses.iterator().next() );
104
105
106 message.setSubject(getConfigurationService().getPropertyValueAsString(OLEKeyConstants.CFDA_UPDATE_EMAIL_SUBJECT_LINE));
107 message.setMessage(builder.toString());
108 mailService.sendMessage(message);
109
110 }
111 catch (IOException ioe) {
112 LOG.warn("Exception while updating CFDA codes.", ioe);
113 return false;
114 }
115 catch (Exception iae) {
116
117 LOG.warn("The email address for "+CfdaBatchStep.class+":"+OLEConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES+" is invalid.", iae);
118 return true;
119 }
120 return true;
121 }
122
123
124
125
126
127
128 public void setCfdaService(CfdaService cfdaService) {
129 this.cfdaService = cfdaService;
130 }
131
132
133
134
135
136
137 public void setMailService(MailService mailService) {
138 this.mailService = mailService;
139 }
140
141
142
143
144
145
146 @Override
147 public void setParameterService(ParameterService parameterService) {
148 this.parameterService = parameterService;
149 }
150
151
152
153
154
155 public ConfigurationService getConfigurationService() {
156 return configurationService;
157 }
158
159
160
161
162
163 public void setConfigurationService(ConfigurationService configurationService) {
164 this.configurationService = configurationService;
165 }
166
167 }
168