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: daigle $'
|
9
|
* '$Date: 2008-08-05 17:33:45 -0700 (Tue, 05 Aug 2008) $'
|
10
|
* '$Revision: 4212 $'
|
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
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
35
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
36
|
|
37
|
/**
|
38
|
* A class to parse document ids
|
39
|
* The docid is of the form siteCode.uniqueId.rev
|
40
|
*/
|
41
|
public class DocumentIdentifier
|
42
|
{
|
43
|
private String docid = null;
|
44
|
private String rev = null;
|
45
|
private String uniqueId = null;
|
46
|
private String siteCode = null;
|
47
|
private String separator = null;
|
48
|
|
49
|
/**
|
50
|
* Constructor to build a docid object and parse an incoming string.
|
51
|
*/
|
52
|
public DocumentIdentifier(String docid) throws AccessionNumberException {
|
53
|
this.docid = docid;
|
54
|
try {
|
55
|
this.separator = PropertyService.getProperty("document.accNumSeparator");
|
56
|
} catch (PropertyNotFoundException pnfe) {
|
57
|
throw new AccessionNumberException(
|
58
|
"Could not get property 'accNumSeparator' :" + pnfe.getMessage());
|
59
|
}
|
60
|
|
61
|
if (docid.endsWith(this.separator)) {
|
62
|
throw new AccessionNumberException("Accession number cannot end with "
|
63
|
+ "a seperator.");
|
64
|
}
|
65
|
if (docid.startsWith(this.separator)) {
|
66
|
throw new AccessionNumberException("Accession number cannot begin with "
|
67
|
+ "a seperator.");
|
68
|
}
|
69
|
|
70
|
parseDocid();
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* parses the docid into its parts
|
75
|
*/
|
76
|
private void parseDocid() throws AccessionNumberException
|
77
|
{
|
78
|
try {
|
79
|
int firstIndex = docid.indexOf(separator);
|
80
|
int lastIndex = docid.lastIndexOf(separator);
|
81
|
if(firstIndex != lastIndex)
|
82
|
{ //this docid contains a revision number
|
83
|
rev = docid.substring(lastIndex + 1);
|
84
|
uniqueId = docid.substring(firstIndex + 1, lastIndex);
|
85
|
siteCode = docid.substring(0, firstIndex);
|
86
|
}
|
87
|
else
|
88
|
{
|
89
|
uniqueId = docid.substring(firstIndex + 1);
|
90
|
siteCode = docid.substring(0, firstIndex);
|
91
|
rev = getNewestRev();
|
92
|
}
|
93
|
} catch (StringIndexOutOfBoundsException e) {
|
94
|
throw new
|
95
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
96
|
"Use accession number format as: " +
|
97
|
"sitecode" + separator + "uniqueid" +
|
98
|
separator + "revisionid");
|
99
|
} catch (SQLException e) {
|
100
|
throw new
|
101
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
102
|
"DB Error when reading revisionid");
|
103
|
} catch (ClassNotFoundException e) {
|
104
|
throw new
|
105
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
106
|
e.getMessage());
|
107
|
}
|
108
|
|
109
|
try {
|
110
|
if(rev.equals("newest")) {
|
111
|
rev = getNewestRev();
|
112
|
}
|
113
|
} catch (SQLException e) {
|
114
|
throw new
|
115
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
116
|
"DB Error when reading revisionid");
|
117
|
} catch (ClassNotFoundException e) {
|
118
|
throw new
|
119
|
AccessionNumberException("Error in DocumentIdentifier.parseDocid(). " +
|
120
|
e.getMessage());
|
121
|
}
|
122
|
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* returns the newest revision number for a document
|
127
|
*/
|
128
|
private String getNewestRev() throws SQLException, ClassNotFoundException
|
129
|
{
|
130
|
PreparedStatement pstmt = null;
|
131
|
DBConnection dbConn = null;
|
132
|
int serialNumber = -1;
|
133
|
|
134
|
try {
|
135
|
/*try {
|
136
|
//this connection is prone to error for some reason so we
|
137
|
//try to connect it three times before quiting.
|
138
|
conn = util.openDBConnection();
|
139
|
} catch(SQLException sqle) {
|
140
|
try {
|
141
|
conn = util.openDBConnection();
|
142
|
} catch(SQLException sqlee) {
|
143
|
try {
|
144
|
conn = util.openDBConnection();
|
145
|
} catch(SQLException sqleee) {
|
146
|
System.out.println("error getting db connection in " +
|
147
|
"MetacatReplication.handleGetDocumentRequest: " +
|
148
|
sqleee.getMessage());
|
149
|
}
|
150
|
}
|
151
|
}*/
|
152
|
dbConn=DBConnectionPool.
|
153
|
getDBConnection("DocumentIdentifier.getNewestRev");
|
154
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
155
|
pstmt = dbConn.prepareStatement("select rev from xml_documents where " +
|
156
|
"docid like '" + docid + "'");
|
157
|
pstmt.execute();
|
158
|
ResultSet rs = pstmt.getResultSet();
|
159
|
boolean tablehasrows = rs.next();
|
160
|
if (tablehasrows) {
|
161
|
String retStr = rs.getString(1);
|
162
|
pstmt.close();
|
163
|
//conn.close();
|
164
|
return retStr;
|
165
|
}
|
166
|
//conn.close();
|
167
|
} catch(SQLException e) {
|
168
|
System.out.println("error in DocumentIdentifier.getNewestRev(): " +
|
169
|
e.getMessage());
|
170
|
/*try {
|
171
|
conn.close();
|
172
|
} catch(SQLException e2) { throw e2; }*/
|
173
|
throw e;
|
174
|
}
|
175
|
finally
|
176
|
{
|
177
|
try
|
178
|
{
|
179
|
pstmt.close();
|
180
|
}//try
|
181
|
finally
|
182
|
{
|
183
|
DBConnectionPool.returnDBConnection(dbConn,serialNumber);
|
184
|
}//finally
|
185
|
}//finally
|
186
|
|
187
|
|
188
|
|
189
|
return "1";
|
190
|
}
|
191
|
|
192
|
private int countSeparator()
|
193
|
{
|
194
|
int count = 0;
|
195
|
for(int i=0; i<docid.length(); i++)
|
196
|
{
|
197
|
if(docid.charAt(i) == '.')
|
198
|
{
|
199
|
count++;
|
200
|
}
|
201
|
}
|
202
|
return count;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* returns the revision number encoded in this docid
|
207
|
*/
|
208
|
public String getRev()
|
209
|
{
|
210
|
return rev;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* returns the uniqueId encoded in this docid
|
215
|
*/
|
216
|
public String getUniqueId()
|
217
|
{
|
218
|
return uniqueId;
|
219
|
}
|
220
|
|
221
|
/**
|
222
|
* returns the siteCode encoded in this docid
|
223
|
*/
|
224
|
public String getSiteCode()
|
225
|
{
|
226
|
return siteCode;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* returns the separator used in the accession number
|
231
|
*/
|
232
|
public String getSeparator()
|
233
|
{
|
234
|
return separator;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* returns <siteCode><sepatator><uniqueId>
|
239
|
*/
|
240
|
public String getIdentifier()
|
241
|
{
|
242
|
return siteCode + separator + uniqueId;
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* returns the whole docid as a string
|
247
|
*/
|
248
|
public String toString()
|
249
|
{
|
250
|
return docid;
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* Test driver. The first command line argument is the docid you want to
|
255
|
* create an object for. For instance ">java DocumentIdentifer nceas.1.2"
|
256
|
* will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
|
257
|
*/
|
258
|
public static void main(String args[])
|
259
|
{
|
260
|
try
|
261
|
{
|
262
|
DocumentIdentifier d = new DocumentIdentifier(args[0]);
|
263
|
System.out.println("rev: " + d.getRev());
|
264
|
System.out.println("uniqueId: " + d.getUniqueId());
|
265
|
System.out.println("siteCode: " + d.getSiteCode());
|
266
|
}
|
267
|
catch(Exception e)
|
268
|
{
|
269
|
System.out.println("error: " + e.getMessage());
|
270
|
}
|
271
|
}
|
272
|
}
|