1 12 13 16 package com.openedit.modules.userlog; 17 18 import java.io.Reader ; 19 import java.io.StringWriter ; 20 import java.text.DateFormat ; 21 import java.text.SimpleDateFormat ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.dom4j.Attribute; 31 import org.dom4j.Document; 32 import org.dom4j.DocumentHelper; 33 import org.dom4j.Element; 34 import org.dom4j.io.OutputFormat; 35 import org.dom4j.io.SAXReader; 36 import org.dom4j.io.XMLWriter; 37 import org.openedit.repository.filesystem.StringItem; 38 39 import com.openedit.OpenEditException; 40 import com.openedit.modules.changelog.RevisionItem; 41 import com.openedit.page.Page; 42 import com.openedit.page.manage.PageManager; 43 import com.openedit.users.User; 44 import com.openedit.users.UserListener; 45 import com.openedit.users.UserManager; 46 import com.openedit.util.PathUtilities; 47 import com.openedit.util.XmlUtil; 48 49 50 57 public class UserLog implements UserListener 58 { 59 protected Log log = LogFactory.getLog(UserLog.class); 60 protected DateFormat fieldFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 61 protected List fieldChangeStack; 62 protected List fieldNewChanges; 63 protected String fieldLogFilePath = "/openedit/stats/userlog.xml"; 64 protected int fieldMaxLength = 50; 65 protected PageManager fieldPageManager; 66 protected UserManager fieldUserManager; 67 protected int fieldTimeStampCounter; 68 69 74 public void setLogFilePath(String string) 75 { 76 fieldLogFilePath = string; 77 } 78 79 84 public String getLogFilePath() 85 { 86 return fieldLogFilePath; 87 } 88 89 94 public void setMaxLength(int i) 95 { 96 fieldMaxLength = i; 97 } 98 99 104 public int getMaxLength() 105 { 106 return fieldMaxLength; 107 } 108 109 114 public void setNewChanges(List inStack) 115 { 116 fieldNewChanges = inStack; 117 } 118 119 124 public List getNewChanges() 125 { 126 if (fieldNewChanges == null) 127 { 128 fieldNewChanges = new ArrayList (); 129 } 130 131 return fieldNewChanges; 132 } 133 134 139 public int changeCount() 140 { 141 return getChangeStack().size(); 142 } 143 144 149 public int changeNotificationCount() 150 { 151 return getNewChanges().size(); 152 } 153 154 157 public void clearNotifications() 158 { 159 getNewChanges().clear(); 160 } 161 162 168 public void init() throws Exception 169 { 170 setChangeStack(new ArrayList ()); 171 172 Reader reader = null; 173 try 174 { 175 Page page = getPageManager().getPage( getLogFilePath()); 176 177 if (page.exists()) 178 { 179 reader = page.getReader(); 180 Document doc = new SAXReader(false).read(reader ); 181 182 List changes = doc.getRootElement().elements("Change"); 184 185 for (int i = 0; (i < getMaxLength()) && (i < changes.size()); i++) 186 { 187 Element change = (Element) changes.get(i); 188 addChange(change); 189 } 190 Collections.reverse( getChangeStack() ); 192 193 Attribute counterAttribute = doc.getRootElement().attribute("timeStampCounter"); 195 if (counterAttribute != null) 196 { 197 String counter = counterAttribute.getValue(); 198 if (counter != null && counter.length() > 0) 199 { 200 setTimeStampCounter(Integer.parseInt(counter)); 201 } 202 } 203 else 204 { 205 setTimeStampCounter(0); 206 } 207 208 } 209 } 210 catch (Exception e) 211 { 212 throw new RuntimeException ("Could not load user log " + e); 213 } 214 finally 215 { 216 if ( reader != null) 217 { 218 reader.close(); 219 } 220 } 221 222 224 233 234 clearNotifications(); 235 236 getUserManager().addUserListener(this); 237 } 238 239 247 public void userAdded(User inUser) 248 { 249 Change change = new Change(); 250 change.put("type", "register"); 251 change.put("username", inUser.getUserName()); 252 change.put("date", getFormat().format(new Date ())); 253 change.put("fullname", inUser.getShortDescription()); 254 log(change); 255 save(); 256 } 257 258 public void userDeleted(User inUser) 259 { 260 Change change = new Change(); 261 change.put("type", "delete"); 262 change.put("username", inUser.getUserName()); 263 change.put("date", getFormat().format(new Date ())); 264 change.put("fullname", inUser.getShortDescription()); 265 log(change); 266 save(); 267 } 268 269 public void userLoggedOut(User inUser) 270 { 271 Change change = new Change(); 272 change.put("type", "logout"); 273 change.put("username", inUser.getUserName()); 274 change.put("date", getFormat().format(new Date ())); 275 change.put("fullname", inUser.getShortDescription()); 276 log(change); 277 save(); 278 } 279 280 public void userLoggedIn(User inUser) 281 { 282 Change change = new Change(); 283 change.put("type", "login"); 284 change.put("username", inUser.getUserName()); 285 change.put("date", getFormat().format(new Date ())); 286 change.put("fullname", inUser.getShortDescription()); 287 log(change); 288 save(); 289 } 290 291 296 public void resetNotificationsToDate(Date inDate) 297 { 298 List changes = new ArrayList (); 300 301 for (Iterator iter = getNewChanges().iterator(); iter.hasNext();) 302 { 303 RevisionItem rev = (RevisionItem) iter.next(); 304 305 if (rev.lastModified() != null && rev.lastModified().getTime() > inDate.getTime()) 306 { 307 changes.add(rev); 308 } 309 } 310 311 clearNotifications(); 312 getNewChanges().addAll(changes); 313 } 314 315 319 public void save() 320 { 321 incrementTimeStampCounter(); 322 try 323 { 324 Page page = getPageManager().getPage(getLogFilePath()); 325 StringWriter out = new StringWriter (); 326 327 new XmlUtil().saveXml(toXmlDocument(), out, page.getCharacterEncoding()); 328 329 StringItem item = new StringItem(getLogFilePath(),out.toString(),page.getCharacterEncoding()); 330 item.setMakeVersion(false); 331 page.setContentItem(item); 332 getPageManager().putPage(page); 333 334 335 if (getTimeStampCounter() >= getMaxLength()) 336 { 337 setTimeStampCounter(0); 338 String stampedFileName = "userlog-" + new SimpleDateFormat ("yyyy-MM-dd-H-m-s").format(new Date ()) 339 + ".xml"; 340 String stampedPath = PathUtilities.extractDirectoryPath(getLogFilePath()) 341 + "/" + stampedFileName; 342 Page stampedPage = getPageManager().getPage(stampedPath); 343 stampedPage.getContentItem().setMakeVersion(false); 344 getPageManager().copyPage(page, stampedPage); 345 346 347 new XmlUtil().saveXml(toXmlDocument(), out, page.getCharacterEncoding()); 348 349 item = new StringItem(getLogFilePath(),out.toString(),page.getCharacterEncoding()); 350 item.setMakeVersion(false); 351 page.setContentItem(item); 352 getPageManager().putPage(page); 353 354 } 355 } 356 catch (Exception ex) 357 { 358 log.error(ex); 359 return; 360 } 361 } 362 363 368 public Document toNotifyXmlDocument() 369 { 370 return buildDocument(getNewChanges()); 372 } 373 374 379 public String toNotifyXmlString() throws Exception 380 { 381 StringWriter out = new StringWriter (); 382 new XMLWriter(out, OutputFormat.createPrettyPrint()).write( 383 buildDocument(getNewChanges()).getRootElement()); 384 385 return out.toString(); 386 } 387 388 393 public Document toXmlDocument() 394 { 395 return buildDocument(getChangeStack()); 396 } 397 398 403 protected void setChangeStack(List stack) 404 { 405 fieldChangeStack = stack; 406 } 407 408 413 public List getChangeStack() 414 { 415 return fieldChangeStack; 416 } 417 418 423 protected void setFormat(DateFormat inFormat) 424 { 425 fieldFormat = inFormat; 426 } 427 428 433 protected DateFormat getFormat() 434 { 435 return fieldFormat; 436 } 437 438 public String formatDate(Change inChange) 440 { 441 return SimpleDateFormat.getDateTimeInstance().format(inChange.get("date")); 442 } 443 450 protected void addChange(Element inElement) throws Exception 451 { 452 Change change = new Change(); 453 String typeAttr = inElement.attributeValue("type"); 454 if (typeAttr != null) 455 { 456 change.put("type", typeAttr.toString()); 457 } 458 459 for (Iterator iter = inElement.elementIterator(); iter.hasNext();) 460 { 461 Element element = (Element) iter.next(); 462 change.put(element.getName(), element.getText()); 463 464 } 465 log(change); 466 } 467 468 protected Document buildDocument(List inStack) 469 { 470 Element root = DocumentHelper.createElement("UserLog"); 471 root.addAttribute("timeStampCounter", "" + getTimeStampCounter()); 472 473 for (Iterator iter = inStack.iterator(); iter.hasNext();) 474 { 475 Change change = (Change) iter.next(); 476 Element changeElem = root.addElement("Change"); 477 478 for (Iterator iterator = change.keys().iterator(); iterator.hasNext();) 479 { 480 String key = (String ) iterator.next(); 481 if (key.equals("type")) 482 { 483 changeElem.addAttribute(key, change.get(key)); 484 } 485 else 486 { 487 Element propElem = changeElem.addElement(key); 488 propElem.setText(change.get(key)); 489 } 490 } 491 } 492 493 return DocumentHelper.createDocument(root); 494 } 495 496 501 protected void log(Change inChange) 502 { 503 504 if ( getChangeStack().size() > 0) 505 { 506 getChangeStack().add(0,inChange); 507 } 508 else 509 { 510 getChangeStack().add(inChange); 511 } 512 513 if (getChangeStack().size() > getMaxLength()) 514 { 515 getChangeStack().remove(getChangeStack().size() - 1); 516 } 517 if ( getNewChanges().size() > 0) 518 { 519 getNewChanges().add(0,inChange); 520 } 521 else 522 { 523 getNewChanges().add(inChange); 524 } 525 } 526 public PageManager getPageManager() 527 { 528 return fieldPageManager; 529 } 530 public void setPageManager(PageManager inPageManager) 531 { 532 fieldPageManager = inPageManager; 533 } 534 535 public int getTimeStampCounter() 536 { 537 return fieldTimeStampCounter; 538 } 539 540 public void setTimeStampCounter(int inTimeStampCounter) 541 { 542 fieldTimeStampCounter = inTimeStampCounter; 543 } 544 545 public void incrementTimeStampCounter() 546 { 547 setTimeStampCounter(getTimeStampCounter() + 1); 548 } 549 550 public UserManager getUserManager() 551 { 552 return fieldUserManager; 553 } 554 555 public void setUserManager(UserManager inUserManager) 556 { 557 fieldUserManager = inUserManager; 558 } 559 } 560 | Popular Tags |