1 25 26 29 package net.killingar.forum.internal.managers; 30 31 import net.killingar.forum.internal.*; 32 33 import java.sql.Connection ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.sql.Statement ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 40 public class ComicManager extends AbstractManager implements java.io.Serializable 41 { 42 45 public void addComic(Comic comic) throws SQLException , AccessDeniedException 46 { 47 manager.checkMyAccess(AccessLevel.addComic); 48 49 Connection c = null; 50 Statement statement = null; 51 ResultSet result = null; 52 53 try 54 { 55 c = getNewConnection(); 56 statement = c.createStatement(); 57 58 statement.executeUpdate("insert into Comics (Name, URL, System, Position) values ('"+ 59 Utils.disableSQL(comic.name)+"', '"+ 60 Utils.disableSQL(comic.URL)+"', '"+ 61 Utils.disableSQL(comic.system)+"', '"+ 62 Utils.disableSQL(comic.position)+"')"); 63 } 64 finally { closeAll(c, statement, result); } 65 } 66 67 70 public void removeComic(long comicID) throws SQLException , AccessDeniedException 71 { 72 manager.checkMyAccess(AccessLevel.removeComic); 73 74 Connection c = null; 75 Statement statement = null; 76 ResultSet result = null; 77 78 try 79 { 80 c = getNewConnection(); 81 statement = c.createStatement(); 82 83 statement.executeUpdate("delete from Comics where ID = "+comicID); 84 statement.executeUpdate("delete from Strips where manager = "+comicID); 85 } 86 finally { closeAll(c, statement, result); } 87 } 88 89 92 public void changeComic(Comic comic) throws SQLException , AccessDeniedException 93 { 94 manager.checkMyAccess(AccessLevel.changeComic); 95 96 Connection c = null; 97 Statement statement = null; 98 ResultSet result = null; 99 100 try 101 { 102 c = getNewConnection(); 103 statement = c.createStatement(); 104 105 statement.executeUpdate("update Comics set Name = '"+Utils.disableSQL(comic.name)+"', URL = '"+Utils.disableSQL(comic.URL)+"', Latest = "+comic.latestID+", First = "+comic.firstID+", Position = '"+Utils.disableSQL(comic.position)+"', System = '"+Utils.disableSQL(comic.system)+"' where ID = "+comic.ID); 106 } 107 finally { closeAll(c, statement, result); } 108 } 109 110 113 public long addStrip(Strip strip) throws SQLException , AccessDeniedException 114 { 115 manager.checkMyAccess(AccessLevel.changeComic); 116 117 Connection c = null; 118 Statement statement = null; 119 ResultSet result = null; 120 121 try 122 { 123 c = getNewConnection(); 124 statement = c.createStatement(); 125 126 result = statement.executeQuery("select Latest from Comics where ID = "+strip.parentID); 127 result.next(); 128 129 if (result.getString(1) != null) 130 strip.prevID = result.getLong(1); 131 statement.executeUpdate("insert into Strips (Comic, URL, Prev, Time, Description, SystemDescription) values ("+strip.parentID+", '"+Utils.disableSQL(strip.URL)+"', "+result.getString(1)+", '"+strip.time+"', '"+Utils.disableSQL(strip.description)+"', '"+Utils.disableSQL(strip.systemDescription)+"')"); 132 result = statement.getGeneratedKeys(); 133 result.next(); 134 135 strip.ID = result.getLong(1); 136 statement.executeUpdate("update Strips set Next = "+strip.ID+" where ID = "+strip.prevID); 137 statement.executeUpdate("update Comics set Latest = "+strip.ID+" where ID = "+strip.parentID); 138 result = statement.executeQuery("select First from Comics where ID = "+strip.parentID); 139 140 result.next(); 141 if (result.getString(1) == null) 142 statement.executeUpdate("update Comics set First = "+strip.ID+" where ID = "+strip.parentID); 143 144 return strip.ID; 145 } 146 finally { closeAll(c, statement, result); } 147 } 148 149 152 public void removeStrip(long ID) throws SQLException , AccessDeniedException 153 { 154 manager.checkMyAccess(AccessLevel.changeComic); 155 156 Connection c = null; 157 Statement statement = null; 158 ResultSet result = null; 159 160 try 161 { 162 c = getNewConnection(); 163 statement = c.createStatement(); 164 165 Strip strip = getStrip(ID); 166 if (strip.prevID == -1 && strip.nextID == -1) { 168 statement.executeUpdate("update Comics set First = NULL, Latest = NULL where ID = "+strip.parentID); 169 170 statement.executeUpdate("delete from ComicPositions where Position = "+strip.ID); 171 } 172 173 else if (strip.prevID == -1) { 175 Strip nextID = getStrip(strip.nextID); 176 statement.executeUpdate("update Comics set First = "+nextID.ID+" where ID = "+strip.parentID); 177 statement.executeUpdate("update Strips set Prev = NULL where ID = "+nextID.ID); 178 179 statement.executeUpdate("delete from ComicPositions where Position = "+strip.ID); 180 } 181 182 else if (strip.nextID == -1) { 184 Strip prevID = getStrip(strip.prevID); 185 statement.executeUpdate("update Comics set Latest = "+prevID.ID+" where ID = "+strip.parentID); 186 statement.executeUpdate("update Strips set Next = NULL where ID = "+prevID.ID); 187 188 statement.executeUpdate("update ComicPositions set Position = "+prevID.ID+" where Position = "+strip.ID); 189 } 190 191 else { 193 Strip 194 nextID = getStrip(strip.nextID), 195 prevID = getStrip(strip.prevID); 196 statement.executeUpdate("update Strips set Prev = "+prevID.ID+" where ID = "+nextID.ID); 197 statement.executeUpdate("update Strips set Next = "+nextID.ID+" where ID = "+prevID.ID); 198 199 statement.executeUpdate("update ComicPositions set Position = "+prevID.ID+" where Position = "+strip.ID); 200 } 201 202 statement.executeUpdate("delete from Strips where ID = "+strip.ID); 203 } 204 finally { closeAll(c, statement, result); } 205 } 206 207 210 public void changeStrip(Strip strip) throws SQLException , AccessDeniedException 211 { 212 manager.checkMyAccess(AccessLevel.changeComic); 213 214 Connection c = null; 215 Statement statement = null; 216 ResultSet result = null; 217 218 try 219 { 220 c = getNewConnection(); 221 statement = c.createStatement(); 222 223 statement.executeUpdate("update Strips set URL = '"+Utils.disableSQL(strip.URL)+"', LocalURL = "+((strip.localURL == null)?"null":"'"+Utils.disableSQL(strip.localURL)+"'")+", Next = "+strip.nextID+", Prev = "+strip.prevID+", Time = '"+strip.time+"', Description = "+((strip.description == null)?"null":"'"+Utils.disableSQL(strip.description)+"'")+" where ID = "+strip.ID); 224 } 225 finally { closeAll(c, statement, result); } 226 } 227 228 231 public Comic[] getComics() throws SQLException , AccessDeniedException 232 { 233 manager.checkMyAccess(AccessLevel.comics); 234 235 Connection c = null; 236 Statement statement = null; 237 ResultSet result = null; 238 239 try 240 { 241 c = getNewConnection(); 242 statement = c.createStatement(); 243 244 result = statement.executeQuery("select ID, Name, URL, Latest, First, System, Position from Comics"); 245 List v = new ArrayList (); 246 while(result.next()) 247 { 248 v.add(new Comic( 249 result.getLong(1), 250 result.getString(2), 251 result.getString(3), 252 result.getString(4) == null? -1: result.getLong(4), 253 result.getString(5) == null? -1: result.getLong(5), 254 result.getString(6), 255 result.getString(7))); 256 } 257 258 Comic r[] = new Comic[v.size()]; 259 v.toArray(r); 260 261 return r; 262 } 263 finally { closeAll(c, statement, result); } 264 } 265 266 269 public long[][] getNumberOfStripsInComics() throws SQLException , AccessDeniedException 270 { 271 manager.checkMyAccess(AccessLevel.comics); 272 273 Connection c = null; 274 Statement statement = null; 275 ResultSet result = null; 276 277 try 278 { 279 c = getNewConnection(); 280 statement = c.createStatement(); 281 282 result = statement.executeQuery("select Comics.ID, count(Strips.ID) from Comics, Strip where Comic.ID = Strip.Comic group by Comic.ID"); 283 List 284 a = new ArrayList (), 285 b = new ArrayList (); 286 for(int i = 0; result.next(); i++) 287 { 288 a.add(result.getString(1)); 289 b.add(result.getString(2)); 290 } 291 292 if (a.size() == 0 || b.size() != a.size())return null; 293 long r[][] = new long[2][a.size()]; 294 for(int i = 0; i < a.size(); i++) 295 { 296 r[0][i] = Long.parseLong((String )a.get(i)); 297 r[1][i] = Long.parseLong((String )b.get(i)); 298 } 299 300 301 return r; 302 } 303 finally { closeAll(c, statement, result); } 304 } 305 306 309 public long getNumberOfComics() throws SQLException , AccessDeniedException 310 { 311 manager.checkMyAccess(AccessLevel.comics); 312 313 Connection c = null; 314 Statement statement = null; 315 ResultSet result = null; 316 317 try 318 { 319 c = getNewConnection(); 320 statement = c.createStatement(); 321 322 result = statement.executeQuery("select count(*) from Comic"); 323 long r = 0; 324 if (result.next()) 325 r = result.getLong(1); 326 327 return r; 328 } 329 finally { closeAll(c, statement, result); } 330 } 331 332 335 public Comic getComic(long comicID) throws SQLException , AccessDeniedException 336 { 337 manager.checkMyAccess(AccessLevel.comics); 338 339 Connection c = null; 340 Statement statement = null; 341 ResultSet result = null; 342 343 try 344 { 345 c = getNewConnection(); 346 statement = c.createStatement(); 347 348 result = statement.executeQuery("select ID, Name, URL, Latest, First, System, Position from Comics where ID = "+comicID); 349 Comic r = null; 350 if (result.next()) 351 { 352 r = new Comic( 353 result.getLong(1), 354 result.getString(2), 355 result.getString(3), 356 result.getString(4) == null? -1: result.getLong(4), 357 result.getString(5) == null? -1: result.getLong(5), 358 result.getString(6), 359 result.getString(7)); 360 } 361 362 return r; 363 } 364 finally { closeAll(c, statement, result); } 365 } 366 367 370 public Strip getStrip(long stripID) throws SQLException , AccessDeniedException 371 { 372 manager.checkMyAccess(AccessLevel.comics); 373 374 Connection c = null; 375 Statement statement = null; 376 ResultSet result = null; 377 378 try 379 { 380 c = getNewConnection(); 381 statement = c.createStatement(); 382 383 result = statement.executeQuery("select ID, Comic, URL, LocalURL, Next, Prev, Time, Description, SystemDescription from Strips where ID = "+stripID); 384 Strip r = null; 385 386 if (result.next()) 387 { 388 r = new Strip( 389 result.getLong(1), 390 result.getLong(2), 391 result.getString(3), 392 result.getString(4), 393 result.getString(5) == null? -1: result.getLong(5), 394 result.getString(6) == null? -1: result.getLong(6), 395 result.getDate(7), 396 result.getString(8), 397 result.getString(9)); 398 } 399 400 return r; 401 } 402 finally { closeAll(c, statement, result); } 403 } 404 405 408 public Strip[] getStrips(long comicID) throws SQLException , AccessDeniedException 409 { 410 manager.checkMyAccess(AccessLevel.comics); 411 412 Connection c = null; 413 Statement statement = null; 414 ResultSet result = null; 415 416 try 417 { 418 c = getNewConnection(); 419 statement = c.createStatement(); 420 421 result = statement.executeQuery("select ID, Comic, URL, LocalURL, Next, Prev, Time, Description, SystemDescription from Strips where Comic = "+comicID); 422 List v = new ArrayList (); 423 while(result.next()) 424 { 425 v.add(new Strip( 426 result.getLong(1), 427 result.getLong(2), 428 result.getString(3), 429 result.getString(4), 430 result.getString(5) == null? -1: result.getLong(5), 431 result.getString(6) == null? -1: result.getLong(6), 432 result.getDate(7), 433 result.getString(8), 434 result.getString(9))); 435 } 436 437 Strip r[] = new Strip[v.size()]; 438 v.toArray(r); 439 440 return r; 441 } 442 finally { closeAll(c, statement, result); } 443 } 444 445 449 public long getStripPosition(long stripID) throws SQLException , AccessDeniedException 450 { 451 manager.checkMyAccess(AccessLevel.comics); 452 453 Strip s = getStrip(stripID); 454 455 Connection c = null; 456 Statement statement = null; 457 ResultSet result = null; 458 459 try 460 { 461 c = getNewConnection(); 462 statement = c.createStatement(); 463 464 result = statement.executeQuery("select count(*) from Strips where Comic = "+s.parentID+" AND ID <= "+s.ID); 465 long r = 0; 466 if (result.next()) 467 r = result.getLong(1); 468 469 return r; 470 } 471 finally { closeAll(c, statement, result); } 472 } 473 474 477 public long getNumberOfStrips(long comicID) throws SQLException , AccessDeniedException 478 { 479 manager.checkMyAccess(AccessLevel.comics); 480 481 Connection c = null; 482 Statement statement = null; 483 ResultSet result = null; 484 485 try 486 { 487 c = getNewConnection(); 488 statement = c.createStatement(); 489 490 result = statement.executeQuery("select count(*) from Strips where Comic = "+comicID); 491 long r = 0; 492 if (result.next()) 493 r = result.getLong(1); 494 495 return r; 496 } 497 finally { closeAll(c, statement, result); } 498 } 499 500 503 public long getPosition(long userID, long comicID) throws SQLException , AccessDeniedException 504 { 505 manager.checkMyAccess(AccessLevel.comics); 506 507 Connection c = null; 508 Statement statement = null; 509 ResultSet result = null; 510 511 try 512 { 513 c = getNewConnection(); 514 statement = c.createStatement(); 515 516 result = statement.executeQuery("select Position from ComicPositions where User = "+userID+" AND Comic = "+comicID); 517 long r; 518 if (!result.next()) 519 r = getComic(comicID).firstID; 520 else 521 r = result.getLong(1); 522 523 return r; 524 } 525 finally { closeAll(c, statement, result); } 526 } 527 528 531 public void setPosition(long userID, long comicID, long position) throws SQLException , AccessDeniedException 532 { 533 if (manager.getUserID() != userID) 534 throw new AccessDeniedException("attempt to change another users comic position"); 535 536 manager.checkMyAccess(AccessLevel.comics); 537 538 Connection c = null; 539 Statement statement = null; 540 ResultSet result = null; 541 542 try 543 { 544 c = getNewConnection(); 545 statement = c.createStatement(); 546 547 result = statement.executeQuery("select * from ComicPositions where User = "+userID+" AND Comic = "+comicID); 548 if (!result.next()) 549 statement.executeUpdate("insert into ComicPositions (User, Comic, Position) values ("+userID+", "+comicID+", "+position+")"); 550 else 551 statement.executeUpdate("update ComicPositions set Position = "+position+" where User = "+userID+" AND Comic = "+comicID); 552 } 553 finally { closeAll(c, statement, result); } 554 } 555 } 556 | Popular Tags |