1 25 26 package net.killingar.forum.internal.managers; 27 28 import it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap; 29 import net.killingar.forum.internal.Area; 30 import net.killingar.forum.internal.HotlistEntry; 31 import net.killingar.forum.internal.IDItemImpl; 32 33 import java.sql.Connection ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.sql.Statement ; 37 import java.util.ArrayList ; 38 39 public final class AreaHotlistManager extends AbstractManager implements java.io.Serializable 40 { 41 public static final long systemActive = 0L; 42 public static final long systemPassive = 1L; 43 private transient Object lock = new Object (); 44 private Long2ObjectAVLTreeMap flatHotlistCache; 45 private HotlistEntry hotlistCache[]; 46 private long lastUpdated = 0; 47 48 public AreaHotlistManager() 49 { 50 flatHotlistCache = new Long2ObjectAVLTreeMap(); 51 hotlistCache = null; 52 System.err.println("AreaHotlistManager created"); 53 } 54 55 public final void actionPerformed(String s) 56 { 57 if (s.equals("logout")) 58 { 59 synchronized (lock) 60 { 61 flatHotlistCache.clear(); 62 hotlistCache = null; 63 } 64 } 65 } 66 67 70 public final void addArea(long areaID, long system) 71 throws SQLException 72 { 73 synchronized (lock) 74 { 75 if (hasArea(areaID, system)) 76 { 77 System.err.println("AreaHotlistManager addArea() trivial rejection"); 78 return; 79 } 80 81 Connection c = null; 82 Statement statement = null; 83 ResultSet result = null; 84 85 try 86 { 87 c = getNewConnection(); 88 statement = c.createStatement(); 89 result = statement.executeQuery("select count(Areas.ID) from AreaHotlist, Areas where System = " + system + " AND (Areas.AreaGroup = AreaHotlist.AreaGroup or Areas.ID = AreaHotlist.Area) AND AreaHotlist.User = " + super.manager.getUserID() + " AND Areas.ID = " + areaID); 90 if (result.next()) 91 { 92 if (result.getInt(1) == 1) 93 { 94 result.close(); 95 return; 96 } 97 98 if (result.getInt(1) > 1) 99 statement.executeUpdate("delete from AreaHotlist where System = " + system + " AND Area = " + areaID + " AND AreaGroup is null AND User = " + super.manager.getUserID()); 100 else 101 statement.executeUpdate("insert into AreaHotlist (User, Area, AreaGroup, System) values (" + super.manager.getUserID() + ", " + areaID + ", null, " + system + ")"); 102 } 103 104 flatHotlistCache.put(system, null); 105 hotlistCache = null; 106 } 107 finally { closeAll(c, statement, result); } 108 } 109 } 110 111 114 public final void addAreaGroup(long areaGroupID, long system) 115 throws SQLException 116 { 117 synchronized (lock) 118 { 119 if (hasAreaGroup(areaGroupID, system)) 120 { 121 return; 122 } 123 124 Connection c = null; 125 Statement statement = null; 126 ResultSet result = null; 127 128 try 129 { 130 c = getNewConnection(); 131 statement = c.createStatement(); 132 statement.executeUpdate("insert into AreaHotlist (User, Area, AreaGroup, System) values (" + super.manager.getUserID() + ", null, " + areaGroupID + ", " + system + ")"); 133 flatHotlistCache.put(system, null); 134 hotlistCache = null; 135 } 136 finally { closeAll(c, statement, result); } 137 } 138 } 139 140 143 public final void removeArea(long areaID, long system) 144 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException 145 { 146 synchronized (lock) 147 { 148 if (!hasArea(areaID, system)) 149 { 150 return; 151 } 152 153 Connection c = null; 154 Statement statement = null; 155 ResultSet result = null; 156 157 try 158 { 159 c = getNewConnection(); 160 statement = c.createStatement(); 161 AreaManager areaMgr = (AreaManager)manager.getManager(AreaManager.class.getName()); 162 if (hasAreaGroup(areaMgr.getArea(areaID).getAreaGroupID(), system)) 163 statement.executeUpdate("insert into AreaHotlist (User, Area, AreaGroup, System) values (" + super.manager.getUserID() + ", " + areaID + ", null, " + system + ")"); 164 else 165 statement.executeUpdate("delete from AreaHotlist where System = " + system + " AND Area = " + areaID + " AND AreaGroup is null AND User = " + super.manager.getUserID()); 166 flatHotlistCache.put(system, null); 167 hotlistCache = null; 168 } 169 finally { closeAll(c, statement, result); } 170 } 171 } 172 173 176 public final void removeAreaGroup(long areaGroupID, long system) 177 throws Exception 178 { 179 synchronized (lock) 180 { 181 if (!hasAreaGroup(areaGroupID, system)) 182 { 183 return; 184 } 185 186 Connection c = null; 187 Statement statement = null; 188 ResultSet result = null; 189 190 try 191 { 192 c = getNewConnection(); 193 statement = c.createStatement(); 194 195 statement.executeUpdate("delete from AreaHotlist where System = " + system + " AND AreaGroup = " + areaGroupID + " AND Area is null AND User = " + super.manager.getUserID()); 196 flatHotlistCache.put(system, null); 197 hotlistCache = null; 198 } 199 finally { closeAll(c, statement, result); } 200 } 201 } 202 203 206 public final boolean hasAreaGroup(long areaGroupID, long system) 207 throws SQLException 208 { 209 HotlistEntry ahotlistentry[] = getHotlist(); 210 for (int i = 0; i < ahotlistentry.length; i++) 211 if (ahotlistentry[i].system == system && ahotlistentry[i].areaGroupID == areaGroupID) 212 return true; 213 return false; 214 } 215 216 219 public final boolean hasArea(long areaID, long system) 220 throws SQLException 221 { 222 Area aarea[] = getFlatHotlist(system); 223 for (int i = 0; i < aarea.length; i++) 224 if (((IDItemImpl)(aarea[i])).ID == areaID) 225 return true; 226 return false; 227 } 228 229 public final boolean getHasArea(long areaID) 230 throws SQLException 231 { 232 return hasArea(areaID, systemPassive) || hasArea(areaID, systemActive); 233 } 234 235 238 public final HotlistEntry[] getHotlist() 239 throws SQLException 240 { 241 synchronized (lock) 242 { 243 if (System.currentTimeMillis()-lastUpdated > 1000*60) hotlistCache = null; 245 246 if (hotlistCache != null) 247 { 248 return hotlistCache; 249 } 250 else 251 { 252 Connection c = null; 253 Statement statement = null; 254 ResultSet result = null; 255 256 try 257 { 258 lastUpdated = System.currentTimeMillis(); 259 260 c = getNewConnection(); 261 statement = c.createStatement(); 262 result = statement.executeQuery("select Area, AreaGroup, System from AreaHotlist where User = " + super.manager.getUserID()); 263 ArrayList arraylist = new ArrayList (); 264 while (result.next()) 265 arraylist.add(new HotlistEntry(result.getLong(1), result.getLong(2), result.getLong(3))); 266 hotlistCache = new HotlistEntry[arraylist.size()]; 267 arraylist.toArray(hotlistCache); 268 result.close(); 269 return hotlistCache; 270 } 271 finally { closeAll(c, statement, result); } 272 } 273 } 274 } 275 276 279 public final Area[] getFlatHotlist(long system) 280 throws SQLException 281 { 282 synchronized (lock) 283 { 284 if (flatHotlistCache.containsKey(system)) 285 { 286 Area areas[] = (Area[])flatHotlistCache.get(system); 287 if (areas != null) 288 return areas; 289 } 290 291 Connection c = null; 292 Statement statement = null; 293 ResultSet result = null; 294 295 try 296 { 297 c = getNewConnection(); 298 statement = c.createStatement(); 299 result = statement.executeQuery("select Areas.ID, Areas.AreaGroup, Areas.Name, Areas.Mode, Areas.Description from AreaHotlist, Areas where System = " + system + " AND (Areas.AreaGroup = AreaHotlist.AreaGroup or Areas.ID = AreaHotlist.Area) AND AreaHotlist.User = " + super.manager.getUserID() + " order by Areas.AreaGroup, Areas.ID"); 300 ArrayList arraylist = new ArrayList (); 301 Area lastArea = null; 302 while (result.next()) 303 { 304 if (lastArea != null && lastArea.ID == result.getLong(1)) 305 { 306 arraylist.remove(arraylist.size()-1); 307 continue; 308 } 309 310 lastArea = new Area( 311 result.getLong(1), 312 result.getLong(2), 313 result.getString(3), 314 result.getString(4), 315 result.getString(5)); 316 317 arraylist.add(lastArea); 318 } 319 320 result.close(); 321 Area areas[] = new Area[arraylist.size()]; 322 arraylist.toArray(areas); 323 flatHotlistCache.put(system, areas); 324 return areas; 325 } 326 finally { closeAll(c, statement, result); } 327 } 328 } 329 } | Popular Tags |