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
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: berkley $'
|
10
|
* '$Date: 2001-01-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
|
11
|
* '$Revision: 675 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.io.*;
|
31
|
import java.util.*;
|
32
|
import java.lang.*;
|
33
|
import java.sql.*;
|
34
|
|
35
|
/**
|
36
|
* A class to parse document ids
|
37
|
* The docid is of the form siteCode.uniqueId.rev
|
38
|
*/
|
39
|
public class DocumentIdentifier
|
40
|
{
|
41
|
private String docid = null;
|
42
|
private String rev = null;
|
43
|
private String uniqueId = null;
|
44
|
private String siteCode = null;
|
45
|
private String separator = null;
|
46
|
private MetaCatUtil util = new MetaCatUtil();
|
47
|
|
48
|
/**
|
49
|
* Constructor to build a docid object and parse an incoming string.
|
50
|
*/
|
51
|
public DocumentIdentifier(String docid) throws AccessionNumberException
|
52
|
{
|
53
|
this.docid = docid;
|
54
|
//int numSeps = countSeparator();
|
55
|
//if(numSeps > 2 || numSeps < 1)
|
56
|
//{
|
57
|
// throw new AccessionNumberException("Accession number can contain " +
|
58
|
// "no more than 2 and no fewer than 1 separators.");
|
59
|
//}
|
60
|
if(docid.endsWith("."))
|
61
|
{
|
62
|
throw new AccessionNumberException("Accession number cannot end with " +
|
63
|
"a seperator.");
|
64
|
}
|
65
|
if(docid.startsWith("."))
|
66
|
{
|
67
|
throw new AccessionNumberException("Accession number cannot begin with " +
|
68
|
"a seperator.");
|
69
|
}
|
70
|
|
71
|
parseDocid();
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* parses the docid into its parts
|
76
|
*/
|
77
|
private void parseDocid()
|
78
|
{
|
79
|
separator = util.getOption("accNumSeparator");
|
80
|
|
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
|
|
96
|
if(rev.equals("newest"))
|
97
|
{
|
98
|
rev = getNewestRev();
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* returns the newest revision number for a document
|
104
|
*/
|
105
|
private String getNewestRev()
|
106
|
{
|
107
|
PreparedStatement pstmt;
|
108
|
Connection conn = null;
|
109
|
|
110
|
try
|
111
|
{
|
112
|
conn = util.openDBConnection();
|
113
|
pstmt = conn.prepareStatement("select rev from xml_documents where " +
|
114
|
"docid like '" + docid + "'");
|
115
|
pstmt.execute();
|
116
|
ResultSet rs = pstmt.getResultSet();
|
117
|
boolean tablehasrows = rs.next();
|
118
|
if(tablehasrows)
|
119
|
{
|
120
|
String retStr = rs.getString(1);
|
121
|
pstmt.close();
|
122
|
conn.close();
|
123
|
return retStr;
|
124
|
}
|
125
|
}
|
126
|
catch(Exception e)
|
127
|
{
|
128
|
try
|
129
|
{
|
130
|
conn.close();
|
131
|
}
|
132
|
catch(Exception e2)
|
133
|
{}
|
134
|
System.out.println("error closing db connection in " +
|
135
|
"DocumentIdentifier.getNewestRev " +
|
136
|
e.getMessage());
|
137
|
}
|
138
|
return "1";
|
139
|
}
|
140
|
|
141
|
private int countSeparator()
|
142
|
{
|
143
|
int count = 0;
|
144
|
for(int i=0; i<docid.length(); i++)
|
145
|
{
|
146
|
if(docid.charAt(i) == '.')
|
147
|
{
|
148
|
count++;
|
149
|
}
|
150
|
}
|
151
|
return count;
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* returns the revision number encoded in this docid
|
156
|
*/
|
157
|
public String getRev()
|
158
|
{
|
159
|
return rev;
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* returns the uniqueId encoded in this docid
|
164
|
*/
|
165
|
public String getUniqueId()
|
166
|
{
|
167
|
return uniqueId;
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* returns the siteCode encoded in this docid
|
172
|
*/
|
173
|
public String getSiteCode()
|
174
|
{
|
175
|
return siteCode;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* returns the separator used in the accession number
|
180
|
*/
|
181
|
public String getSeparator()
|
182
|
{
|
183
|
return separator;
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* returns <siteCode><sepatator><uniqueId>
|
188
|
*/
|
189
|
public String getIdentifier()
|
190
|
{
|
191
|
return siteCode + separator + uniqueId;
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* returns the whole docid as a string
|
196
|
*/
|
197
|
public String toString()
|
198
|
{
|
199
|
return docid;
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Test driver. The first command line argument is the docid you want to
|
204
|
* create an object for. For instance ">java DocumentIdentifer nceas.1.2"
|
205
|
* will return "rev: 2 \n uniqueId: 1 \n siteCode: nceas \n"
|
206
|
*/
|
207
|
public static void main(String args[])
|
208
|
{
|
209
|
try
|
210
|
{
|
211
|
DocumentIdentifier d = new DocumentIdentifier(args[0]);
|
212
|
System.out.println("rev: " + d.getRev());
|
213
|
System.out.println("uniqueId: " + d.getUniqueId());
|
214
|
System.out.println("siteCode: " + d.getSiteCode());
|
215
|
}
|
216
|
catch(Exception e)
|
217
|
{
|
218
|
System.out.println("error: " + e.getMessage());
|
219
|
}
|
220
|
}
|
221
|
}
|