Project

General

Profile

« Previous | Next » 

Revision 2768

Added by Matt Jones over 18 years ago

Cleaned up AccessionNumber class to get rid of unused methods. It seems the entire accession_number table is no longer used but has not been deleted from the build. Will do in subsequent commit.

View differences:

AccessionNumber.java
28 28

  
29 29
package edu.ucsb.nceas.metacat;
30 30

  
31
import java.net.*;
32
import java.sql.*;
31
import java.sql.PreparedStatement;
32
import java.sql.ResultSet;
33
import java.sql.SQLException;
33 34

  
34 35
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
35 36

  
......
45 46

  
46 47
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
47 48

  
48
  //private Connection conn = null;
49 49
  private String sitecode = null;
50 50
  private String sep = null;
51 51
  private String docid = null;
......
54 54
  /**
55 55
   * Construct an AccessionNumber
56 56
   */
57
  //public AccessionNumber ()
58
         //throws SQLException, ClassNotFoundException
59
  //{
60
    // Open a connection to the database from here
61
    //this((new MetaCatUtil()).openDBConnection());
62
  //}
63

  
64
  /**
65
   * Construct an AccessionNumber
66
   *
67
   * @param conn the db connection to read from and write Accession# to
68
   */
69
  public AccessionNumber ()
57
  private AccessionNumber ()
70 58
  {
71 59
    this.sitecode = MetaCatUtil.getOption("sitecode");
72 60
    this.sep = MetaCatUtil.getOption("accNumSeparator");
73
    //this.conn = conn;
74 61
  }
75 62

  
76 63
  /** NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
......
173 160
    }
174 161
  }
175 162

  
176
  /**
177
   * Generate an Accession Number of form <sidecode>.<uniqueid>.<revisionid>
178
   * where <revisionid> is always 1.
179
   *
180
   * @param docid <sitecode>.<uniqueid> part of Accession Number
181
   * @param action INSERT, UPDATE or DELETE action
182
   */
183
  public String generate(String docid, String action)
184
                throws AccessionNumberException, SQLException
185
  {
186
    return generate(docid, "1", action);
187
  }
188

  
189
  /**
190
   * Generate an Accession Number of form <sidecode>.<uniqueid>.<revisionid>
191
   *
192
   * @param docid <sitecode>.<uniqueid> part of Accession Number
193
   * @param rev <revisionid> of Accession Number
194
   * @param action INSERT, UPDATE or DELETE action
195
   */
196
  public String generate(String docid, String rev, String action)
197
                throws AccessionNumberException, SQLException
198
  {
199
    try {
200
      // INSERT
201
      if ( action.equals("INSERT")) {
202

  
203
        // get a new docid
204
        if ( docid == null ) {
205
          String sitecode = getSitecode();
206
          String uniqueid = getUniqueID(sitecode);
207

  
208
          return sitecode + sep + uniqueid;
209

  
210
        // docid is used; throw an exception to prevent the insertion
211
        } else if ( accNumberUsed(docid) ) {
212
          throw new AccessionNumberException
213
                    ("Accession number " + docid + " is already in use");
214
        // rev is <> 1; throw an exception to prevent the insertion
215
        } else if ( !rev.equals("1") ) {
216
          throw new AccessionNumberException
217
                    ("Revision number must be 1");
218
        // docid is not used and rev=1; return it
219
        } else {
220
          return docid;
221
        }
222

  
223
      // UPDATE or DELETE
224
      } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
225

  
226
        // Accession# is not provided; throw an exception to prevent the action
227
        if ( docid == null ) {
228
          throw new AccessionNumberException("Accession number is required");
229

  
230
        // Accession# is not current (not in xml_documents); throw an exception
231
        } else if ( !accNumberIsCurrent(docid) ) {
232
          throw new AccessionNumberException
233
          ("Document not found for Accession number " + docid);
234

  
235
        // Revision number is not the recent one; throw an exception
236
        } else if ( !rev.equals(getLastRevision(docid)) ) {
237
          throw new AccessionNumberException
238
          ("Revision number must be the recent");
239

  
240
        // Accession# is current (present in xml_documents); return it
241
        } else {
242
          return docid;
243
        }
244
      }
245

  
246
    } catch (SQLException e) {
247
      throw new SQLException
248
      ("Error on AccessionNumber.generate(): " + e.getMessage());
249
    }
250

  
251
    // never comes here
252
    throw new
253
    AccessionNumberException("Fatal Error in accession number generation");
254
  }
255

  
256
  // get local sitecode
257
  private String getSitecode()
258
  {
259
    return this.sitecode;
260
  }
261

  
262
  // get Unique ID from DB sequence
263
  private String getUniqueID (String sitecode) throws SQLException
264
  {
265
    String uniqueid;
266
    String sysdate = dbAdapter.getDateTimeFunction();
267
    DBConnection conn = null;
268
    int serialNumber = -1;
269
    try {
270
      PreparedStatement pstmt = null;
271

  
272
      //check out DBConnection
273
      conn=DBConnectionPool.getDBConnection("AccessionNumber.getUniqueID");
274
      serialNumber=conn.getCheckOutSerialNumber();
275
      pstmt = conn.prepareStatement
276
              ("INSERT INTO accession_number (site_code, date_created) " +
277
               "VALUES (?, " + sysdate + ")");
278
      pstmt.setString(1, sitecode);
279
      pstmt.execute();
280
      pstmt.close();
281

  
282
      uniqueid = "" + dbAdapter.getUniqueID(conn.getConnections(),
283
                                                          "accession_number");
284
      //because we only count the connection usage one when it check in
285
      //but it use twice. So we need to increase one
286
      conn.increaseUsageCount(1);
287

  
288
    } catch (SQLException e) {
289
      throw new
290
      SQLException("Error on AccessionNumber.getUniqueID(): "+e.getMessage());
291
    }
292
    finally
293
    {
294
      DBConnectionPool.returnDBConnection(conn, serialNumber);
295
    }
296
    return uniqueid;
297
  }
298

  
299 163
  /** check for existence of Accesssion Number xml_acc_numbers table */
300 164
  public static boolean accNumberUsed ( String accNumber )
301 165
                  throws SQLException {
......
438 302
    return rev;
439 303
  }
440 304

  
441
  // get the next revision number for docid
442
  private String getNextRevision(String docid) throws SQLException
443
  {
444
    String rev = "";
445
    DBConnection conn = null;
446
    int serialNumber = -1;
447
    try {
448
      PreparedStatement pstmt = null;
449
      //check out DBConnection
450
      conn=DBConnectionPool.getDBConnection("AccessionNumber.getNextRevision");
451
      serialNumber=conn.getCheckOutSerialNumber();
452
      pstmt = conn.prepareStatement
453
              ("SELECT rev+1 FROM xml_documents WHERE docid='" + docid + "'");
454
      pstmt.execute();
455

  
456
      ResultSet rs = pstmt.getResultSet();
457
      boolean hasRow = rs.next();
458
      rev = rs.getString(1);
459
      pstmt.close();
460

  
461
    } catch (SQLException e) {
462
      throw new SQLException(
463
      "Error on AccessionNumber.getNextRevision(): " + e.getMessage());
464
    }
465
    finally
466
    {
467
      DBConnectionPool.returnDBConnection(conn, serialNumber);
468
    }
469

  
470
    return rev;
471
  }
472

  
473 305
  /**
474 306
   * returns the docid encoded in this accession number
475 307
   */

Also available in: Unified diff