KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > userlog > UserLog


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 /*
14  * Created on Apr 18, 2003
15  */

16 package com.openedit.modules.userlog;
17
18 import java.io.Reader JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
51  * update the user log each time something changes there should probably a MAX length for the
52  * user log. This would be configured TODO: We should consider using
53  * org.apache.log4j.xml.XMLLayout for performance reasons
54  *
55  * @author cburkey
56  */

57 public class UserLog implements UserListener
58 {
59     protected Log log = LogFactory.getLog(UserLog.class);
60     protected DateFormat JavaDoc fieldFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
61     protected List JavaDoc fieldChangeStack;
62     protected List JavaDoc fieldNewChanges;
63     protected String JavaDoc fieldLogFilePath = "/openedit/stats/userlog.xml";
64     protected int fieldMaxLength = 50;
65     protected PageManager fieldPageManager;
66     protected UserManager fieldUserManager;
67     protected int fieldTimeStampCounter;
68     
69     /**
70      * DOCUMENT ME!
71      *
72      * @param string
73      */

74     public void setLogFilePath(String JavaDoc string)
75     {
76         fieldLogFilePath = string;
77     }
78
79     /**
80      * DOCUMENT ME!
81      *
82      * @return
83      */

84     public String JavaDoc getLogFilePath()
85     {
86         return fieldLogFilePath;
87     }
88
89     /**
90      * DOCUMENT ME!
91      *
92      * @param i
93      */

94     public void setMaxLength(int i)
95     {
96         fieldMaxLength = i;
97     }
98
99     /**
100      * DOCUMENT ME!
101      *
102      * @return
103      */

104     public int getMaxLength()
105     {
106         return fieldMaxLength;
107     }
108
109     /**
110      * DOCUMENT ME!
111      *
112      * @param inStack
113      */

114     public void setNewChanges(List JavaDoc inStack)
115     {
116         fieldNewChanges = inStack;
117     }
118
119     /**
120      * DOCUMENT ME!
121      *
122      * @return
123      */

124     public List JavaDoc getNewChanges()
125     {
126         if (fieldNewChanges == null)
127         {
128             fieldNewChanges = new ArrayList JavaDoc();
129         }
130
131         return fieldNewChanges;
132     }
133
134     /**
135      * DOCUMENT ME!
136      *
137      * @return
138      */

139     public int changeCount()
140     {
141         return getChangeStack().size();
142     }
143
144     /**
145      * DOCUMENT ME!
146      *
147      * @return
148      */

149     public int changeNotificationCount()
150     {
151         return getNewChanges().size();
152     }
153
154     /**
155      * DOCME
156      */

157     public void clearNotifications()
158     {
159         getNewChanges().clear();
160     }
161
162     /**
163      * DOCUMENT ME!
164      *
165      * @throws Exception DOCUMENT ME!
166      * @throws RuntimeException DOCUMENT ME!
167      */

168     public void init() throws Exception JavaDoc
169     {
170         setChangeStack(new ArrayList JavaDoc());
171
172         Reader JavaDoc 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                 //newst ones come first
183
List JavaDoc 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                 //Put the first elements back in the top of the list
191
Collections.reverse( getChangeStack() );
192                 
193 // save the time stamp counter
194
Attribute counterAttribute = doc.getRootElement().attribute("timeStampCounter");
195                 if (counterAttribute != null)
196                 {
197                     String JavaDoc 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 JavaDoc e)
211         {
212             throw new RuntimeException JavaDoc("Could not load user log " + e);
213         }
214         finally
215         {
216             if ( reader != null)
217             {
218                 reader.close();
219             }
220         }
221         
222         //TODO: Save the last notified time someplace
223

224         //this is good after a reboot to get us back where we where
225
/*
226         GregorianCalendar yesterday = new GregorianCalendar();
227         yesterday.set(GregorianCalendar.HOUR_OF_DAY, 24);
228         yesterday.set(GregorianCalendar.MINUTE, 0);
229         yesterday.set(GregorianCalendar.SECOND, 0);
230         yesterday.add(GregorianCalendar.DAY_OF_YEAR, -1);
231         getChangeLog().resetNotificationsToDate(yesterday.getTime());
232         */

233
234         clearNotifications();
235
236         getUserManager().addUserListener(this);
237     }
238     
239     /**
240      * DOCUMENT ME!
241      *
242      * @param inPage
243      * @param inRevision
244      *
245      * @throws OpenEditException
246      */

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 JavaDoc()));
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 JavaDoc()));
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 JavaDoc()));
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 JavaDoc()));
286         change.put("fullname", inUser.getShortDescription());
287         log(change);
288         save();
289     }
290
291     /**
292      * Only keep notifications that are after a certain date
293      *
294      * @param inDate
295      */

296     public void resetNotificationsToDate(Date JavaDoc inDate)
297     {
298         // TODO Auto-generated method stub
299
List JavaDoc changes = new ArrayList JavaDoc();
300
301         for (Iterator JavaDoc 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     /**
316      * Save the list of changes to the days change log file
317      *
318      */

319     public void save()
320     {
321         incrementTimeStampCounter();
322         try
323         {
324             Page page = getPageManager().getPage(getLogFilePath());
325             StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc stampedFileName = "userlog-" + new SimpleDateFormat JavaDoc("yyyy-MM-dd-H-m-s").format(new Date JavaDoc())
339                                             + ".xml";
340                 String JavaDoc 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 JavaDoc ex)
357         {
358             log.error(ex);
359             return;
360         }
361     }
362
363     /**
364      * DOCUMENT ME!
365      *
366      * @return
367      */

368     public Document toNotifyXmlDocument()
369     {
370         // TODO Auto-generated method stub
371
return buildDocument(getNewChanges());
372     }
373
374     /**
375      * Outputs the changes as an XML element, in UTF-8 encoding.
376      *
377      * TODO: This should move to using a generator
378      */

379     public String JavaDoc toNotifyXmlString() throws Exception JavaDoc
380     {
381         StringWriter JavaDoc out = new StringWriter JavaDoc();
382         new XMLWriter(out, OutputFormat.createPrettyPrint()).write(
383             buildDocument(getNewChanges()).getRootElement());
384
385         return out.toString();
386     }
387
388     /**
389      * DOCUMENT ME!
390      *
391      * @return
392      */

393     public Document toXmlDocument()
394     {
395         return buildDocument(getChangeStack());
396     }
397
398     /**
399      * DOCUMENT ME!
400      *
401      * @param stack
402      */

403     protected void setChangeStack(List JavaDoc stack)
404     {
405         fieldChangeStack = stack;
406     }
407
408     /**
409      * DOCUMENT ME!
410      *
411      * @return
412      */

413     public List JavaDoc getChangeStack()
414     {
415         return fieldChangeStack;
416     }
417
418     /**
419      * DOCUMENT ME!
420      *
421      * @param inFormat
422      */

423     protected void setFormat(DateFormat JavaDoc inFormat)
424     {
425         fieldFormat = inFormat;
426     }
427
428     /**
429      * DOCUMENT ME!
430      *
431      * @return
432      */

433     protected DateFormat JavaDoc getFormat()
434     {
435         return fieldFormat;
436     }
437     
438     //doesn't work
439
public String JavaDoc formatDate(Change inChange)
440     {
441         return SimpleDateFormat.getDateTimeInstance().format(inChange.get("date"));
442     }
443     /**
444      * DOCUMENT ME!
445      *
446      * @param inChange
447      *
448      * @throws Exception DOCUMENT ME!
449      */

450     protected void addChange(Element inElement) throws Exception JavaDoc
451     {
452         Change change = new Change();
453         String JavaDoc typeAttr = inElement.attributeValue("type");
454         if (typeAttr != null)
455         {
456             change.put("type", typeAttr.toString());
457         }
458         
459         for (Iterator JavaDoc 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 JavaDoc inStack)
469     {
470         Element root = DocumentHelper.createElement("UserLog");
471         root.addAttribute("timeStampCounter", "" + getTimeStampCounter());
472
473         for (Iterator JavaDoc iter = inStack.iterator(); iter.hasNext();)
474         {
475             Change change = (Change) iter.next();
476             Element changeElem = root.addElement("Change");
477             
478             for (Iterator JavaDoc iterator = change.keys().iterator(); iterator.hasNext();)
479             {
480                 String JavaDoc key = (String JavaDoc) 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     /**
497      * DOCUMENT ME!
498      *
499      * @param revision
500      */

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