001: package org.enhydra.shark.eventaudit;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.Properties;
006:
007: import javax.mail.Authenticator;
008: import javax.mail.Message;
009: import javax.mail.PasswordAuthentication;
010: import javax.mail.Session;
011: import javax.mail.Transport;
012: import javax.mail.internet.InternetAddress;
013: import javax.mail.internet.MimeMessage;
014:
015: import org.enhydra.shark.Shark;
016: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
017: import org.enhydra.shark.api.common.SharkConstants;
018: import org.enhydra.shark.api.internal.eventaudit.AssignmentEventAuditPersistenceObject;
019: import org.enhydra.shark.api.internal.eventaudit.CreateProcessEventAuditPersistenceObject;
020: import org.enhydra.shark.api.internal.eventaudit.DataEventAuditPersistenceObject;
021: import org.enhydra.shark.api.internal.eventaudit.EventAuditException;
022: import org.enhydra.shark.api.internal.eventaudit.EventAuditManagerInterface;
023: import org.enhydra.shark.api.internal.eventaudit.StateEventAuditPersistenceObject;
024: import org.enhydra.shark.api.internal.instancepersistence.PersistenceException;
025: import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
026: import org.enhydra.shark.api.internal.working.CallbackUtilities;
027: import org.enhydra.shark.utilities.MiscUtilities;
028:
029: /**
030: * This persistent manager send an email to every person that has to accept a new task
031: * DODSPersistentManager is the default persistent manager of Enhydra-Shark.
032: * <p>
033: * In addition to original functionality (coded against beta 2 by Mathias), there is now
034: * new configuration procedure, and separation from other components - persistence manager
035: * and tool agents.
036: *
037: * @version 1.1
038: * @author Mathias Holst
039: * @since 9. August 2004
040: */
041: public class SMTPEventAuditManager implements
042: EventAuditManagerInterface {
043:
044: private boolean DEBUG;
045:
046: private boolean enable_email;
047:
048: private CallbackUtilities cus;
049:
050: private static final String _PARAM_PREFIX = "SMTPEventAuditManager";
051:
052: private static final String _PARAM_DEBUG = ".Debug";
053:
054: private static final String _PARAM_ENABLE = ".Enable";
055:
056: private static final String _PARAM_SERVER_ADDR = ".Server";
057:
058: private static final String _PARAM_SERVER_PORT = ".Port";
059:
060: private static final String _PARAM_AUTH = ".Auth";
061:
062: protected static final String _PARAM_PASSWD = ".Passwd";
063:
064: private static final String _PARAM_SOURCE = ".Source";
065:
066: private static final String _PARAM_MESSAGE_TEMPLATE = ".Message";
067:
068: private static final String _PARAM_SUBJECT_TEMPLATE = ".Subject";
069:
070: private String _subject;
071:
072: private String _message;
073:
074: public void configure(CallbackUtilities cut) throws Exception {
075: DEBUG = Boolean.valueOf(
076: cut.getProperty(_PARAM_PREFIX + _PARAM_DEBUG, "false"))
077: .booleanValue();
078: enable_email = Boolean
079: .valueOf(
080: cut.getProperty(_PARAM_PREFIX + _PARAM_ENABLE,
081: "false")).booleanValue();
082: _subject = cut.getProperty(_PARAM_PREFIX
083: + _PARAM_SUBJECT_TEMPLATE, "New task: {activity}");
084: _message = cut.getProperty(_PARAM_PREFIX
085: + _PARAM_MESSAGE_TEMPLATE,
086: "Dear {person},\n\nyou have a new task!\n\n"
087: + "name: {activity}\n"
088: + "workflow: {process}\n"
089: + "workflow id: {definition}");
090: this .cus = cut;
091: if (DEBUG)
092: System.err.println(_PARAM_PREFIX + " configured");
093: }
094:
095: /**
096: * Description of the Method
097: *
098: * @param text_msg Description of the Parameter
099: * @param subject Description of the Parameter
100: * @param destination_address Description of the Parameter
101: * @throws Exception
102: */
103: private void sendEmail(String text_msg, String subject,
104: String destination_address) throws Exception {
105: // Get system properties
106: try {
107: // FIXME: can we create this props object only once
108: final Properties props = new Properties();
109: // Setup mail server
110: props.put("mail.smtp.host", cus.getProperty(_PARAM_PREFIX
111: + _PARAM_SERVER_ADDR));
112: props.put("mail.smtp.port", cus.getProperty(_PARAM_PREFIX
113: + _PARAM_SERVER_PORT));
114: // User name
115: final String auth = cus.getProperty(_PARAM_PREFIX
116: + _PARAM_AUTH);
117: javax.mail.Session session;
118: if (auth.equals("")) {
119: session = Session.getInstance(props);
120: } else {
121: props.put("mail.smtp.user", auth);
122: props.put("mail.smtp.auth", "true");
123: // Get session
124: session = Session.getInstance(props,
125: new Authenticator() {
126: public PasswordAuthentication getPasswordAuthentication() {
127: return new PasswordAuthentication(auth,
128: cus.getProperty(_PARAM_PREFIX
129: + _PARAM_PASSWD));
130: }
131: });
132:
133: }
134: // Define message
135: final MimeMessage message = new MimeMessage(session);
136: message.setFrom(new InternetAddress(cus
137: .getProperty(_PARAM_PREFIX + _PARAM_SOURCE)));
138: message.addRecipient(Message.RecipientType.TO,
139: new InternetAddress(destination_address));
140: message.setSubject(subject);
141: message.setContent(text_msg, "text/plain");
142: // Send message
143: Transport.send(message);
144: } catch (Exception e) {
145: if (DEBUG) {
146: e.printStackTrace();
147: }
148: throw e;
149: }
150: }
151:
152: /**
153: * Description of the Method
154: *
155: * @param assea Description of the Parameter
156: * @param shandle
157: * @exception PersistenceException Description of the Exception
158: */
159: public void persist(WMSessionHandle shandle,
160: AssignmentEventAuditPersistenceObject assea)
161: throws EventAuditException {
162: // senhd email to person that has to accpet new task
163: if (enable_email) {
164: try {
165: // get user's login name
166: final String login = assea.getNewResourceUsername();
167: // get user's real name
168:
169: UserGroupManager admin = (UserGroupManager) Shark
170: .getInstance().getPlugIn(
171: SharkConstants.PLUGIN_USER_GROUP);
172:
173: final String person = admin.getUserRealName(shandle,
174: login);
175: // get user's email-address
176: final String email = admin.getUserEMailAddress(shandle,
177: login);
178: // define subject of email
179: final String subject = parse(_subject, person, assea
180: .getActivityName(), assea.getProcessName(),
181: assea.getProcessDefinitionId());
182: final String msg = parse(_message, person, assea
183: .getActivityName(), assea.getProcessName(),
184: assea.getProcessDefinitionId());
185: // finally send email
186: sendEmail(msg, subject, email);
187: } catch (Exception e) {
188: System.err.println(e);
189: if (DEBUG) {
190: e.printStackTrace();
191: }
192: }
193: }
194: }
195:
196: private String parse(String template, String person,
197: String activity, String process, String definition) {
198: String ret = template;
199: if (-1 != template.indexOf("{person}")) {
200: ret = MiscUtilities.replaceAll(ret, "{person}", person);
201: }
202: if (-1 != template.indexOf("{activity}")) {
203: ret = MiscUtilities.replaceAll(ret, "{activity}", activity);
204: }
205: if (-1 != template.indexOf("{process}")) {
206: ret = MiscUtilities.replaceAll(ret, "{process}", process);
207: }
208: if (-1 != template.indexOf("{definition}")) {
209: ret = MiscUtilities.replaceAll(ret, "{definition}",
210: definition);
211: }
212: return ret;
213: }
214:
215: public void persist(WMSessionHandle shandle,
216: CreateProcessEventAuditPersistenceObject cpea)
217: throws EventAuditException {
218: }
219:
220: public void persist(WMSessionHandle shandle,
221: DataEventAuditPersistenceObject dea)
222: throws EventAuditException {
223: }
224:
225: public void persist(WMSessionHandle shandle,
226: StateEventAuditPersistenceObject sea)
227: throws EventAuditException {
228: }
229:
230: public boolean restore(WMSessionHandle shandle,
231: AssignmentEventAuditPersistenceObject assea)
232: throws EventAuditException {
233: return false;
234: }
235:
236: public boolean restore(WMSessionHandle shandle,
237: CreateProcessEventAuditPersistenceObject cpea)
238: throws EventAuditException {
239: return false;
240: }
241:
242: public boolean restore(WMSessionHandle shandle,
243: DataEventAuditPersistenceObject dea)
244: throws EventAuditException {
245: return false;
246: }
247:
248: public boolean restore(WMSessionHandle shandle,
249: StateEventAuditPersistenceObject sea)
250: throws EventAuditException {
251: return false;
252: }
253:
254: public List restoreProcessHistory(WMSessionHandle shandle,
255: String procId) throws EventAuditException {
256: return new ArrayList();
257: }
258:
259: public List restoreActivityHistory(WMSessionHandle shandle,
260: String procId, String actId) throws EventAuditException {
261: return new ArrayList();
262: }
263:
264: public void delete(WMSessionHandle shandle,
265: AssignmentEventAuditPersistenceObject assea)
266: throws EventAuditException {
267: }
268:
269: public void delete(WMSessionHandle shandle,
270: CreateProcessEventAuditPersistenceObject cpea)
271: throws EventAuditException {
272: }
273:
274: public void delete(WMSessionHandle shandle,
275: DataEventAuditPersistenceObject dea)
276: throws EventAuditException {
277: }
278:
279: public void delete(WMSessionHandle shandle,
280: StateEventAuditPersistenceObject sea)
281: throws EventAuditException {
282: }
283: }
|