KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Keeps track of how much time you've spent doing something by acting as a stopwatch.
28  */

29 package net.killingar.forum.internal.managers;
30
31 import net.killingar.forum.internal.AccessDeniedException;
32 import net.killingar.forum.internal.TimeReport;
33
34 import java.sql.*;
35 import java.util.ArrayList JavaDoc;
36
37 public class Timekeeper extends AbstractManager
38 {
39     /**
40      * Get a list of time reports.
41      */

42     public TimeReport[] getTimeReports() throws SQLException
43     {
44         Connection c = null;
45         Statement statement = null;
46         ResultSet result = null;
47
48         try
49         {
50             c = getNewConnection();
51             statement = c.createStatement();
52
53             // get the stopped reports
54
result = statement.executeQuery("select ID, Owner, Seconds, Name from Timekeeper where StartTime is null AND Owner = "+manager.getUserID());
55
56             ArrayList JavaDoc v = new ArrayList JavaDoc();
57             while (result.next())
58             {
59                 v.add(new TimeReport(
60                     result.getLong(1),
61                     result.getLong(2),
62                     result.getLong(3),
63                     result.getString(4),
64                     null));
65             }
66
67             // get the running time reports
68
result = statement.executeQuery("select ID, Owner, Seconds, StartTime, Name, StartTime from Timekeeper where StartTime is not null AND Owner = "+manager.getUserID());
69
70             while (result.next())
71             {
72                 v.add(new TimeReport(
73                     result.getLong(1),
74                     result.getLong(2),
75                     result.getLong(3)+(System.currentTimeMillis()-result.getTimestamp(4).getTime())/1000,
76                     result.getString(5),
77                     result.getTimestamp(6)));
78             }
79
80             // return the data
81
TimeReport r[] = new TimeReport[v.size()];
82             v.toArray(r);
83
84             return r;
85         }
86         finally { closeAll(c, statement, result); }
87     }
88
89     /**
90      * Add a time report.
91      */

92     public void addTimeReport(String JavaDoc name) throws SQLException
93     {
94         Connection c = null;
95         PreparedStatement statement = null;
96         ResultSet result = null;
97
98         try
99         {
100             c = getNewConnection();
101             statement = c.prepareStatement("insert into Timekeeper (Owner, Name) values(?, ?)");
102             statement.setLong(1, manager.getUserID());
103             statement.setString(2, name);
104
105             statement.executeUpdate();
106         }
107         finally { closeAll(c, statement, result); }
108     }
109
110     /**
111      * Remove a time report.
112      */

113     public void removeTimeReport(long timeReportID) throws SQLException
114     {
115         Connection c = null;
116         Statement statement = null;
117         ResultSet result = null;
118
119         try
120         {
121             c = getNewConnection();
122             statement = c.createStatement();
123
124             statement.executeUpdate("delete from Timekeeper where ID = "+timeReportID);
125         }
126         finally { closeAll(c, statement, result); }
127     }
128
129     /**
130      * Get time report data.
131      */

132     public TimeReport getTimeReport(long timeReportID) throws SQLException, AccessDeniedException
133     {
134         Connection c = null;
135         Statement statement = null;
136         ResultSet result = null;
137
138         try
139         {
140             c = getNewConnection();
141             statement = c.createStatement();
142
143             result = statement.executeQuery("select Owner, Seconds, Name, StartTime from Timekeeper where ID = "+timeReportID);
144             if (!result.next())
145                 return null;
146
147             TimeReport t = new TimeReport(timeReportID, result.getLong(1), result.getLong(2), result.getString(3), result.getTimestamp(4));
148
149             if (t.ownerID == manager.getUserID())
150                 return t;
151             else
152                 throw new AccessDeniedException("attemt to get another users timekeeper data");
153         }
154         finally { closeAll(c, statement, result); }
155     }
156
157     /**
158      * Start a time report.
159      */

160     public void startTime(long timeReportID) throws SQLException, AccessDeniedException
161     {
162         getTimeReport(timeReportID);
163
164         Connection c = null;
165         Statement statement = null;
166         ResultSet result = null;
167
168         try
169         {
170             c = getNewConnection();
171             statement = c.createStatement();
172
173             statement.executeUpdate("update Timekeeper set StartTime = NOW() where id = "+timeReportID);
174         }
175         finally { closeAll(c, statement, result); }
176     }
177
178     /**
179      * Stop the time report and increase the stored amount of time with the amount of time passed since the timer was started.
180      */

181     public void stopTime(long timeReportID) throws SQLException, AccessDeniedException
182     {
183         getTimeReport(timeReportID);
184
185         Connection c = null;
186         Statement statement = null;
187         ResultSet result = null;
188
189         try
190         {
191             c = getNewConnection();
192             statement = c.createStatement();
193
194             statement.executeUpdate("update Timekeeper set Seconds = Seconds+(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(StartTime)) where id = "+timeReportID+" AND StartTime is not NULL");
195             statement.executeUpdate("update Timekeeper set StartTime = null where id = "+timeReportID);
196         }
197         finally { closeAll(c, statement, result); }
198     }
199
200     /**
201      * Add/subtract a number of seconds to a time report
202      */

203     public void modifyTime(long timeReportID, long seconds) throws SQLException, AccessDeniedException
204     {
205         getTimeReport(timeReportID);
206
207         Connection c = null;
208         Statement statement = null;
209         ResultSet result = null;
210
211         try
212         {
213             c = getNewConnection();
214             statement = c.createStatement();
215
216             statement.executeUpdate("update Timekeeper set Seconds = Seconds+"+seconds+" where id = "+timeReportID);
217         }
218         finally { closeAll(c, statement, result); }
219     }
220
221     /**
222      * Returns true if this user has any timers started.
223      */

224     public boolean hasStartedTimes() throws SQLException
225     {
226         Connection c = null;
227         Statement statement = null;
228         ResultSet result = null;
229
230         try
231         {
232             c = getNewConnection();
233             statement = c.createStatement();
234
235             result = statement.executeQuery("select count(*) from Timekeeper where StartTime is not null AND Owner = "+manager.getUserID());
236             if (!result.next())
237                 return false;
238
239             return result.getInt(1) > 0;
240         }
241         finally { closeAll(c, statement, result); }
242     }
243 }
Popular Tags