Project

General

Profile

« Previous | Next » 

Revision 459

Added by bojilova over 23 years ago

change Assession# generation to use the same db connection

View differences:

DocumentImpl.java
441 441
    }
442 442
  }
443 443

  
444
 /** creates SQL code and inserts new document into DB connection */
445
  private void writeDocumentToDB(String action, String user) 
446
               throws SQLException, Exception {
447
    try {
448
      PreparedStatement pstmt = null;
449

  
450
      if (action.equals("INSERT")) {
451
        //AccessionNumber ac = new AccessionNumber();
452
        //this.docid = ac.generate(docid, "INSERT");
453
        pstmt = conn.prepareStatement(
454
            "INSERT INTO xml_documents " +
455
            "(docid, rootnodeid, docname, doctype, " +
456
            "user_owner, user_updated, date_created, date_updated) " +
457
            "VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate)");
458
        // Bind the values to the query
459
        pstmt.setString(1, this.docid);
460
        pstmt.setLong(2, rootnodeid);
461
        pstmt.setString(3, docname);
462
        pstmt.setString(4, doctype);
463
        pstmt.setString(5, user);
464
        pstmt.setString(6, user);
465
      } else if (action.equals("UPDATE")) {
466

  
467
        // Save the old document entry in a backup table
468
        DocumentImpl.archiveDocRevision( conn, docid, user );
469

  
470
        // Delete index for the old version of docid
471
        // The new index is inserting on the next calls to DBSAXNode
472
        pstmt = conn.prepareStatement(
473
                "DELETE FROM xml_index WHERE docid='" + this.docid + "'");
474
        pstmt.execute();
475
        pstmt.close();
476

  
477
        // Update the new document to reflect the new node tree
478
        pstmt = conn.prepareStatement(
479
            "UPDATE xml_documents " +
480
            "SET rootnodeid = ?, docname = ?, doctype = ?, " +
481
            "user_updated = ?, date_updated = sysdate WHERE docid LIKE ?");
482
        // Bind the values to the query
483
        pstmt.setLong(1, rootnodeid);
484
        pstmt.setString(2, docname);
485
        pstmt.setString(3, doctype);
486
        pstmt.setString(4, user);
487
        pstmt.setString(5, this.docid);
488
      } else {
489
        System.err.println("Action not supported: " + action);
490
      }
491

  
492
      // Do the insertion
493
      pstmt.execute();
494
      pstmt.close();
495

  
496
    } catch (SQLException sqle) {
497
      throw sqle;
498
//    } catch (AccessionNumberException ane) {
499
//      MetaCatUtil.debugMessage("Invalid accession number.");
500
//      MetaCatUtil.debugMessage(ane.getMessage());
501
//      throw ane;
502
    } catch (Exception e) {
503
      throw e;
504
    }
505
  }
506

  
444 507
  /**
508
   * Get the document title
509
   */
510
  public String getTitle() {
511
    return doctitle;
512
  }
513

  
514
  /**
515
   * Set the document title
516
   *
517
   * @param title the new title for the document
518
   */
519
  public void setTitle( String title ) {
520
    this.doctitle = title;
521
    try {
522
      PreparedStatement pstmt;
523
      pstmt = conn.prepareStatement(
524
            "UPDATE xml_documents " +
525
            " SET doctitle = ? " +
526
            "WHERE docid = ?");
527

  
528
      // Bind the values to the query
529
      pstmt.setString(1, doctitle);
530
      pstmt.setString(2, docid);
531

  
532
      // Do the insertion
533
      pstmt.execute();
534
      pstmt.close();
535
    } catch (SQLException e) {
536
      System.out.println(e.getMessage());
537
    }
538
  }
539

  
540
  /**
541
   * Look up the title of the first child element named "title"
542
   * and record it as the document title
543
   */
544
/*   NOT NEEDED ANY MORE
545
  public void setTitleFromChildElement() {
546
    String title = null;
547
    long assigned_id=0;
548
    PreparedStatement pstmt;
549
    try {
550
      pstmt = conn.prepareStatement(
551
              "SELECT nodedata FROM xml_nodes " +
552
              "WHERE nodetype = 'TEXT' " +
553
              "AND rootnodeid = ? " +
554
              "AND parentnodeid IN " +
555
              "  (SELECT nodeid FROM xml_nodes " +
556
              "  WHERE nodename = 'title' " +
557
              "  AND nodetype =  'ELEMENT' " +
558
              "  AND rootnodeid = ? ) " +
559
              "ORDER BY nodeid");
560
*/
561
      // The above query might be slow, and probably will be because
562
      // it gets ALL of the title elements while searching for one
563
      // title in a small subtree but it avoids the problem of using
564
      // Oracle's Hierarchical Query syntax which is not portable --
565
      // the commented out SQL that follows shows an equivalent query
566
      // using Oracle-specific hierarchical query
567
      /*
568
      pstmt = conn.prepareStatement(
569
              "SELECT nodedata FROM xml_nodes " +
570
              "WHERE nodetype = 'TEXT' " +
571
              "AND parentnodeid IN " +
572
              "(SELECT nodeid FROM xml_nodes " +
573
              "WHERE nodename = 'title' " +
574
              "START WITH nodeid = ? " +
575
              "CONNECT BY PRIOR nodeid = parentnodeid)");
576
      */
577
/*
578
      // Bind the values to the query
579
      pstmt.setLong(1, rootnodeid);
580
      pstmt.setLong(2, rootnodeid);
581

  
582
      pstmt.execute();
583
      ResultSet rs = pstmt.getResultSet();
584
      boolean tableHasRows = rs.next();
585
      if (tableHasRows) {
586
        title = rs.getString(1);
587
      }
588
      pstmt.close();
589
    } catch (SQLException e) {
590
      System.out.println("Error getting title: " + e.getMessage());
591
    }
592

  
593
    // assign the new title
594
    this.setTitle(title);
595
  }
596
*/
597

  
598
  /**
445 599
   * Write an XML file to the database, given a filename
446 600
   *
447 601
   * @param conn the JDBC connection to the database
......
470 624
                throws Exception {
471 625

  
472 626
    // Determine if the docid is OK for INSERT or UPDATE
473
    AccessionNumber ac = new AccessionNumber();
627
    AccessionNumber ac = new AccessionNumber(conn);
474 628
    String newdocid = ac.generate(docid, action);
475 629

  
476 630
    if ( action.equals("UPDATE") ) {
......
542 696
                throws Exception {
543 697

  
544 698
    // Determine if the docid is OK for DELETE
545
    AccessionNumber ac = new AccessionNumber();
699
    AccessionNumber ac = new AccessionNumber(conn);
546 700
    String newdocid = ac.generate(docid, "DELETE");
547 701

  
548 702
    // check for 'write' permission for 'user' to delete this document
......
640 794
    }
641 795
  }
642 796

  
643
  /** creates SQL code and inserts new document into DB connection */
644
  private void writeDocumentToDB(String action, String user) 
645
               throws SQLException, Exception {
646
    try {
647
      PreparedStatement pstmt = null;
648

  
649
      if (action.equals("INSERT")) {
650
        //AccessionNumber ac = new AccessionNumber();
651
        //this.docid = ac.generate(docid, "INSERT");
652
        pstmt = conn.prepareStatement(
653
            "INSERT INTO xml_documents " +
654
            "(docid, rootnodeid, docname, doctype, " +
655
            "user_owner, user_updated, date_created, date_updated) " +
656
            "VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate)");
657
        // Bind the values to the query
658
        pstmt.setString(1, this.docid);
659
        pstmt.setLong(2, rootnodeid);
660
        pstmt.setString(3, docname);
661
        pstmt.setString(4, doctype);
662
        pstmt.setString(5, user);
663
        pstmt.setString(6, user);
664
      } else if (action.equals("UPDATE")) {
665

  
666
        // Save the old document entry in a backup table
667
        DocumentImpl.archiveDocRevision( conn, docid, user );
668

  
669
        // Delete index for the old version of docid
670
        // The new index is inserting on the next calls to DBSAXNode
671
        pstmt = conn.prepareStatement(
672
                "DELETE FROM xml_index WHERE docid='" + this.docid + "'");
673
        pstmt.execute();
674
        pstmt.close();
675

  
676
        // Update the new document to reflect the new node tree
677
        pstmt = conn.prepareStatement(
678
            "UPDATE xml_documents " +
679
            "SET rootnodeid = ?, docname = ?, doctype = ?, " +
680
            "user_updated = ?, date_updated = sysdate WHERE docid LIKE ?");
681
        // Bind the values to the query
682
        pstmt.setLong(1, rootnodeid);
683
        pstmt.setString(2, docname);
684
        pstmt.setString(3, doctype);
685
        pstmt.setString(4, user);
686
        pstmt.setString(5, this.docid);
687
      } else {
688
        System.err.println("Action not supported: " + action);
689
      }
690

  
691
      // Do the insertion
692
      pstmt.execute();
693
      pstmt.close();
694

  
695
    } catch (SQLException sqle) {
696
      throw sqle;
697
//    } catch (AccessionNumberException ane) {
698
//      MetaCatUtil.debugMessage("Invalid accession number.");
699
//      MetaCatUtil.debugMessage(ane.getMessage());
700
//      throw ane;
701
    } catch (Exception e) {
702
      throw e;
703
    }
704
  }
705

  
706 797
  /**
707
   * Get the document title
708
   */
709
  public String getTitle() {
710
    return doctitle;
711
  }
712

  
713
  /**
714
   * Set the document title
715
   *
716
   * @param title the new title for the document
717
   */
718
  public void setTitle( String title ) {
719
    this.doctitle = title;
720
    try {
721
      PreparedStatement pstmt;
722
      pstmt = conn.prepareStatement(
723
            "UPDATE xml_documents " +
724
            " SET doctitle = ? " +
725
            "WHERE docid = ?");
726

  
727
      // Bind the values to the query
728
      pstmt.setString(1, doctitle);
729
      pstmt.setString(2, docid);
730

  
731
      // Do the insertion
732
      pstmt.execute();
733
      pstmt.close();
734
    } catch (SQLException e) {
735
      System.out.println(e.getMessage());
736
    }
737
  }
738

  
739
  /**
740
   * Look up the title of the first child element named "title"
741
   * and record it as the document title
742
   */
743
  public void setTitleFromChildElement() {
744
    String title = null;
745
    long assigned_id=0;
746
    PreparedStatement pstmt;
747
    try {
748
      pstmt = conn.prepareStatement(
749
              "SELECT nodedata FROM xml_nodes " +
750
              "WHERE nodetype = 'TEXT' " +
751
              "AND rootnodeid = ? " +
752
              "AND parentnodeid IN " +
753
              "  (SELECT nodeid FROM xml_nodes " +
754
              "  WHERE nodename = 'title' " +
755
              "  AND nodetype =  'ELEMENT' " +
756
              "  AND rootnodeid = ? ) " +
757
              "ORDER BY nodeid");
758

  
759
      // The above query might be slow, and probably will be because
760
      // it gets ALL of the title elements while searching for one
761
      // title in a small subtree but it avoids the problem of using
762
      // Oracle's Hierarchical Query syntax which is not portable --
763
      // the commented out SQL that follows shows an equivalent query
764
      // using Oracle-specific hierarchical query
765
      /*
766
      pstmt = conn.prepareStatement(
767
              "SELECT nodedata FROM xml_nodes " +
768
              "WHERE nodetype = 'TEXT' " +
769
              "AND parentnodeid IN " +
770
              "(SELECT nodeid FROM xml_nodes " +
771
              "WHERE nodename = 'title' " +
772
              "START WITH nodeid = ? " +
773
              "CONNECT BY PRIOR nodeid = parentnodeid)");
774
      */
775

  
776
      // Bind the values to the query
777
      pstmt.setLong(1, rootnodeid);
778
      pstmt.setLong(2, rootnodeid);
779

  
780
      pstmt.execute();
781
      ResultSet rs = pstmt.getResultSet();
782
      boolean tableHasRows = rs.next();
783
      if (tableHasRows) {
784
        title = rs.getString(1);
785
      }
786
      pstmt.close();
787
    } catch (SQLException e) {
788
      System.out.println("Error getting title: " + e.getMessage());
789
    }
790

  
791
    // assign the new title
792
    this.setTitle(title);
793
  }
794

  
795
  /** Save a document entry in the xml_revisions table */
796
  private static void archiveDocRevision(Connection conn, String docid,
797
                                         String user) throws SQLException {
798
    // create a record in xml_revisions table 
799
    // for that document as selected from xml_documents
800
    PreparedStatement pstmt = conn.prepareStatement(
801
      "INSERT INTO xml_revisions " +
802
        "(revisionid, docid, rootnodeid, docname, doctype, doctitle, " +
803
        "user_owner, user_updated, date_created, date_updated) " +
804
      "SELECT null, ?, rootnodeid, docname, doctype, doctitle," + 
805
        "user_owner, ?, sysdate, sysdate "+
806
      "FROM xml_documents " +
807
      "WHERE docid = ?");
808
    // Bind the values to the query and execute it
809
    pstmt.setString(1, docid);
810
    pstmt.setString(2, user);
811
    pstmt.setString(3, docid);
812
    pstmt.execute();
813
    pstmt.close();
814

  
815
  }
816

  
817
  /**
818 798
   * Set up the parser handlers for writing the document to the database
819 799
   */
820 800
  private static XMLReader initializeParser(Connection conn,
......
855 835
    return parser;
856 836
  }
857 837

  
838
  /** Save a document entry in the xml_revisions table */
839
  private static void archiveDocRevision(Connection conn, String docid,
840
                                         String user) throws SQLException {
841
    // create a record in xml_revisions table 
842
    // for that document as selected from xml_documents
843
    PreparedStatement pstmt = conn.prepareStatement(
844
      "INSERT INTO xml_revisions " +
845
        "(revisionid, docid, rootnodeid, docname, doctype, doctitle, " +
846
        "user_owner, user_updated, date_created, date_updated) " +
847
      "SELECT null, ?, rootnodeid, docname, doctype, doctitle," + 
848
        "user_owner, ?, sysdate, sysdate "+
849
      "FROM xml_documents " +
850
      "WHERE docid = ?");
851
    // Bind the values to the query and execute it
852
    pstmt.setString(1, docid);
853
    pstmt.setString(2, user);
854
    pstmt.setString(3, docid);
855
    pstmt.execute();
856
    pstmt.close();
857

  
858
  }
859

  
858 860
  /**
859 861
   * the main routine used to test the DBWriter utility.
860 862
   * <p>

Also available in: Unified diff