001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.cg.batch;
017    
018    import java.io.IOException;
019    import java.util.Date;
020    import java.util.List;
021    
022    import org.apache.log4j.Logger;
023    import org.kuali.kfs.module.cg.CGConstants;
024    import org.kuali.kfs.module.cg.businessobject.CfdaUpdateResults;
025    import org.kuali.kfs.module.cg.service.CfdaService;
026    import org.kuali.kfs.sys.KFSKeyConstants;
027    import org.kuali.kfs.sys.batch.AbstractStep;
028    import org.kuali.rice.kns.mail.InvalidAddressException;
029    import org.kuali.rice.kns.mail.MailMessage;
030    import org.kuali.rice.kns.service.KualiConfigurationService;
031    import org.kuali.rice.kns.service.MailService;
032    import org.kuali.rice.kns.service.ParameterService;
033    
034    /**
035     * 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
036     * Kuali. Codes set to be managed automatically are reconciled with what's on the web page. Codes managed manually are left alone.
037     * 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.
038     */
039    public class CfdaBatchStep extends AbstractStep {
040    
041        private static Logger LOG = org.apache.log4j.Logger.getLogger(CfdaBatchStep.class);
042    
043        private CfdaService cfdaService;
044        private MailService mailService;
045        private ParameterService parameterService;
046        private KualiConfigurationService configurationService;
047        /**
048         * See the class description.
049         * 
050         * @see org.kuali.kfs.sys.batch.Step#execute(String, Date)
051         */
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                List<String> listservAddresses = parameterService.getParameterValues(CfdaBatchStep.class, CGConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES);
059                if (listservAddresses == null || listservAddresses.size() == 0) {
060                    LOG.fatal("Couldn't find address to send notification to.");
061                    return true;
062                }
063                
064                for (String listserv : listservAddresses) {
065                    if (LOG.isDebugEnabled()) {
066                        LOG.debug("Mailing to: "+listserv);
067                    }
068                    message.addToAddress(listserv);
069                }
070                
071                message.setFromAddress(listservAddresses.get(0));
072    
073                // TODO this message should come from some config file.
074                StringBuilder builder = new StringBuilder();
075                builder.append("The CFDA batch script is complete.\n");
076                builder.append(" - ");
077                builder.append(results.getNumberOfRecordsDeactivatedBecauseNoLongerOnWebSite());
078                builder.append(" records were deactivated because they are no longer on the web site.\n");
079                builder.append(" - ");
080                builder.append(results.getNumberOfRecordsInKfsDatabase());
081                builder.append(" records were in the KFS database.\n");
082                builder.append(" - ");
083                builder.append(results.getNumberOfRecordsNewlyAddedFromWebSite());
084                builder.append(" records were newly added from the web site.\n");
085                builder.append(" - ");
086                builder.append(results.getNumberOfRecordsNotUpdatedBecauseManual());
087                builder.append(" records were not updated because they are manual.\n");
088                builder.append(" - ");
089                builder.append(results.getNumberOfRecordsReActivated());
090                builder.append(" records were re-activated.\n");
091                builder.append(" - ");
092                builder.append(results.getNumberOfRecordsRetrievedFromWebSite());
093                builder.append(" records were retrieved from the web site.\n");
094                builder.append(" - ");
095                builder.append(results.getNumberOfRecordsUpdatedBecauseAutomatic());
096                builder.append(" records were updated because they are automatic.\n");
097                builder.append(" - ");
098                builder.append(results.getNumberOfRecrodsNotUpdatedForHistoricalPurposes());
099                builder.append(" records were not updated for historical reasons.\n");
100                builder.append(" - Message\n");
101                builder.append(null != results.getMessage() ? results.getMessage() : "");
102    
103                message.setSubject(getConfigurationService().getPropertyString(KFSKeyConstants.CFDA_UPDATE_EMAIL_SUBJECT_LINE));
104                message.setMessage(builder.toString());
105                mailService.sendMessage(message);
106    
107            }
108            catch (IOException ioe) {
109                LOG.warn("Exception while updating CFDA codes.", ioe);
110                return false;
111            }
112            catch (InvalidAddressException iae) {
113    
114               LOG.warn("The email address for "+CfdaBatchStep.class+":"+CGConstants.RESULT_SUMMARY_TO_EMAIL_ADDRESSES+" is invalid.", iae);
115                return true;
116            }
117            return true;
118        }
119    
120        /**
121         * Sets the {@link CfdaService}. For use by Spring.
122         * 
123         * @param cfdaService The service to be assigned.
124         */
125        public void setCfdaService(CfdaService cfdaService) {
126            this.cfdaService = cfdaService;
127        }
128    
129        /**
130         * Set the {@link MailService}. For use by Spring.
131         * 
132         * @param mailService The service to be assigned.
133         */
134        public void setMailService(MailService mailService) {
135            this.mailService = mailService;
136        }
137        
138        /**
139         * Sets the {@link ParameterService}. For use by Spring.
140         * 
141         * @param parameterService The service to be assigned.
142         */
143        public void setParameterService(ParameterService parameterService) {
144            this.parameterService = parameterService;
145        }
146    
147        /**
148         * Gets the configurationService attribute. 
149         * @return Returns the configurationService.
150         */
151        public KualiConfigurationService getConfigurationService() {
152            return configurationService;
153        }
154    
155        /**
156         * Sets the configurationService attribute value.
157         * @param configurationService The configurationService to set.
158         */
159        public void setConfigurationService(KualiConfigurationService configurationService) {
160            this.configurationService = configurationService;
161        }
162    
163    }
164