Revision 160
Added by bojilova over 24 years ago
src/edu/ucsb/nceas/metacat/AccessionNumber.java | ||
---|---|---|
13 | 13 |
|
14 | 14 |
import java.net.*; |
15 | 15 |
import java.sql.*; |
16 |
import java.util.PropertyResourceBundle; |
|
16 | 17 |
|
17 | 18 |
/** |
18 | 19 |
* A class that gets Accession Number, check for uniqueness and register it |
... | ... | |
20 | 21 |
*/ |
21 | 22 |
public class AccessionNumber { |
22 | 23 |
|
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) { |
|
24 |
public static String generate (String accNumber, String action) |
|
25 |
throws ClassNotFoundException, StringIndexOutOfBoundsException, SQLException |
|
26 |
{ |
|
35 | 27 |
|
36 |
this.conn = conn; |
|
37 |
this.globalName = getGlobalName(accNumber); |
|
38 |
this.localId = getLocalId(accNumber); |
|
39 |
this.accNumber = put(); |
|
40 |
|
|
28 |
String globalName = null; |
|
29 |
String localId = null; |
|
30 |
PropertyResourceBundle options = null; |
|
31 |
|
|
32 |
options = (PropertyResourceBundle)PropertyResourceBundle.getBundle("metacat"); |
|
33 |
String dbDriver = (String)options.handleGetObject("dbDriver"); |
|
34 |
String defaultDB = (String)options.handleGetObject("defaultDB"); |
|
35 |
String user = (String)options.handleGetObject("user"); |
|
36 |
String password = (String)options.handleGetObject("password"); |
|
37 |
String defaultGlobalName = (String)options.handleGetObject("defaultGlobalName"); |
|
38 |
String sep = (String)options.handleGetObject("accNumberSeparator"); |
|
39 |
|
|
40 |
|
|
41 |
try { |
|
42 |
// Open a new connection to the database |
|
43 |
Connection conn = MetaCatUtil.openDBConnection(dbDriver, defaultDB, user, password); |
|
44 |
conn.setAutoCommit(true); |
|
45 |
|
|
46 |
// split the acc # in 2 parts - global name & local id |
|
47 |
if ( accNumber != null ) { |
|
48 |
globalName = getGlobalName(accNumber, sep); |
|
49 |
localId = getLocalId(accNumber, sep); |
|
50 |
} |
|
51 |
|
|
52 |
// register unique acc # |
|
53 |
if ( action == "INSERT" ) |
|
54 |
if ( accNumber == null ) |
|
55 |
return put(conn, defaultGlobalName, null, sep); |
|
56 |
else |
|
57 |
return put(conn, globalName, localId, sep); |
|
58 |
|
|
59 |
} catch (SQLException e) { |
|
60 |
System.out.println("Error on AccessionNumber.genAccessionNumber(): " + e.getMessage()); |
|
61 |
throw e; |
|
62 |
} |
|
63 |
|
|
64 |
return null; |
|
41 | 65 |
} |
42 |
|
|
66 |
|
|
67 |
private static String put (Connection conn, String globalName, String localId, String sep) |
|
68 |
throws SQLException |
|
69 |
{ |
|
70 |
|
|
71 |
Integer l = null; |
|
72 |
try { |
|
73 |
if ( localId == null ) |
|
74 |
l = new Integer(get(conn, globalName) + 1); |
|
75 |
else if ( !unique(conn, globalName, localId) ) |
|
76 |
l = new Integer(get(conn, globalName) + 1); |
|
77 |
else |
|
78 |
l = new Integer(localId); |
|
79 |
|
|
80 |
// insert globalName & l |
|
81 |
PreparedStatement pstmt; |
|
82 |
pstmt = conn.prepareStatement("INSERT INTO xml_acc_numbers (global_name, local_id) " + |
|
83 |
"VALUES (?, ?)"); |
|
84 |
pstmt.setString(1,globalName); |
|
85 |
pstmt.setString(2,l.toString()); |
|
86 |
pstmt.execute(); |
|
87 |
|
|
88 |
} catch (SQLException e) { |
|
89 |
System.out.println("Error on AccessionNumber.put(conn, globalName, localId): " + e.getMessage()); |
|
90 |
throw e; |
|
91 |
} |
|
92 |
return globalName + sep + l; |
|
93 |
} |
|
94 |
|
|
43 | 95 |
/** check for existance of Accesssion Number */ |
44 |
public boolean check (String globalName, String localId) { |
|
96 |
private static boolean unique (Connection conn, String globalName, String localId) |
|
97 |
throws SQLException |
|
98 |
{ |
|
45 | 99 |
|
46 | 100 |
boolean hasAccNumber = false; |
47 | 101 |
|
... | ... | |
57 | 111 |
pstmt.close(); |
58 | 112 |
|
59 | 113 |
} catch (SQLException e) { |
60 |
System.out.println("Error on AccessionNumber.check(globalName, localId): " + e.getMessage()); |
|
114 |
System.out.println("Error on AccessionNumber.unique(globalName, localId): " + e.getMessage()); |
|
115 |
throw e; |
|
61 | 116 |
} |
62 | 117 |
|
63 | 118 |
return hasAccNumber; |
64 | 119 |
} |
65 | 120 |
|
66 | 121 |
//** get the last in order local ID by a given global name */ |
67 |
public int get (String globalName) { |
|
68 |
|
|
122 |
private static int get (Connection conn, String globalName) |
|
123 |
throws SQLException |
|
124 |
{ |
|
69 | 125 |
try { |
70 | 126 |
PreparedStatement pstmt; |
71 | 127 |
pstmt = conn.prepareStatement("SELECT max(local_id) FROM xml_acc_numbers " + |
... | ... | |
81 | 137 |
pstmt.close(); |
82 | 138 |
} catch (SQLException e) { |
83 | 139 |
System.out.println("Error on AccessionNumber.get(): " + e.getMessage()); |
140 |
throw e; |
|
84 | 141 |
} |
85 | 142 |
|
86 |
return -1;
|
|
143 |
return 0;
|
|
87 | 144 |
} |
88 | 145 |
|
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 | 146 |
|
121 |
|
|
122 | 147 |
// get the global part of the accession number |
123 |
private String getGlobalName (String accNumber)
|
|
148 |
private static String getGlobalName (String accNumber, String sep)
|
|
124 | 149 |
throws StringIndexOutOfBoundsException { |
125 | 150 |
|
126 |
return accNumber.substring(0, accNumber.lastIndexOf(":"));
|
|
151 |
return accNumber.substring(0, accNumber.lastIndexOf(sep));
|
|
127 | 152 |
} |
128 | 153 |
|
129 | 154 |
// get the local part of the accession number |
130 |
private String getLocalId (String accNumber)
|
|
155 |
private static String getLocalId (String accNumber, String sep)
|
|
131 | 156 |
throws StringIndexOutOfBoundsException { |
132 | 157 |
|
133 |
return accNumber.substring(accNumber.lastIndexOf(":")+1);
|
|
158 |
return accNumber.substring(accNumber.lastIndexOf(sep)+1);
|
|
134 | 159 |
} |
135 | 160 |
|
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 | 161 |
} |
Also available in: Unified diff
gen acc # in new connection
changing to static methods
include action parameter