1 25 26 30 package net.killingar.forum.internal.managers; 31 32 import java.sql.*; 33 import java.util.ArrayList ; 34 35 public final class TimeManager extends AbstractManager implements java.io.Serializable 36 { 37 public static final String 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); 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 62 public Timestamp getSystemTime(String system) throws SQLException 63 { 64 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 88 public void setSystemTime(String 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 system) throws SQLException {setSystemTime(system, new Timestamp(System.currentTimeMillis()));} 120 121 124 public Timestamp getTime(String system, long data) throws SQLException 125 { 126 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 151 public DataPair[] getTimes(String system) throws SQLException 152 { 153 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 v = new ArrayList (); 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 183 public Timestamp[] getTimes(String system, long data[]) throws SQLException 184 { 185 StringBuffer sb = new StringBuffer (" 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 (""); 198 199 Timestamp r[] = new Timestamp[data.length]; 200 for (int i = 0; i < r.length; i++) 201 r[i] = beginning; 202 203 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 234 public void setTime(String system, long data, Timestamp time) throws SQLException 235 { 236 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 system, long data) throws SQLException {setTime(system, data, new Timestamp(System.currentTimeMillis()));} 270 271 274 public Timestamp getUserTime(long userID, String system) throws SQLException 275 { 276 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 system) throws SQLException {return getUserTime(manager.getUserID(), system);} 297 298 299 302 public void setUserTime(String system, Timestamp time) throws SQLException 303 { 304 long userID = manager.getUserID(); 305 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 system) throws SQLException {setUserTime(system, new Timestamp(System.currentTimeMillis()));} 340 341 344 public Timestamp getUserDataTime(long userID, String 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 system, long data) throws SQLException {return getUserDataTime(manager.getUserID(), system, data);} 368 369 372 public DataPair[] getUserDataTimes(long userID, String 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 v = new ArrayList (); 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 system) throws SQLException { return getUserDataTimes(manager.getUserID(), system); } 400 401 404 public Timestamp[] getUserDataTimes(long userID, String system, long data[]) throws SQLException 405 { 406 StringBuffer sb = new StringBuffer (" 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 (""); 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 system, long data[]) throws SQLException {return getUserDataTimes(manager.getUserID(), system, data);} 451 452 455 public void setUserDataTime(String 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 system, long data) throws SQLException {setUserDataTime(system, data, new Timestamp(System.currentTimeMillis()));} 495 } | Popular Tags |