1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.lang.reflect.Constructor ; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Vector ; 36 37 import org.apache.slide.common.Service; 38 import org.apache.slide.common.ServiceAccessException; 39 import org.apache.slide.common.Uri; 40 import org.apache.slide.content.NodeProperty; 41 import org.apache.slide.content.NodeRevisionContent; 42 import org.apache.slide.content.NodeRevisionDescriptor; 43 import org.apache.slide.content.NodeRevisionDescriptors; 44 import org.apache.slide.content.NodeRevisionNumber; 45 import org.apache.slide.content.RevisionAlreadyExistException; 46 import org.apache.slide.content.RevisionDescriptorNotFoundException; 47 import org.apache.slide.content.RevisionNotFoundException; 48 import org.apache.slide.lock.LockTokenNotFoundException; 49 import org.apache.slide.lock.NodeLock; 50 import org.apache.slide.security.NodePermission; 51 import org.apache.slide.structure.LinkNode; 52 import org.apache.slide.structure.ObjectAlreadyExistsException; 53 import org.apache.slide.structure.ObjectNode; 54 import org.apache.slide.structure.ObjectNotFoundException; 55 import org.apache.slide.util.logger.Logger; 56 57 80 public class OldJDBCAdapter extends AbstractRDBMSAdapter { 81 82 protected static final String LOG_CHANNEL = 83 StandardRDBMSAdapter.class.getName(); 84 86 88 90 protected static final int OBJECTS_URI = 1; 91 protected static final int OBJECTS_CLASS = 2; 92 93 protected static final int CHILDREN_URI = 1; 94 protected static final int CHILDREN_CHILDURI = 2; 95 96 protected static final int LINKS_LINK = 1; 97 protected static final int LINKS_LINKTO = 2; 98 99 101 protected static final int PERMISSIONS_OBJECT = 1; 102 protected static final int PERMISSIONS_REVISION_NUMBER = 2; 103 protected static final int PERMISSIONS_SUBJECT = 3; 104 protected static final int PERMISSIONS_ACTION = 4; 105 protected static final int PERMISSIONS_INHERITABLE = 5; 106 protected static final int PERMISSIONS_NEGATIVE = 6; 107 108 110 protected static final int LOCKS_ID = 1; 111 protected static final int LOCKS_OBJECT = 2; 112 protected static final int LOCKS_SUBJECT = 3; 113 protected static final int LOCKS_TYPE = 4; 114 protected static final int LOCKS_EXPIRATIONDATE = 5; 115 protected static final int LOCKS_INHERITABLE = 6; 116 protected static final int LOCKS_EXCLUSIVE = 7; 117 protected static final int LOCKS_OWNER = 8; 118 119 121 protected static final int REVISIONS_URI = 1; 122 protected static final int REVISIONS_ISVERSIONED = 2; 123 protected static final int REVISIONS_INITIALREVISION = 3; 124 125 protected static final int WORKINGREVISION_URI = 1; 126 protected static final int WORKINGREVISION_BASEREVISION = 2; 127 protected static final int WORKINGREVISION_NUMBER = 3; 128 129 protected static final int LATESTREVISIONS_URI = 1; 130 protected static final int LATESTREVISIONS_BRANCHNAME = 2; 131 protected static final int LATESTREVISIONS_NUMBER = 3; 132 133 protected static final int BRANCHES_URI = 1; 134 protected static final int BRANCHES_NUMBER = 2; 135 protected static final int BRANCHES_CHILDNUMBER = 3; 136 137 protected static final int REVISION_URI = 1; 138 protected static final int REVISION_NUMBER = 2; 139 protected static final int REVISION_BRANCHNAME = 3; 140 141 protected static final int LABEL_URI = 1; 142 protected static final int LABEL_NUMBER = 2; 143 protected static final int LABEL_LABEL = 3; 144 145 protected static final int PROPERTY_URI = 1; 146 protected static final int PROPERTY_NUMBER = 2; 147 protected static final int PROPERTY_NAME = 3; 148 protected static final int PROPERTY_VALUE = 4; 149 protected static final int PROPERTY_NAMESPACE = 5; 150 protected static final int PROPERTY_TYPE = 6; 151 protected static final int PROPERTY_PROTECTED = 7; 152 153 public OldJDBCAdapter(Service service, Logger logger) { 154 super(service, logger); 155 } 156 157 159 166 public ObjectNode retrieveObject(Connection connection, Uri uri) 167 throws ServiceAccessException, ObjectNotFoundException { 168 169 ObjectNode result = null; 170 PreparedStatement statement = null; 171 172 try { 173 174 statement = 175 connection.prepareStatement( 176 "select * from objects where uri= ?"); 177 statement.setString(1, uri.toString()); 178 179 ResultSet res = statement.executeQuery(); 180 181 183 String className; 184 185 if (res.next()) { 186 className = res.getString(OBJECTS_CLASS); 188 } else { 189 throw new ObjectNotFoundException(uri); 191 } 192 193 closeStatement(statement); 194 195 statement = 197 connection.prepareStatement( 198 "select * from children where uri= ?"); 199 statement.setString(1, uri.toString()); 200 res = statement.executeQuery(); 201 202 Vector childrenVector = new Vector (); 203 204 while (res.next()) { 206 childrenVector.addElement(res.getString(CHILDREN_CHILDURI)); 208 } 209 closeStatement(statement); 210 211 statement = 212 connection.prepareStatement( 213 "select * from links where linkto= ?"); 214 statement.setString(1, uri.toString()); 215 res = statement.executeQuery(); 216 217 Vector linksVector = new Vector (); 218 219 while (res.next()) { 221 linksVector.addElement(res.getString(LINKS_LINKTO)); 223 } 224 225 closeStatement(statement); 226 227 if (className.equals("org.apache.slide.structure.LinkNode")) { 228 229 String linkTo = new String (); 230 statement = 231 connection.prepareStatement( 232 "select * from links where link= ?"); 233 statement.setString(1, uri.toString()); 234 res = statement.executeQuery(); 235 236 if (res.next()) 237 linkTo = res.getString(LINKS_LINKTO); 238 239 closeStatement(statement); 240 241 result = 242 new LinkNode( 243 uri.toString(), 244 childrenVector, 245 linksVector, 246 linkTo); 247 248 } else { 249 250 try { 251 Class objclass = Class.forName(className); 252 253 Class [] argClasses = 254 { 255 Class.forName("java.lang.String"), 256 Class.forName("java.util.Vector"), 257 Class.forName("java.util.Vector")}; 258 Object [] arguments = 259 { uri.toString(), childrenVector, linksVector }; 260 261 Constructor constructor = 262 objclass.getConstructor(argClasses); 263 result = (ObjectNode) constructor.newInstance(arguments); 264 } catch (Exception e) { 265 throw createException(e, uri); 267 } 268 269 } 270 271 } catch (SQLException e) { 272 throw createException(e, uri); 273 } finally { 274 closeStatement(statement); 275 } 276 return result; 277 } 278 279 286 public void storeObject(Connection connection, Uri uri, ObjectNode object) 287 throws ServiceAccessException, ObjectNotFoundException { 288 289 PreparedStatement statement = null; 290 291 try { 292 statement = 293 connection.prepareStatement( 294 "select * from objects where uri= ?"); 295 statement.setString(1, uri.toString()); 296 297 ResultSet res = statement.executeQuery(); 298 299 301 if (!res.next()) { 302 throw new ObjectNotFoundException(uri); 303 } 304 305 closeStatement(statement); 306 307 statement = 309 connection.prepareStatement( 310 "delete from children where uri= ?"); 311 statement.setString(1, object.getUri()); 312 statement.execute(); 313 closeStatement(statement); 314 315 statement = null; 316 Enumeration children = object.enumerateChildren(); 317 while (children.hasMoreElements()) { 318 if (statement == null) { 319 statement = 320 connection.prepareStatement( 321 "insert into children values(?, ?)"); 322 } 323 statement.setString(1, object.getUri()); 324 statement.setString(2, (String ) children.nextElement()); 325 statement.execute(); 326 } 327 closeStatement(statement); 328 329 341 342 statement = 344 connection.prepareStatement("delete from links where link= ?"); 345 statement.setString(1, object.getUri()); 346 statement.execute(); 347 closeStatement(statement); 348 349 if (object instanceof LinkNode) { 350 statement = 351 connection.prepareStatement( 352 "insert into links values(?,?)"); 353 statement.setString(1, object.getUri()); 354 statement.setString(2, ((LinkNode) object).getLinkedUri()); 355 statement.execute(); 356 closeStatement(statement); 357 } 358 359 } catch (SQLException e) { 360 throw createException(e, uri); 361 } finally { 362 closeStatement(statement); 363 } 364 365 } 366 367 376 public void createObject(Connection connection, Uri uri, ObjectNode object) 377 throws ServiceAccessException, ObjectAlreadyExistsException { 378 379 PreparedStatement statement = null; 380 try { 381 382 String className = object.getClass().getName(); 383 statement = 384 connection.prepareStatement( 385 "select * from objects where uri= ?"); 386 statement.setString(1, uri.toString()); 387 ResultSet res = statement.executeQuery(); 388 if (res.next()) { 390 throw new ObjectAlreadyExistsException(uri.toString()); 391 } 392 393 closeStatement(statement); 394 statement = 395 connection.prepareStatement("insert into objects values(?,?)"); 396 statement.setString(1, uri.toString()); 397 statement.setString(2, className); 398 statement.execute(); 399 closeStatement(statement); 400 statement = null; 401 Enumeration children = object.enumerateChildren(); 403 while (children.hasMoreElements()) { 404 if (statement == null) { 405 statement = 406 connection.prepareStatement( 407 "insert into children values(?,?)"); 408 } 409 statement.setString(1, uri.toString()); 410 statement.setString(2, (String ) children.nextElement()); 411 statement.execute(); 412 } 413 closeStatement(statement); 414 if (object instanceof LinkNode) { 425 statement = 426 connection.prepareStatement( 427 "insert into links values(?,?)"); 428 statement.setString(1, uri.toString()); 429 statement.setString(2, ((LinkNode) object).getLinkedUri()); 430 statement.execute(); 431 closeStatement(statement); 432 } 433 434 } catch (SQLException e) { 435 throw createException(e, uri); 436 } finally { 437 closeStatement(statement); 438 } 439 440 } 441 442 449 public void removeObject(Connection connection, Uri uri, ObjectNode object) 450 throws ServiceAccessException, ObjectNotFoundException { 451 452 PreparedStatement statement = null; 453 454 try { 455 456 statement = 458 connection.prepareStatement("delete from objects where uri= ?"); 459 statement.setString(1, object.getUri()); 460 statement.execute(); 461 closeStatement(statement); 462 463 statement = 465 connection.prepareStatement("delete from children where uri=?"); 466 statement.setString(1, object.getUri()); 467 statement.execute(); 468 closeStatement(statement); 469 470 475 476 statement = 478 connection.prepareStatement("delete from links where link= ?"); 479 statement.setString(1, object.getUri()); 480 statement.execute(); 481 closeStatement(statement); 482 483 } catch (SQLException e) { 484 throw createException(e, uri); 485 } 486 } 487 488 494 public void grantPermission(Connection connection, Uri uri, NodePermission permission) 495 throws ServiceAccessException { 496 497 PreparedStatement statement = null; 498 499 try { 500 int inheritable = 0; 501 if (permission.isInheritable()) { 502 inheritable = 1; 503 } 504 505 int negative = 0; 506 if (permission.isNegative()) { 507 negative = 1; 508 } 509 510 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 511 String revisionNumberStr = 512 (revisionNumber == null) ? null : revisionNumber.toString(); 513 514 statement = connection.prepareStatement 515 ("insert into permissions values(?,?,?,?,?,?)"); 516 statement.setString(1, permission.getObjectUri()); 517 statement.setString(2, revisionNumberStr); 518 statement.setString(3, permission.getSubjectUri()); 519 statement.setString(4, permission.getActionUri()); 520 statement.setInt(5, inheritable); 521 statement.setInt(6, negative); 522 statement.execute(); 523 } catch (SQLException e) { 524 throw createException(e,uri); 525 } finally { 526 closeStatement(statement); 527 } 528 529 } 530 531 532 538 public void revokePermission(Connection connection,Uri uri, NodePermission permission) 539 throws ServiceAccessException { 540 541 542 543 PreparedStatement statement = null; 544 545 try { 546 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 547 if(revisionNumber != null) { 548 statement = connection.prepareStatement 549 ("delete from permissions where object= ? and subject = ? and action = ? and revisionnumber = ? "); 550 statement.setString(4, revisionNumber.toString()); 551 } 552 else { 553 statement = connection.prepareStatement 554 ("delete from permissions where object = ? and subject = ? and action = ? and revisionnumber is NULL"); 555 } 556 557 statement.setString(1, permission.getObjectUri()); 558 statement.setString(2, permission.getSubjectUri()); 559 statement.setString(3, permission.getActionUri()); 560 561 statement.execute(); 562 } catch (SQLException e) { 563 throw createException(e,uri); 564 } finally { 565 closeStatement(statement); 566 } 567 568 } 569 570 571 577 public void revokePermissions(Connection connection,Uri uri) 578 throws ServiceAccessException { 579 580 PreparedStatement statement = null; 581 582 try { 583 584 statement = connection.prepareStatement 585 ("delete from permissions where object= ?"); 586 statement.setString(1, uri.toString()); 587 statement.execute(); 588 } catch (SQLException e) { 589 throw createException(e,uri); 590 } finally { 591 closeStatement(statement); 592 } 593 594 } 595 596 597 603 public Enumeration enumeratePermissions(Connection connection,Uri uri) 604 throws ServiceAccessException { 605 606 Vector permissionVector = new Vector (); 607 PreparedStatement statement = null; 608 609 try { 610 statement = connection.prepareStatement 611 ("select * from permissions where object= ?"); 612 statement.setString(1, uri.toString()); 613 ResultSet res = statement.executeQuery(); 614 615 while (res.next()) { 616 String object = res.getString(PERMISSIONS_OBJECT); 617 String revision = res.getString(PERMISSIONS_REVISION_NUMBER); 618 String subject = res.getString(PERMISSIONS_SUBJECT); 619 String action = res.getString(PERMISSIONS_ACTION); 620 621 boolean inheritable = false; 622 if (res.getInt(PERMISSIONS_INHERITABLE) == 1) { 623 inheritable = true; 624 } 625 boolean negative = false; 626 if (res.getInt(PERMISSIONS_NEGATIVE) == 1) { 627 negative = true; 628 } 629 NodePermission permission = 630 new NodePermission(object,revision,subject, 631 action,inheritable,negative); 632 permissionVector.addElement(permission); 633 } 634 635 } catch (SQLException e) { 636 throw createException(e,uri); 637 } finally { 638 closeStatement(statement); 639 } 640 641 return permissionVector.elements(); 642 } 643 644 650 public void putLock(Connection connection,Uri uri, NodeLock lock) 651 throws ServiceAccessException { 652 653 PreparedStatement statement = null; 654 655 try { 656 int inheritable = 0; 657 if (lock.isInheritable()) { 658 inheritable = 1; 659 } 660 661 int exclusive = 0; 662 if (lock.isExclusive()) { 663 exclusive = 1; 664 } 665 666 statement = connection.prepareStatement 667 ("insert into locks values(?,?,?,?,?,?,?,?)"); 668 statement.setString(1, lock.getLockId()); 669 statement.setString(2, lock.getObjectUri()); 670 statement.setString(3, lock.getSubjectUri()); 671 statement.setString(4, lock.getTypeUri()); 672 statement.setString 673 (5, String.valueOf(lock.getExpirationDate().getTime())); 674 statement.setInt(6,inheritable); 675 statement.setInt(7, exclusive); 676 statement.setString(8,lock.getOwnerInfo()); 677 statement.execute(); 678 } catch (SQLException e) { 679 throw createException(e,uri); 680 } finally { 681 closeStatement(statement); 682 } 683 684 } 685 686 687 694 public void renewLock(Connection connection,Uri uri, NodeLock lock) 695 throws ServiceAccessException, LockTokenNotFoundException { 696 697 PreparedStatement statement = null; 698 699 try { 700 701 int inheritable = 0; 702 if (lock.isInheritable()) { 703 inheritable = 1; 704 } 705 706 int exclusive = 0; 707 if (lock.isExclusive()) { 708 exclusive = 1; 709 } 710 711 statement = connection.prepareStatement 712 ("delete from locks where id=?"); 713 statement.setString(1, lock.getLockId()); 714 statement.execute(); 715 closeStatement(statement); 716 717 statement = connection.prepareStatement 718 ("insert into locks values(?,?,?,?,?,?,?,?)"); 719 statement.setString(1, lock.getLockId()); 720 statement.setString(2, lock.getObjectUri()); 721 statement.setString(3, lock.getSubjectUri()); 722 statement.setString(4, lock.getTypeUri()); 723 statement.setString 724 (5, String.valueOf(lock.getExpirationDate().getTime())); 725 statement.setInt(6, inheritable); 726 statement.setInt(7, exclusive); 727 statement.setString(8,lock.getOwnerInfo()); 728 statement.execute(); 729 730 } catch (SQLException e) { 731 throw createException(e,uri); 732 } finally { 733 closeStatement(statement); 734 } 735 736 } 737 738 739 746 public void removeLock(Connection connection,Uri uri, NodeLock lock) 747 throws ServiceAccessException, LockTokenNotFoundException { 748 749 Statement statement = null; 750 751 try { 752 753 statement = connection.createStatement(); 754 755 String s = null; 756 757 s = "delete from locks where id='" + lock.getLockId() + "'"; 758 statement.execute(s); 759 760 } catch (SQLException e) { 761 throw createException(e,uri); 762 } finally { 763 closeStatement(statement); 764 } 765 766 } 767 768 769 776 public void killLock(Connection connection,Uri uri, NodeLock lock) 777 throws ServiceAccessException, LockTokenNotFoundException { 778 779 removeLock(connection,uri, lock); 780 781 } 782 783 784 791 public Enumeration enumerateLocks(Connection connection,Uri uri) 792 throws ServiceAccessException { 793 794 Vector lockVector = new Vector (); 795 PreparedStatement statement = null; 796 797 try { 798 799 statement = connection.prepareStatement 800 ("select * from locks where object= ?"); 801 statement.setString(1, uri.toString()); 802 statement.execute(); 803 ResultSet res = statement.getResultSet(); 804 805 while (res.next()) { 806 Date expirationDate = null; 807 try { 808 Long timeValue = new Long (res.getString 809 (LOCKS_EXPIRATIONDATE)); 810 expirationDate = new Date (timeValue.longValue()); 811 } catch (NumberFormatException e) { 812 expirationDate = new Date (); 813 } 814 NodeLock lock = 815 new NodeLock(res.getString(LOCKS_ID), 816 res.getString(LOCKS_OBJECT), 817 res.getString(LOCKS_SUBJECT), 818 res.getString(LOCKS_TYPE), 819 expirationDate, 820 (res.getInt(LOCKS_INHERITABLE) == 1), 821 (res.getInt(LOCKS_EXCLUSIVE) == 1), 822 res.getString(LOCKS_OWNER) 823 ); 824 lockVector.addElement(lock); 825 } 826 827 } catch (SQLException e) { 828 throw createException(e,uri); 829 } finally { 830 closeStatement(statement); 831 } 832 return lockVector.elements(); 833 } 834 835 843 public NodeRevisionDescriptors retrieveRevisionDescriptors(Connection connection,Uri uri) 844 throws ServiceAccessException, RevisionDescriptorNotFoundException { 845 846 NodeRevisionDescriptors revisionDescriptors = null; 847 PreparedStatement statement = null; 848 PreparedStatement statement2 = null; 849 850 try { 851 ResultSet res = null; 852 853 NodeRevisionNumber initialRevision = new NodeRevisionNumber(); 854 Hashtable workingRevisions = new Hashtable (); 855 Hashtable latestRevisionNumbers = new Hashtable (); 856 Hashtable branches = new Hashtable (); 857 boolean isVersioned = false; 858 859 statement = connection.prepareStatement 860 ("select * from revisions where uri= ?"); 861 statement.setString(1, uri.toString()); 862 res = statement.executeQuery(); 863 864 if (res.next()) { 865 int isVersionedInt = res.getInt(REVISIONS_ISVERSIONED); 866 if (isVersionedInt == 1) { 867 isVersioned = true; 868 } 869 } else { 870 throw new RevisionDescriptorNotFoundException(uri.toString()); 871 } 872 873 closeStatement(statement); 874 875 statement = connection.prepareStatement 876 ("select * from workingrevision where uri= ?"); 877 statement.setString(1, uri.toString()); 878 res = statement.executeQuery(); 879 880 while(res.next()) { 881 } 883 884 closeStatement(statement); 885 886 statement = connection.prepareStatement 887 ("select * from latestrevisions where uri=?"); 888 statement.setString(1, uri.toString()); 889 res = statement.executeQuery(); 890 891 while(res.next()) { 892 latestRevisionNumbers 893 .put(res.getString(LATESTREVISIONS_BRANCHNAME), 894 new NodeRevisionNumber 895 (res.getString(LATESTREVISIONS_NUMBER))); 896 } 897 closeStatement(statement); 898 899 statement = connection.prepareStatement 900 ("select * from revision where uri= ?"); 901 statement.setString(1, uri.toString()); 902 res = statement.executeQuery(); 903 904 while(res.next()) { 905 String currentRevisionNumber = res.getString(REVISION_NUMBER); 906 907 if (statement2 == null){ 909 statement2 = connection.prepareStatement 910 ("select * from branches where uri = ? and xnumber = ?"); 911 } 912 statement2.setString(1, uri.toString()); 913 statement2.setString(2, currentRevisionNumber); 914 ResultSet res2 = statement2.executeQuery(); 915 Vector childList = new Vector (); 916 917 while (res2.next()) { 918 childList.addElement(new NodeRevisionNumber 919 (res2.getString(BRANCHES_CHILDNUMBER))); 920 } 921 922 branches.put(new NodeRevisionNumber(currentRevisionNumber), 923 childList); 924 925 res2.close(); 926 } 927 closeStatement(statement2); 928 929 revisionDescriptors = new NodeRevisionDescriptors 930 (uri.toString(), initialRevision, workingRevisions, 931 latestRevisionNumbers, branches, isVersioned); 932 933 } catch (SQLException e) { 934 throw createException(e,uri); 935 } finally { 936 closeStatement(statement); 937 closeStatement(statement2); 938 } 939 return revisionDescriptors; 940 } 941 942 943 950 public void createRevisionDescriptors 951 (Connection connection,Uri uri, NodeRevisionDescriptors revisionDescriptors) 952 throws ServiceAccessException { 953 954 957 PreparedStatement statement = null; 958 959 try { 960 961 963 int isVersioned = 0; 964 if (revisionDescriptors.isVersioned()) { 965 isVersioned = 1; 966 } 967 968 statement = connection.prepareStatement 969 ("insert into revisions values(?,?,?)"); 970 statement.setString(1,uri.toString()); 971 statement.setInt(2, isVersioned); 972 statement.setString 973 (3, revisionDescriptors.getInitialRevision().toString()); 974 statement.execute(); 975 closeStatement(statement); 976 977 980 982 if (revisionDescriptors.getLatestRevision() != null) { 984 statement = connection.prepareStatement 985 ("insert into latestrevisions values(?,?,?)"); 986 statement.setString(1, uri.toString()); 987 statement.setString 988 (2, NodeRevisionDescriptors.MAIN_BRANCH.toString()); 989 statement.setString 990 (3, revisionDescriptors.getLatestRevision().toString()); 991 statement.execute(); 992 closeStatement(statement); 993 } 994 995 998 } catch (SQLException e) { 999 throw createException(e,uri); 1000 } finally { 1001 closeStatement(statement); 1002 } 1003 1004 } 1005 1006 1007 1016 public void storeRevisionDescriptors 1017 (Connection connection,Uri uri, NodeRevisionDescriptors revisionDescriptors) 1018 throws ServiceAccessException, RevisionDescriptorNotFoundException { 1019 1020 removeRevisionDescriptors(connection,uri); 1021 createRevisionDescriptors(connection,uri, revisionDescriptors); 1022 1023 } 1024 1025 1026 1032 public void removeRevisionDescriptors(Connection connection,Uri uri) 1033 throws ServiceAccessException { 1034 1035 PreparedStatement statement = null; 1036 1037 try { 1038 1039 statement = connection.prepareStatement 1040 ("delete from revisions where uri= ?"); 1041 statement.setString(1, uri.toString()); 1042 statement.execute(); 1043 closeStatement(statement); 1044 1045 statement = connection.prepareStatement 1046 ("delete from workingrevision where uri= ?"); 1047 statement.setString(1, uri.toString()); 1048 statement.execute(); 1049 closeStatement(statement); 1050 1051 statement = connection.prepareStatement 1052 ("delete from latestrevisions where uri= ?"); 1053 statement.setString(1, uri.toString()); 1054 statement.execute(); 1055 closeStatement(statement); 1056 1057 statement = connection.prepareStatement 1058 ("delete from branches where uri= ?"); 1059 statement.setString(1, uri.toString()); 1060 statement.execute(); 1061 closeStatement(statement); 1062 1063 } catch (SQLException e) { 1064 throw createException(e,uri); 1065 } finally { 1066 closeStatement(statement); 1067 } 1068 1069 } 1070 1071 1072 1078 public NodeRevisionDescriptor retrieveRevisionDescriptor 1079 (Connection connection,Uri uri, NodeRevisionNumber revisionNumber) 1080 throws ServiceAccessException, RevisionDescriptorNotFoundException { 1081 1082 NodeRevisionDescriptor revisionDescriptor = null; 1083 PreparedStatement statement = null; 1084 1085 if(revisionNumber == null) 1086 throw new RevisionDescriptorNotFoundException(uri.toString()); 1087 1088 try { 1089 1090 ResultSet res = null; 1091 1092 String branchName = null; 1093 Vector labels = new Vector (); 1094 Hashtable properties = new Hashtable (); 1095 1096 1099 statement = connection.prepareStatement 1100 ("select * from revision where uri= ? and xnumber = ?"); 1101 statement.setString(1, uri.toString()); 1102 statement.setString(2, revisionNumber.toString()); 1103 res = statement.executeQuery(); 1104 1105 if (res.next()) { 1106 branchName = res.getString(REVISION_BRANCHNAME); 1107 } else { 1108 throw new RevisionDescriptorNotFoundException(uri.toString()); 1109 } 1110 1111 closeStatement(statement); 1112 1113 1115 statement = connection.prepareStatement 1116 ("select * from label where uri= ? and xnumber = ?"); 1117 statement.setString(1, uri.toString()); 1118 statement.setString(2, revisionNumber.toString()); 1119 res = statement.executeQuery(); 1120 1121 while (res.next()) { 1122 labels.addElement(res.getString(LABEL_LABEL)); 1123 } 1124 1125 closeStatement(statement); 1126 1127 1129 statement = connection.prepareStatement 1130 ("select * from property where uri= ? and xnumber = ?"); 1131 statement.setString(1, uri.toString()); 1132 statement.setString(2, revisionNumber.toString()); 1133 res = statement.executeQuery(); 1134 1135 while (res.next()) { 1136 String propertyName = res.getString(PROPERTY_NAME); 1137 String propertyNamespace = res.getString(PROPERTY_NAMESPACE); 1138 NodeProperty property = 1139 new NodeProperty(propertyName, 1140 res.getString(PROPERTY_VALUE), 1141 propertyNamespace, 1142 res.getString(PROPERTY_TYPE), 1143 (res.getInt(PROPERTY_PROTECTED) == 1)); 1144 properties.put(propertyNamespace + propertyName, property); 1145 } 1146 1147 revisionDescriptor = 1148 new NodeRevisionDescriptor(revisionNumber, branchName, 1149 labels, properties); 1150 1151 } catch (SQLException e) { 1152 throw createException(e,uri); 1153 } finally { 1154 closeStatement(statement); 1155 } 1156 1157 return revisionDescriptor; 1158 } 1159 1160 1161 1168 public void createRevisionDescriptor 1169 (Connection connection,Uri uri, NodeRevisionDescriptor revisionDescriptor) 1170 throws ServiceAccessException { 1171 1172 PreparedStatement statement = null; 1173 1174 try { 1175 1176 statement = connection.prepareStatement 1177 ("insert into revision values(?, ?, ?)"); 1178 statement.setString(1, uri.toString()); 1179 statement.setString 1180 (2, revisionDescriptor.getRevisionNumber().toString()); 1181 statement.setString(3, revisionDescriptor.getBranchName()); 1182 statement.execute(); 1183 closeStatement(statement); 1184 1185 statement = null; 1187 Enumeration labels = revisionDescriptor.enumerateLabels(); 1188 while (labels.hasMoreElements()) { 1189 if (statement == null){ 1190 statement = connection.prepareStatement 1191 ("insert into label values(?,?,?)"); 1192 } 1193 statement.setString(1, uri.toString()); 1194 statement.setString 1195 (2, revisionDescriptor.getRevisionNumber().toString()); 1196 statement.setString(3, (String )labels.nextElement()); 1197 statement.execute(); 1198 } 1199 closeStatement(statement); 1200 1201 statement = null; 1203 Enumeration properties = revisionDescriptor.enumerateProperties(); 1204 while (properties.hasMoreElements()) { 1205 NodeProperty property = 1206 (NodeProperty) properties.nextElement(); 1207 int protectedProperty = 0; 1208 if (property.isProtected()) { 1209 protectedProperty = 1; 1210 } 1211 if (statement == null){ 1212 statement = connection.prepareStatement 1213 ("insert into property values(?,?,?,?,?,?,?)"); 1214 } 1215 statement.setString(1, uri.toString()); 1216 statement.setString 1217 (2, revisionDescriptor.getRevisionNumber().toString()); 1218 statement.setString(3, property.getName()); 1219 statement.setString(4, property.getValue().toString()); 1220 statement.setString(5, property.getNamespace()); 1221 statement.setString(6, property.getType()); 1222 statement.setInt(7, protectedProperty); 1223 statement.execute(); 1224 } 1225 closeStatement(statement); 1226 1227 } catch (SQLException e) { 1228 throw createException(e,uri); 1229 } finally { 1230 closeStatement(statement); 1231 } 1232 1233 } 1234 1235 1236 1245 public void storeRevisionDescriptor 1246 (Connection connection,Uri uri, NodeRevisionDescriptor revisionDescriptor) 1247 throws ServiceAccessException, RevisionDescriptorNotFoundException { 1248 1249 removeRevisionDescriptor(connection,uri, revisionDescriptor.getRevisionNumber()); 1250 createRevisionDescriptor(connection,uri, revisionDescriptor); 1251 } 1252 1253 1254 1261 public void removeRevisionDescriptor(Connection connection,Uri uri, NodeRevisionNumber number) 1262 throws ServiceAccessException { 1263 1264 PreparedStatement statement = null; 1265 1266 try { 1267 1268 statement = connection.prepareStatement 1269 ("delete from revision where uri= ? and xnumber = ?"); 1270 statement.setString(1, uri.toString()); 1271 statement.setString(2, number.toString()); 1272 statement.execute(); 1273 closeStatement(statement); 1274 1275 1277 statement = connection.prepareStatement 1278 ("delete from label where uri= ? and xnumber = ?"); 1279 statement.setString(1, uri.toString()); 1280 statement.setString(2, number.toString()); 1281 statement.execute(); 1282 closeStatement(statement); 1283 1284 1286 statement = connection.prepareStatement 1287 ("delete from property where uri= ? and xnumber = ?"); 1288 statement.setString(1, uri.toString()); 1289 statement.setString(2, number.toString()); 1290 statement.execute(); 1291 1292 } catch (SQLException e) { 1293 throw createException(e,uri); 1294 } finally { 1295 closeStatement(statement); 1296 } 1297 1298 } 1299 1300 1301 protected void closeStatement(Statement statement) { 1302 try { 1303 if (statement != null) { 1304 statement.close(); 1305 } 1306 } catch (SQLException e) { 1307 getLogger().log(e, LOG_CHANNEL, Logger.WARNING); 1308 } 1309 } 1310 1311 protected void close(PreparedStatement statement, ResultSet resultSet) { 1312 try { 1313 if (resultSet != null) { 1314 resultSet.close(); 1315 } 1316 } catch (SQLException e) { 1317 getLogger().log( 1318 this, 1319 e, 1320 OldJDBCAdapter.LOG_CHANNEL, 1321 Logger.WARNING); 1322 } finally { 1323 try { 1324 if (statement != null) { 1325 statement.close(); 1326 } 1327 } catch (SQLException e) { 1328 getLogger().log(e, LOG_CHANNEL, Logger.WARNING); 1329 } 1330 } 1331 } 1332 1333 1336 protected ServiceAccessException createException(Exception e, Uri uri) { 1337 getLogger().log( 1338 "Error on " + uri.toString() + ": " + e.getMessage(), 1339 LOG_CHANNEL, 1340 Logger.ERROR); 1341 return new ServiceAccessException(service, e); 1342 } 1343 1344 1347 protected ServiceAccessException createException(SQLException e, Uri uri) { 1348 e.printStackTrace(); 1349 getLogger().log( 1350 "Error " 1351 + e.getErrorCode() 1352 + "," + e.getSQLState() 1353 + " on " 1354 + uri.toString() 1355 + ": " 1356 + e.getMessage(), 1357 LOG_CHANNEL, 1358 Logger.ERROR); 1359 return new ServiceAccessException(service, e); 1360 } 1361 1362 1365 public NodeRevisionContent retrieveRevisionContent(Connection conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, boolean temporaryConnection) throws ServiceAccessException, RevisionNotFoundException { 1366 throw createException(new UnsupportedOperationException ("ContentStore interface not suported"),uri); 1367 } 1368 1369 1372 public void createRevisionContent(Connection conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionAlreadyExistException { 1373 throw createException(new UnsupportedOperationException ("ContentStore interface not suported"),uri); 1374 1375 } 1376 1379 public void storeRevisionContent(Connection conn, Uri uri, NodeRevisionDescriptor revisionDescriptor, NodeRevisionContent revisionContent) throws ServiceAccessException, RevisionNotFoundException { 1380 throw createException(new UnsupportedOperationException ("ContentStore interface not suported"),uri); 1381 } 1382 1383 1386 public void removeRevisionContent(Connection conn, Uri uri, NodeRevisionDescriptor revisionDescriptor) throws ServiceAccessException { 1387 throw createException(new UnsupportedOperationException ("ContentStore interface not suported"),uri); 1388 } 1389} 1390 | Popular Tags |