001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.cg.batch;
017:
018: import java.io.IOException;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.log4j.Logger;
023: import org.kuali.core.bo.user.AuthenticationUserId;
024: import org.kuali.core.bo.user.KualiGroup;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.exceptions.GroupNotFoundException;
027: import org.kuali.core.exceptions.UserNotFoundException;
028: import org.kuali.core.mail.InvalidAddressException;
029: import org.kuali.core.mail.MailMessage;
030: import org.kuali.core.service.KualiGroupService;
031: import org.kuali.core.service.MailService;
032: import org.kuali.core.service.UniversalUserService;
033: import org.kuali.kfs.batch.AbstractStep;
034: import org.kuali.module.cg.service.CfdaService;
035: import org.kuali.module.cg.service.CfdaUpdateResults;
036:
037: /**
038: * 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
039: * Kuali. Codes set to be managed automatically are reconciled with what's on the web page. Codes managed manually are left alone.
040: * Finally an email containing a summary of what was done by the step execution is sent to the member of the KUALI_CGCFDA workgroup.
041: */
042: public class CfdaBatchStep extends AbstractStep {
043:
044: private static Logger LOG = org.apache.log4j.Logger
045: .getLogger(CfdaBatchStep.class);
046: private static String MAIL_RECIPIENTS_GROUP_NAME = "KUALI_CGCFDA";
047:
048: private CfdaService cfdaService;
049: private MailService mailService;
050: private KualiGroupService kualiGroupService;
051: private UniversalUserService universalUserService;
052:
053: /**
054: * See the class description.
055: *
056: * @see org.kuali.kfs.batch.Step#execute()
057: */
058: public boolean execute(String jobName) throws InterruptedException {
059: MailMessage message = new MailMessage();
060:
061: try {
062: CfdaUpdateResults results = cfdaService.update();
063:
064: KualiGroup workgroup = kualiGroupService
065: .getByGroupName(MAIL_RECIPIENTS_GROUP_NAME);
066: List<String> memberNetworkIds = workgroup.getGroupUsers();
067: for (String id : memberNetworkIds) {
068: try {
069: AuthenticationUserId authId = new AuthenticationUserId(
070: id.toUpperCase());
071: UniversalUser user = universalUserService
072: .getUniversalUser(authId);
073: String address = user.getPersonEmailAddress();
074: if (!StringUtils.isEmpty(address)) {
075: message.addToAddress(address);
076: }
077: } catch (UserNotFoundException unfe) {
078: LOG.info("User " + id + " doesn't exist.", unfe);
079: }
080: }
081:
082: // TODO this message should come from some config file.
083: StringBuilder builder = new StringBuilder();
084: builder.append("The CFDA batch script is complete.\n");
085: builder.append(" - ");
086: builder
087: .append(results
088: .getNumberOfRecordsDeactivatedBecauseNoLongerOnWebSite());
089: builder
090: .append(" records were deactivated because they are no longer on the web site.\n");
091: builder.append(" - ");
092: builder.append(results.getNumberOfRecordsInKfsDatabase());
093: builder.append(" records were in the KFS database.\n");
094: builder.append(" - ");
095: builder.append(results
096: .getNumberOfRecordsNewlyAddedFromWebSite());
097: builder
098: .append(" records were newly added from the web site.\n");
099: builder.append(" - ");
100: builder.append(results
101: .getNumberOfRecordsNotUpdatedBecauseManual());
102: builder
103: .append(" records were not updated because they are manual.\n");
104: builder.append(" - ");
105: builder.append(results.getNumberOfRecordsReActivated());
106: builder.append(" records were re-activated.\n");
107: builder.append(" - ");
108: builder.append(results
109: .getNumberOfRecordsRetrievedFromWebSite());
110: builder
111: .append(" records were retrieved from the web site.\n");
112: builder.append(" - ");
113: builder.append(results
114: .getNumberOfRecordsUpdatedBecauseAutomatic());
115: builder
116: .append(" records were updated because they are automatic.\n");
117: builder.append(" - ");
118: builder
119: .append(results
120: .getNumberOfRecrodsNotUpdatedForHistoricalPurposes());
121: builder
122: .append(" records were not updated for historical reasons.\n");
123: builder.append(" - Message\n");
124: builder.append(null != results.getMessage() ? results
125: .getMessage() : "");
126:
127: message.setMessage(builder.toString());
128: mailService.sendMessage(message);
129:
130: } catch (IOException ioe) {
131: LOG.warn("Exception while updating CFDA codes.", ioe);
132: return false;
133: } catch (GroupNotFoundException gnfe) {
134: LOG.fatal(
135: "Couldn't find workgroup to send notification to.",
136: gnfe);
137: return true;
138: } catch (InvalidAddressException iae) {
139: LOG.warn(
140: "The email address for one or more of the members of the "
141: + MAIL_RECIPIENTS_GROUP_NAME
142: + " workgroup is invalid.", iae);
143: return true;
144: }
145: return true;
146: }
147:
148: /**
149: * Sets the {@link CfdaService}. For use by Spring.
150: *
151: * @param cfdaService The service to be assigned.
152: */
153: public void setCfdaService(CfdaService cfdaService) {
154: this .cfdaService = cfdaService;
155: }
156:
157: /**
158: * Set the {@link MailService}. For use by Spring.
159: *
160: * @param mailService The service to be assigned.
161: */
162: public void setMailService(MailService mailService) {
163: this .mailService = mailService;
164: }
165:
166: /**
167: * Sets the {@link KualiGroupService}. For use by Spring.
168: *
169: * @param kualiGroupService The service to be assigned.
170: */
171: public void setKualiGroupService(KualiGroupService kualiGroupService) {
172: this .kualiGroupService = kualiGroupService;
173: }
174:
175: /**
176: * Sets the {@link UniversalUserService}. For use by Spring.
177: *
178: * @param universalUserService The service to be assigned.
179: */
180: public void setUniversalUserService(
181: UniversalUserService universalUserService) {
182: this.universalUserService = universalUserService;
183: }
184:
185: }
|