Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *    Release: @release@
9
 *
10
 *   '$Author: bojilova $'
11
 *     '$Date: 2000-12-14 15:07:30 -0800 (Thu, 14 Dec 2000) $'
12
 * '$Revision: 618 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.net.*;
18
import java.sql.*;
19

    
20
/**
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
 * 
27
 */
28
public class AccessionNumber  {
29
  
30
  private Connection conn = null;
31
  private String sitecode = null;
32
  private String sep = null;
33
    
34
  /** 
35
   * Construct an AccessionNumber 
36
   */
37
  public AccessionNumber () 
38
         throws SQLException, ClassNotFoundException  {
39
        
40
    MetaCatUtil util = new MetaCatUtil();
41

    
42
    this.sitecode = util.getOption("sitecode");
43
    this.sep = util.getOption("accNumSeparator");
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) {
55
        
56
    MetaCatUtil util = new MetaCatUtil();
57

    
58
    this.sitecode = util.getOption("sitecode");
59
    this.sep = util.getOption("accNumSeparator");
60
    this.conn = conn;
61

    
62
  }
63

    
64
  /**
65
   * Generate an Accession Number of form <sidecode>.<createdate>
66
   */
67
  public String generate(String accNumber, String action) 
68
                throws AccessionNumberException, SQLException 
69
  {
70
    try {
71
      // INSERT
72
      if ( action.equals("INSERT")) {
73

    
74
        // get a new Accession#
75
        if ( accNumber == null ) {
76
          String sitecode = getSitecode();
77
          String uniqueid = getUniqueID();
78

    
79
          return sitecode + sep + uniqueid;
80

    
81
        // Accession# is not used; return it
82
        } else if ( !accNumberUsed(accNumber) ) {
83
          return accNumber;
84

    
85
        // Accession# is used; throw an exception to prevent the insertion
86
        } else {
87
          throw new AccessionNumberException
88
                    ("Accession number " + accNumber + " is already in use.");
89
        }
90

    
91
      // UPDATE or DELETE
92
      } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
93

    
94
        // Accession# is not provided; throw an exception to prevent the action
95
        if ( accNumber == null ) {
96
          throw new AccessionNumberException("Accession number is required.");
97

    
98
        // Accession# is not current (not in xml_documents); throw an exception
99
        } else if ( !accNumberIsCurrent(accNumber) ) {
100
          throw new AccessionNumberException
101
          ("Document not found for Accession number " + accNumber);
102

    
103
        // Accession# is current (present in xml_documents); return it
104
        } else {
105
          return accNumber;
106
        }
107
      }
108
 
109
    } catch (SQLException e) {
110
      throw new SQLException
111
      ("Error on AccessionNumber.generate(accNumber, action): "+e.getMessage());
112
    }    
113
 
114
    // never comes here
115
    throw new
116
    AccessionNumberException("Fatal Error in accession number generation.");
117
  }
118
  
119
  // get local sitecode
120
  private String getSitecode()
121
  {
122
    return this.sitecode;
123
  }
124

    
125
  // get Unique ID from DB sequence
126
  private String getUniqueID () throws SQLException
127
  {
128
    String uniqueid;
129
    
130
    try {
131
      PreparedStatement pstmt;
132
      pstmt = conn.prepareStatement
133
              ("SELECT accnum_uniqueid_seq.nextval FROM dual");
134
      pstmt.execute();
135

    
136
      ResultSet rs = pstmt.getResultSet();
137
      boolean hasRow = rs.next();
138
      uniqueid = rs.getString(1);
139
      pstmt.close();
140
      
141
    } catch (SQLException e) {
142
      throw new 
143
      SQLException("Error on AccessionNumber.getUniqueID(): "+e.getMessage());
144
    }
145

    
146
    return uniqueid;
147
  }
148

    
149
  /** check for existence of Accesssion Number xml_acc_numbers table */
150
  public boolean accNumberUsed ( String accNumber )
151
                  throws SQLException {
152
        
153
    boolean hasAccNumber = false;
154
        
155
    try {
156
      PreparedStatement pstmt;
157
      pstmt = conn.prepareStatement(
158
                "SELECT 'x' FROM xml_documents " + 
159
                "WHERE docid LIKE ? " +
160
                "UNION " +
161
                "SELECT 'x' FROM xml_revisions " +
162
                "WHERE docid LIKE ?");
163
      pstmt.setString(1,accNumber);
164
      pstmt.setString(2,accNumber);
165
      pstmt.execute();
166
      ResultSet rs = pstmt.getResultSet();
167
      hasAccNumber = rs.next();
168
      pstmt.close();
169
            
170
    } catch (SQLException e) {
171
      throw new SQLException
172
      ("Error on AccessionNumber.accNumberUsed(accNumber): " + e.getMessage());
173
    }    
174
        
175
    return hasAccNumber;
176
  }    
177
    
178
  /** check for existence of Accesssion Number in xml_documents table */
179
  private boolean accNumberIsCurrent(String accNumber) throws SQLException {
180
        
181
    boolean hasCurrentAccNumber = false;
182
        
183
    try {
184
      PreparedStatement pstmt;
185
      pstmt = conn.prepareStatement(
186
                "SELECT 'x' FROM xml_documents " + 
187
                "WHERE docid LIKE ?");
188
      pstmt.setString(1, accNumber);
189
      pstmt.execute();
190
      ResultSet rs = pstmt.getResultSet();
191
      hasCurrentAccNumber = rs.next();
192
      pstmt.close();
193
            
194
    } catch (SQLException e) {
195
      throw new SQLException(
196
          "Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
197
          e.getMessage());
198
    }    
199
  
200
    return hasCurrentAccNumber;
201
  }    
202

    
203
}
204

    
205
/**
206
 * '$Log$
207
 * 'Revision 1.16  2000/12/06 17:59:03  berkley
208
 * 'made replication on insert or update us.  Also made a method in AccessionNumber public so that you can tell if an accession number has already been used.e place be
209
 * '
210
 * 'Revision 1.15  2000/11/30 22:41:32  bojilova
211
 * 'change the generation of Accession# in the form of <sidecode>.<createdate>
212
 * '
213
 * 'Revision 1.14  2000/09/28 18:05:46  bojilova
214
 * 'Changed to prevent the insertion if the provided Accession# is in use as Dan suggested.
215
 * '
216
 * 'Revision 1.13  2000/09/20 20:15:58  bojilova
217
 * 'change Assession# generation to use the same db connection
218
 * '
219
 * 'Revision 1.12  2000/08/30 18:19:41  bojilova
220
 * 'cleared static methods in AccessionNumber classes for fixing bug found
221
 * 'when multiple requests to the servlet at a time.
222
 * '
223
 * 'Revision 1.11  2000/08/14 20:53:33  jones
224
 * 'Added "release" keyword to all metacat source files so that the release
225
 * 'number will be evident in software distributions.
226
 * '
227
 * 'Revision 1.10  2000/06/27 04:31:07  jones
228
 * 'Fixed bugs associated with the new UPDATE and DELETE functions of
229
 * 'DBWriter.  There were problematic interactions between some static
230
 * 'variables used in DBEntityResolver and the way in which the
231
 * 'Servlet objects are re-used across multiple client invocations.
232
 * '
233
 * 'Generally cleaned up error reporting.  Now all errors and success
234
 * 'results are reported as XML documents from MetaCatServlet.  Need
235
 * 'to make the command line tools do the same.
236
 * '
237
 * 'Revision 1.9  2000/06/26 10:35:04  jones
238
 * 'Merged in substantial changes to DBWriter and associated classes and to
239
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
240
 * 'functions.  The command line tools and the parameters for the
241
 * 'servlet have changed substantially.
242
 * '
243
 * 'Revision 1.8.2.5  2000/06/26 08:38:01  jones
244
 * 'Added DELETE feature to DBWriter.  Now takes an action "DELETE" and a
245
 * 'docid and will move the record from the xml_documents table to the
246
 * 'xml_revisions table.
247
 * 'Modified option parsing to support option symbols on command line.
248
 * '
249
 * 'Revision 1.8.2.4  2000/06/25 23:11:40  jones
250
 * 'Documentation update
251
 * '
252
 * 'Revision 1.8.2.3  2000/06/25 23:08:31  jones
253
 * 'Minor change to excpetion handling
254
 * ''
255
 */
(2-2/42)