1 25 26 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 ; 36 37 public class Timekeeper extends AbstractManager 38 { 39 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 result = statement.executeQuery("select ID, Owner, Seconds, Name from Timekeeper where StartTime is null AND Owner = "+manager.getUserID()); 55 56 ArrayList v = new ArrayList (); 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 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 TimeReport r[] = new TimeReport[v.size()]; 82 v.toArray(r); 83 84 return r; 85 } 86 finally { closeAll(c, statement, result); } 87 } 88 89 92 public void addTimeReport(String 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 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 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 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 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 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 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 |