1
|
/**
|
2
|
* Name: AccessionNumber.java
|
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
|
8
|
*
|
9
|
* Version: '$Id: AccessionNumber.java 147 2000-06-12 16:06:24Z bojilova $'
|
10
|
*/
|
11
|
|
12
|
package edu.ucsb.nceas.metacat;
|
13
|
|
14
|
import java.net.*;
|
15
|
import java.sql.*;
|
16
|
|
17
|
/**
|
18
|
* A class that gets Accession Number, check for uniqueness and register it
|
19
|
* into db connection
|
20
|
*/
|
21
|
public class AccessionNumber {
|
22
|
|
23
|
String globalName;
|
24
|
String localId;
|
25
|
String accNumber;
|
26
|
Connection conn;
|
27
|
|
28
|
/** Split the Accession Number in 2 parts by ":" character
|
29
|
*
|
30
|
* @param conn the JDBC Connection to which all information is written
|
31
|
* @param accNumber the Accession Number provided for processing
|
32
|
*
|
33
|
*/
|
34
|
public AccessionNumber (Connection conn, String accNumber) {
|
35
|
|
36
|
this.conn = conn;
|
37
|
this.globalName = getGlobalName(accNumber);
|
38
|
this.localId = getLocalId(accNumber);
|
39
|
this.accNumber = put();
|
40
|
|
41
|
}
|
42
|
|
43
|
/** check for existance of Accesssion Number */
|
44
|
public boolean check (String globalName, String localId) {
|
45
|
|
46
|
boolean hasAccNumber = false;
|
47
|
|
48
|
try {
|
49
|
PreparedStatement pstmt;
|
50
|
pstmt = conn.prepareStatement("SELECT 'x' FROM xml_acc_numbers " +
|
51
|
"WHERE global_name LIKE ? AND local_id = ?");
|
52
|
pstmt.setString(1,globalName);
|
53
|
pstmt.setString(2,localId);
|
54
|
pstmt.execute();
|
55
|
ResultSet rs = pstmt.getResultSet();
|
56
|
hasAccNumber = rs.next();
|
57
|
pstmt.close();
|
58
|
|
59
|
} catch (SQLException e) {
|
60
|
System.out.println("Error on AccessionNumber.check(globalName, localId): " + e.getMessage());
|
61
|
}
|
62
|
|
63
|
return hasAccNumber;
|
64
|
}
|
65
|
|
66
|
//** get the last in order local ID by a given global name */
|
67
|
public int get (String globalName) {
|
68
|
|
69
|
try {
|
70
|
PreparedStatement pstmt;
|
71
|
pstmt = conn.prepareStatement("SELECT max(local_id) FROM xml_acc_numbers " +
|
72
|
"WHERE global_name LIKE ?");
|
73
|
pstmt.setString(1,globalName);
|
74
|
pstmt.execute();
|
75
|
ResultSet rs = pstmt.getResultSet();
|
76
|
boolean hasLocalId = rs.next();
|
77
|
|
78
|
if (hasLocalId)
|
79
|
return rs.getInt(1);
|
80
|
|
81
|
pstmt.close();
|
82
|
} catch (SQLException e) {
|
83
|
System.out.println("Error on AccessionNumber.get(): " + e.getMessage());
|
84
|
}
|
85
|
|
86
|
return -1;
|
87
|
}
|
88
|
|
89
|
//** register next unique number for a local ID by a given global name */
|
90
|
public String put () {
|
91
|
|
92
|
if (!check(globalName, localId)) {
|
93
|
put(globalName, localId);
|
94
|
return globalName + ":" + localId;
|
95
|
} else {
|
96
|
// get max local Id, put next Id
|
97
|
int l = get(globalName);
|
98
|
System.out.println(l);
|
99
|
if ( l == -1 ) l = 1;
|
100
|
else l += 1;
|
101
|
put (globalName, (new Integer(l)).toString());
|
102
|
return globalName + ":" + l;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
private void put (String globalName, String localId) {
|
107
|
|
108
|
try {
|
109
|
PreparedStatement pstmt;
|
110
|
pstmt = conn.prepareStatement("INSERT INTO xml_acc_numbers (global_name, local_id) " +
|
111
|
"VALUES (?, ?)");
|
112
|
pstmt.setString(1,globalName);
|
113
|
pstmt.setString(2,localId);
|
114
|
pstmt.execute();
|
115
|
|
116
|
} catch (SQLException e) {
|
117
|
System.out.println("Error on AccessionNumber.put(globalName, localId): " + e.getMessage());
|
118
|
}
|
119
|
}
|
120
|
|
121
|
|
122
|
// get the global part of the accession number
|
123
|
private String getGlobalName (String accNumber)
|
124
|
throws StringIndexOutOfBoundsException {
|
125
|
|
126
|
return accNumber.substring(0, accNumber.lastIndexOf(":"));
|
127
|
}
|
128
|
|
129
|
// get the local part of the accession number
|
130
|
private String getLocalId (String accNumber)
|
131
|
throws StringIndexOutOfBoundsException {
|
132
|
|
133
|
return accNumber.substring(accNumber.lastIndexOf(":")+1);
|
134
|
}
|
135
|
|
136
|
static public void main(String[] args) {
|
137
|
|
138
|
String defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
|
139
|
|
140
|
if (args.length < 4)
|
141
|
{
|
142
|
System.err.println("Wrong number of arguments!!!");
|
143
|
System.err.println("USAGE: java AccessionNumber " +
|
144
|
"<userID> <accessionNumber> <user> <password> [dbstring]");
|
145
|
return;
|
146
|
} else {
|
147
|
try {
|
148
|
|
149
|
String userID = args[0];
|
150
|
String accNum = args[1];
|
151
|
String user = args[2];
|
152
|
String password = args[3];
|
153
|
String dbstring = null;
|
154
|
|
155
|
if (args.length <= 4)
|
156
|
dbstring = defaultDB;
|
157
|
else
|
158
|
dbstring = args[4];
|
159
|
|
160
|
// Open a connection to the database
|
161
|
// Load the Oracle JDBC driver
|
162
|
Class.forName ("oracle.jdbc.driver.OracleDriver");
|
163
|
|
164
|
// Connect to the database
|
165
|
Connection conn = DriverManager.getConnection( dbstring, user, password);
|
166
|
|
167
|
AccessionNumber an = new AccessionNumber(conn, accNum);
|
168
|
System.out.println("Accession Number registration finished for: " + an.accNumber);
|
169
|
|
170
|
} catch (Exception e) {
|
171
|
System.err.println(e.getMessage());
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
|
176
|
}
|