1 package org.jahia.content; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Statement ; 8 import java.sql.Timestamp ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 12 import org.jahia.exceptions.JahiaException; 13 import org.jahia.registries.ServicesRegistry; 14 15 26 27 public class ObjectLinkDB { 28 29 private static org.apache.log4j.Logger logger = 30 org.apache.log4j.Logger.getLogger(ObjectLinkDB.class); 31 32 private static ObjectLinkDB singletonInstance = null; 33 34 37 protected static synchronized ObjectLinkDB getInstance () { 38 if (singletonInstance == null) { 39 singletonInstance = new ObjectLinkDB(); 40 } 41 return singletonInstance; 42 } 43 44 46 protected ObjectLink getObjectLink (int linkID) 47 throws JahiaException { 48 49 ObjectLink link = null; 50 Connection dbConn = null; 51 Statement stmt = null; 52 ResultSet rs = null; 53 54 final String selectFields = "id," + 55 "left_oid," + 56 "right_oid," + 57 "type," + 58 "status," + 59 "creation_date," + 60 "creation_user," + 61 "lastmodif_date," + 62 "lastmodif_user"; 63 64 try { 65 String sqlQuery = "SELECT " + 66 selectFields + 67 " FROM jahia_link WHERE id=" + linkID; 68 69 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 70 stmt = dbConn.createStatement(); 71 rs = stmt.executeQuery( sqlQuery); 72 73 if (rs.next()) { 75 int id = rs.getInt("id"); 77 String leftOID = rs.getString("left_oid"); 78 String rightOID = rs.getString("right_oid"); 79 String type = rs.getString("type"); 80 int status = rs.getInt("status"); 81 Timestamp creationDate = rs.getTimestamp("creation_date"); 82 String creationUserKey = rs.getString("creation_user"); 83 Timestamp lastModificationDate = rs.getTimestamp( 84 "lastmodif_date"); 85 String lastModificationUserKey = rs.getString("lastmodif_user"); 86 87 try { 88 link = new ObjectLink(id, ObjectKey.getInstance(leftOID), 89 ObjectKey.getInstance(rightOID), type, 90 status, 91 creationDate, creationUserKey, 92 lastModificationDate, 93 lastModificationUserKey, new HashMap (), 94 new HashMap (), new HashMap ()); 95 } catch (ClassNotFoundException cnfe) { 96 logger.error("Couldn't create instance of an ObjectKey", cnfe); 97 } 98 } 99 100 } catch (SQLException se) { 101 throw new JahiaException("Cannot load object link " + linkID + 102 "from the database", 103 se.getMessage(), 104 JahiaException.DATABASE_ERROR, 105 JahiaException.ERROR_SEVERITY, se); 106 } finally { 107 try { 108 if (stmt != null) 109 stmt.close(); 110 } catch (SQLException ex) { 111 throw new JahiaException("Cannot free resources", 112 "Cannot free resources", 113 JahiaException.DATABASE_ERROR, 114 JahiaException.WARNING_SEVERITY, ex); 115 } 116 } 117 118 return link; 119 } 120 121 protected ArrayList findByLeftObjectKey (ObjectKey leftObjectKey) 122 throws JahiaException { 123 124 ArrayList resultList = new ArrayList (); 125 Connection dbConn = null; 126 PreparedStatement stmt = null; 127 ResultSet rs = null; 128 129 final String selectFields = "id," + 130 "left_oid," + 131 "right_oid," + 132 "type," + 133 "status," + 134 "creation_date," + 135 "creation_user," + 136 "lastmodif_date," + 137 "lastmodif_user"; 138 139 try { 140 String sqlQuery = "SELECT " + 141 selectFields + 142 " FROM jahia_link WHERE left_oid=?"; 143 144 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 145 stmt = dbConn.prepareStatement(sqlQuery); 146 stmt.setString(1, leftObjectKey.toString()); 147 rs = stmt.executeQuery(); 149 150 while (rs.next()) { 151 int id = rs.getInt("id"); 152 String leftOID = rs.getString("left_oid"); 153 String rightOID = rs.getString("right_oid"); 154 String type = rs.getString("type"); 155 int status = rs.getInt("status"); 156 Timestamp creationDate = rs.getTimestamp("creation_date"); 157 158 String creationUserKey = rs.getString("creation_user"); 159 Timestamp lastModificationDate = rs.getTimestamp( 160 "lastmodif_date"); 161 String lastModificationUserKey = rs.getString("lastmodif_user"); 162 163 try { 164 resultList.add(new ObjectLink(id, 165 ObjectKey.getInstance(leftOID), 166 ObjectKey.getInstance(rightOID), 167 type, 168 status, 169 creationDate, creationUserKey, 170 lastModificationDate, 171 lastModificationUserKey, 172 new HashMap (), 173 new HashMap (), new HashMap ())); 174 } catch (ClassNotFoundException cnfe) { 175 logger.error("Couldn't create instance of an ObjectKey", cnfe); 176 } 177 } 178 179 } catch (SQLException se) { 180 throw new JahiaException( 181 "Cannot load object link by left object key " + leftObjectKey + 182 "from the database", 183 se.getMessage(), 184 JahiaException.DATABASE_ERROR, 185 JahiaException.ERROR_SEVERITY, se); 186 } finally { 187 try { 188 if (stmt != null) 189 stmt.close(); 190 } catch (SQLException ex) { 191 throw new JahiaException("Cannot free resources", 192 "Cannot free resources", 193 JahiaException.DATABASE_ERROR, 194 JahiaException.WARNING_SEVERITY, ex); 195 } 196 } 197 198 return resultList; 199 } 200 201 protected ArrayList findByRightObjectKey (ObjectKey rightObjectKey) 202 throws JahiaException { 203 204 ArrayList resultList = new ArrayList (); 205 Connection dbConn = null; 206 PreparedStatement stmt = null; 207 ResultSet rs = null; 208 209 final String selectFields = "id," + 210 "left_oid," + 211 "right_oid," + 212 "type," + 213 "status," + 214 "creation_date," + 215 "creation_user," + 216 "lastmodif_date," + 217 "lastmodif_user"; 218 219 try { 220 String sqlQuery = "SELECT " + 221 selectFields + 222 " FROM jahia_link WHERE right_oid=?"; 223 224 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 225 stmt = dbConn.prepareStatement(sqlQuery); 226 stmt.setString(1, rightObjectKey.toString()); 227 rs = stmt.executeQuery(); 229 230 while (rs.next()) { 231 int id = rs.getInt("id"); 232 String leftOID = rs.getString("left_oid"); 233 String rightOID = rs.getString("right_oid"); 234 String type = rs.getString("type"); 235 int status = rs.getInt("status"); 236 Timestamp creationDate = rs.getTimestamp("creation_date"); 237 String creationUserKey = rs.getString("creation_user"); 238 Timestamp lastModificationDate = rs.getTimestamp( 239 "lastmodif_date"); 240 String lastModificationUserKey = rs.getString("lastmodif_user"); 241 242 try { 243 resultList.add(new ObjectLink(id, 244 ObjectKey.getInstance(leftOID), 245 ObjectKey.getInstance(rightOID), 246 type, 247 status, 248 creationDate, creationUserKey, 249 lastModificationDate, 250 lastModificationUserKey, 251 new HashMap (), 252 new HashMap (), new HashMap ())); 253 } catch (ClassNotFoundException cnfe) { 254 logger.error("Couldn't create instance of an ObjectKey", cnfe); 255 } 256 } 257 258 } catch (SQLException se) { 259 throw new JahiaException( 260 "Cannot load object link by right object key " + rightObjectKey + 261 "from the database", 262 se.getMessage(), 263 JahiaException.DATABASE_ERROR, 264 JahiaException.ERROR_SEVERITY, se); 265 } finally { 266 try { 267 if (stmt != null) 268 stmt.close(); 269 } catch (SQLException ex) { 270 throw new JahiaException("Cannot free resources", 271 "Cannot free resources", 272 JahiaException.DATABASE_ERROR, 273 JahiaException.WARNING_SEVERITY, ex); 274 } 275 } 276 277 return resultList; 278 } 279 280 protected ArrayList findByLeftAndRightObjectKeys (ObjectKey leftObjectKey, 281 ObjectKey rightObjectKey) 282 throws JahiaException { 283 284 ArrayList resultList = new ArrayList (); 285 Connection dbConn = null; 286 PreparedStatement stmt = null; 287 ResultSet rs = null; 288 289 final String selectFields = "id," + 290 "left_oid," + 291 "right_oid," + 292 "type," + 293 "status," + 294 "creation_date," + 295 "creation_user," + 296 "lastmodif_date," + 297 "lastmodif_user"; 298 299 try { 300 String sqlQuery = "SELECT " + 301 selectFields + 302 " FROM jahia_link WHERE left_oid=? AND right_oid=?"; 303 304 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 305 stmt = dbConn.prepareStatement(sqlQuery); 306 stmt.setString(1, leftObjectKey.toString()); 307 stmt.setString(2, rightObjectKey.toString()); 308 rs = stmt.executeQuery(); 310 311 while (rs.next()) { 312 int id = rs.getInt("id"); 313 String leftOID = rs.getString("left_oid"); 314 String rightOID = rs.getString("right_oid"); 315 String type = rs.getString("type"); 316 int status = rs.getInt("status"); 317 Timestamp creationDate = rs.getTimestamp("creation_date"); 318 String creationUserKey = rs.getString("creation_user"); 319 Timestamp lastModificationDate = rs.getTimestamp( 320 "lastmodif_date"); 321 String lastModificationUserKey = rs.getString("lastmodif_user"); 322 323 try { 324 resultList.add(new ObjectLink(id, 325 ObjectKey.getInstance(leftOID), 326 ObjectKey.getInstance(rightOID), 327 type, 328 status, 329 creationDate, creationUserKey, 330 lastModificationDate, 331 lastModificationUserKey, 332 new HashMap (), 333 new HashMap (), new HashMap ())); 334 } catch (ClassNotFoundException cnfe) { 335 logger.error("Couldn't create instance of an ObjectKey", cnfe); 336 } 337 } 338 339 } catch (SQLException se) { 340 throw new JahiaException( 341 "Cannot load object link by right object key " + rightObjectKey + 342 "from the database", 343 se.getMessage(), 344 JahiaException.DATABASE_ERROR, 345 JahiaException.ERROR_SEVERITY, se); 346 } finally { 347 try { 348 if (stmt != null) 349 stmt.close(); 350 } catch (SQLException ex) { 351 throw new JahiaException("Cannot free resources", 352 "Cannot free resources", 353 JahiaException.DATABASE_ERROR, 354 JahiaException.WARNING_SEVERITY, ex); 355 } 356 } 357 358 return resultList; 359 } 360 361 protected ArrayList findByTypeAndLeftAndRightObjectKeys (String type, 362 ObjectKey leftObjectKey, 363 ObjectKey rightObjectKey) 364 throws JahiaException { 365 366 ArrayList resultList = new ArrayList (); 367 Connection dbConn = null; 368 PreparedStatement stmt = null; 369 ResultSet rs = null; 370 371 final String selectFields = "id," + 372 "left_oid," + 373 "right_oid," + 374 "type," + 375 "status," + 376 "creation_date," + 377 "creation_user," + 378 "lastmodif_date," + 379 "lastmodif_user"; 380 381 try { 382 String sqlQuery = "SELECT " + 383 selectFields + 384 " FROM jahia_link WHERE type=? AND left_oid=? AND right_oid=?"; 385 386 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 387 stmt = dbConn.prepareStatement(sqlQuery); 388 stmt.setString(1, type); 389 stmt.setString(2, leftObjectKey.toString()); 390 stmt.setString(3, rightObjectKey.toString()); 391 rs = stmt.executeQuery(); 393 394 while (rs.next()) { 395 int id = rs.getInt("id"); 396 String leftOID = rs.getString("left_oid"); 397 String rightOID = rs.getString("right_oid"); 398 int status = rs.getInt("status"); 399 Timestamp creationDate = rs.getTimestamp("creation_date"); 400 String creationUserKey = rs.getString("creation_user"); 401 Timestamp lastModificationDate = rs.getTimestamp( 402 "lastmodif_date"); 403 String lastModificationUserKey = rs.getString("lastmodif_user"); 404 405 try { 406 resultList.add(new ObjectLink(id, 407 ObjectKey.getInstance(leftOID), 408 ObjectKey.getInstance(rightOID), 409 type, 410 status, 411 creationDate, creationUserKey, 412 lastModificationDate, 413 lastModificationUserKey, 414 new HashMap (), 415 new HashMap (), new HashMap ())); 416 } catch (ClassNotFoundException cnfe) { 417 logger.error("Couldn't create instance of an ObjectKey", 418 cnfe); 419 } 420 } 421 422 } catch (SQLException se) { 423 throw new JahiaException( 424 "Cannot load object link by right object key " + rightObjectKey + 425 "from the database", 426 se.getMessage(), 427 JahiaException.DATABASE_ERROR, 428 JahiaException.ERROR_SEVERITY, se); 429 } finally { 430 try { 431 if (stmt != null) 432 stmt.close(); 433 } catch (SQLException ex) { 434 throw new JahiaException("Cannot free resources", 435 "Cannot free resources", 436 JahiaException.DATABASE_ERROR, 437 JahiaException.WARNING_SEVERITY, ex); 438 } 439 } 440 441 return resultList; 442 } 443 444 protected ArrayList findByTypeAndLeftObjectKey (String type, 445 ObjectKey leftObjectKey) 446 throws JahiaException { 447 448 ArrayList resultList = new ArrayList (); 449 Connection dbConn = null; 450 PreparedStatement stmt = null; 451 ResultSet rs = null; 452 453 final String selectFields = "id," + 454 "left_oid," + 455 "right_oid," + 456 "type," + 457 "status," + 458 "creation_date," + 459 "creation_user," + 460 "lastmodif_date," + 461 "lastmodif_user"; 462 463 try { 464 String sqlQuery = "SELECT " + 465 selectFields + 466 " FROM jahia_link WHERE type=? AND left_oid=?"; 467 468 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 469 stmt = dbConn.prepareStatement(sqlQuery); 470 stmt.setString(1, type); 471 stmt.setString(2, leftObjectKey.toString()); 472 rs = stmt.executeQuery(); 474 475 while (rs.next()) { 476 int id = rs.getInt("id"); 477 String leftOID = rs.getString("left_oid"); 478 String rightOID = rs.getString("right_oid"); 479 int status = rs.getInt("status"); 480 Timestamp creationDate = rs.getTimestamp("creation_date"); 481 String creationUserKey = rs.getString("creation_user"); 482 Timestamp lastModificationDate = rs.getTimestamp( 483 "lastmodif_date"); 484 String lastModificationUserKey = rs.getString("lastmodif_user"); 485 486 try { 487 resultList.add(new ObjectLink(id, 488 ObjectKey.getInstance(leftOID), 489 ObjectKey.getInstance(rightOID), 490 type, 491 status, 492 creationDate, creationUserKey, 493 lastModificationDate, 494 lastModificationUserKey, 495 new HashMap (), 496 new HashMap (), new HashMap ())); 497 } catch (ClassNotFoundException cnfe) { 498 logger.error("Couldn't create instance of an ObjectKey", 499 cnfe); 500 } 501 } 502 503 } catch (SQLException se) { 504 throw new JahiaException( 505 "Cannot load object link by type " + type + 506 " and left object key " + leftObjectKey + 507 "from the database", 508 se.getMessage(), 509 JahiaException.DATABASE_ERROR, 510 JahiaException.ERROR_SEVERITY, se); 511 } finally { 512 try { 513 if (stmt != null) 514 stmt.close(); 515 } catch (SQLException ex) { 516 throw new JahiaException("Cannot free resources", 517 "Cannot free resources", 518 JahiaException.DATABASE_ERROR, 519 JahiaException.WARNING_SEVERITY, ex); 520 } 521 } 522 523 return resultList; 524 } 525 526 protected ArrayList findByTypeAndRightObjectKey (String type, 527 ObjectKey rightObjectKey) 528 throws JahiaException { 529 530 ArrayList resultList = new ArrayList (); 531 Connection dbConn = null; 532 PreparedStatement stmt = null; 533 ResultSet rs = null; 534 535 final String selectFields = "id," + 536 "left_oid," + 537 "right_oid," + 538 "type," + 539 "status," + 540 "creation_date," + 541 "creation_user," + 542 "lastmodif_date," + 543 "lastmodif_user"; 544 545 try { 546 String sqlQuery = "SELECT " + 547 selectFields + 548 " FROM jahia_link WHERE type=? AND right_oid=?"; 549 550 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 551 stmt = dbConn.prepareStatement(sqlQuery); 552 stmt.setString(1, type); 553 stmt.setString(2, rightObjectKey.toString()); 554 rs = stmt.executeQuery(); 556 557 while (rs.next()) { 558 int id = rs.getInt("id"); 559 String leftOID = rs.getString("left_oid"); 560 String rightOID = rs.getString("right_oid"); 561 int status = rs.getInt("status"); 562 Timestamp creationDate = rs.getTimestamp("creation_date"); 563 String creationUserKey = rs.getString("creation_user"); 564 Timestamp lastModificationDate = rs.getTimestamp( 565 "lastmodif_date"); 566 String lastModificationUserKey = rs.getString("lastmodif_user"); 567 568 try { 569 resultList.add(new ObjectLink(id, 570 ObjectKey.getInstance(leftOID), 571 ObjectKey.getInstance(rightOID), 572 type, 573 status, 574 creationDate, creationUserKey, 575 lastModificationDate, 576 lastModificationUserKey, 577 new HashMap (), 578 new HashMap (), new HashMap ())); 579 } catch (ClassNotFoundException cnfe) { 580 logger.error("Couldn't create instance of an ObjectKey", 581 cnfe); 582 } 583 } 584 585 } catch (SQLException se) { 586 throw new JahiaException( 587 "Cannot load object link by type "+type+" and right object key " + rightObjectKey + 588 "from the database", 589 se.getMessage(), 590 JahiaException.DATABASE_ERROR, 591 JahiaException.ERROR_SEVERITY, se); 592 } finally { 593 try { 594 if (stmt != null) 595 stmt.close(); 596 } catch (SQLException ex) { 597 throw new JahiaException("Cannot free resources", 598 "Cannot free resources", 599 JahiaException.DATABASE_ERROR, 600 JahiaException.WARNING_SEVERITY, ex); 601 } 602 } 603 604 return resultList; 605 } 606 607 protected void createObjectLink (ObjectLink link) 608 throws JahiaException { 609 Connection dbConn = null; 610 PreparedStatement stmt = null; 611 ResultSet rs = null; 612 613 int linkID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_link"); 614 link.setID(linkID); 615 616 try { 617 String sqlQuery = "INSERT INTO jahia_link (" + 618 "id," + 619 "left_oid," + 620 "right_oid," + 621 "type," + 622 "status," + 623 "creation_date," + 624 "creation_user," + 625 "lastmodif_date," + 626 "lastmodif_user" + 627 ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 628 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 629 stmt = dbConn.prepareStatement(sqlQuery); 630 stmt.setInt(1, link.getID()); 631 stmt.setString(2, link.getLeftObjectKey().toString()); 632 stmt.setString(3, link.getRightObjectKey().toString()); 633 stmt.setString(4, link.getType()); 634 stmt.setInt(5, link.getStatus()); 635 stmt.setTimestamp(6, new Timestamp (link.getCreationDate().getTime())); 636 stmt.setString(7, link.getCreationUserKey()); 637 stmt.setTimestamp(8, new Timestamp (link.getLastModificationDate().getTime())); 638 stmt.setString(9, link.getLastModificationUserKey()); 639 stmt.execute(); 640 } catch (SQLException se) { 641 throw new JahiaException("Cannot store link in the database", 642 se.getMessage(), 643 JahiaException.DATABASE_ERROR, 644 JahiaException.ERROR_SEVERITY, se); 645 } finally { 646 try { 647 if (stmt != null) 648 stmt.close(); 649 } catch (SQLException ex) { 650 throw new JahiaException("Cannot free resources", 651 "Cannot free resources", 652 JahiaException.DATABASE_ERROR, 653 JahiaException.WARNING_SEVERITY, ex); 654 } 655 } 656 } 657 658 protected void updateObjectLink (ObjectLink link) 659 throws JahiaException { 660 Connection dbConn = null; 661 PreparedStatement stmt = null; 662 ResultSet rs = null; 663 664 try { 665 String sqlQuery = "UPDATE jahia_link SET " + 666 "id=?," + 667 "left_oid=?," + 668 "right_oid=?," + 669 "type=?," + 670 "status=?," + 671 "creation_date=?," + 672 "creation_user=?," + 673 "lastmodif_date=?," + 674 "lastmodif_user=? " + 675 "WHERE id=?"; 676 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 677 stmt = dbConn.prepareStatement(sqlQuery); 678 stmt.setInt(1, link.getID()); 679 stmt.setString(2, link.getLeftObjectKey().toString()); 680 stmt.setString(3, link.getRightObjectKey().toString()); 681 stmt.setString(4, link.getType()); 682 stmt.setInt(5, link.getStatus()); 683 stmt.setTimestamp(6, new Timestamp (link.getCreationDate().getTime())); 684 stmt.setString(7, link.getCreationUserKey()); 685 stmt.setTimestamp(8, new Timestamp (link.getLastModificationDate().getTime())); 686 stmt.setString(9, link.getLastModificationUserKey()); 687 stmt.setInt(10, link.getID()); 688 stmt.executeUpdate(); 689 } catch (SQLException se) { 690 throw new JahiaException("Cannot store link in the database", 691 se.getMessage(), 692 JahiaException.DATABASE_ERROR, 693 JahiaException.ERROR_SEVERITY, se); 694 } finally { 695 try { 696 if (stmt != null) 697 stmt.close(); 698 } catch (SQLException ex) { 699 throw new JahiaException("Cannot free resources", 700 "Cannot free resources", 701 JahiaException.DATABASE_ERROR, 702 JahiaException.WARNING_SEVERITY, ex); 703 } 704 } 705 } 706 707 708 protected void removeObjectLink (int linkID) 709 throws JahiaException { 710 711 Connection dbConn = null; 712 Statement stmt = null; 713 ResultSet rs = null; 714 715 try { 716 String sqlQuery = "DELETE FROM jahia_link WHERE id=" + linkID; 717 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 718 stmt = dbConn.createStatement(); 719 stmt.executeUpdate(sqlQuery); 720 } catch (SQLException se) { 721 throw new JahiaException("Cannot remove link from database", 722 se.getMessage(), 723 JahiaException.DATABASE_ERROR, 724 JahiaException.ERROR_SEVERITY, se); 725 } finally { 726 try { 727 if (stmt != null) 728 stmt.close(); 729 } catch (SQLException ex) { 730 throw new JahiaException("Cannot free resources", 731 "Cannot free resources", 732 JahiaException.DATABASE_ERROR, 733 JahiaException.WARNING_SEVERITY, ex); 734 } 735 } 736 } 737 738 } | Popular Tags |