1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that represents a docid
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Chad Berkley
|
7
|
*
|
8
|
* '$Author: jones $'
|
9
|
* '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
|
10
|
* '$Revision: 3077 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat;
|
28
|
|
29
|
import java.io.*;
|
30
|
import java.util.*;
|
31
|
import java.lang.*;
|
32
|
import java.sql.*;
|
33
|
|
34
|
/**
|
35
|
* A class to parse document ids
|
36
|
* The docid is of the form siteCode.uniqueId.rev
|
37
|
*/
|
38
|
public class DocumentIdentifier
|
39
|
{
|
40
|
private String docid = null;
|
41
|
private String rev = null;
|
42
|
private String uniqueId = null;
|
43
|
private String siteCode = null;
|
44
|
private String separator = null;
|
45
|
private MetaCatUtil util = new MetaCatUtil();
|
46
|
|
47
|
/**
|
48
|
* Constructor to build a docid object and parse an incoming string.
|
49
|
*/
|
50
|
public DocumentIdentifier(String docid) throws AccessionNumberException
|
51
|
{
|
52
|
this.docid = docid;
|
53
|
this.separator = util.getOption("accNumSeparator");
|
54
|
|
55
|
if(docid.endsWith(this.separator))
|
56
|
{
|
57
|
throw new AccessionNumberException("Accession number cannot end with " +
|
58
|
"a seperator.");
|
59
|
}
|
60
|
if(docid.startsWith(this.separator))
|
61
|
{
|
62
|
throw new AccessionNumberException("Accession number cannot begin with " +
|
63
|
"a seperator.");
|
64
|
}
|
65
|
|
66
|
parseDocid();
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* parses the docid into its parts
|
71
|
*/
|
72
|
private void parseDocid() throws AccessionNumberException
|
73
|
{
|
74
|
try {
|
75
|
int firstIndex = docid.indexOf(separator);
|
76
|
int lastIndex = docid.lastIndexOf(separator);
|
77
|
if(firstIndex != lastIndex)
|
78
|
{ //this docid contains a revision number
|
79
|
rev = docid.substring(lastIndex + 1);
|
80
|
uniqueId = docid.substring(firstIndex + 1, lastIndex);
|
81
|
siteCode = docid.substring(0, firstIndex);
|
82
|
}
|
83
|
else
|
84
|
{
|
85
|
uniqueId = docid.substring(firstIndex + 1);
|
86
|
siteCode = docid.substring(0, firstIndex);
|
87
|
rev = getNewestRev();
|
88
|
}
|
89
|
} catch (StringIndexOutOfBoundsException e) {
|
90
|
throw new
|
91
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
92
|
"Use accession number format as: " +
|
93
|
"sitecode" + separator + "uniqueid" +
|
94
|
separator + "revisionid");
|
95
|
} catch (SQLException e) {
|
96
|
throw new
|
97
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
98
|
"DB Error when reading revisionid");
|
99
|
} catch (ClassNotFoundException e) {
|
100
|
throw new
|
101
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
102
|
e.getMessage());
|
103
|
}
|
104
|
|
105
|
try {
|
106
|
if(rev.equals("newest")) {
|
107
|
rev = getNewestRev();
|
108
|
}
|
109
|
} catch (SQLException e) {
|
110
|
throw new
|
111
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
112
|
"DB Error when reading revisionid");
|
113
|
} catch (ClassNotFoundException e) {
|
114
|
throw new
|
115
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
116
|
e.getMessage());
|
117
|
}
|
118
|
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* returns the newest revision number for a document
|
123
|
*/
|
124
|
private String getNewestRev() throws SQLException, ClassNotFoundException
|
125
|
{
|
126
|
PreparedStatement pstmt = null;
|
127
|
DBConnection dbConn = null;
|
128
|
int serialNumber = -1;
|
129
|
|
130
|
try {
|
131
|
/*try {
|
132
|
//this connection is prone to error for some reason so we
|
133
|
//try to connect it three times before quiting.
|
134
|
conn = util.openDBConnection();
|
135
|
} catch(SQLException sqle) {
|
136
|
try {
|
137
|
conn = util.openDBConnection();
|
138
|
} catch(SQLException sqlee) {
|
139
|
try {
|
140
|
conn = util.openDBConnection();
|
141
|
} catch(SQLException sqleee) {
|
142
|
System.out.println("error getting db connection in " +
|
143
|
"MetacatReplication.handleGetDocumentRequest: " +
|
144
|
sqleee.getMessage());
|
145
|
}
|
146
|
}
|
147
|
}*/
|
148
|
dbConn=DBConnectionPool.
|
149
|
getDBConnection("DocumentIdentifier.getNewestRev");
|
150
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
151
|
pstmt = dbConn.prepareStatement("select rev from xml_documents where " +
|
152
|
"docid like '" + docid + "'");
|
153
|
pstmt.execute();
|
154
|
ResultSet rs = pstmt.getResultSet();
|
155
|
boolean tablehasrows = rs.next();
|
156
|
if (tablehasrows) {
|
157
|
String retStr = rs.getString(1);
|
158
|
pstmt.close();
|
159
|
//conn.close();
|
160
|
return retStr;
|
161
|
}
|
162
|
//conn.close();
|
163
|
} catch(SQLException e) {
|
164
|
System.out.println("error in DocumentIdentifier.getNewestRev(): " +
|
165
|
e.getMessage());
|
166
|
/*try {
|
167
|
conn.close();
|
168
|
} catch(SQLException e2) { throw e2; }*/
|
169
|
throw e;
|
170
|
}
|
171
|
finally
|
172
|
{
|
173
|
try
|
174
|
{
|
175
|
pstmt.close();
|
176
|
}//try
|
177
|
finally
|
178
|
{
|
179
|
DBConnectionPool.returnDBConnection(dbConn,serialNumber);
|
180
|
}//finally
|
181
|
}//finally
|
182
|
|
183
|
|
184
|
|
185
|
return "1";
|
186
|
}
|
187
|
|
188
|
private int countSeparator()
|
189
|
{
|
190
|
int count = 0;
|
191
|
for(int i=0; i<docid.length(); i++)
|
192
|
{
|
193
|
if(docid.charAt(i) == '.')
|
194
|
{
|
195
|
count++;
|
196
|
}
|
197
|
}
|
198
|
return count;
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* returns the revision number encoded in this docid
|
203
|
*/
|
204
|
public String getRev()
|
205
|
{
|
206
|
return rev;
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* returns the uniqueId encoded in this docid
|
211
|
*/
|
212
|
public String getUniqueId()
|
213
|
{
|
214
|
return uniqueId;
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* returns the siteCode encoded in this docid
|
219
|
*/
|
220
|
public String getSiteCode()
|
221
|
{
|
222
|
return siteCode;
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* returns the separator used in the accession number
|
227
|
*/
|
228
|
public String getSeparator()
|
229
|
{
|
230
|
return separator;
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* returns <siteCode><sepatator><uniqueId>
|
235
|
*/
|
236
|
public String getIdentifier()
|
237
|
{
|
238
|
return siteCode + separator + uniqueId;
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* returns the whole docid as a string
|
243
|
*/
|
244
|
public String toString()
|
245
|
{
|
246
|
return docid;
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Test driver. The first command line argument is the docid you want to
|
251
|
* create an object for. For instance ">java DocumentIdentifer nceas.1.2"
|
252
|
* will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
|
253
|
*/
|
254
|
public static void main(String args[])
|
255
|
{
|
256
|
try
|
257
|
{
|
258
|
DocumentIdentifier d = new DocumentIdentifier(args[0]);
|
259
|
System.out.println("rev: " + d.getRev());
|
260
|
System.out.println("uniqueId: " + d.getUniqueId());
|
261
|
System.out.println("siteCode: " + d.getSiteCode());
|
262
|
}
|
263
|
catch(Exception e)
|
264
|
{
|
265
|
System.out.println("error: " + e.getMessage());
|
266
|
}
|
267
|
}
|
268
|
}
|