Project

General

Profile

« Previous | Next » 

Revision 459

Added by bojilova over 24 years ago

change Assession# generation to use the same db connection

View differences:

src/edu/ucsb/nceas/metacat/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>
src/edu/ucsb/nceas/metacat/AccessionNumber.java
18 18
import java.sql.*;
19 19

  
20 20
/**
21
 * A class that generates an Accession Number and will check a submitted
22
 * accession number for uniqueness and register it into the db connection
21
 * (on insert of XML document)
22
 * Generates a unique Accession Number or if provided check it 
23
 * for uniqueness and register it into the db connection 
24
 * (on update or delete of XML document)
25
 * Check for existance of provided Accession Number
26
 * 
23 27
 */
24 28
public class AccessionNumber  {
29
  
30
  private Connection conn = null;
31
  private String defaultGlobalName = null;
32
  private String sep = null;
25 33
    
26
    /**
27
     * Get an accession number from the user, check it for uniqueness 
28
     * and register it into new db connection. If no accession number is
29
     * provided by the user, generate one from the database and return it.
30
     *
31
     * @param accNumber - accession # if provided or null if not
32
     * @param action - INSERT, UPDATE or DELETE.
33
     * When "INSERT" and accession # provided is not unique, get next one.
34
     * If it is unique, use it.
35
     * When "INSERT" and accession # is null, get a new one.
36
     * When "UPDATE", accession # is required. 
37
     * When "DELETE", accession # is required. 
38
     */
39
    public String generate (String accNumber, String action) 
40
        throws AccessionNumberException, SQLException, ClassNotFoundException
41
    {
34
  /** 
35
   * Construct an AccessionNumber 
36
   */
37
  public AccessionNumber () 
38
         throws SQLException, ClassNotFoundException  {
42 39
        
43
        String globalName = null;
44
        String localId = null;
40
    MetaCatUtil util = new MetaCatUtil();
41

  
42
    this.defaultGlobalName = util.getOption("defaultGlobalName");
43
    this.sep = util.getOption("accNumberSeparator");
44

  
45
    // Open a connection to the database
46
    this.conn = util.openDBConnection();
47
  }  
48

  
49
  /** 
50
   * Construct an AccessionNumber
51
   *
52
   * @param conn the db connection to read from and write Accession# to
53
   */
54
  public AccessionNumber (Connection conn) {
45 55
        
46
        // Open a connection to the database
47
        MetaCatUtil   util = new MetaCatUtil();
56
    MetaCatUtil util = new MetaCatUtil();
48 57

  
49
        String defaultGlobalName = util.getOption("defaultGlobalName");
50
        String sep = util.getOption("accNumberSeparator");
58
    this.defaultGlobalName = util.getOption("defaultGlobalName");
59
    this.sep = util.getOption("accNumberSeparator");
60
    this.conn = conn;
51 61

  
52
        try {
53
            // Open a new connection to the database
54
            Connection conn = util.openDBConnection();
55
            conn.setAutoCommit(true);
62
  }
56 63

  
57
            // split the acc # in 2 parts - global name & local id
58
            if ( accNumber != null ) {
59
                globalName = getGlobalName(accNumber, sep);
60
                localId = getLocalId(accNumber, sep);
61
            }    
64
  /**
65
   * Get an Accession Number, check it for uniqueness and 
66
   * register it into db connection. If no Accession Number is
67
   * provided, generate one from the database and return it.
68
   *
69
   * @param accNumber - Accession # if provided or null if not
70
   * @param action - INSERT, UPDATE or DELETE.
71
   * When "INSERT" and accession # provided is not unique, get new one.
72
   * If it is unique, use it.
73
   * When "INSERT" and accession # is null, get a new one.
74
   * When "UPDATE", accession # is required and it is checked for existance.
75
   * When "DELETE", accession # is required and it is checked for existance.
76
   */
77
  public String generate (String accNumber, String action) 
78
         throws AccessionNumberException, SQLException   {
79
        
80
    String globalName = null;
81
    String localId = null;
62 82

  
63
            // register unique acc #
64
            if ( action.equals("INSERT")) {
65
                if ( accNumber == null )
66
                  return put(conn, defaultGlobalName, null, sep);
67
                else
68
                  return put(conn, globalName, localId, sep);
69
            } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
70
                if ( accNumber == null ) {
71
                  throw (new AccessionNumberException("Accession number is " +
72
                         "required."));
73
                } else if (!accNumberIsCurrent(conn, accNumber)) {
74
                  throw (new AccessionNumberException("Document " +
75
                         "not found for accession #: " + accNumber));
76
                } else {
77
                  return (globalName + sep + localId);
78
                }
79
            }
83
    try {
84

  
85
      conn.setAutoCommit(true);
86

  
87
      // split the acc # in 2 parts - global name & local id
88
      if ( accNumber != null ) {
89
        globalName = getGlobalName(accNumber, sep);
90
        localId = getLocalId(accNumber, sep);
91
      }    
92

  
93
      // register unique acc #
94
      if ( action.equals("INSERT")) {
95
        if ( accNumber == null )
96
          return put(defaultGlobalName, null, sep);
97
        else
98
          return put(globalName, localId, sep);
99
      } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
100
        if ( accNumber == null ) {
101
          throw (new AccessionNumberException("Accession number is " +
102
          "required."));
103
        } else if (!accNumberIsCurrent(accNumber)) {
104
          throw (new AccessionNumberException("Document " +
105
          "not found for accession #: " + accNumber));
106
        } else {
107
          return (globalName + sep + localId);
108
        }
109
      }
80 110
 
81
            conn.close();        
111
      //conn.close();        
82 112

  
83
        } catch (StringIndexOutOfBoundsException siobe) {
84
            MetaCatUtil.debugMessage(
85
                       "Error on AccessionNumber.generate(): " + 
86
                       siobe.getMessage());
87
            throw (new AccessionNumberException("Accession number invalid, " +
88
                   "expecting character \'" + sep + "'."));
89
        } catch (SQLException e) {
90
            System.err.println(
91
                       "Error on AccessionNumber.genAccessionNumber(): " + 
92
                       e.getMessage());
93
            throw e;
94
        }    
113
    } catch (StringIndexOutOfBoundsException siobe) {
114
      MetaCatUtil.debugMessage(
115
                 "Error on AccessionNumber.generate(): " + 
116
                  siobe.getMessage());
117
      throw (new AccessionNumberException("Accession number invalid, " +
118
                 "expecting character \'" + sep + "'."));
119
    } catch (SQLException e) {
120
      throw new SQLException(
121
                 "Error on AccessionNumber.generate(accNumber, action): " +
122
                  e.getMessage());
123
    }    
95 124
        
96
        throw (new AccessionNumberException("Fatal Error in " +
97
               "accession number generation: "));
98
    }    
125
    throw (new AccessionNumberException("Fatal Error in " +
126
           "accession number generation: "));
127
  }    
99 128

  
100
    /** put unique accession # into db connection */
101
    private String put (Connection conn, String globalName, 
102
                String localId, String sep) 
129
  /** put unique accession # into db connection */
130
  private String put (String globalName, String localId, String sep) 
103 131
                throws SQLException
104
    {
132
  {
105 133
        
106
        Integer l = null;
107
        try {
108
            if ( localId == null ) 
109
                l = new Integer(get(conn, globalName) + 1); 
110
            else if ( accNumberUsed(conn, globalName, localId) )
111
                l = new Integer(get(conn, globalName) + 1); 
112
            else
113
                l = new Integer(localId); 
134
    Integer l = null;
135
    try {
136
      if ( localId == null ) 
137
         l = new Integer(get(globalName) + 1); 
138
      else if ( accNumberUsed(globalName, localId) )
139
         l = new Integer(get(globalName) + 1); 
140
      else
141
         l = new Integer(localId); 
114 142

  
115
            // insert globalName & l
116
            PreparedStatement pstmt;
117
            pstmt = conn.prepareStatement(
118
                    "INSERT INTO xml_acc_numbers (global_name, local_id) " + 
119
                    "VALUES (?, ?)");
120
            pstmt.setString(1,globalName);
121
            pstmt.setString(2,l.toString());
122
            pstmt.execute();
143
     // insert globalName & l
144
     PreparedStatement pstmt;
145
     pstmt = conn.prepareStatement(
146
             "INSERT INTO xml_acc_numbers (global_name, local_id) " + 
147
             "VALUES (?, ?)");
148
     pstmt.setString(1,globalName);
149
     pstmt.setString(2,l.toString());
150
     pstmt.execute();
123 151
            
124
            pstmt.close();
152
     pstmt.close();
125 153
            
126
        } catch (SQLException e) {
127
            System.err.println(
128
                   "Error on AccessionNumber.put(conn, globalName, localId): " 
129
                   + e.getMessage());
130
            throw e;
131
        }    
132
        return globalName + sep + l;
133
    }
154
    } catch (SQLException e) {
155
      throw new SQLException(
156
            "Error on AccessionNumber.put(globalName, localId, sep): " 
157
            + e.getMessage());
158
    }    
134 159

  
135
    /** check for existence of Accesssion Number xml_acc_numbers table */
136
    private boolean accNumberUsed(Connection conn, String globalName, 
137
                String localId) throws SQLException {
160
    return globalName + sep + l;
161
  }
162

  
163
  /** check for existence of Accesssion Number xml_acc_numbers table */
164
  private boolean accNumberUsed(String globalName, String localId)
165
                      throws SQLException {
138 166
        
139
      boolean hasAccNumber = false;
167
    boolean hasAccNumber = false;
140 168
        
141
      try {
142
        PreparedStatement pstmt;
143
        pstmt = conn.prepareStatement(
169
    try {
170
      PreparedStatement pstmt;
171
      pstmt = conn.prepareStatement(
144 172
                "SELECT 'x' FROM xml_acc_numbers " + 
145 173
                "WHERE global_name LIKE ? AND local_id = ?");
146
        pstmt.setString(1,globalName);
147
        pstmt.setString(2,localId);
148
        pstmt.execute();
149
        ResultSet rs = pstmt.getResultSet();
150
        hasAccNumber = rs.next();
151
        pstmt.close();
174
      pstmt.setString(1,globalName);
175
      pstmt.setString(2,localId);
176
      pstmt.execute();
177
      ResultSet rs = pstmt.getResultSet();
178
      hasAccNumber = rs.next();
179
      pstmt.close();
152 180
            
153
      } catch (SQLException e) {
154
        System.err.println("Error on AccessionNumber.unique(globalName, " +
155
                           "localId): " + e.getMessage());
156
        throw e;
157
      }    
181
    } catch (SQLException e) {
182
      throw new SQLException(
183
                "Error on AccessionNumber.accNumberUsed(globalName, " +
184
                  "localId): " + e.getMessage());
185
    }    
158 186
        
159
      return hasAccNumber;
160
    }    
187
    return hasAccNumber;
188
  }    
161 189
    
162
    /** check for existence of Accesssion Number in xml_documents table */
163
    private boolean accNumberIsCurrent(
164
                Connection conn, String accNumber) throws SQLException {
190
  /** check for existence of Accesssion Number in xml_documents table */
191
  private boolean accNumberIsCurrent(String accNumber) throws SQLException {
165 192
        
166
      boolean hasCurrentAccNumber = false;
193
    boolean hasCurrentAccNumber = false;
167 194
        
168
      try {
169
        PreparedStatement pstmt;
170
        pstmt = conn.prepareStatement(
195
    try {
196
      PreparedStatement pstmt;
197
      pstmt = conn.prepareStatement(
171 198
                "SELECT 'x' FROM xml_documents " + 
172 199
                "WHERE docid LIKE ?");
173
        pstmt.setString(1, accNumber);
174
        pstmt.execute();
175
        ResultSet rs = pstmt.getResultSet();
176
        hasCurrentAccNumber = rs.next();
177
        pstmt.close();
200
      pstmt.setString(1, accNumber);
201
      pstmt.execute();
202
      ResultSet rs = pstmt.getResultSet();
203
      hasCurrentAccNumber = rs.next();
204
      pstmt.close();
178 205
            
179
      } catch (SQLException e) {
180
        System.err.println(
181
          "Error on AccessionNumber.accNumberIsCurrent(globalName, " +
182
          "localId): " + e.getMessage());
183
        throw e;
184
      }    
185
      return hasCurrentAccNumber;
206
    } catch (SQLException e) {
207
      throw new SQLException(
208
          "Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
209
          e.getMessage());
186 210
    }    
211
  
212
    return hasCurrentAccNumber;
213
  }    
187 214

  
188
    /** get the last in order local ID by a given global name */
189
    private int get (Connection conn, String globalName) 
190
                throws SQLException
191
    {
192
        try {
193
            PreparedStatement pstmt;
194
            pstmt = conn.prepareStatement(
195
                    "SELECT max(local_id) FROM xml_acc_numbers " + 
196
                    "WHERE global_name LIKE ?");
197
            pstmt.setString(1,globalName);
198
            pstmt.execute();
199
            ResultSet rs = pstmt.getResultSet();
200
            boolean hasLocalId = rs.next();
215
  /** get the last in order local ID by a given global name */
216
  private int get (String globalName) throws SQLException  {
217
        
218
    try {
219
      PreparedStatement pstmt;
220
      pstmt = conn.prepareStatement(
221
              "SELECT max(local_id) FROM xml_acc_numbers " + 
222
              "WHERE global_name LIKE ?");
223
      pstmt.setString(1,globalName);
224
      pstmt.execute();
225
      ResultSet rs = pstmt.getResultSet();
226
      boolean hasLocalId = rs.next();
201 227

  
202
            if (hasLocalId)
203
                return rs.getInt(1);
228
      if (hasLocalId) {
229
        return rs.getInt(1);
230
      }
204 231

  
205
            pstmt.close();
206
        } catch (SQLException e) {
207
            System.err.println(
208
                   "Error on AccessionNumber.get(): " + e.getMessage());
209
            throw e;
210
        }    
232
      pstmt.close();
233
    } catch (SQLException e) {
234
      throw new SQLException(
235
            "Error on AccessionNumber.get(globalName): " + e.getMessage());
236
    }    
211 237
        
212
        return 0;
213
    }
238
    return 0;
239
  }
214 240

  
215 241
    
216
    // get the global part of the accession number
217
    private String getGlobalName (String accNumber, String sep) 
218
        throws StringIndexOutOfBoundsException {
242
  // get the global part of the accession number
243
  private String getGlobalName (String accNumber, String sep) 
244
          throws StringIndexOutOfBoundsException {
219 245
        
220
        return accNumber.substring(0, accNumber.lastIndexOf(sep));
221
    }    
246
    return accNumber.substring(0, accNumber.lastIndexOf(sep));
247
  }    
222 248

  
223
    // get the local part of the accession number
224
    private String getLocalId (String accNumber, String sep)
225
        throws StringIndexOutOfBoundsException {
249
  // get the local part of the accession number
250
  private String getLocalId (String accNumber, String sep)
251
          throws StringIndexOutOfBoundsException {
226 252

  
227
        return accNumber.substring(accNumber.lastIndexOf(sep)+1);
228
    }    
253
    return accNumber.substring(accNumber.lastIndexOf(sep)+1);
254
  }    
229 255
}
230 256

  
231 257
/**
232 258
 * '$Log$
259
 * 'Revision 1.12  2000/08/30 18:19:41  bojilova
260
 * 'cleared static methods in AccessionNumber classes for fixing bug found
261
 * 'when multiple requests to the servlet at a time.
262
 * '
233 263
 * 'Revision 1.11  2000/08/14 20:53:33  jones
234 264
 * 'Added "release" keyword to all metacat source files so that the release
235 265
 * 'number will be evident in software distributions.

Also available in: Unified diff