KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Planning manager.
28  */

29 package net.killingar.forum.internal.managers;
30
31 import net.killingar.forum.internal.*;
32
33 import java.sql.*;
34 import java.util.ArrayList JavaDoc;
35
36 public final class PlanningManager extends AbstractManager implements java.io.Serializable JavaDoc
37 {
38     /**
39      * Add an event with access restricted to a specific group.
40      * If groupID is given as -1 there is no restriction.
41      */

42     public void addEvent(Event event) throws SQLException, AccessDeniedException
43     {
44         manager.checkMyAccess(AccessLevel.addEvent);
45         boolean denied = true;
46         // check that the user is a member of the group he wants the event to apply to
47
if (event.groupID != -1)
48         {
49             Group groups[] = manager.getGroupsOfUser(manager.getUserID());
50             for (int i = 0; i < groups.length; i++)
51             {
52                 if (groups[i].ID == event.groupID)
53                 {
54                     denied = false;
55                     break;
56                 }
57             }
58         }
59
60         // else denied = true;
61
if (denied)
62         manager.checkMyAccess(AccessLevel.addGlobalEvent);
63
64         Connection c = null;
65         PreparedStatement statement = null;
66         ResultSet result = null;
67
68         try
69         {
70             c = getNewConnection();
71             statement = c.prepareStatement("insert into Events (User, UserGroup, Name, Description, Time, Created, LastChanged, LastChangedUser) values(?, ?, ?, ?, ?, NOW(), NOW(), ?)");
72             statement.setLong(1, manager.getUserID());
73             statement.setLong(2, event.groupID);
74             statement.setString(3, event.name);
75             statement.setString(4, event.description);
76             statement.setTimestamp(5, event.time);
77             statement.setLong(6, manager.getUserID());
78
79             statement.executeUpdate();
80         }
81         finally { closeAll(c, statement, result); }
82     }
83
84     /**
85      * Change an event.
86      */

87     public void changeEvent(Event event, boolean clear) throws SQLException, AccessDeniedException
88     {
89         if (getEvent(event.ID).ownerID != manager.getUserID())
90             manager.checkMyAccess(AccessLevel.changeEvent);
91         Connection c = null;
92         PreparedStatement statement = null;
93         ResultSet result = null;
94
95         if ("".equals(event.description))
96             event.description = null;
97
98         try
99         {
100             c = getNewConnection();
101             statement = c.prepareStatement("update Events set Name = ?, Description = ?, UserGroup = ?, Frozen = ?, Time = ?, LastChangedUser = ?, LastChanged = NOW() where ID = ?");
102             statement.setString(1, event.name);
103             statement.setString(2, event.description);
104             statement.setLong(3, event.groupID);
105             statement.setBoolean(4, event.frozen);
106             statement.setTimestamp(5, event.time);
107             statement.setLong(6, manager.getUserID());
108             statement.setLong(7, event.ID);
109
110             statement.executeUpdate();
111             if (clear)
112                 statement.executeUpdate("delete from EventData where event = "+event.ID);
113         }
114         finally { closeAll(c, statement, result); }
115     }
116
117     /**
118      * Remove an event.
119      */

120     public void removeEvent(long eventID) throws SQLException, AccessDeniedException
121     {
122         if (getEvent(eventID).ownerID != manager.getUserID())
123             manager.checkMyAccess(AccessLevel.removeEvent);
124
125         Connection c = null;
126         Statement statement = null;
127         ResultSet result = null;
128
129         try
130         {
131             c = getNewConnection();
132             statement = c.createStatement();
133
134             //statement.executeUpdate("delete from Events where ID = " + eventID);
135
//statement.executeUpdate("delete from Eventdata where Event = " + eventID);
136

137             statement.executeUpdate("update Events set Visible = 0, Frozen = 1 where ID = " + eventID);
138         }
139         finally { closeAll(c, statement, result); }
140     }
141
142     /**
143      * Returns true if the planning hasn't been read since the last change.
144      */

145     public boolean isUnreadPlanning() throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, SQLException
146     {
147         Connection c = null;
148         Statement statement = null;
149         ResultSet result = null;
150
151         try
152         {
153             c = getNewConnection();
154             statement = c.createStatement();
155
156             // 1) get the latest time
157
result = statement.executeQuery(
158                 "select "+
159                     "MAX(EventData.UserTime) "+
160                 "from "+
161                     "EventData, Events "+
162                 "where "+
163                     "Events.Visible = 1 AND "+
164                     "Events.ID = EventData.Event AND "+
165                     "(Events.Usergroup is null"+manager.getGroupsString("Events.UserGroup", " OR ")+") ");
166
167             // 2) get the current time and compare
168
boolean r = false;
169             if (result.next() && result.getTimestamp(1).after(getTimeOnPlanning()))
170                 r = true;
171             return r;
172         }
173         finally { closeAll(c, statement, result); }
174     }
175
176     /**
177      * Get time the current user last read the planning.
178      */

179     public Timestamp getTimeOnPlanning() throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, SQLException
180     {
181         return ((TimeManager)manager.getManager(TimeManager.class.getName())).getUserTime(TimeManager.systemPlanning);
182     }
183
184     /**
185      * Get a list of events. Ordered by creation time, oldest first.
186      */

187     public Event[] getEvents() throws SQLException
188     {
189         String JavaDoc groupsString;
190         {
191             Group groups[] = manager.getGroupsOfUser(manager.getUserID());
192             if (groups.length > 0)
193             {
194                 StringBuffer JavaDoc s = new StringBuffer JavaDoc("where Visible = 1 AND (");
195                 for (int i = 0; i < groups.length; i++)
196                 {
197                     if (i != 0)
198                         s.append(" OR ");
199                     s.append(" UserGroup = ");
200                     s.append(groups[i].ID);
201                 }
202
203                 s.append(" OR UserGroup = -1 OR UserGroup is null)");
204                 groupsString = s.toString();
205             }
206             else
207                 groupsString = "where Visible = 1";
208         }
209
210         Connection c = null;
211         Statement statement = null;
212         ResultSet result = null;
213
214         try
215         {
216             c = getNewConnection();
217             statement = c.createStatement();
218
219             result = statement.executeQuery("select * from Events "+groupsString+" order by Time asc");
220             ArrayList JavaDoc v = new ArrayList JavaDoc();
221             while (result.next())
222             {
223                 v.add(new Event(
224                     result.getLong("ID"),
225                     result.getLong("User"),
226                     result.getLong("UserGroup"),
227                     result.getString("Name"),
228                     result.getString("Description"),
229                     result.getTimestamp("Time"),
230                     result.getTimestamp("Created"),
231                     result.getInt("Frozen") == 1));
232             }
233
234             Event r[] = new Event[v.size()];
235             v.toArray(r);
236             return r;
237         }
238         finally { closeAll(c, statement, result); }
239     }
240
241     /**
242      * Get an event.
243      */

244     public Event getEvent(long eventID) throws SQLException
245     {
246         Connection c = null;
247         Statement statement = null;
248         ResultSet result = null;
249
250         try
251         {
252             c = getNewConnection();
253             statement = c.createStatement();
254
255             result = statement.executeQuery("select * from Events where ID = "+eventID);
256             Event r = null;
257             if (result.next())
258             {
259                 r = new Event(
260                     eventID,
261                     result.getLong("User"),
262                     result.getLong("UserGroup"),
263                     result.getString("Name"),
264                     result.getString("Description"),
265                     result.getTimestamp("Time"),
266                     result.getTimestamp("Created"),
267                     result.getInt("Frozen") == 1);
268             }
269
270             return r;
271         }
272         finally { closeAll(c, statement, result); }
273     }
274
275     /**
276      * Get the event data of an event and a user.
277      */

278     public EventData getEventData(long eventID, long userID) throws SQLException
279     {
280         Connection c = null;
281         Statement statement = null;
282         ResultSet result = null;
283
284         try
285         {
286             c = getNewConnection();
287             statement = c.createStatement();
288
289             result = statement.executeQuery("select * from EventData where Event = "+eventID+" AND User = "+userID);
290             EventData r = null;
291             if (result.next() && result.getString(1) != null)
292                 r = new EventData(eventID, userID, result.getTimestamp("UserTime"), result.getInt("Data"));
293             return r;
294         }
295         finally { closeAll(c, statement, result); }
296     }
297
298     /**
299      * Set a users event data on an event.
300      */

301     public void setEventData(long userID, long eventID, int data) throws SQLException, AccessDeniedException
302     {
303         if (userID != manager.getUserID())
304             manager.checkMyAccess(AccessLevel.setEventData);
305         if (getEvent(eventID).frozen)
306             throw new AccessDeniedException("attempt to change data in a frozen event");
307
308         Connection c = null;
309         PreparedStatement statement = null;
310         ResultSet result = null;
311
312         try
313         {
314             c = getNewConnection();
315
316             statement = c.prepareStatement("delete from EventData where Event = ? and User = ?");
317             statement.setLong(1, eventID);
318             statement.setLong(2, userID);
319             statement.executeUpdate();
320             statement.close();
321
322             statement = c.prepareStatement("insert into EventData (Event, User, Data, UserTime) values(?, ?, ?, NOW())");
323             statement.setLong(1, eventID);
324             statement.setLong(2, userID);
325             statement.setInt(3, data);
326             statement.executeUpdate();
327         }
328         finally { closeAll(c, statement, result); }
329     }
330
331     /**
332      * Nofify the system that the user has read the planning.
333      */

334     public void setReadPlanning() throws Exception JavaDoc
335     {
336         ((TimeManager)manager.getManager(TimeManager.class.getName())).setUserTime(TimeManager.systemPlanning);
337     }
338 }
Popular Tags