1 25 26 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 ; 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 latestUpdate = new Timestamp(System.currentTimeMillis()-1001); 58 messages = net.killingar.forum.actions.Utils.buildTree(areamgr.getMessages(areaID, true), objects); } 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 (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 (); 91 else 92 { 93 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 node.subnodes.set(i, newNode); 101 objects.put(inMessage.getId(), newNode); 102 return; 103 } 104 } 105 } 106 107 node.subnodes.add(newNode); 109 objects.put(inMessage.getId(), newNode); 110 } 111 112 public synchronized IDItem get(Long id) throws SQLException 113 { 114 throw new RuntimeException ("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 |