|
From: Michael K. <ko...@us...> - 2006-02-16 15:21:17
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/message In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8500/org/cobricks/message Modified Files: ChannelEmail.java ChannelMailbox.java MessageManager.java MessageManagerImpl.java Subscription.java properties.txt Added Files: ChannelNewsletter.java Log Message: --- NEW FILE: ChannelNewsletter.java --- /* * Copyright (c) 2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.message; import java.io.*; import java.util.*; import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.SchedulerTask; import org.cobricks.core.util.LogUtil; import org.cobricks.user.User; import org.cobricks.user.UserManager; /** * Message channel storing the message for use in a newsletter. * * @author mic...@ac... * @version $Date: 2006/02/16 15:21:08 $ */ public class ChannelNewsletter implements MessageChannel, SchedulerTask { static Logger logger = Logger.getLogger(ChannelNewsletter.class); private String name; private String description; private CoreManager coreManager; private MessageManager messageManager; private String smtphost; private String fromemail; public ChannelNewsletter() { } // for MessageChannel public void init(String name, CoreManager coreManager) { this.name = name; this.description = "Newsletter"; this.coreManager = coreManager; messageManager = (MessageManager) coreManager.getComponentDirectory().getManager("messageManager"); } // for SchedulerTask public void init(CoreManager coreManager) { this.coreManager = coreManager; messageManager = (MessageManager) coreManager.getComponentDirectory().getManager("messageManager"); } public String getName() { return name; } public String getDescription() { return description; } public boolean sendMessage(Message message, int recipientid) { logger.info("store message for newsletter for user "+recipientid); messageManager.addMessageToNewsletter(recipientid, name, message); return true; } /** * Method for the SchedulerTask interface - is called regularly * and handles sending the newsletters */ public void doIt(String[] args) { String nlname = "newsletterd"; for (int i=0; i<args.length; i++) { String tmps = args[i]; if (tmps.startsWith("nlname=")) nlname = tmps.substring(7); } // load properties this.smtphost = coreManager.getProperty("message.email.smtphost"); if (smtphost == null || smtphost.trim().length()<1) { logger.warn("no SMTP host defined"); } this.fromemail = coreManager.getProperty("message.email.defaultfrom"); // check if there are entries in the newsletter table that match .. List msgs = messageManager.getNewsletterMessages(nlname); //TBD messageManager.removeNewsletterMessages(nlname); // ... and send mails Integer userid = null; ListIterator i = msgs.listIterator(); StringBuffer msgcontent = new StringBuffer(""); while (i.hasNext()) { Map m = (Map)i.next(); Integer useridtmp = (Integer)m.get("userid"); if (!useridtmp.equals(userid)) { if (userid != null) { sendEmail(userid.intValue(), msgcontent.toString()); msgcontent = new StringBuffer(""); } userid = useridtmp; } } if (userid != null) { sendEmail(userid.intValue(), msgcontent.toString()); } } public void sendEmail(int recipientId, String msgcontent) { if (smtphost == null) return; if (msgcontent == null || msgcontent.length()<1) return; logger.debug("sending newsletter mail to user "+recipientId); try { UserManager userManager = (UserManager) coreManager.getComponentDirectory().getManager("userManager"); User user = userManager.getUser(recipientId); if (user == null) return; String emailto = user.getAttributeAsString(User.EMAIL); // from address String emailfrom = this.fromemail; // subject String subject = "Newsletter"; // TBD // and now send email logger.info("sending email to "+emailto); //MailUtil. //sendMail(smtphost, emailto, emailfrom, subject, msgcontent); } catch (Exception e) { logger.error("failed sending email: "+e.toString()); } } } Index: ChannelEmail.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/ChannelEmail.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- ChannelEmail.java 10 Aug 2005 13:35:34 -0000 1.4 +++ ChannelEmail.java 16 Feb 2006 15:21:08 -0000 1.5 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -15,7 +15,7 @@ import java.io.*; import java.util.*; -import org.apache.log4j.*; +import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.util.LogUtil; @@ -46,10 +46,12 @@ private String fromemail; - public ChannelEmail() { + public ChannelEmail() + { } - public void init(String name, CoreManager coreManager) { + public void init(String name, CoreManager coreManager) + { this.name = name; this.description = "EMail"; this.coreManager = coreManager; @@ -62,16 +64,22 @@ this.fromemail = coreManager.getProperty("message.email.defaultfrom"); } - public String getName() { + public String getName() + { return name; } - public String getDescription() { + public String getDescription() + { return description; } - public boolean sendMessage(Message message, int recipientId) { + /** + * + */ + public boolean sendMessage(Message message, int recipientId) + { if (smtphost == null) return false; try { UserManager userManager = (UserManager) @@ -106,8 +114,7 @@ } catch (Exception e) { logger.error("failed sending email: "+e.toString()); } - return true; - + return true; } } Index: Subscription.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/Subscription.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Subscription.java 7 Dec 2005 14:01:11 -0000 1.2 +++ Subscription.java 16 Feb 2006 15:21:08 -0000 1.3 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004 Cobricks Group. All rights reserved. + * Copyright (c) 2004-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software Index: ChannelMailbox.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/ChannelMailbox.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ChannelMailbox.java 23 Feb 2004 08:20:59 -0000 1.2 +++ ChannelMailbox.java 16 Feb 2006 15:21:08 -0000 1.3 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2004 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -15,7 +15,7 @@ import java.io.*; import java.util.*; -import org.apache.log4j.*; +import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.core.util.LogUtil; @@ -27,7 +27,7 @@ * Message channel using the internal mailbox. * * @author mic...@ac... - * @version $ $ + * @version $Date$ */ public class ChannelMailbox Index: properties.txt =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/properties.txt,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- properties.txt 26 Jul 2005 10:56:45 -0000 1.3 +++ properties.txt 16 Feb 2006 15:21:08 -0000 1.4 @@ -20,10 +20,18 @@ # # message delivery channels -message.channels=email,mailbox,event +message.channels=email,mailbox,event,newsletterd,newsletterw message.channel.email.class=org.cobricks.message.ChannelEmail message.channel.mailbox.class=org.cobricks.message.ChannelMailbox message.channel.event.class=org.cobricks.message.ChannelEvent +message.channel.newsletterd.class=org.cobricks.message.ChannelNewsletter +message.channel.newsletterw.class=org.cobricks.message.ChannelNewsletter +scheduler.task.newsletterd.time=cron:30,0,*,*,* +scheduler.task.newsletterd.class=org.cobricks.message.ChannelNewsletter +scheduler.task.newsletterd.args=nlname=newsletterd +scheduler.task.newsletterw.time=cron:40,0,*,*,0 +scheduler.task.newsletterw.class=org.cobricks.message.ChannelNewsletter +scheduler.task.newsletterw.args=nlname=newsletterw # # email channel Index: MessageManager.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/MessageManager.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- MessageManager.java 7 Dec 2005 14:01:11 -0000 1.6 +++ MessageManager.java 16 Feb 2006 15:21:08 -0000 1.7 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -36,6 +36,12 @@ public List getMailboxMessages(int userid); public void addMessageToMailbox(int userid, Message message); public void deleteMessageFromMailbox(int userid, int msgid); + public void addMessageToNewsletter(int userid, String nlname, + Message message); + public List getNewsletterMessages(String nlname); + public List getNewsletterMessages(int userid, String nlname); + public void removeNewsletterMessages(String nlname); + public void removeNewsletterMessages(int userid, String nlname); public List getSubscriptions(int userid); public Set getSubscriptions(String domain, String action, int userid, Index: MessageManagerImpl.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/message/MessageManagerImpl.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- MessageManagerImpl.java 20 Dec 2005 18:09:24 -0000 1.8 +++ MessageManagerImpl.java 16 Feb 2006 15:21:08 -0000 1.9 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. + * Copyright (c) 2003-2006 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software @@ -12,6 +12,15 @@ package org.cobricks.message; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -24,6 +33,8 @@ import java.util.Set; import org.apache.log4j.Logger; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; import org.cobricks.core.ComponentDirectory; import org.cobricks.core.ComponentEvent; @@ -33,6 +44,8 @@ import org.cobricks.core.db.DBAccess; import org.cobricks.core.db.DBAccessImpl; import org.cobricks.core.util.LogUtil; +import org.cobricks.core.util.ResourceUtil; +import org.cobricks.portal.PortalManager; import org.cobricks.user.User; import org.cobricks.user.UserManager; @@ -50,7 +63,7 @@ static Logger logger = Logger.getLogger(MessageManagerImpl.class); static String COMP_NAME = "Cobricks-2 Message Manager"; - static String COMP_VERSION = "V0.2 (Feb. 2004)"; + static String COMP_VERSION = "V0.3 (Feb. 2006)"; ChannelDirectory channelDirectory; MessageQueue messageQueue; @@ -307,6 +320,73 @@ /** + * + */ + public void addMessageToNewsletter(int userid, String nlname, + Message message) + { + // insert message in database + Map attrs = new HashMap(); + attrs.put("userid", new Integer(userid)); + attrs.put("subject", message.getSubject()); + attrs.put("nlname", nlname); + attrs.put("content", message.getContent()); + attrs.put("senderid", new Integer(message.getSenderId())); + attrs.put("sender", message.getSenderDescription()); + attrs.put("senddate", message.getSendDate()); + if (message.getExpirationDate()!=null) + attrs.put("expirationdate", message.getExpirationDate()); + int msgid = dbAccess.sqlInsert("msg_newsletter", attrs); + } + + + /** + * Return the newsletter messages for a particular newsletter + */ + public List getNewsletterMessages(String nlname) + { + String sql = "select * from msg_newsletter where nlname='" + +nlname+"' order by userid, senddate"; + return dbAccess.sqlQuery(sql); + } + + /** + * Return the newsletter messages for a particular user + * and a particular newsletter + */ + public List getNewsletterMessages(int userid, String nlname) + { + String sql = "select * from msg_newsletter where userid = " + +Integer.toString(userid)+" and nlname='" + +nlname+"' order by senddate"; + return dbAccess.sqlQuery(sql); + } + + + /** + * Remove the newsletter messages for a particular newsletter + */ + public void removeNewsletterMessages(String nlname) + { + String sql = "delete from msg_newsletter where nlname='" + +nlname+"'"; + dbAccess.sqlExecute(sql); + } + + /** + * Remove the newsletter messages for a particular user + * and a particular newsletter + */ + public void removeNewsletterMessages(int userid, String nlname) + { + String sql = "delete from msg_newsletter where userid = " + +Integer.toString(userid)+" and nlname='" + +nlname+"'"; + dbAccess.sqlExecute(sql); + } + + + /** * Return the subscriptions of a particular user. */ public List getSubscriptions(int userid) @@ -432,6 +512,64 @@ } + /** + * Return the template content for a given template name (as + * used in subscriptions). + */ + public String getTemplate(String tname) + { + /* the template name is either an URL, then check there, + or a relative path to the template directory in + the jar file (static) or in the WEB-INF/conf/templates + directory */ + try { + InputStream is = null; + + // check if it is an URL + if (tname.indexOf(":")>0) { + URL url = new URL(tname); + is = url.openStream(); + } + else { + // check in config dir + String configdir = coreManager.getProperty("configdir"); + File file = new File(configdir+File.separator + +"org.cobricks.message"+File.separator + +"templates"+File.separator+tname); + if (file.exists()) + is = new FileInputStream(file); + if (is == null) { + // check if the file is in the component resources + is = ResourceUtil. + getComponentResourceAsStream("org.cobricks.message", + "templates/"+tname); + } + } + + if (is == null) { + logger.info("failed loading message template "+tname); + return null; + } + + StringBuffer sb = new StringBuffer(""); + BufferedReader in = + new BufferedReader(new InputStreamReader(is)); + while (true) { + String tmps = in.readLine(); + if (tmps == null) break; + sb.append(tmps); + sb.append("\n"); + } + return (sb.toString()); + + } catch (Exception e) { + logger.warn(LogUtil.ex("Failed loading template "+tname, e)); + } + + return null; + } + + /* ************************************************************ */ /** @@ -455,8 +593,12 @@ class ComponentEventThread extends Thread { ComponentEvent event; + VelocityContext velocityContext; ComponentEventThread(ComponentEvent event) { this.event = event; + PortalManager portalManager = (PortalManager)coreManager. + getComponentDirectory().getManager("portalManager"); + velocityContext = portalManager.getVelocityRootContext(); } public void run() { Set subscriptions = @@ -479,22 +621,42 @@ new ConditionProcessor(coreManager); cond = conditionProcessor. check(scondition, userid, event.getUserId(), - event.getObjectId(), event.getObjectType()); + event.getObjectId(), event.getObjectType(), + event.getContent()); } catch (Exception e) { logger.warn(LogUtil.ex("Failed processing condition: " +scondition, e)); } if (!cond) continue; + logger.debug("condition "+scondition+" is true ..."); // otherwise the condition is true and we can continue - } + } // now create a message and deliver it to the user Message message = new Message(); message.setSenderId(event.getUserId()); message.setSendDate(new Date()); message.setSubject("notification "+event.getDomain() +"/"+event.getAction()); - // tbd: format event with template ... - message.setContent(event.toString()); + + String templateName = subscr.getTemplateName(); + String templ = getTemplate(templateName); + if (templ == null) continue; + + // parse template as Velocity template + Writer sw = new StringWriter(); + try { + Object o = event.getContent(); + if (o!=null) + velocityContext.put("obj", o); + Velocity.evaluate(velocityContext, sw, + "message template", templ); + } catch (Exception e) { + logger.warn(LogUtil.ex("failed parsing template", e)); + } + templ = sw.toString(); + + message.setContent(templ); + // deliver message to channel String channelname = subscr.getChannelName(); if (channelname.startsWith("PRIORITY=")) { |