001/*
002 * Copyright 2007 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.coa.batch;
017
018import java.io.IOException;
019import java.util.Collection;
020import java.util.Date;
021
022import org.apache.log4j.Logger;
023import org.kuali.ole.coa.businessobject.CfdaUpdateResults;
024import org.kuali.ole.coa.service.CfdaService;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.OLEKeyConstants;
027import org.kuali.ole.sys.batch.AbstractStep;
028import org.kuali.rice.core.api.config.property.ConfigurationService;
029import org.kuali.rice.core.api.mail.MailMessage;
030import org.kuali.rice.coreservice.framework.parameter.ParameterService;
031import org.kuali.rice.krad.service.MailService;
032
033/**
034 * 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
035 * Kuali. Codes set to be managed automatically are reconciled with what's on the web page. Codes managed manually are left alone.
036 * 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.
037 */
038public class CfdaBatchStep extends AbstractStep {
039
040    private static final Logger LOG = org.apache.log4j.Logger.getLogger(CfdaBatchStep.class);
041
042    protected CfdaService cfdaService;
043    protected MailService mailService;
044    protected ParameterService parameterService;
045    protected ConfigurationService configurationService;
046    /**
047     * See the class description.
048     *
049     * @see org.kuali.ole.sys.batch.Step#execute(String, Date)
050     */
051    @Override
052    public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
053        MailMessage message = new MailMessage();
054
055        try {
056            CfdaUpdateResults results = cfdaService.update();
057
058            // TODO this message should come from some config file.
059            StringBuilder builder = new StringBuilder();
060            builder.append("The CFDA batch script is complete.\n");
061            builder.append(" - ");
062            builder.append(results.getNumberOfRecordsDeactivatedBecauseNoLongerOnWebSite());
063            builder.append(" records were deactivated because they are no longer on the web site.\n");
064            builder.append(" - ");
065            builder.append(results.getNumberOfRecordsInKfsDatabase());
066            builder.append(" records were in the KFS database.\n");
067            builder.append(" - ");
068            builder.append(results.getNumberOfRecordsNewlyAddedFromWebSite());
069            builder.append(" records were newly added from the web site.\n");
070            builder.append(" - ");
071            builder.append(results.getNumberOfRecordsNotUpdatedBecauseManual());
072            builder.append(" records were not updated because they are manual.\n");
073            builder.append(" - ");
074            builder.append(results.getNumberOfRecordsReActivated());
075            builder.append(" records were re-activated.\n");
076            builder.append(" - ");
077            builder.append(results.getNumberOfRecordsRetrievedFromWebSite());
078            builder.append(" records were retrieved from the web site.\n");
079            builder.append(" - ");
080            builder.append(results.getNumberOfRecordsUpdatedBecauseAutomatic());
081            builder.append(" records were updated because they are automatic.\n");
082            builder.append(" - ");
083            builder.append(results.getNumberOfRecrodsNotUpdatedForHistoricalPurposes());
084            builder.append(" records were not updated for historical reasons.\n");
085            builder.append(" - Message\n");
086            builder.append(null != results.getMessage() ? results.getMessage() : "");
087
088            LOG.info(message.toString());
089
090            Collection<String> listservAddresses = parameterService.getParameterValuesAsString(CfdaBatchStep.class, OLEConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES);
091            if (listservAddresses.isEmpty()) {
092                LOG.fatal("No addresses for notification to in " + OLEConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES + " parameter.  Aborting Email.");
093                return true;
094            }
095
096            for (String listserv : listservAddresses) {
097                if (LOG.isInfoEnabled()) {
098                    LOG.info("Mailing to: "+listserv);
099                }
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