try {
OBJECT_M.put(theKeyObj, theObj);
Graph aGraph = DataSourceUtil.describe(theSource, theObj);
if (aGraph.size() == 0) {
return theObj;
}
final Resource aTmpRes = EmpireUtil.asResource(aTmpSupportsRdfId);
Set<URI> aProps = new HashSet<URI>();
Iterator<Statement> sIter = aGraph.match(aTmpRes, null, null);
while (sIter.hasNext()) {
Statement aStmt = sIter.next();
aProps.add(aStmt.getPredicate());
}
final SupportsRdfId aSupportsRdfId = asSupportsRdfId(theObj);
final EmpireGenerated aEmpireGenerated = asEmpireGenerated(theObj);
aEmpireGenerated.setAllTriples(aGraph);
OBJECT_M.put(theKeyObj, theObj);
final Resource aRes = EmpireUtil.asResource(aSupportsRdfId);
addNamespaces(theObj.getClass());
Map<URI, AccessibleObject> theCachedMap = ACCESSORS_BY_CLASS.get(theObj.getClass());
if (theCachedMap == null) {
theCachedMap = cacheAccessibles(theObj.getClass(), aRes);
}
Set<URI> aUsedProps = new HashSet<URI>();
for (URI aProp : aProps) {
AccessibleObject aAccess = theCachedMap.get(aProp);
if (aAccess == null && RDF.TYPE.equals(aProp)) {
// TODO: the following block should be entirely removed (leaving continue only)
// right now, leaving it until the code review: code review before removing the following block
// my understanding is that the following block was only necessary when having a support for a single-typed objects,
// which is no longer the case
// we can skip the rdf:type property. it's basically assigned in the @RdfsClass annotation on the
// java class, so we can figure it out later if need be. TODO: of course, if something has multiple types
// that information is lost, which is not good.
/*
URI aType = (URI) aGraph.getValue(aRes, aProp);
if (!TYPE_TO_CLASS.containsKey(aType) ||
!TYPE_TO_CLASS.get(aType).isAssignableFrom(theObj.getClass())) {
if (TYPE_TO_CLASS.containsKey(aType) && !TYPE_TO_CLASS.get(aType).getName().equals(theObj.getClass().getName())) {
// TODO: this might just be an error
LOGGER.warn("Asserted rdf:type of the individual does not match the rdf:type annotation on the object. " + aType + " " + TYPE_TO_CLASS.get(aType) + " " + theObj.getClass() + " " +TYPE_TO_CLASS.get(aType).isAssignableFrom(theObj.getClass())+ " " +TYPE_TO_CLASS.get(aType).equals(theObj.getClass()) + " " + TYPE_TO_CLASS.get(aType).getName().equals(theObj.getClass().getName()));
}
else {
// if they're not equals() or isAssignableFrom, but have the same name, this is usually
// means that the class loaders don't match. so probably not an error, so no warning.
}
}
*/
continue;
}
else if (aAccess == null) {
// this must be data that is not covered by the bean (perhaps accessible by a different view/bean for a differnent type of an individual)
continue;
}
aUsedProps.add(aProp);
ToObjectFunction aFunc = new ToObjectFunction(theSource, aRes, aAccess, aProp);
Object aValue = aFunc.apply(GraphUtil.getObjects(aGraph, aRes, aProp));
boolean aOldAccess = aAccess.isAccessible();
try {
setAccessible(aAccess, true);
set(aAccess, theObj, aValue);
}
catch (InvocationTargetException e) {
// oh crap
throw new InvalidRdfException(e);
}
catch (IllegalAccessException e) {
// this should not happen since we toggle the accessibility of the field, but we'll re-throw regardless
throw new InvalidRdfException(e);
}
catch (IllegalArgumentException e) {
// this is "likely" to happen. we'll get this exception if the rdf does not match the java. for example
// if something is specified to be an int in the java class, but it typed as a float (though down conversion
// in that case might work) the set call will fail.
// TODO: shouldnt this be an error?
LOGGER.warn("Probable type mismatch: {} {}", aValue, aAccess);
}
catch (RuntimeException e) {
// TODO: i dont like keying on a RuntimeException here to get the error condition, but since the
// Function interface does not throw anything, this is the best we can do. maybe consider a
// version of the Function interface that has a throws clause, it would make this more clear.
// this was probably an error converting from a Value to an Object
throw new InvalidRdfException(e);
}
finally {
setAccessible(aAccess, aOldAccess);
}
}
sIter = aGraph.match(aTmpRes, null, null);
Graph aInstanceTriples = Graphs.newGraph();
while (sIter.hasNext()) {
Statement aStmt = sIter.next();
if (aUsedProps.contains(aStmt.getPredicate())) {
aInstanceTriples.add(aStmt);
}
}
aEmpireGenerated.setInstanceTriples(aInstanceTriples);