View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Parses data from a government web page listing the valid CFDA codes. The codes are then compared with what's in the CFDA table in
35   * Kuali. Codes set to be managed automatically are reconciled with what's on the web page. Codes managed manually are left alone.
36   * Finally an email containing a summary of what was done by the step execution is sent to the member of the CG_CFDA_BATCH_NOTIFY workgroup.
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       * See the class description.
48       *
49       * @see org.kuali.ole.sys.batch.Step#execute(String, Date)
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              // TODO this message should come from some config file.
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      * Sets the {@link CfdaService}. For use by Spring.
125      *
126      * @param cfdaService The service to be assigned.
127      */
128     public void setCfdaService(CfdaService cfdaService) {
129         this.cfdaService = cfdaService;
130     }
131 
132     /**
133      * Set the {@link MailService}. For use by Spring.
134      *
135      * @param mailService The service to be assigned.
136      */
137     public void setMailService(MailService mailService) {
138         this.mailService = mailService;
139     }
140 
141     /**
142      * Sets the {@link ParameterService}. For use by Spring.
143      *
144      * @param parameterService The service to be assigned.
145      */
146     @Override
147     public void setParameterService(ParameterService parameterService) {
148         this.parameterService = parameterService;
149     }
150 
151     /**
152      * Gets the configurationService attribute.
153      * @return Returns the configurationService.
154      */
155     public ConfigurationService getConfigurationService() {
156         return configurationService;
157     }
158 
159     /**
160      * Sets the configurationService attribute value.
161      * @param configurationService The configurationService to set.
162      */
163     public void setConfigurationService(ConfigurationService configurationService) {
164         this.configurationService = configurationService;
165     }
166 
167 }
168