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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-08-29 10:20:19 -0700 (Fri, 29 Aug 2008) $'
11
 * '$Revision: 4335 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33

    
34
import edu.ucsb.nceas.metacat.service.PropertyService;
35
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
36

    
37
/**
38
 * (on insert of XML document)
39
 * Generates a unique Accession Number or if provided check it
40
 * for uniqueness and register it into the db connection
41
 * (on update or delete of XML document)
42
 * Check for existance of provided Accession Number
43
 *
44
 */
45
public class AccessionNumber  {
46

    
47
  private String sitecode = null;
48
  private String sep = null;
49
  private String docid = null;
50
  private String rev = null;
51

    
52
    /**
53
	 * Construct an AccessionNumber
54
	 */
55
	private AccessionNumber() throws AccessionNumberException {
56
		try {
57
			this.sitecode = PropertyService.getProperty("document.sitecode");
58
			this.sep = PropertyService.getProperty("document.accNumSeparator");
59
		} catch (PropertyNotFoundException pnfe) {
60
			throw new AccessionNumberException("Could not retrieve property "
61
					+ "in constructor: " + pnfe.getMessage());
62
		}
63
	}
64

    
65
  /**
66
	 * NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
67
	 * Construct an AccessionNumber
68
	 * 
69
	 * @param conn
70
	 *            the db connection to read Accession number from
71
	 * @param accnum
72
	 *            the accession number to be checked for validness
73
	 */
74
  public AccessionNumber (String accnum, String action)
75
           throws AccessionNumberException, SQLException, NumberFormatException
76
 {
77
    this();
78

    
79
    this.rev = null;
80
    this.docid = accnum;
81
    if ( accnum != null ) {
82
      int firstIndex = accnum.indexOf(this.sep);
83
      int lastIndex = accnum.lastIndexOf(this.sep);
84
      if ( firstIndex != lastIndex ) {
85
        //this docid contains a revision number
86
        this.rev = accnum.substring(lastIndex + 1);
87
        this.docid = accnum.substring(0, lastIndex);
88
      }
89
    }
90

    
91
    // INSERT
92
    if ( action.equals("INSERT")) {
93

    
94
        if(rev != null){
95
            try {
96
                Integer.parseInt(rev);
97
            }
98
            catch (java.lang.NumberFormatException e) {
99
                throw new AccessionNumberException(
100
                    "Revision number is required");
101
            }
102
        }
103

    
104
      // check accession number for validness
105
      if ( docid == null ) {
106
        throw new AccessionNumberException("Accession number is required");
107

    
108
      // rev is not provided; throw an exception to prevent the insertion
109
      } else if ( rev == null ) {
110
        throw new AccessionNumberException
111
                  ("Revision number is required");
112

    
113
      // docid is used; throw an exception to prevent the insertion
114
      } else if ( accNumberUsed(docid) ) {
115
        throw new AccessionNumberException
116
                  ("Accession number " + docid + " is already in use");
117

    
118
      // rev is <> 1; throw an exception to prevent the insertion
119
      } /*else if ( !rev.equals("1")) {
120
        throw new AccessionNumberException("Revision number must be 1");
121
      }*/
122

    
123
    // UPDATE or DELETE
124
    } else if ( action.equals("UPDATE") || action.equals("DELETE")) {
125
      String l_rev = "";
126

    
127
      int reversionNumber = 1;
128

    
129
      if(rev != null){
130
          try{
131
              reversionNumber = Integer.parseInt(rev);
132
          } catch (java.lang.NumberFormatException e){
133
              throw new AccessionNumberException(
134
                      "Revision number is required");
135
          }
136
      }
137

    
138
      // Accession# is not provided; throw an exception to prevent the action
139
      if ( docid == null ) {
140
        throw new AccessionNumberException("Accession number is required");
141

    
142
      // rev is not provided; throw an exception to prevent the action
143
      } else if ( rev == null ) {
144
        throw new AccessionNumberException
145
                  ("Revision number is required");
146

    
147
      // Accession# is not current (not in xml_documents); throw an exception
148
      } else if ( !accNumberIsCurrent(docid) ) {
149
        throw new AccessionNumberException
150
                  ("Document not found for Accession number " + docid);
151

    
152
      //Revision number is less than or equal the recent one; throw a exception
153
      } else if ( action.equals("UPDATE") &&
154
                  reversionNumber <= getLastRevisionNumber(docid) ) {
155
        throw new AccessionNumberException
156
                 ("Next revision number couldn't be less than or equal "
157
                                              + getLastRevisionNumber(docid));
158

    
159
      // Revision number is not the recent one; throw an exception
160
      } else if ( action.equals("DELETE") &&
161
                  !rev.equals(l_rev = getLastRevision(docid)) ) {
162
        throw new AccessionNumberException
163
                  ("Last revision number is "+ l_rev);
164
      }
165
    }
166
  }
167

    
168
  /** check for existence of Accesssion Number xml_acc_numbers table */
169
  public static boolean accNumberUsed ( String accNumber )
170
                  throws SQLException {
171

    
172
    boolean hasAccNumber = false;
173
    DBConnection conn = null;
174
    int serialNumber = -1;
175

    
176
    try {
177
      PreparedStatement pstmt = null;
178
      //check out DBConnection
179
      conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberUsed");
180
      serialNumber=conn.getCheckOutSerialNumber();
181
      pstmt = conn.prepareStatement(
182
                "SELECT 'x' FROM xml_documents " +
183
                "WHERE docid = ? " +
184
                "UNION " +
185
                "SELECT 'x' FROM xml_revisions " +
186
                "WHERE docid = ?");
187
      pstmt.setString(1,accNumber);
188
      pstmt.setString(2,accNumber);
189
      pstmt.execute();
190
      ResultSet rs = pstmt.getResultSet();
191
      hasAccNumber = rs.next();
192
      pstmt.close();
193

    
194
    } catch (SQLException e) {
195
      throw new SQLException
196
      ("Error on AccessionNumber.accNumberUsed(accNumber): " + e.getMessage());
197
    }
198
    finally
199
    {
200
      DBConnectionPool.returnDBConnection(conn, serialNumber);
201
    }
202

    
203
    return hasAccNumber;
204
  }
205

    
206
  // check for existence of Accesssion Number in xml_documents table
207
  private boolean accNumberIsCurrent(String accNumber) throws SQLException {
208

    
209
    boolean hasCurrentAccNumber = false;
210
    DBConnection conn = null;
211
    int serialNumber = -1;
212

    
213
    try {
214
      PreparedStatement pstmt = null;
215
      //check out DBConnection
216
      conn=DBConnectionPool.getDBConnection("AccessionNumber.accNumberIsCurre");
217
      serialNumber=conn.getCheckOutSerialNumber();
218
      pstmt = conn.prepareStatement(
219
                "SELECT 'x' FROM xml_documents " +
220
                "WHERE docid = ?");
221
      pstmt.setString(1, accNumber);
222
      pstmt.execute();
223
      ResultSet rs = pstmt.getResultSet();
224
      hasCurrentAccNumber = rs.next();
225
      pstmt.close();
226

    
227
    } catch (SQLException e) {
228
      throw new SQLException(
229
          "Error on AccessionNumber.accNumberIsCurrent(String accNumber): " +
230
          e.getMessage());
231
    }
232
    finally
233
    {
234
      DBConnectionPool.returnDBConnection(conn, serialNumber);
235
    }
236

    
237
    return hasCurrentAccNumber;
238
  }
239

    
240
  // get the recent revision number for docid
241
  private String getLastRevision(String docid) throws SQLException
242
  {
243
    String rev = "";
244
    DBConnection conn = null;
245
    int serialNumber = -1;
246

    
247
    try {
248
      PreparedStatement pstmt = null;
249
      //check out DBConnection
250
      conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevision");
251
      serialNumber=conn.getCheckOutSerialNumber();
252
      pstmt = conn.prepareStatement
253
              ("SELECT rev FROM xml_documents WHERE docid='" + docid + "'");
254
      pstmt.execute();
255

    
256
      ResultSet rs = pstmt.getResultSet();
257
      boolean hasRow = rs.next();
258
      rev = rs.getString(1);
259
      pstmt.close();
260

    
261
    } catch (SQLException e) {
262
      throw new SQLException(
263
      "Error on AccessionNumber.getLastRevision(): " + e.getMessage());
264
    }
265
    finally
266
    {
267
      DBConnectionPool.returnDBConnection(conn,serialNumber);
268
    }
269

    
270
    return rev;
271
  }
272

    
273
  /**
274
    * Get last revision number from database for a docid
275
    * The return value is integer because we want compare it to there new one
276
    * @param docid <sitecode>.<uniqueid> part of Accession Number
277
    */
278
  private int getLastRevisionNumber(String docId) throws SQLException
279
  {
280
    int rev = 1;
281
    DBConnection conn =null;
282
    int serialNumber = -1;
283

    
284
    try {
285
      PreparedStatement pStmt = null;
286
      //check out DBConnection
287
      conn=DBConnectionPool.getDBConnection("AccessionNumber.getLastRevisionN");
288
      serialNumber=conn.getCheckOutSerialNumber();
289

    
290
      pStmt = conn.prepareStatement
291
              ("SELECT rev FROM xml_documents WHERE docid='" + docId + "'");
292
      pStmt.execute();
293

    
294
      ResultSet rs = pStmt.getResultSet();
295
      boolean hasRow = rs.next();
296
      rev = rs.getInt(1);
297
      pStmt.close();
298

    
299
    } catch (SQLException e) {
300
      throw new SQLException(
301
      "Error on AccessionNumber.getLastRevision(): " + e.getMessage());
302
    }
303
    finally
304
    {
305
      DBConnectionPool.returnDBConnection(conn,serialNumber);
306
    }
307
    return rev;
308
  }
309

    
310
  /**
311
   * returns the docid encoded in this accession number
312
   */
313
  public String getDocid() {
314

    
315
    return this.docid;
316
  }
317

    
318
  /**
319
   * returns the revision number encoded in this accession number
320
   */
321
  public String getRev()
322
  {
323
    return this.rev;
324
  }
325

    
326
}
(6-6/68)