Project

General

Profile

« Previous | Next » 

Revision 618

Added by bojilova almost 24 years ago

changed acc# generation to use db sequence instead of timestamp

View differences:

src/edu/ucsb/nceas/metacat/AccessionNumber.java
28 28
public class AccessionNumber  {
29 29
  
30 30
  private Connection conn = null;
31
  private String server = null;
32
  private String defaultGlobalName = null;
31
  private String sitecode = null;
33 32
  private String sep = null;
34 33
    
35 34
  /** 
......
40 39
        
41 40
    MetaCatUtil util = new MetaCatUtil();
42 41

  
43
    this.server = util.getOption("server");
44
    this.defaultGlobalName = util.getOption("defaultGlobalName");
45
    this.sep = util.getOption("accNumberSeparator");
42
    this.sitecode = util.getOption("sitecode");
43
    this.sep = util.getOption("accNumSeparator");
46 44

  
47 45
    // Open a connection to the database
48 46
    this.conn = util.openDBConnection();
......
57 55
        
58 56
    MetaCatUtil util = new MetaCatUtil();
59 57

  
60
    this.server = util.getOption("server");
61
    this.defaultGlobalName = util.getOption("defaultGlobalName");
62
    this.sep = util.getOption("accNumberSeparator");
58
    this.sitecode = util.getOption("sitecode");
59
    this.sep = util.getOption("accNumSeparator");
63 60
    this.conn = conn;
64 61

  
65 62
  }
......
76 73

  
77 74
        // get a new Accession#
78 75
        if ( accNumber == null ) {
79
          String sidecode = getSidecode();
80
          String createdate = getSysdate();
81
          return sidecode + "." + createdate;
76
          String sitecode = getSitecode();
77
          String uniqueid = getUniqueID();
82 78

  
79
          return sitecode + sep + uniqueid;
80

  
83 81
        // Accession# is not used; return it
84 82
        } else if ( !accNumberUsed(accNumber) ) {
85 83
          return accNumber;
......
118 116
    AccessionNumberException("Fatal Error in accession number generation.");
119 117
  }
120 118
  
121
  // get local server name
122
  private String getSidecode()
119
  // get local sitecode
120
  private String getSitecode()
123 121
  {
124
    return this.server;
122
    return this.sitecode;
125 123
  }
126 124

  
127
  // get today date from db connection
128
  private String getSysdate () throws SQLException
125
  // get Unique ID from DB sequence
126
  private String getUniqueID () throws SQLException
129 127
  {
130
    String sysdate;
128
    String uniqueid;
131 129
    
132 130
    try {
133 131
      PreparedStatement pstmt;
134 132
      pstmt = conn.prepareStatement
135
              ("SELECT to_char(sysdate,'mmddyyyyhhmiss') FROM dual");
133
              ("SELECT accnum_uniqueid_seq.nextval FROM dual");
136 134
      pstmt.execute();
137 135

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

  
148
    return sysdate;
146
    return uniqueid;
149 147
  }
150 148

  
151

  
152
  ///////////////////////////////////////////////////////////
153
  // THE OLD APPROACH - accession# of form globalname:localid
154
  /**
155
   * Get an Accession Number, check it for uniqueness and 
156
   * register it into db connection. If no Accession Number is
157
   * provided, generate one from the database and return it.
158
   *
159
   * @param accNumber - Accession # if provided or null if not
160
   * @param action - INSERT, UPDATE or DELETE.
161
   * When "INSERT" and accession # provided is not unique, get new one.
162
   * If it is unique, use it.
163
   * When "INSERT" and accession # is null, get a new one.
164
   * When "UPDATE", accession # is required and it is checked for existance.
165
   * When "DELETE", accession # is required and it is checked for existance.
166
   */
167
  public String gen (String accNumber, String action) 
168
         throws AccessionNumberException, SQLException   {
169
        
170
    String globalName = null;
171
    String localId = null;
172

  
173
    try {
174

  
175
      conn.setAutoCommit(true);
176

  
177
      // split the acc # in 2 parts - global name & local id
178
      if ( accNumber != null ) {
179
        globalName = getGlobalName(accNumber, sep);
180
        localId = getLocalId(accNumber, sep);
181
      }    
182

  
183
      // INSERT
184
      if ( action.equals("INSERT")) {
185
        // get a unique Acc#
186
        if ( accNumber == null ) {
187
          return put(defaultGlobalName, null, sep);
188
        // Acc# is not used; return it
189
        } else if ( !accNumberUsed(accNumber) ) {
190
          return put(globalName, localId, sep);
191
        // Acc# is used; throw an exception to prevent the insertion
192
        } else {
193
          throw 
194
          new AccessionNumberException("Accession number is already in use.");
195
        }
196
      // UPDATE or DELETE
197
      } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
198
        // Acc# is not provided; throw an exception to prevent the action
199
        if ( accNumber == null ) {
200
          throw (new AccessionNumberException("Accession number is " +
201
          "required."));
202
        // Acc# is not current (not in xml_documents); throw an exception
203
        } else if (!accNumberIsCurrent(accNumber)) {
204
          throw (new AccessionNumberException("Document " +
205
          "not found for accession #: " + accNumber));
206
        // Acc# is current; return it
207
        } else {
208
          return accNumber;
209
        }
210
      }
211
 
212
      //conn.close();        
213

  
214
    } catch (StringIndexOutOfBoundsException siobe) {
215
      MetaCatUtil.debugMessage(
216
                 "Error on AccessionNumber.generate(): " + 
217
                  siobe.getMessage());
218
      throw (new AccessionNumberException("Accession number invalid, " +
219
                 "expecting character \'" + sep + "'."));
220
    } catch (SQLException e) {
221
      throw new SQLException(
222
                 "Error on AccessionNumber.generate(accNumber, action): " +
223
                  e.getMessage());
224
    }    
225
        
226
    throw (new AccessionNumberException("Fatal Error in " +
227
           "accession number generation"));
228
  }    
229

  
230
  /** put unique accession # into db connection */
231
  private String put (String globalName, String localId, String sep) 
232
                throws SQLException
233
  {
234
        
235
    Integer l = null;
236
    try {
237
      if ( localId == null ) 
238
         l = new Integer(get(globalName) + 1); 
239
      //else if ( accNumberUsed(globalName, localId) )
240
      //   l = new Integer(get(globalName) + 1); 
241
      else
242
         l = new Integer(localId); 
243

  
244
     // insert globalName & l
245
     PreparedStatement pstmt;
246
     pstmt = conn.prepareStatement(
247
             "INSERT INTO xml_acc_numbers (global_name, local_id) " + 
248
             "VALUES (?, ?)");
249
     pstmt.setString(1,globalName);
250
     pstmt.setString(2,l.toString());
251
     pstmt.execute();
252
            
253
     pstmt.close();
254
            
255
    } catch (SQLException e) {
256
      throw new SQLException(
257
            "Error on AccessionNumber.put(globalName, localId, sep): " 
258
            + e.getMessage());
259
    }    
260

  
261
    return globalName + sep + l;
262
  }
263

  
264 149
  /** check for existence of Accesssion Number xml_acc_numbers table */
265 150
  public boolean accNumberUsed ( String accNumber )
266 151
                  throws SQLException {
......
315 200
    return hasCurrentAccNumber;
316 201
  }    
317 202

  
318
  /** get the last in order local ID by a given global name */
319
  private int get (String globalName) throws SQLException  {
320
        
321
    try {
322
      PreparedStatement pstmt;
323
      pstmt = conn.prepareStatement(
324
              "SELECT max(local_id) FROM xml_acc_numbers " + 
325
              "WHERE global_name LIKE ?");
326
      pstmt.setString(1,globalName);
327
      pstmt.execute();
328
      ResultSet rs = pstmt.getResultSet();
329
      boolean hasLocalId = rs.next();
330

  
331
      if (hasLocalId) {
332
        return rs.getInt(1);
333
      }
334

  
335
      pstmt.close();
336
    } catch (SQLException e) {
337
      throw new SQLException(
338
            "Error on AccessionNumber.get(globalName): " + e.getMessage());
339
    }    
340
        
341
    return 0;
342
  }
343

  
344
    
345
  // get the global part of the accession number
346
  private String getGlobalName (String accNumber, String sep) 
347
          throws StringIndexOutOfBoundsException {
348
        
349
    return accNumber.substring(0, accNumber.lastIndexOf(sep));
350
  }    
351

  
352
  // get the local part of the accession number
353
  private String getLocalId (String accNumber, String sep)
354
          throws StringIndexOutOfBoundsException {
355

  
356
    return accNumber.substring(accNumber.lastIndexOf(sep)+1);
357
  }    
358 203
}
359 204

  
360 205
/**
361 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
 * '
362 210
 * 'Revision 1.15  2000/11/30 22:41:32  bojilova
363 211
 * 'change the generation of Accession# in the form of <sidecode>.<createdate>
364 212
 * '

Also available in: Unified diff