Revision 459
Added by bojilova over 23 years ago
AccessionNumber.java | ||
---|---|---|
18 | 18 |
import java.sql.*; |
19 | 19 |
|
20 | 20 |
/** |
21 |
* A class that generates an Accession Number and will check a submitted |
|
22 |
* accession number for uniqueness and register it into the db connection |
|
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 |
* |
|
23 | 27 |
*/ |
24 | 28 |
public class AccessionNumber { |
29 |
|
|
30 |
private Connection conn = null; |
|
31 |
private String defaultGlobalName = null; |
|
32 |
private String sep = null; |
|
25 | 33 |
|
26 |
/** |
|
27 |
* Get an accession number from the user, check it for uniqueness |
|
28 |
* and register it into new db connection. If no accession number is |
|
29 |
* provided by the user, generate one from the database and return it. |
|
30 |
* |
|
31 |
* @param accNumber - accession # if provided or null if not |
|
32 |
* @param action - INSERT, UPDATE or DELETE. |
|
33 |
* When "INSERT" and accession # provided is not unique, get next one. |
|
34 |
* If it is unique, use it. |
|
35 |
* When "INSERT" and accession # is null, get a new one. |
|
36 |
* When "UPDATE", accession # is required. |
|
37 |
* When "DELETE", accession # is required. |
|
38 |
*/ |
|
39 |
public String generate (String accNumber, String action) |
|
40 |
throws AccessionNumberException, SQLException, ClassNotFoundException |
|
41 |
{ |
|
34 |
/** |
|
35 |
* Construct an AccessionNumber |
|
36 |
*/ |
|
37 |
public AccessionNumber () |
|
38 |
throws SQLException, ClassNotFoundException { |
|
42 | 39 |
|
43 |
String globalName = null; |
|
44 |
String localId = null; |
|
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) { |
|
45 | 55 |
|
46 |
// Open a connection to the database |
|
47 |
MetaCatUtil util = new MetaCatUtil(); |
|
56 |
MetaCatUtil util = new MetaCatUtil(); |
|
48 | 57 |
|
49 |
String defaultGlobalName = util.getOption("defaultGlobalName"); |
|
50 |
String sep = util.getOption("accNumberSeparator"); |
|
58 |
this.defaultGlobalName = util.getOption("defaultGlobalName"); |
|
59 |
this.sep = util.getOption("accNumberSeparator"); |
|
60 |
this.conn = conn; |
|
51 | 61 |
|
52 |
try { |
|
53 |
// Open a new connection to the database |
|
54 |
Connection conn = util.openDBConnection(); |
|
55 |
conn.setAutoCommit(true); |
|
62 |
} |
|
56 | 63 |
|
57 |
// split the acc # in 2 parts - global name & local id |
|
58 |
if ( accNumber != null ) { |
|
59 |
globalName = getGlobalName(accNumber, sep); |
|
60 |
localId = getLocalId(accNumber, sep); |
|
61 |
} |
|
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; |
|
62 | 82 |
|
63 |
// register unique acc # |
|
64 |
if ( action.equals("INSERT")) { |
|
65 |
if ( accNumber == null ) |
|
66 |
return put(conn, defaultGlobalName, null, sep); |
|
67 |
else |
|
68 |
return put(conn, globalName, localId, sep); |
|
69 |
} else if ( action.equals("UPDATE") || action.equals("DELETE")) { |
|
70 |
if ( accNumber == null ) { |
|
71 |
throw (new AccessionNumberException("Accession number is " + |
|
72 |
"required.")); |
|
73 |
} else if (!accNumberIsCurrent(conn, accNumber)) { |
|
74 |
throw (new AccessionNumberException("Document " + |
|
75 |
"not found for accession #: " + accNumber)); |
|
76 |
} else { |
|
77 |
return (globalName + sep + localId); |
|
78 |
} |
|
79 |
} |
|
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 |
} |
|
80 | 110 |
|
81 |
conn.close();
|
|
111 |
//conn.close();
|
|
82 | 112 |
|
83 |
} catch (StringIndexOutOfBoundsException siobe) { |
|
84 |
MetaCatUtil.debugMessage( |
|
85 |
"Error on AccessionNumber.generate(): " + |
|
86 |
siobe.getMessage()); |
|
87 |
throw (new AccessionNumberException("Accession number invalid, " + |
|
88 |
"expecting character \'" + sep + "'.")); |
|
89 |
} catch (SQLException e) { |
|
90 |
System.err.println( |
|
91 |
"Error on AccessionNumber.genAccessionNumber(): " + |
|
92 |
e.getMessage()); |
|
93 |
throw e; |
|
94 |
} |
|
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 |
} |
|
95 | 124 |
|
96 |
throw (new AccessionNumberException("Fatal Error in " +
|
|
97 |
"accession number generation: "));
|
|
98 |
}
|
|
125 |
throw (new AccessionNumberException("Fatal Error in " + |
|
126 |
"accession number generation: ")); |
|
127 |
} |
|
99 | 128 |
|
100 |
/** put unique accession # into db connection */ |
|
101 |
private String put (Connection conn, String globalName, |
|
102 |
String localId, String sep) |
|
129 |
/** put unique accession # into db connection */ |
|
130 |
private String put (String globalName, String localId, String sep) |
|
103 | 131 |
throws SQLException |
104 |
{
|
|
132 |
{ |
|
105 | 133 |
|
106 |
Integer l = null;
|
|
107 |
try {
|
|
108 |
if ( localId == null )
|
|
109 |
l = new Integer(get(conn, globalName) + 1);
|
|
110 |
else if ( accNumberUsed(conn, globalName, localId) )
|
|
111 |
l = new Integer(get(conn, globalName) + 1);
|
|
112 |
else
|
|
113 |
l = new Integer(localId);
|
|
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); |
|
114 | 142 |
|
115 |
// insert globalName & l
|
|
116 |
PreparedStatement pstmt;
|
|
117 |
pstmt = conn.prepareStatement(
|
|
118 |
"INSERT INTO xml_acc_numbers (global_name, local_id) " +
|
|
119 |
"VALUES (?, ?)");
|
|
120 |
pstmt.setString(1,globalName);
|
|
121 |
pstmt.setString(2,l.toString());
|
|
122 |
pstmt.execute();
|
|
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(); |
|
123 | 151 |
|
124 |
pstmt.close();
|
|
152 |
pstmt.close(); |
|
125 | 153 |
|
126 |
} catch (SQLException e) { |
|
127 |
System.err.println( |
|
128 |
"Error on AccessionNumber.put(conn, globalName, localId): " |
|
129 |
+ e.getMessage()); |
|
130 |
throw e; |
|
131 |
} |
|
132 |
return globalName + sep + l; |
|
133 |
} |
|
154 |
} catch (SQLException e) { |
|
155 |
throw new SQLException( |
|
156 |
"Error on AccessionNumber.put(globalName, localId, sep): " |
|
157 |
+ e.getMessage()); |
|
158 |
} |
|
134 | 159 |
|
135 |
/** check for existence of Accesssion Number xml_acc_numbers table */ |
|
136 |
private boolean accNumberUsed(Connection conn, String globalName, |
|
137 |
String localId) throws SQLException { |
|
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 { |
|
138 | 166 |
|
139 |
boolean hasAccNumber = false;
|
|
167 |
boolean hasAccNumber = false; |
|
140 | 168 |
|
141 |
try {
|
|
142 |
PreparedStatement pstmt;
|
|
143 |
pstmt = conn.prepareStatement(
|
|
169 |
try { |
|
170 |
PreparedStatement pstmt; |
|
171 |
pstmt = conn.prepareStatement( |
|
144 | 172 |
"SELECT 'x' FROM xml_acc_numbers " + |
145 | 173 |
"WHERE global_name LIKE ? AND local_id = ?"); |
146 |
pstmt.setString(1,globalName);
|
|
147 |
pstmt.setString(2,localId);
|
|
148 |
pstmt.execute();
|
|
149 |
ResultSet rs = pstmt.getResultSet();
|
|
150 |
hasAccNumber = rs.next();
|
|
151 |
pstmt.close();
|
|
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(); |
|
152 | 180 |
|
153 |
} catch (SQLException e) {
|
|
154 |
System.err.println("Error on AccessionNumber.unique(globalName, " +
|
|
155 |
"localId): " + e.getMessage());
|
|
156 |
throw e;
|
|
157 |
}
|
|
181 |
} catch (SQLException e) { |
|
182 |
throw new SQLException(
|
|
183 |
"Error on AccessionNumber.accNumberUsed(globalName, " +
|
|
184 |
"localId): " + e.getMessage());
|
|
185 |
} |
|
158 | 186 |
|
159 |
return hasAccNumber;
|
|
160 |
}
|
|
187 |
return hasAccNumber; |
|
188 |
} |
|
161 | 189 |
|
162 |
/** check for existence of Accesssion Number in xml_documents table */ |
|
163 |
private boolean accNumberIsCurrent( |
|
164 |
Connection conn, String accNumber) throws SQLException { |
|
190 |
/** check for existence of Accesssion Number in xml_documents table */ |
|
191 |
private boolean accNumberIsCurrent(String accNumber) throws SQLException { |
|
165 | 192 |
|
166 |
boolean hasCurrentAccNumber = false;
|
|
193 |
boolean hasCurrentAccNumber = false; |
|
167 | 194 |
|
168 |
try {
|
|
169 |
PreparedStatement pstmt;
|
|
170 |
pstmt = conn.prepareStatement(
|
|
195 |
try { |
|
196 |
PreparedStatement pstmt; |
|
197 |
pstmt = conn.prepareStatement( |
|
171 | 198 |
"SELECT 'x' FROM xml_documents " + |
172 | 199 |
"WHERE docid LIKE ?"); |
173 |
pstmt.setString(1, accNumber);
|
|
174 |
pstmt.execute();
|
|
175 |
ResultSet rs = pstmt.getResultSet();
|
|
176 |
hasCurrentAccNumber = rs.next();
|
|
177 |
pstmt.close();
|
|
200 |
pstmt.setString(1, accNumber); |
|
201 |
pstmt.execute(); |
|
202 |
ResultSet rs = pstmt.getResultSet(); |
|
203 |
hasCurrentAccNumber = rs.next(); |
|
204 |
pstmt.close(); |
|
178 | 205 |
|
179 |
} catch (SQLException e) { |
|
180 |
System.err.println( |
|
181 |
"Error on AccessionNumber.accNumberIsCurrent(globalName, " + |
|
182 |
"localId): " + e.getMessage()); |
|
183 |
throw e; |
|
184 |
} |
|
185 |
return hasCurrentAccNumber; |
|
206 |
} catch (SQLException e) { |
|
207 |
throw new SQLException( |
|
208 |
"Error on AccessionNumber.accNumberIsCurrent(String accNumber): " + |
|
209 |
e.getMessage()); |
|
186 | 210 |
} |
211 |
|
|
212 |
return hasCurrentAccNumber; |
|
213 |
} |
|
187 | 214 |
|
188 |
/** get the last in order local ID by a given global name */ |
|
189 |
private int get (Connection conn, String globalName) |
|
190 |
throws SQLException |
|
191 |
{ |
|
192 |
try { |
|
193 |
PreparedStatement pstmt; |
|
194 |
pstmt = conn.prepareStatement( |
|
195 |
"SELECT max(local_id) FROM xml_acc_numbers " + |
|
196 |
"WHERE global_name LIKE ?"); |
|
197 |
pstmt.setString(1,globalName); |
|
198 |
pstmt.execute(); |
|
199 |
ResultSet rs = pstmt.getResultSet(); |
|
200 |
boolean hasLocalId = rs.next(); |
|
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(); |
|
201 | 227 |
|
202 |
if (hasLocalId) |
|
203 |
return rs.getInt(1); |
|
228 |
if (hasLocalId) { |
|
229 |
return rs.getInt(1); |
|
230 |
} |
|
204 | 231 |
|
205 |
pstmt.close(); |
|
206 |
} catch (SQLException e) { |
|
207 |
System.err.println( |
|
208 |
"Error on AccessionNumber.get(): " + e.getMessage()); |
|
209 |
throw e; |
|
210 |
} |
|
232 |
pstmt.close(); |
|
233 |
} catch (SQLException e) { |
|
234 |
throw new SQLException( |
|
235 |
"Error on AccessionNumber.get(globalName): " + e.getMessage()); |
|
236 |
} |
|
211 | 237 |
|
212 |
return 0;
|
|
213 |
}
|
|
238 |
return 0; |
|
239 |
} |
|
214 | 240 |
|
215 | 241 |
|
216 |
// get the global part of the accession number
|
|
217 |
private String getGlobalName (String accNumber, String sep)
|
|
218 |
throws StringIndexOutOfBoundsException { |
|
242 |
// get the global part of the accession number |
|
243 |
private String getGlobalName (String accNumber, String sep) |
|
244 |
throws StringIndexOutOfBoundsException {
|
|
219 | 245 |
|
220 |
return accNumber.substring(0, accNumber.lastIndexOf(sep));
|
|
221 |
}
|
|
246 |
return accNumber.substring(0, accNumber.lastIndexOf(sep)); |
|
247 |
} |
|
222 | 248 |
|
223 |
// get the local part of the accession number
|
|
224 |
private String getLocalId (String accNumber, String sep)
|
|
225 |
throws StringIndexOutOfBoundsException { |
|
249 |
// get the local part of the accession number |
|
250 |
private String getLocalId (String accNumber, String sep) |
|
251 |
throws StringIndexOutOfBoundsException {
|
|
226 | 252 |
|
227 |
return accNumber.substring(accNumber.lastIndexOf(sep)+1);
|
|
228 |
}
|
|
253 |
return accNumber.substring(accNumber.lastIndexOf(sep)+1); |
|
254 |
} |
|
229 | 255 |
} |
230 | 256 |
|
231 | 257 |
/** |
232 | 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 |
* ' |
|
233 | 263 |
* 'Revision 1.11 2000/08/14 20:53:33 jones |
234 | 264 |
* 'Added "release" keyword to all metacat source files so that the release |
235 | 265 |
* 'number will be evident in software distributions. |
Also available in: Unified diff
change Assession# generation to use the same db connection