1
|
/**
|
2
|
* Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
|
3
|
* License, Version 2.0 (the "License"); you may not use this file except in
|
4
|
* compliance with the License. You may obtain a copy of the License at
|
5
|
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
|
6
|
* agreed to in writing, software distributed under the License is distributed on
|
7
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
8
|
* or implied. See the License for the specific language governing permissions and
|
9
|
* limitations under the License.
|
10
|
*/
|
11
|
package edu.ucsb.nceas.metacat.oaipmh.provider.server.catalog;
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.Properties;
|
16
|
import java.util.StringTokenizer;
|
17
|
|
18
|
import edu.ucsb.nceas.metacat.oaipmh.provider.server.OAIHandler;
|
19
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
20
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
|
24
|
import ORG.oclc.oai.server.catalog.RecordFactory;
|
25
|
|
26
|
|
27
|
/**
|
28
|
* MetacatRecordFactory converts native Metacat documents to OAI-PMH records.
|
29
|
*/
|
30
|
public class MetacatRecordFactory extends RecordFactory {
|
31
|
|
32
|
private static final Logger logger =
|
33
|
Logger.getLogger(MetacatRecordFactory.class);
|
34
|
|
35
|
private String repositoryIdentifier = null;
|
36
|
private String context = null;
|
37
|
private final String TEST_CONTEXT = "knb";
|
38
|
private final String LSID_PREFIX = "urn:lsid:knb.ecoinformatics.org:";
|
39
|
|
40
|
|
41
|
/**
|
42
|
* Construct a MetacatRecordFactory capable of producing the Crosswalk(s)
|
43
|
* specified in the properties file.
|
44
|
*
|
45
|
* @param properties
|
46
|
* Contains information to configure the factory: specifically, the
|
47
|
* names of the crosswalk(s) supported
|
48
|
* @exception IllegalArgumentException
|
49
|
* Something is wrong with the argument.
|
50
|
*/
|
51
|
public MetacatRecordFactory(Properties properties)
|
52
|
throws IllegalArgumentException {
|
53
|
super(properties);
|
54
|
|
55
|
if (OAIHandler.isIntegratedWithMetacat()) {
|
56
|
try {
|
57
|
context = PropertyService.getProperty("application.context");
|
58
|
}
|
59
|
catch (PropertyNotFoundException e) {
|
60
|
logger.error("PropertyNotFoundException: " +
|
61
|
"unable to determine application.context value");
|
62
|
}
|
63
|
}
|
64
|
else {
|
65
|
context = TEST_CONTEXT;
|
66
|
}
|
67
|
|
68
|
repositoryIdentifier=properties.getProperty("oaipmh.repositoryIdentifier");
|
69
|
if (repositoryIdentifier == null) {
|
70
|
String errorStr =
|
71
|
"oaipmh.repositoryIdentifier is missing from the properties file";
|
72
|
throw new IllegalArgumentException(errorStr);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
|
77
|
/**
|
78
|
* Utility method to parse the 'local identifier' from the OAI identifier
|
79
|
*
|
80
|
* @param oaiIdentifier OAI identifier e.g.
|
81
|
* "urn:lsid:knb.ecoinformatics.org:knb-lter-gce:169"
|
82
|
*
|
83
|
* @return local identifier, e.g. "knb-lter-gce.169"
|
84
|
*/
|
85
|
public String fromOAIIdentifier(String oaiIdentifier) {
|
86
|
String localIdentifier = null;
|
87
|
|
88
|
if (oaiIdentifier != null) {
|
89
|
String[] oaiIdentifierArray = splitOAIIdentifier(oaiIdentifier);
|
90
|
int len = oaiIdentifierArray.length;
|
91
|
if (len >= 2) {
|
92
|
String scope = oaiIdentifierArray[len - 2];
|
93
|
String identifier = oaiIdentifierArray[len - 1];
|
94
|
localIdentifier = scope + "." + identifier;
|
95
|
}
|
96
|
}
|
97
|
|
98
|
return localIdentifier;
|
99
|
}
|
100
|
|
101
|
|
102
|
/**
|
103
|
* Construct an OAI identifier from the native item
|
104
|
*
|
105
|
* @param nativeItem native Item object
|
106
|
* @return OAI identifier,
|
107
|
* e.g. urn:lsid:knb.ecoinformatics.org:knb-lter-gce:169
|
108
|
*/
|
109
|
public String getOAIIdentifier(Object nativeItem) {
|
110
|
String localIdentifier = getLocalIdentifier(nativeItem);
|
111
|
StringBuffer sb = new StringBuffer();
|
112
|
|
113
|
if (localIdentifier != null) {
|
114
|
String[] localIdentifierArray = splitLocalIdentifier(localIdentifier);
|
115
|
|
116
|
if (localIdentifierArray.length == 2) {
|
117
|
sb.append(LSID_PREFIX);
|
118
|
String scope = localIdentifierArray[0];
|
119
|
sb.append(scope);
|
120
|
sb.append(":");
|
121
|
String identifier = localIdentifierArray[1];
|
122
|
sb.append(identifier);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
return sb.toString();
|
127
|
}
|
128
|
|
129
|
|
130
|
/**
|
131
|
* Construct an OAI identifier from the native item
|
132
|
*
|
133
|
* @param nativeItem
|
134
|
* native Item object
|
135
|
* @return OAI identifier
|
136
|
*/
|
137
|
public String getOAIIdentifierOld(Object nativeItem) {
|
138
|
String localIdentifier = getLocalIdentifier(nativeItem);
|
139
|
StringBuffer sb = new StringBuffer();
|
140
|
|
141
|
sb.append("http://");
|
142
|
sb.append(repositoryIdentifier);
|
143
|
sb.append("/" + context + "/metacat/");
|
144
|
sb.append(localIdentifier);
|
145
|
|
146
|
return sb.toString();
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Extract the local identifier from the native item
|
152
|
*
|
153
|
* @param nativeItem
|
154
|
* native Item object
|
155
|
* @return local identifier
|
156
|
*/
|
157
|
public String getLocalIdentifier(Object nativeItem) {
|
158
|
HashMap nativeItemMap = (HashMap) nativeItem;
|
159
|
String localIdentifier = (String) nativeItemMap.get("localIdentifier");
|
160
|
return localIdentifier;
|
161
|
}
|
162
|
|
163
|
|
164
|
/**
|
165
|
* get the datestamp from the item
|
166
|
*
|
167
|
* @param nativeItem
|
168
|
* a native item presumably containing a datestamp somewhere
|
169
|
* @return a String containing the datestamp for the item
|
170
|
* @exception IllegalArgumentException
|
171
|
* Something is wrong with the argument.
|
172
|
*/
|
173
|
public String getDatestamp(Object nativeItem)
|
174
|
throws IllegalArgumentException {
|
175
|
return (String) ((HashMap) nativeItem).get("lastModified");
|
176
|
}
|
177
|
|
178
|
|
179
|
/**
|
180
|
* get the setspec from the item
|
181
|
*
|
182
|
* @param nativeItem
|
183
|
* a native item presumably containing a setspec somewhere
|
184
|
* @return a String containing the setspec for the item
|
185
|
* @exception IllegalArgumentException
|
186
|
* Something is wrong with the argument.
|
187
|
*/
|
188
|
public Iterator getSetSpecs(Object nativeItem)
|
189
|
throws IllegalArgumentException {
|
190
|
return null;
|
191
|
}
|
192
|
|
193
|
|
194
|
/**
|
195
|
* Get the about elements from the item
|
196
|
*
|
197
|
* @param nativeItem
|
198
|
* a native item presumably containing about information somewhere
|
199
|
* @return a Iterator of Strings containing <about>s for the item
|
200
|
* @exception IllegalArgumentException
|
201
|
* Something is wrong with the argument.
|
202
|
*/
|
203
|
public Iterator getAbouts(Object nativeItem) throws IllegalArgumentException {
|
204
|
return null;
|
205
|
}
|
206
|
|
207
|
|
208
|
/**
|
209
|
* Is the record deleted?
|
210
|
*
|
211
|
* @param nativeItem
|
212
|
* a native item presumably containing a possible delete indicator
|
213
|
* @return true if record is deleted, false if not
|
214
|
* @exception IllegalArgumentException
|
215
|
* Something is wrong with the argument.
|
216
|
*/
|
217
|
public boolean isDeleted(Object nativeItem) throws IllegalArgumentException {
|
218
|
return false;
|
219
|
}
|
220
|
|
221
|
|
222
|
/**
|
223
|
* Allows classes that implement RecordFactory to override the default
|
224
|
* create() method. This is useful, for example, if the entire <record>
|
225
|
* is already packaged as the native record. Return null if you want the
|
226
|
* default handler to create it by calling the methods above individually.
|
227
|
*
|
228
|
* @param nativeItem
|
229
|
* the native record
|
230
|
* @param schemaURL
|
231
|
* the schemaURL desired for the response
|
232
|
* @param the
|
233
|
* metadataPrefix from the request
|
234
|
* @return a String containing the OAI <record> or null if the default
|
235
|
* method should be used.
|
236
|
*/
|
237
|
public String quickCreate(Object nativeItem, String schemaLocation,
|
238
|
String metadataPrefix) {
|
239
|
// Don't perform quick creates
|
240
|
return null;
|
241
|
}
|
242
|
|
243
|
|
244
|
/**
|
245
|
* Convert a native "item" to a "record" String. Use this version of
|
246
|
* createHeader if the setSpecs are derived from the nativeItem itself.
|
247
|
*
|
248
|
* @param nativeItem
|
249
|
* the native record.
|
250
|
* @return String[0] = "header" XML string String[1] = oai-identifier.
|
251
|
* @exception IllegalArgumentException
|
252
|
* One of the header components for this record is bad.
|
253
|
*/
|
254
|
public String[] createHeader(Object nativeItem)
|
255
|
throws IllegalArgumentException {
|
256
|
String oaiIdentifier = getOAIIdentifier(nativeItem);
|
257
|
String datestamp = getDatestamp(nativeItem);
|
258
|
Iterator setSpecs = getSetSpecs(nativeItem);
|
259
|
boolean isDeleted = isDeleted(nativeItem);
|
260
|
|
261
|
String[] headerArray =
|
262
|
createHeader(oaiIdentifier, datestamp, setSpecs, isDeleted);
|
263
|
return headerArray;
|
264
|
}
|
265
|
|
266
|
|
267
|
private String[] splitLocalIdentifier(String s) {
|
268
|
String[] tokenPair = new String[2];
|
269
|
|
270
|
int lastDotIndex = s.lastIndexOf('.');
|
271
|
String scope = s.substring(0, lastDotIndex);
|
272
|
String identifier = s.substring(lastDotIndex + 1);
|
273
|
tokenPair[0] = scope;
|
274
|
tokenPair[1] = identifier;
|
275
|
|
276
|
return tokenPair;
|
277
|
}
|
278
|
|
279
|
|
280
|
private String[] splitOAIIdentifier(String s) {
|
281
|
StringTokenizer tokenizer = new StringTokenizer(s, ":");
|
282
|
String[] tokens = new String[tokenizer.countTokens()];
|
283
|
for (int i=0; i<tokens.length; ++i) {
|
284
|
tokens[i] = tokenizer.nextToken();
|
285
|
}
|
286
|
return tokens;
|
287
|
}
|
288
|
|
289
|
}
|