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.util.List;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.apache.log4j.Logger;
022: import org.kuali.core.bo.user.AuthenticationUserId;
023: import org.kuali.core.bo.user.KualiGroup;
024: import org.kuali.core.bo.user.UniversalUser;
025: import org.kuali.core.exceptions.GroupNotFoundException;
026: import org.kuali.core.exceptions.UserNotFoundException;
027: import org.kuali.core.mail.InvalidAddressException;
028: import org.kuali.core.mail.MailMessage;
029: import org.kuali.core.service.KualiGroupService;
030: import org.kuali.core.service.MailService;
031: import org.kuali.core.service.UniversalUserService;
032: import org.kuali.kfs.batch.AbstractStep;
033: import org.kuali.module.cg.service.CloseService;
034:
035: /**
036: * @see CloseService#close()
037: */
038: public class CloseBatchStep extends AbstractStep {
039:
040: private static Logger LOG = org.apache.log4j.Logger
041: .getLogger(CloseBatchStep.class);
042: private static String MAIL_RECIPIENTS_GROUP_NAME = "KUALI_CGCFDA";
043: private static String STATUS_SUBJECT = "Close Batch Step: ";
044:
045: private CloseService closeService;
046: private MailService mailService;
047: private KualiGroupService kualiGroupService;
048: private UniversalUserService universalUserService;
049:
050: /**
051: * See the class description.
052: *
053: * @see org.kuali.kfs.batch.Step#execute()
054: */
055: public boolean execute(String jobName) {
056:
057: MailMessage message = new MailMessage();
058:
059: try {
060:
061: // TODO this message should come from some config file.
062: StringBuilder builder = new StringBuilder();
063:
064: try {
065: closeService.close();
066: message.setSubject(STATUS_SUBJECT + "SUCCEEDED");
067: builder
068: .append("The Close batch script ran successfully.\n");
069: } catch (Exception e) {
070: message.setSubject(STATUS_SUBJECT + "FAILED");
071: builder
072: .append("An Exception was encountered when running the close job.\n");
073: builder.append("The exception message is: ").append(
074: e.getMessage()).append("\n");
075: builder.append(
076: "The message from the exception cause is: ")
077: .append(e.getCause().getMessage()).append("\n");
078: builder
079: .append("Please see the log for more details.\n");
080:
081: LOG
082: .error(
083: "The following exception was encountered during the close batch process.",
084: e);
085: }
086:
087: KualiGroup workgroup = kualiGroupService
088: .getByGroupName(MAIL_RECIPIENTS_GROUP_NAME);
089: List<String> memberNetworkIds = workgroup.getGroupUsers();
090: for (String id : memberNetworkIds) {
091: try {
092: AuthenticationUserId authId = new AuthenticationUserId(
093: id.toUpperCase());
094: UniversalUser user = universalUserService
095: .getUniversalUser(authId);
096: String address = user.getPersonEmailAddress();
097: if (null != address
098: && !StringUtils.isEmpty(address)) {
099: message.addToAddress(address);
100: }
101: } catch (UserNotFoundException unfe) {
102: LOG.info("User " + id + " doesn't exist.", unfe);
103: }
104: }
105:
106: // Don't send it if no recipients were specified.
107: if (0 != message.getToAddresses().size()) {
108: message.setMessage(builder.toString());
109: String from = mailService.getBatchMailingList();
110: if (null != from) {
111: message.setFromAddress(from);
112: }
113: mailService.sendMessage(message);
114: }
115:
116: } catch (GroupNotFoundException gnfe) {
117: LOG.fatal(
118: "Couldn't find workgroup to send notification to.",
119: gnfe);
120: return true;
121: } catch (InvalidAddressException iae) {
122: LOG.warn(
123: "The email address for one or more of the members of the "
124: + MAIL_RECIPIENTS_GROUP_NAME
125: + " workgroup is invalid.", iae);
126: return true;
127: }
128:
129: return true;
130: }
131:
132: /**
133: * Sets the {@link CloseService}. For use by Spring.
134: *
135: * @param closeService The value to be used to assign to the local attribute <code>closeService</code>.
136: */
137: public void setCloseService(
138: org.kuali.module.cg.service.CloseService closeService) {
139: this .closeService = closeService;
140: }
141:
142: /**
143: * Sets the {@link KualiGroupService}. For use by Spring.
144: *
145: * @param kualiGroupService The service to be assigned.
146: */
147: public void setKualiGroupService(KualiGroupService kualiGroupService) {
148: this .kualiGroupService = kualiGroupService;
149: }
150:
151: /**
152: * Set the {@link MailService}. For use by Spring.
153: *
154: * @param mailService The service to be assigned.
155: */
156: public void setMailService(MailService mailService) {
157: this .mailService = mailService;
158: }
159:
160: /**
161: * Sets the {@link UniversalUserService}. For use by Spring.
162: *
163: * @param universalUserService The service to be assigned.
164: */
165: public void setUniversalUserService(
166: UniversalUserService universalUserService) {
167: this.universalUserService = universalUserService;
168: }
169:
170: }
|