1 64 65 70 package com.jcorporate.expresso.services.dbobj; 71 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.dbobj.RequestContext; 74 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 75 import com.jcorporate.expresso.core.logging.LogException; 76 import com.jcorporate.expresso.core.misc.ConfigManager; 77 import com.jcorporate.expresso.core.misc.ConfigurationException; 78 import com.jcorporate.expresso.core.misc.EMailSender; 79 import com.jcorporate.expresso.core.misc.StringUtil; 80 import com.jcorporate.expresso.core.security.User; 81 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 82 import org.apache.log4j.Logger; 83 84 import java.util.Enumeration ; 85 import java.util.Iterator ; 86 import java.util.Vector ; 87 88 89 94 public class Event 95 extends SecuredDBObject { 96 private static Logger log = Logger.getLogger(Event.class); 97 98 103 public static final String SYSTEM_ERROR = "SYSERROR"; 104 105 108 public Event() 109 throws DBException { 110 super(); 111 } 112 113 117 public Event(int uid) 118 throws DBException { 119 super(uid); 120 } 121 122 129 public Event(RequestContext request) 130 throws DBException { 131 super(request); 132 } 133 134 146 public Event(String dbName, String theEvent, String theMessage, 147 boolean success) 148 throws DBException, LogException { 149 this(SecuredDBObject.SYSTEM_ACCOUNT); 150 setDataContext(dbName); 151 setField("Event", theEvent); 152 153 154 try { 155 retrieve(); 156 } catch (DBException de) { 157 158 159 if (!theEvent.equalsIgnoreCase(SYSTEM_ERROR)) { 160 new Event(dbName, SYSTEM_ERROR, 161 "Could not find event '" + theEvent + "'", false); 162 throw new DBException("No such event '" + theEvent + "'"); 163 } 164 } 165 166 Vector mailMessage = new Vector (1); 167 mailMessage.addElement(theMessage); 168 sendMail(mailMessage, success); 169 } 170 171 175 public Vector getValues() 176 throws DBException { 177 return getValuesDefault("Event", "Descrip"); 178 } 179 180 181 190 public void sendMail(Vector theMessage, boolean success) 191 throws DBException, LogException { 192 EventMail myEventMails = new EventMail(SecuredDBObject.SYSTEM_ACCOUNT); 193 myEventMails.setDataContext(getDataContext()); 194 195 EventMail oneEventMail = null; 196 User oneUser = new User(); 197 oneUser.setDataContext(getDataContext()); 198 199 FastStringBuffer bigString = new FastStringBuffer(256); 200 String oneRecipient = (""); 201 202 for (Enumeration e = theMessage.elements(); e.hasMoreElements();) { 203 bigString.append((String ) e.nextElement()); 204 bigString.append("\n"); 205 } 206 207 bigString.append("\nFrom Server:"); 208 bigString.append(Setup.getValueRequired(getDataContext(), "HTTPServ")); 209 bigString.append(", Database/context:"); 210 bigString.append(getDataContext()); 211 212 String dbDescrip = ""; 213 214 try { 215 dbDescrip = StringUtil.notNull(ConfigManager.getContext(getDataContext()).getDescription()); 216 } catch (ConfigurationException ce) { 217 throw new DBException(ce); 218 } 219 if (!dbDescrip.equals("")) { 220 bigString.append(" ("); 221 bigString.append(dbDescrip); 222 bigString.append(")"); 223 } 224 225 myEventMails.setField("Event", this.getField("Event")); 226 227 if (success) { 228 myEventMails.setField("Success", "Y"); 229 } else { 230 myEventMails.setField("Success", "N"); 231 } 232 233 int theCount = 0; 234 235 for (Iterator em = myEventMails.searchAndRetrieveList().iterator(); 236 em.hasNext();) { 237 oneEventMail = (EventMail) em.next(); 238 theCount++; 239 oneUser.clear(); 240 oneUser.setUid(oneEventMail.getField("ExpUid")); 241 242 if (!oneUser.find()) { 243 log.error("Attempting to send event notification " + 244 "for event '" + getField("Event") + "' to " + 245 "user " + oneEventMail.getField("ExpUid") + 246 ", but this user not found in the '" + getDataContext() + 247 "' context."); 248 continue; 249 } 250 251 oneRecipient = oneUser.getEmail(); 252 253 if (log.isInfoEnabled()) { 254 log.info("Notifying " + oneRecipient + ":" + 255 this.getField("Descrip")); 256 } 257 258 try { 259 String subject = null; 260 261 if (success) { 262 subject = (this.getField("Descrip")); 263 } else { 264 subject = ("ERROR:" + getField("Descrip")); 265 } 266 267 EMailSender ems = new EMailSender(); 268 ems.setDBName(getDataContext()); 269 ems.send(oneRecipient, subject, bigString.toString()); 270 } catch (Exception e) { 271 throw new DBException("Error sending e-mail", e); 272 } 273 } 274 275 276 if (log.isInfoEnabled()) { 277 log.info("Sent " + theCount + " e-mail notifications"); 278 } 279 280 if (theCount == 0) { 281 String successFlag = "N"; 282 283 if (success) { 284 successFlag = "Y"; 285 } 286 287 log.warn("No users in db/context '" + getDataContext() + "' were set " + 288 "to receive notice of event '" + getField("Event") + 289 "' with " + " success flag '" + successFlag + "', so " + 290 "no notices were sent."); 291 } 292 } 293 294 295 298 protected synchronized void setupFields() 299 throws DBException { 300 setTargetTable("EVENT"); 301 setDescription("DBevent"); 302 setCharset("ISO-8859-1"); 303 addField("Event", "char", 30, false, "eventCode"); 304 addField("Descrip", "varchar", 80, false, "eventDescrip"); 305 setStringFilter("Event", "stripFilter"); 306 setStringFilter("Descrip", "standardFilter"); 307 addKey("Event"); 308 addDetail("com.jcorporate.expresso.services.dbobj.EventMail", "Event", 309 "Event"); 310 } 311 312 313 316 public synchronized void populateDefaultValues() 317 throws DBException { 318 if (log.isInfoEnabled()) { 319 log.info("Populating default values for Events in db '" + getDataContext() + 320 "'"); 321 } 322 323 Event oneEvent = new Event(); 324 oneEvent.setDataContext(getDataContext()); 325 oneEvent.setField("Event", SYSTEM_ERROR); 326 327 if (!oneEvent.find()) { 328 oneEvent.setField("Descrip", "System Error"); 329 oneEvent.add(); 330 if (log.isInfoEnabled()) { 331 log.info("Added 'SYSERROR' event"); 332 } 333 } else { 334 if (log.isInfoEnabled()) { 335 log.info("'SYSERROR' event already set up"); 336 } 337 } 338 339 oneEvent.clear(); 340 oneEvent.setField("Event", "HEALTH"); 341 342 if (!oneEvent.find()) { 343 oneEvent.setField("Descrip", "System Health Check"); 344 oneEvent.add(); 345 if (log.isInfoEnabled()) { 346 log.info("Added 'HEALTH' event"); 347 } 348 } else { 349 if (log.isInfoEnabled()) { 350 log.info("'HEALTH' event already set up"); 351 } 352 } 353 354 oneEvent.clear(); 355 oneEvent.setField("Event", "DOWNLOAD"); 356 357 if (!oneEvent.find()) { 358 oneEvent.setField("Descrip", "User Download Activity"); 359 oneEvent.add(); 360 if (log.isInfoEnabled()) { 361 log.info("Added 'DOWNLOAD' event"); 362 } 363 } else { 364 if (log.isInfoEnabled()) { 365 log.info("'DOWNLOAD' event already set up"); 366 } 367 } 368 369 oneEvent.clear(); 370 oneEvent.setField("Event", "REGISTER"); 371 372 if (!oneEvent.find()) { 373 oneEvent.setField("Descrip", "User Registration Activity"); 374 oneEvent.add(); 375 if (log.isInfoEnabled()) { 376 log.info("Added 'REGISTER' event"); 377 } 378 } else { 379 if (log.isInfoEnabled()) { 380 log.info("'REGISTER' event already set up"); 381 } 382 } 383 } 384 385 386 } 387 | Popular Tags |