KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > managers > AreaHotlistManager


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 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 JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 public final class AreaHotlistManager extends AbstractManager implements java.io.Serializable JavaDoc
40 {
41     public static final long systemActive = 0L;
42     public static final long systemPassive = 1L;
43     private transient Object JavaDoc lock = new Object JavaDoc();
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 JavaDoc s)
56     {
57         if (s.equals("logout"))
58         {
59             synchronized (lock)
60             {
61                 flatHotlistCache.clear();
62                 hotlistCache = null;
63             }
64         }
65     }
66
67     /**
68      * Add an area.
69      */

70     public final void addArea(long areaID, long system)
71         throws SQLException JavaDoc
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 JavaDoc c = null;
82             Statement JavaDoc statement = null;
83             ResultSet JavaDoc 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     /**
112      * Add an area group.
113      */

114     public final void addAreaGroup(long areaGroupID, long system)
115         throws SQLException JavaDoc
116     {
117         synchronized (lock)
118         {
119             if (hasAreaGroup(areaGroupID, system))
120             {
121                 return;
122             }
123
124             Connection JavaDoc c = null;
125             Statement JavaDoc statement = null;
126             ResultSet JavaDoc 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     /**
141      * Remove an area.
142      */

143     public final void removeArea(long areaID, long system)
144         throws SQLException JavaDoc, ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
145     {
146         synchronized (lock)
147         {
148             if (!hasArea(areaID, system))
149             {
150                 return;
151             }
152
153             Connection JavaDoc c = null;
154             Statement JavaDoc statement = null;
155             ResultSet JavaDoc 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     /**
174      * Remove an area group.
175      */

176     public final void removeAreaGroup(long areaGroupID, long system)
177         throws Exception JavaDoc
178     {
179         synchronized (lock)
180         {
181             if (!hasAreaGroup(areaGroupID, system))
182             {
183                 return;
184             }
185
186             Connection JavaDoc c = null;
187             Statement JavaDoc statement = null;
188             ResultSet JavaDoc 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     /**
204      * Returns true if the current user has the specified area group in his hotlist.
205      */

206     public final boolean hasAreaGroup(long areaGroupID, long system)
207         throws SQLException JavaDoc
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     /**
217      * Returns true if the current user has the specified area in his hotlist.
218      */

219     public final boolean hasArea(long areaID, long system)
220         throws SQLException JavaDoc
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 JavaDoc
231     {
232         return hasArea(areaID, systemPassive) || hasArea(areaID, systemActive);
233     }
234
235     /**
236      * Gets the users hotlist.
237      */

238     public final HotlistEntry[] getHotlist()
239         throws SQLException JavaDoc
240     {
241         synchronized (lock)
242         {
243             if (System.currentTimeMillis()-lastUpdated > 1000*60) // 1 minute timeout
244
hotlistCache = null;
245
246             if (hotlistCache != null)
247             {
248                 return hotlistCache;
249             }
250             else
251             {
252                 Connection JavaDoc c = null;
253                 Statement JavaDoc statement = null;
254                 ResultSet JavaDoc 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 JavaDoc arraylist = new ArrayList JavaDoc();
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     /**
277      * Gets the users hotlist as a flat list.
278      */

279     public final Area[] getFlatHotlist(long system)
280         throws SQLException JavaDoc
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 JavaDoc c = null;
292             Statement JavaDoc statement = null;
293             ResultSet JavaDoc 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 JavaDoc arraylist = new ArrayList JavaDoc();
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