1 10 package org.mmbase.applications.email; 11 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import java.util.Locale ; 15 import java.util.MissingResourceException ; 16 import java.util.ResourceBundle ; 17 18 import org.mmbase.module.Module; 19 import org.mmbase.module.core.*; 20 21 import org.mmbase.storage.search.*; 22 import org.mmbase.storage.search.implementation.*; 23 24 import org.mmbase.util.functions.Functions; 25 import org.mmbase.util.functions.Parameter; 26 import org.mmbase.util.functions.Parameters; 27 import org.mmbase.util.logging.Logger; 28 import org.mmbase.util.logging.Logging; 29 30 43 public class EmailBuilder extends MMObjectBuilder { 44 45 private static final Logger log = Logging.getLoggerInstance(EmailBuilder.class); 46 47 public final static Parameter[] MAIL_PARAMETERS = { 48 new Parameter("type", String .class) 49 }; 50 51 52 public final static Parameter[] STARTMAIL_PARAMETERS = MAIL_PARAMETERS; 53 public final static Parameter[] SETTYPE_PARAMETERS = MAIL_PARAMETERS; 54 55 public final static int STATE_UNKNOWN = -1; public final static int STATE_WAITING = 0; public final static int STATE_DELIVERED = 1; public final static int STATE_FAILED = 2; public final static int STATE_SPAMGARDE = 3; public final static int STATE_QUEUED = 4; 63 64 public final static int TYPE_ONESHOT = 1; public final static int TYPE_ONESHOTKEEP = 3; 69 public final static String EMAILTYPE_RESOURCE = "org.mmbase.applications.email.resources.mailtype"; 70 public final static String EMAILSTATUS_RESOURCE = "org.mmbase.applications.email.resources.mailstatus"; 71 72 static String usersBuilder; 73 static String usersEmailField; 74 static String groupsBuilder; 75 76 private static SendMailInterface sendmail; 78 79 private static EmailExpireHandler expirehandler; 81 82 protected int expireTime = 60; 83 protected int sleepTime = 60*30; 84 85 88 public boolean init() { 89 super.init (); 90 91 sendmail = (SendMailInterface) Module.getModule("sendmail"); 93 94 String property = getInitParameter("expireTime"); 95 if (property != null) { 96 try { 97 expireTime = Integer.parseInt(property); 98 } catch(NumberFormatException nfe) { 99 log.warn("property: expireTime contained an invalid integer value:'" + property +"'(" + nfe + ")"); 100 } 101 } 102 103 property = getInitParameter("sleepTime"); 104 if (property != null) { 105 try { 106 sleepTime = Integer.parseInt(property); 107 } catch(NumberFormatException nfe) { 108 log.warn("property: sleepTime contained an invalid integer value:'" + property +"'(" + nfe + ")"); 109 } 110 } 111 112 if (sleepTime > 0 && expireTime >0) { 113 log.service("Expirehandler started with sleep time " + sleepTime + "sec, expire time " + expireTime + "sec."); 117 expirehandler = new EmailExpireHandler(this, sleepTime, expireTime); 118 } else { 119 log.service("Expirehandler not started"); 120 } 121 122 usersBuilder = getInitParameter("users-builder"); 123 if (usersBuilder == null) usersBuilder = "users"; 124 125 usersEmailField = getInitParameter("users-email-field"); 126 if (usersEmailField == null) usersEmailField = "email"; 127 128 groupsBuilder = getInitParameter("groups-builder"); 129 if (groupsBuilder == null) groupsBuilder = "groups"; 130 131 return true; 132 } 133 134 141 public String getLocaleGUIIndicator(Locale locale, String field, MMObjectNode node) { 142 if (field.equals("mailstatus")) { 143 String val = node.getStringValue("mailstatus"); 144 log.debug("val: " + val); ResourceBundle bundle; 146 bundle = ResourceBundle.getBundle(EMAILTYPE_RESOURCE, locale, getClass().getClassLoader() ); 147 try { 148 return bundle.getString(val); 149 } catch (MissingResourceException e) { 150 return val; 151 } 152 } else if (field.equals("mailtype")){ String val = node.getStringValue("mailtype"); 154 return getMailtypeResource(val,locale); 155 } else { 156 return super.getLocaleGUIIndicator(locale,field,node); 157 } 158 } 159 160 166 protected Object executeFunction(MMObjectNode node, String function, List args) { 167 if (log.isDebugEnabled()) { 168 log.debug("function: " + function); 169 } 170 if (function.equals("info")) { 171 List empty = new ArrayList (); 172 java.util.Map info = (java.util.Map ) super.executeFunction(node, function, empty); 173 info.put("gui", "(mailtype or mailstatus) Gui representation of this object."); 174 if (args == null || args.size() == 0) { 175 return info; 176 } else { 177 return info.get(args.get(0)); 178 } 179 } else if (function.equals("setType") || function.equals("settype") ) { 180 setType(node, args); 181 return null; 182 } else if (function.equals("mail")) { log.debug("We're in mail - args: " + args); 184 setType(node, args); 185 186 int mailType = node.getIntValue("mailtype"); 188 switch(mailType) { 189 case TYPE_ONESHOT : 190 case TYPE_ONESHOTKEEP : 192 EmailHandler.sendMailNode(node); 193 break; 194 default: 196 log.warn("Trying to mail a node with unsupported type " + mailType); 197 } 198 199 String val = node.getStringValue("mailtype"); 200 return null; 201 } else if (function.equals("startmail")) { log.debug("We are in startmail - args: " + args); 203 setType(node, args); 205 206 int mailType = node.getIntValue("mailtype"); 208 log.debug("mailtype: " + mailType); 209 switch(mailType) { 210 case TYPE_ONESHOT : 211 case TYPE_ONESHOTKEEP : 213 new EmailBackgroundHandler(node); 214 break; 215 default: 217 log.warn("Trying to start a mail of a node with unsupported type " + mailType); 218 } 219 String val = node.getStringValue("mailtype"); 220 return null; 221 } 222 if (log.isDebugEnabled()) { 223 log.debug("Function '" + function + "' is not found in email app."); 224 } 225 return super.executeFunction(node, function, args); 226 } 227 228 231 static SendMailInterface getSendMail() { 232 return sendmail; 233 } 234 235 241 private static void setType(MMObjectNode node, List args) { 242 String type = (String ) args.get(0); 243 if ("oneshot".equals(type)) { 244 node.setValue("mailtype", TYPE_ONESHOT); 245 log.debug("Setting mailtype to: " + TYPE_ONESHOT); 246 } else if ("oneshotkeep".equals(type)) { 247 node.setValue("mailtype", TYPE_ONESHOTKEEP); 248 log.debug("Setting mailtype to " + TYPE_ONESHOTKEEP); 249 } else { 250 node.setValue("mailtype", TYPE_ONESHOT); 251 log.debug("Setting mailtype to: " + TYPE_ONESHOT); 252 } 253 } 254 255 263 private String getMailtypeResource(String val, Locale locale) { 264 ResourceBundle bundle = ResourceBundle.getBundle(EMAILTYPE_RESOURCE, locale, getClass().getClassLoader()); 265 try { 266 return bundle.getString(val); 267 } catch (MissingResourceException e) { 268 return val; 269 } 270 } 271 272 273 279 List getDeliveredMailOlderThan(long expireAge) { 280 long age = (System.currentTimeMillis() / 1000) - expireAge; 282 284 NodeSearchQuery query = new NodeSearchQuery(this); 285 BasicCompositeConstraint cons = new BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND); 286 287 cons.addChild(new BasicFieldValueConstraint(query.getField(getField("mailstatus")), new Integer (STATE_DELIVERED))); 288 cons.addChild(new BasicFieldValueConstraint(query.getField(getField("mailtype")), new Integer (TYPE_ONESHOT))); 289 cons.addChild(new BasicFieldValueConstraint(query.getField(getField("mailedtime")), new Long (age)).setOperator(FieldCompareConstraint.LESS)); 290 query.setConstraint(cons); 291 try { 292 return getNodes(query); 293 } catch (SearchQueryException sqe) { 294 log.error(sqe.getMessage()); 295 return new ArrayList (); 296 } 297 298 } 299 } 300 | Popular Tags |