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