-
Notifications
You must be signed in to change notification settings - Fork 129
/
FedoraTypesUtils.java
460 lines (407 loc) · 15.7 KB
/
FedoraTypesUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.utils;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import static javax.jcr.PropertyType.BINARY;
import static javax.jcr.query.Query.JCR_SQL2;
import static org.fcrepo.jcr.FedoraJcrTypes.CONTENT_SIZE;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_BINARY;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_DATASTREAM;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_OBJECT;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_RESOURCE;
import static org.fcrepo.kernel.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.modeshape.jcr.api.JcrConstants.JCR_PATH;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collection;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.PropertyDefinition;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.RowIterator;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.services.functions.AnyTypesPredicate;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.modeshape.jcr.api.Namespaced;
import org.slf4j.Logger;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
/**
* Convenience class with static methods for manipulating Fedora types in the
* JCR.
*
* @author ajs6f
* @date Feb 14, 2013
*/
public abstract class FedoraTypesUtils implements FedoraJcrTypes {
static final Logger LOGGER = getLogger(FedoraTypesUtils.class);
/**
* Predicate for determining whether this {@link Node} is a Fedora resource.
*/
public static Predicate<Node> isFedoraResource = new Predicate<Node>() {
@Override
public boolean apply(final Node node) {
checkArgument(node != null, "null cannot be a Fedora object!");
try {
return map(node.getMixinNodeTypes(), nodetype2name).contains(
FEDORA_RESOURCE);
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Predicate for determining whether this {@link Node} is a frozen node
* (a part of the system version history).
*/
public static Predicate<Node> isFrozen = new Predicate<Node>() {
@Override
public boolean apply(final Node node) {
checkArgument(node != null, "null cannot be a Frozen node!");
try {
return node.getPrimaryNodeType().getName().equals(FROZEN_NODE);
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Predicate for determining whether this {@link Node} is a Fedora resource
* or is a frozen node that was a fedora resource.
*/
public static Predicate<Node> isOrWasFedoraResource = new Predicate<Node>() {
@Override
public boolean apply(final Node node) {
checkArgument(node != null, "null cannot be a Fedora object!");
try {
if (node.getPrimaryNodeType().getName().equals(FROZEN_NODE)) {
PropertyIterator it = node.getProperties(FROZEN_MIXIN_TYPES);
while (it.hasNext()) {
for (Value v : it.nextProperty().getValues()) {
if (v.getString().equals(FEDORA_RESOURCE)) {
return true;
}
}
}
return false;
} else {
return map(node.getMixinNodeTypes(), nodetype2name).contains(
FEDORA_RESOURCE);
}
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Predicate for determining whether this {@link Node} is a Fedora object.
*/
public static Predicate<Node> isFedoraObject =
new AnyTypesPredicate(FEDORA_OBJECT);
/**
* Predicate for determining whether this {@link Node} is a Fedora
* datastream.
*/
public static Predicate<Node> isFedoraDatastream =
new AnyTypesPredicate(FEDORA_DATASTREAM);
/**
* Predicate for objects, datastreams, whatever!
*/
public static Predicate<Node> isFedoraObjectOrDatastream =
new AnyTypesPredicate(FEDORA_OBJECT, FEDORA_DATASTREAM);
/**
* Translates a {@link NodeType} to its {@link String} name.
*/
public static Function<NodeType, String> nodetype2name =
new Function<NodeType, String>() {
@Override
public String apply(final NodeType t) {
checkArgument(t != null, "null has no name!");
return t.getName();
}
};
/**
* Translates a JCR {@link Value} to its {@link String} expression.
*/
public static Function<Value, String> value2string =
new Function<Value, String>() {
@Override
public String apply(final Value v) {
try {
checkArgument(v != null, "null has no appropriate "
+ "String representation!");
return v.getString();
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Constructs an {@link Iterator} of {@link Value}s from any
* {@link Property}, multi-valued or not.
*/
public static Function<Property, Iterator<Value>> property2values =
new Function<Property, Iterator<Value>>() {
@Override
public Iterator<Value> apply(final Property p) {
try {
if (p.isMultiple()) {
LOGGER.debug("Found multi-valued property: {}", p);
return Iterators.forArray(p.getValues());
} else {
LOGGER.debug("Found single-valued property: {}", p);
return Iterators.forArray(new Value[] {p.getValue()});
}
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Check if a JCR property is a multivalued property or not
*/
public static Predicate<Property> isMultipleValuedProperty =
new Predicate<Property>() {
@Override
public boolean apply(final Property p) {
checkNotNull(p, "null is neither multiple nor not multiple!");
try {
return p.isMultiple();
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Check if a JCR property is a binary jcr:data property
*/
public static Predicate<Property> isBinaryContentProperty =
new Predicate<Property>() {
@Override
public boolean apply(final Property p) {
checkNotNull(p, "null is neither binary nor not binary!");
try {
return p.getType() == BINARY && p.getName().equals(JCR_DATA);
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Check if a node is "internal" and should not be exposed e.g. via the REST
* API
*/
public static Predicate<Node> isInternalNode = new Predicate<Node>() {
@Override
public boolean apply(final Node n) {
checkNotNull(n, "null is neither internal nor not internal!");
try {
final NodeType primaryNodeType = n.getPrimaryNodeType();
return primaryNodeType != null
&& primaryNodeType.isNodeType("mode:system");
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* Map a JCR property to an RDF property with the right namespace URI and
* local name
*/
public static Function<Property, com.hp.hpl.jena.rdf.model.Property> getPredicateForProperty =
new Function<Property, com.hp.hpl.jena.rdf.model.Property>() {
@Override
public com.hp.hpl.jena.rdf.model.Property apply(
final Property property) {
LOGGER.trace("Creating predicate for property: {}", property);
try {
if (property instanceof Namespaced) {
final Namespaced nsProperty = (Namespaced) property;
final String uri = nsProperty.getNamespaceURI();
return createProperty(
getRDFNamespaceForJcrNamespace(uri), nsProperty
.getLocalName());
} else {
return createProperty(property.getName());
}
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};
/**
* ISODateTimeFormat is thread-safe and immutable, and the formatters it
* returns are as well.
*/
private static final DateTimeFormatter FMT = ISODateTimeFormat.dateTime();
/**
* Get the JCR Node Type manager
*
* @param node
* @return
* @throws RepositoryException
*/
public static NodeTypeManager getNodeTypeManager(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getNodeTypeManager();
}
/**
* Get the property definition information (containing type and multi-value
* information)
*
* @param node the node to use for inferring the property definition
* @param propertyName the property name to retrieve a definition for
* @return a JCR PropertyDefinition, if available, or null
* @throws javax.jcr.RepositoryException
*/
public static PropertyDefinition getDefinitionForPropertyName(final Node node,
final String propertyName) throws RepositoryException {
final PropertyDefinition[] propertyDefinitions =
node.getPrimaryNodeType().getPropertyDefinitions();
LOGGER.debug("Looking for property name: {}", propertyName);
for (final PropertyDefinition p : propertyDefinitions) {
LOGGER.debug("Checking property: {}", p.getName());
if (p.getName().equals(propertyName)) {
return p;
}
}
for (final NodeType nodeType : node.getMixinNodeTypes()) {
for (final PropertyDefinition p : nodeType.getPropertyDefinitions()) {
if (p.getName().equals(propertyName)) {
return p;
}
}
}
return null;
}
/**
* Convenience method for transforming arrays into {@link Collection}s
* through a mapping {@link Function}.
*
* @param input A Collection<F>.
* @param f A Function<F,T>.
* @return An ImmutableSet copy of input after transformation by f
*/
public static <F, T> Collection<T> map(final F[] input,
final Function<F, T> f) {
return transform(copyOf(input), f);
}
/**
* @param date Instance of java.util.Date.
* @return the lexical form of the XSD dateTime value, e.g.
* "2006-11-13T09:40:55.001Z".
*/
public static String convertDateToXSDString(final long date) {
final DateTime dt = new DateTime(date, DateTimeZone.UTC);
return FMT.print(dt);
}
/**
* Get the JCR Base version for a node
*
* @param node
* @return
* @throws RepositoryException
*/
public static Version getBaseVersion(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getVersionManager()
.getBaseVersion(node.getPath());
}
/**
* Get the JCR VersionHistory for an existing node.
*
* @param node
* @return
* @throws RepositoryException
*/
public static VersionHistory getVersionHistory(final Node node) throws RepositoryException {
return getVersionHistory(node.getSession(), node.getPath());
}
/**
* Get the JCR VersionHistory for a node at a given JCR path
*
* @param session
* @param path
* @return
* @throws RepositoryException
*/
public static VersionHistory getVersionHistory(final Session session,
final String path) throws RepositoryException {
return session.getWorkspace().getVersionManager().getVersionHistory(
path);
}
/**
* @return a double of the size of the fedora:datastream binary content
* @throws RepositoryException
*/
public static long getRepositoryCount(final Repository repository)
throws RepositoryException {
final Session session = repository.login();
try {
final QueryManager queryManager =
session.getWorkspace().getQueryManager();
final String querystring =
"SELECT [" + JCR_PATH + "] FROM ["
+ FEDORA_OBJECT + "]";
final QueryResult queryResults =
queryManager.createQuery(querystring, JCR_SQL2).execute();
return queryResults.getRows().getSize();
} finally {
session.logout();
}
}
/**
* @return a double of the size of the fedora:datastream binary content
* @throws RepositoryException
*/
public static long getRepositorySize(final Repository repository)
throws RepositoryException {
final Session session = repository.login();
long sum = 0;
final QueryManager queryManager =
session.getWorkspace().getQueryManager();
final String querystring =
"SELECT [" + CONTENT_SIZE + "] FROM [" +
FEDORA_BINARY + "]";
final QueryResult queryResults =
queryManager.createQuery(querystring, JCR_SQL2).execute();
for (final RowIterator rows = queryResults.getRows(); rows.hasNext();) {
final Value value =
rows.nextRow().getValue(CONTENT_SIZE);
sum += value.getLong();
}
session.logout();
return sum;
}
}