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-09-20 13:15:58 -0700 (Wed, 20 Sep 2000) $'
12
 * '$Revision: 459 $'
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 defaultGlobalName = 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.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) {
55
        
56
    MetaCatUtil util = new MetaCatUtil();
57

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

    
62
  }
63

    
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;
82

    
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
      }
110
 
111
      //conn.close();        
112

    
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
    }    
124
        
125
    throw (new AccessionNumberException("Fatal Error in " +
126
           "accession number generation: "));
127
  }    
128

    
129
  /** put unique accession # into db connection */
130
  private String put (String globalName, String localId, String sep) 
131
                throws SQLException
132
  {
133
        
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); 
142

    
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();
151
            
152
     pstmt.close();
153
            
154
    } catch (SQLException e) {
155
      throw new SQLException(
156
            "Error on AccessionNumber.put(globalName, localId, sep): " 
157
            + e.getMessage());
158
    }    
159

    
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 {
166
        
167
    boolean hasAccNumber = false;
168
        
169
    try {
170
      PreparedStatement pstmt;
171
      pstmt = conn.prepareStatement(
172
                "SELECT 'x' FROM xml_acc_numbers " + 
173
                "WHERE global_name LIKE ? AND local_id = ?");
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();
180
            
181
    } catch (SQLException e) {
182
      throw new SQLException(
183
                "Error on AccessionNumber.accNumberUsed(globalName, " +
184
                  "localId): " + e.getMessage());
185
    }    
186
        
187
    return hasAccNumber;
188
  }    
189
    
190
  /** check for existence of Accesssion Number in xml_documents table */
191
  private boolean accNumberIsCurrent(String accNumber) throws SQLException {
192
        
193
    boolean hasCurrentAccNumber = false;
194
        
195
    try {
196
      PreparedStatement pstmt;
197
      pstmt = conn.prepareStatement(
198
                "SELECT 'x' FROM xml_documents " + 
199
                "WHERE docid LIKE ?");
200
      pstmt.setString(1, accNumber);
201
      pstmt.execute();
202
      ResultSet rs = pstmt.getResultSet();
203
      hasCurrentAccNumber = rs.next();
204
      pstmt.close();
205
            
206
    } catch (SQLException e) {
207
      throw new SQLException(
208
          "Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
209
          e.getMessage());
210
    }    
211
  
212
    return hasCurrentAccNumber;
213
  }    
214

    
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();
227

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

    
232
      pstmt.close();
233
    } catch (SQLException e) {
234
      throw new SQLException(
235
            "Error on AccessionNumber.get(globalName): " + e.getMessage());
236
    }    
237
        
238
    return 0;
239
  }
240

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

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

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

    
257
/**
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
 * '
263
 * 'Revision 1.11  2000/08/14 20:53:33  jones
264
 * 'Added "release" keyword to all metacat source files so that the release
265
 * 'number will be evident in software distributions.
266
 * '
267
 * 'Revision 1.10  2000/06/27 04:31:07  jones
268
 * 'Fixed bugs associated with the new UPDATE and DELETE functions of
269
 * 'DBWriter.  There were problematic interactions between some static
270
 * 'variables used in DBEntityResolver and the way in which the
271
 * 'Servlet objects are re-used across multiple client invocations.
272
 * '
273
 * 'Generally cleaned up error reporting.  Now all errors and success
274
 * 'results are reported as XML documents from MetaCatServlet.  Need
275
 * 'to make the command line tools do the same.
276
 * '
277
 * 'Revision 1.9  2000/06/26 10:35:04  jones
278
 * 'Merged in substantial changes to DBWriter and associated classes and to
279
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
280
 * 'functions.  The command line tools and the parameters for the
281
 * 'servlet have changed substantially.
282
 * '
283
 * 'Revision 1.8.2.5  2000/06/26 08:38:01  jones
284
 * 'Added DELETE feature to DBWriter.  Now takes an action "DELETE" and a
285
 * 'docid and will move the record from the xml_documents table to the
286
 * 'xml_revisions table.
287
 * 'Modified option parsing to support option symbols on command line.
288
 * '
289
 * 'Revision 1.8.2.4  2000/06/25 23:11:40  jones
290
 * 'Documentation update
291
 * '
292
 * 'Revision 1.8.2.3  2000/06/25 23:08:31  jones
293
 * 'Minor change to excpetion handling
294
 * ''
295
 */
(1-1/28)