KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > caches > MessageCache


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Interface for an item with an ID
28  */

29 package net.killingar.forum.internal.caches;
30
31 import net.killingar.forum.internal.AccessDeniedException;
32 import net.killingar.forum.internal.IDItem;
33 import net.killingar.forum.internal.Message;
34 import net.killingar.forum.internal.ParentIDItemTreeNode;
35 import net.killingar.forum.internal.managers.AreaManager;
36
37 import java.sql.*;
38 import java.util.ArrayList JavaDoc;
39
40 public class MessageCache extends AbstractIDItemCache
41 {
42     protected ParentIDItemTreeNode messages = null;
43     protected AreaManager areamgr = null;
44     protected long areaID;
45
46     public MessageCache(long inAreaID, AreaManager inAreamgr)
47     {
48         areaID = inAreaID;
49         areamgr = inAreamgr;
50     }
51
52     public synchronized ParentIDItemTreeNode getMessagesTree() throws SQLException, AccessDeniedException
53     {
54         if (messages == null)
55         {
56             // get all
57
latestUpdate = new Timestamp(System.currentTimeMillis()-1001);
58             messages = net.killingar.forum.actions.Utils.buildTree(areamgr.getMessages(areaID, true), objects); // build tree and HashMap
59
}
60         else
61             update();
62
63         return messages;
64     }
65
66     public synchronized void add(IDItem inO)
67     {
68         add((Message)inO);
69     }
70
71     public synchronized void add(Message inMessage)
72     {
73         ParentIDItemTreeNode node = messages;
74
75         int indent = 0;
76
77         if (inMessage.getParentID() != 0)
78         {
79             node = (ParentIDItemTreeNode)objects.get(inMessage.getParentID());
80
81             if (node == null)
82                 throw new NullPointerException JavaDoc(inMessage.toString()+" add failed, message id "+inMessage.getParentID()+" not found");
83
84       indent = node.indent+1;
85         }
86
87         ParentIDItemTreeNode newNode = new ParentIDItemTreeNode(inMessage, indent);
88
89         if (node.subnodes == null)
90             node.subnodes = new ArrayList JavaDoc();
91         else
92         {
93             // if this message already exists, replace it
94
for (int i = 0; i != node.subnodes.size(); i++)
95             {
96                 ParentIDItemTreeNode oldNode = (ParentIDItemTreeNode)node.subnodes.get(i);
97                 if (oldNode.item.getId() == inMessage.getId())
98                 {
99                     //i.remove();
100
node.subnodes.set(i, newNode);
101                     objects.put(inMessage.getId(), newNode);
102                     return;
103                 }
104             }
105         }
106
107         // not found, add it
108
node.subnodes.add(newNode);
109         objects.put(inMessage.getId(), newNode);
110     }
111
112     public synchronized IDItem get(Long JavaDoc id) throws SQLException
113     {
114         throw new RuntimeException JavaDoc("this method is not valid");
115     }
116
117     public synchronized ParentIDItemTreeNode getParentIDItemTreeNode(long id) throws SQLException
118     {
119         update();
120
121         return (ParentIDItemTreeNode)objects.get(id);
122     }
123
124     protected synchronized IDItem getObjectFromRow(ResultSet result) throws SQLException
125     {
126         return areamgr.getMessageFromRow(result);
127     }
128
129     protected PreparedStatement createStatement(Connection c) throws SQLException
130     {
131         PreparedStatement statement = c.prepareStatement("select ID, Parent, Area, User, Timecreated, LastChanged, LastChangedUser, Subject, Body, Words, Visible from Messages where Area = ? AND LastChanged > ? order by ID");
132
133         statement.setLong(1, areaID);
134         statement.setTimestamp(2, latestUpdate);
135
136         return statement;
137     }
138 }
139
Popular Tags