KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Manager for times.
28  * NOTE: If no time is defined in the database get* functions returns new years eve 2001.
29  */

30 package net.killingar.forum.internal.managers;
31
32 import java.sql.*;
33 import java.util.ArrayList JavaDoc;
34
35 public final class TimeManager extends AbstractManager implements java.io.Serializable JavaDoc
36 {
37     public static final String JavaDoc
38         systemAreas = "0",
39         systemPlanning = "1",
40         systemWelcome = "2",
41         systemWiki = "3",
42         systemPolls = "4",
43         systemTasks = "5";
44
45     public static final Timestamp beginning = new Timestamp(978303600000L); // new years eve 2001
46

47     public static class DataPair
48     {
49         public Timestamp time;
50         public long data;
51
52         public DataPair(Timestamp t, long d)
53         {
54             time = t;
55             data = d;
56         }
57     }
58
59     /**
60      * Get the time of a system. Returns new years eve 2001 if no data is found.
61      */

62     public Timestamp getSystemTime(String JavaDoc system) throws SQLException
63     {
64         // where User is null && Data is null
65
Connection c = null;
66         PreparedStatement statement = null;
67         ResultSet result = null;
68
69         try
70         {
71             c = getNewConnection();
72             statement = c.prepareStatement("select Time from Times where User is null AND Data is null AND System = ?");
73             statement.setString(1, system);
74             result = statement.executeQuery();
75
76             if (result.next())
77                     return result.getTimestamp(1);
78         }
79         finally { closeAll(c, statement, result); }
80
81         return beginning;
82     }
83
84
85     /**
86      * Set the time of a system. If time is null, the database entry is removed.
87      */

88     public void setSystemTime(String JavaDoc system, Timestamp time) throws SQLException
89     {
90         Connection c = null;
91         PreparedStatement statement = null;
92         ResultSet result = null;
93
94         try
95         {
96             c = getNewConnection();
97             statement = c.prepareStatement("select Time from Times where User is null AND Data is null AND System = ?");
98             statement.setString(1, system);
99             result = statement.executeQuery();
100             if (!result.next())
101             {
102                 statement.close();
103                 statement = c.prepareStatement("insert into Times (System, Time, User, Data) values (?, ?, null, null)");
104         statement.setString(1, system);
105                 statement.setTimestamp(2, time);
106                 statement.executeUpdate();
107             }
108             else
109             {
110                 statement.close();
111                 statement = c.prepareStatement("update Times set Time = ? where User is null AND Data is null AND System = ?");
112                 statement.setTimestamp(1, time);
113                 statement.setString(2, system);
114                 statement.executeUpdate();
115             }
116         }
117         finally { closeAll(c, statement, result); }
118     }
119     public void setSystemTime(String JavaDoc system) throws SQLException {setSystemTime(system, new Timestamp(System.currentTimeMillis()));}
120
121     /**
122      * Get a time. Returns new years eve 2001 if no data is found.
123      */

124     public Timestamp getTime(String JavaDoc system, long data) throws SQLException
125     {
126         // where User is null
127
Connection c = null;
128         PreparedStatement statement = null;
129         ResultSet result = null;
130
131         try
132         {
133             c = getNewConnection();
134             statement = c.prepareStatement("select Time from Times where User is null AND Data = ? AND System = ?");
135             statement.setLong(1, data);
136             statement.setString(2, system);
137
138             result = statement.executeQuery();
139
140             if (result.next())
141                 return result.getTimestamp(1);
142         }
143         finally { closeAll(c, statement, result); }
144
145         return beginning;
146     }
147
148     /**
149      * Get times of a specified system. Returns new years eve 2001 if no data is found.
150      */

151     public DataPair[] getTimes(String JavaDoc system) throws SQLException
152     {
153         // where User is null
154
Connection c = null;
155         PreparedStatement statement = null;
156         ResultSet result = null;
157
158         try
159         {
160             c = getNewConnection();
161             statement = c.prepareStatement("select Time, Data from Times where User is null AND System = ?");
162             statement.setString(1, system);
163             result = statement.executeQuery();
164
165             ArrayList JavaDoc v = new ArrayList JavaDoc();
166             while (result.next())
167             {
168                 v.add(new DataPair(result.getTimestamp(1), result.getLong(2)));
169             }
170
171             DataPair r[] = new DataPair[v.size()];
172             v.toArray(r);
173
174             return r;
175         }
176         finally { closeAll(c, statement, result); }
177     }
178
179     /**
180      * Get a list of times. Returns new years eve 2001 if no data is found.
181      * This is functionally idential to calling getTime() multiple times for each item in the second argument.
182      */

183     public Timestamp[] getTimes(String JavaDoc system, long data[]) throws SQLException
184     {
185         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" AND (");
186         if (data.length > 0)
187         {
188             for (int i = 0; i < data.length; i++)
189             {
190                 if (i != 0)sb.append(" or ");
191                 sb.append("Data = ");
192                 sb.append(Long.toString(data[i]));
193             }
194             sb.append(")");
195         }
196         else
197             sb = new StringBuffer JavaDoc("");
198
199         Timestamp r[] = new Timestamp[data.length];
200         for (int i = 0; i < r.length; i++)
201             r[i] = beginning;
202
203         // where User is null
204
Connection c = null;
205         PreparedStatement statement = null;
206         ResultSet result = null;
207
208         try
209         {
210             c = getNewConnection();
211             statement = c.prepareStatement("select Data, Time from Times where User is null "+sb.toString()+" AND System = ?");
212             statement.setString(1, system);
213             result = statement.executeQuery();
214
215             while (result.next())
216             {
217                 long id = result.getLong(1);
218                 for (int i = 0; i < r.length; i++)
219                 {
220                     if (id == data[i])
221                         r[i] = result.getTimestamp(2);
222                 }
223             }
224         }
225         finally { closeAll(c, statement, result); }
226
227         return r;
228     }
229
230
231     /**
232      * Set a time for a system.
233      */

234     public void setTime(String JavaDoc system, long data, Timestamp time) throws SQLException
235     {
236         // where User is null
237
Connection c = null;
238         PreparedStatement statement = null;
239         ResultSet result = null;
240
241         try
242         {
243             c = getNewConnection();
244             statement = c.prepareStatement("select Time from Times where User is null AND Data = ? AND System = ?");
245             statement.setLong(1, data);
246             statement.setString(2, system);
247
248             result = statement.executeQuery();
249             if (!result.next())
250             {
251                 statement.close();
252                 statement = c.prepareStatement("insert into Times (System, Time, User, Data) values (?, ?, null, ?)");
253                 statement.setString(1, system);
254                 statement.setTimestamp(2, time);
255                 statement.setLong(3, data);
256                 statement.executeUpdate();
257             }
258             else
259             {
260                 statement = c.prepareStatement("update Times set Time = ? where User is null AND Data = ? AND System = ?");
261                 statement.setTimestamp(1, time);
262                 statement.setLong(2, data);
263                 statement.setString(3, system);
264                 statement.executeUpdate();
265             }
266         }
267         finally { closeAll(c, statement, result); }
268     }
269     public void setTime(String JavaDoc system, long data) throws SQLException {setTime(system, data, new Timestamp(System.currentTimeMillis()));}
270
271     /**
272      * Get a user time for a specific system. Returns new years eve 2001 if no data is found.
273      */

274     public Timestamp getUserTime(long userID, String JavaDoc system) throws SQLException
275     {
276         // where Data is null
277
Connection c = null;
278         PreparedStatement statement = null;
279         ResultSet result = null;
280
281         try
282         {
283             c = getNewConnection();
284             statement = c.prepareStatement("select Time from Times where Data is null AND System = ? AND User = ?");
285             statement.setString(1, system);
286             statement.setLong(2, userID);
287             result = statement.executeQuery();
288
289             if (result.next())
290                 return result.getTimestamp(1);
291         }
292         finally { closeAll(c, statement, result); }
293
294         return beginning;
295     }
296     public Timestamp getUserTime(String JavaDoc system) throws SQLException {return getUserTime(manager.getUserID(), system);}
297
298
299     /**
300      * Set the current users time for the specific system.
301      */

302     public void setUserTime(String JavaDoc system, Timestamp time) throws SQLException
303     {
304         long userID = manager.getUserID();
305         // where Data is null
306
Connection c = null;
307         PreparedStatement statement = null;
308         ResultSet result = null;
309
310         try
311         {
312             c = getNewConnection();
313             statement = c.prepareStatement("select Time from Times where User = ? AND Data is null AND System = ?");
314             statement.setLong(1, userID);
315             statement.setString(2, system);
316
317             result = statement.executeQuery();
318             if (!result.next())
319             {
320                 statement.close();
321                 statement = c.prepareStatement("insert into Times (System, Time, User, Data) values (?, ?, ?, null)");
322                 statement.setString(1, system);
323                 statement.setTimestamp(2, time);
324                 statement.setLong(3, userID);
325                 statement.executeUpdate();
326             }
327             else
328             {
329                 statement.close();
330                 statement = c.prepareStatement("update Times set Time = ? where User = ? AND Data is null AND System = ?");
331                 statement.setTimestamp(1, time);
332                 statement.setLong(2, userID);
333                 statement.setString(3, system);
334                 statement.executeUpdate();
335             }
336         }
337         finally { closeAll(c, statement, result); }
338     }
339     public void setUserTime(String JavaDoc system) throws SQLException {setUserTime(system, new Timestamp(System.currentTimeMillis()));}
340
341     /**
342      * Get a user time corresponding to the specified data. Returns new years eve 2001 if no data is found.
343      */

344     public Timestamp getUserDataTime(long userID, String JavaDoc system, long data) throws SQLException
345     {
346         Connection c = null;
347         PreparedStatement statement = null;
348         ResultSet result = null;
349
350         try
351         {
352             c = getNewConnection();
353             statement = c.prepareStatement("select Time from Times where System = ? AND Data = ? AND User = ?");
354             statement.setString(1, system);
355             statement.setLong(2, data);
356             statement.setLong(3, userID);
357
358             result = statement.executeQuery();
359
360             if (result.next())
361                 return result.getTimestamp(1);
362         }
363         finally { closeAll(c, statement, result); }
364
365         return beginning;
366     }
367     public Timestamp getUserDataTime(String JavaDoc system, long data) throws SQLException {return getUserDataTime(manager.getUserID(), system, data);}
368
369     /**
370      * Get user times. Returns new years eve 2001 if no data is found.
371      */

372     public DataPair[] getUserDataTimes(long userID, String JavaDoc system) throws SQLException
373     {
374         Connection c = null;
375         PreparedStatement statement = null;
376         ResultSet result = null;
377
378         try
379         {
380             c = getNewConnection();
381             statement = c.prepareStatement("select Time, Data from Times where System = ? AND User = ?");
382             statement.setString(1, system);
383             statement.setLong(2, userID);
384             result = statement.executeQuery();
385
386             ArrayList JavaDoc v = new ArrayList JavaDoc();
387             while (result.next())
388             {
389                 v.add(new DataPair(result.getTimestamp(1), result.getLong(2)));
390             }
391
392             DataPair r[] = new DataPair[v.size()];
393             v.toArray(r);
394
395             return r;
396         }
397         finally { closeAll(c, statement, result); }
398     }
399     public DataPair[] getUserDataTimes(String JavaDoc system) throws SQLException { return getUserDataTimes(manager.getUserID(), system); }
400
401     /**
402      * Get user times corresponding to the specified data. Returns new years eve 2001 if no data is found.
403      */

404     public Timestamp[] getUserDataTimes(long userID, String JavaDoc system, long data[]) throws SQLException
405     {
406         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" AND (");
407         if (data.length > 0)
408         {
409             for (int i = 0; i < data.length; i++)
410             {
411                 if (i != 0)sb.append(" or ");
412                 sb.append("Data = ");
413                 sb.append(Long.toString(data[i]));
414             }
415             sb.append(")");
416         }
417         else
418             sb = new StringBuffer JavaDoc("");
419
420         Timestamp r[] = new Timestamp[data.length];
421         for (int i = 0; i < r.length; i++)
422             r[i] = beginning;
423
424         Connection c = null;
425         PreparedStatement statement = null;
426         ResultSet result = null;
427
428         try
429         {
430             c = getNewConnection();
431             statement = c.prepareStatement("select Data, Time from Times where System = ? AND User = ?"+sb.toString());
432             statement.setString(1, system);
433             statement.setLong(2, userID);
434             result = statement.executeQuery();
435
436             while (result.next())
437             {
438                 long id = result.getLong(1);
439                 for (int i = 0; i < r.length; i++)
440                 {
441                     if (id == data[i])
442                         r[i] = result.getTimestamp(2);
443                 }
444             }
445         }
446         finally { closeAll(c, statement, result); }
447
448         return r;
449     }
450     public Timestamp[] getUserDataTimes(String JavaDoc system, long data[]) throws SQLException {return getUserDataTimes(manager.getUserID(), system, data);}
451
452     /**
453      * Set a user time corresponding to the specified data.
454      */

455     public void setUserDataTime(String JavaDoc system, long data, Timestamp time) throws SQLException
456     {
457         long userID = manager.getUserID();
458         Connection c = null;
459         PreparedStatement statement = null;
460         ResultSet result = null;
461
462         try
463         {
464             c = getNewConnection();
465             statement = c.prepareStatement("select Time from Times where User = ? AND Data = ? AND System = ?");
466             statement.setLong(1, userID);
467             statement.setLong(2, data);
468             statement.setString(3, system);
469             result = statement.executeQuery();
470
471             if (!result.next())
472             {
473                 statement.close();
474                 statement = c.prepareStatement("insert into Times (System, Time, User, Data) values (?, ?, ?, ?)");
475                 statement.setString(1, system);
476                 statement.setTimestamp(2, time);
477                 statement.setLong(3, userID);
478                 statement.setLong(4, data);
479                 statement.executeUpdate();
480             }
481             else
482             {
483                 statement.close();
484                 statement = c.prepareStatement("update Times set Time = ? where User = ? AND Data = ? AND System = ?");
485                 statement.setTimestamp(1, time);
486                 statement.setLong(2, userID);
487                 statement.setLong(3, data);
488                 statement.setString(4, system);
489                 statement.executeUpdate();
490             }
491         }
492         finally { closeAll(c, statement, result); }
493     }
494     public void setUserDataTime(String JavaDoc system, long data) throws SQLException {setUserDataTime(system, data, new Timestamp(System.currentTimeMillis()));}
495 }
Popular Tags