Project

General

Profile

« Previous | Next » 

Revision 576

Added by bojilova almost 24 years ago

change the generation of Accession# in the form of <sidecode>.<createdate>

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;
31 32
  private String defaultGlobalName = null;
32 33
  private String sep = null;
33 34
    
......
39 40
        
40 41
    MetaCatUtil util = new MetaCatUtil();
41 42

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

  
......
55 57
        
56 58
    MetaCatUtil util = new MetaCatUtil();
57 59

  
60
    this.server = util.getOption("server");
58 61
    this.defaultGlobalName = util.getOption("defaultGlobalName");
59 62
    this.sep = util.getOption("accNumberSeparator");
60 63
    this.conn = conn;
......
62 65
  }
63 66

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

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

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

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

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

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

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

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

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

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

  
148
    return sysdate;
149
  }
150

  
151

  
152
  ///////////////////////////////////////////////////////////
153
  // THE OLD APPROACH - accession# of form globalname:localid
154
  /**
65 155
   * Get an Accession Number, check it for uniqueness and 
66 156
   * register it into db connection. If no Accession Number is
67 157
   * provided, generate one from the database and return it.
......
74 164
   * When "UPDATE", accession # is required and it is checked for existance.
75 165
   * When "DELETE", accession # is required and it is checked for existance.
76 166
   */
77
  public String generate (String accNumber, String action) 
167
  public String gen (String accNumber, String action) 
78 168
         throws AccessionNumberException, SQLException   {
79 169
        
80 170
    String globalName = null;
......
96 186
        if ( accNumber == null ) {
97 187
          return put(defaultGlobalName, null, sep);
98 188
        // Acc# is not used; return it
99
        } else if ( !accNumberUsed(globalName, localId) ) {
189
        } else if ( !accNumberUsed(accNumber) ) {
100 190
          return put(globalName, localId, sep);
101 191
        // Acc# is used; throw an exception to prevent the insertion
102 192
        } else {
......
134 224
    }    
135 225
        
136 226
    throw (new AccessionNumberException("Fatal Error in " +
137
           "accession number generation: "));
227
           "accession number generation"));
138 228
  }    
139 229

  
140 230
  /** put unique accession # into db connection */
......
172 262
  }
173 263

  
174 264
  /** check for existence of Accesssion Number xml_acc_numbers table */
175
  private boolean accNumberUsed(String globalName, String localId)
176
                      throws SQLException {
265
  private boolean accNumberUsed ( String accNumber )
266
                  throws SQLException {
177 267
        
178 268
    boolean hasAccNumber = false;
179 269
        
180 270
    try {
181 271
      PreparedStatement pstmt;
182 272
      pstmt = conn.prepareStatement(
183
                "SELECT 'x' FROM xml_acc_numbers " + 
184
                "WHERE global_name LIKE ? AND local_id = ?");
185
      pstmt.setString(1,globalName);
186
      pstmt.setString(2,localId);
273
                "SELECT 'x' FROM xml_documents " + 
274
                "WHERE docid LIKE ? " +
275
                "UNION " +
276
                "SELECT 'x' FROM xml_revisions " +
277
                "WHERE docid LIKE ?");
278
      pstmt.setString(1,accNumber);
279
      pstmt.setString(2,accNumber);
187 280
      pstmt.execute();
188 281
      ResultSet rs = pstmt.getResultSet();
189 282
      hasAccNumber = rs.next();
190 283
      pstmt.close();
191 284
            
192 285
    } catch (SQLException e) {
193
      throw new SQLException(
194
                "Error on AccessionNumber.accNumberUsed(globalName, " +
195
                  "localId): " + e.getMessage());
286
      throw new SQLException
287
      ("Error on AccessionNumber.accNumberUsed(accNumber): " + e.getMessage());
196 288
    }    
197 289
        
198 290
    return hasAccNumber;
......
267 359

  
268 360
/**
269 361
 * '$Log$
362
 * 'Revision 1.14  2000/09/28 18:05:46  bojilova
363
 * 'Changed to prevent the insertion if the provided Accession# is in use as Dan suggested.
364
 * '
270 365
 * 'Revision 1.13  2000/09/20 20:15:58  bojilova
271 366
 * 'change Assession# generation to use the same db connection
272 367
 * '

Also available in: Unified diff