use of eu.esdihumboldt.hale.common.instance.orient.OGroup in project hale by halestudio.
the class OSerializationHelper method convertFromDB.
/**
* Convert a value received from the database, e.g. {@link ODocument}s to
* {@link Instance}s, {@link Group}s or unwraps contained values.
*
* @param value the value
* @param parent the parent group
* @param childName the name of the child the value is associated to
* @return the converted object
*/
public static Object convertFromDB(Object value, OGroup parent, QName childName) {
if (value instanceof ODocument) {
ODocument doc = (ODocument) value;
if (doc.containsField(BINARY_WRAPPER_FIELD) || doc.containsField(OSerializationHelper.FIELD_SERIALIZATION_TYPE)) {
// extract wrapped ORecordBytes
return OSerializationHelper.deserialize(doc, parent, childName);
} else {
ChildDefinition<?> child = parent.getDefinition().getChild(childName);
if (child.asProperty() != null) {
return new OInstance((ODocument) value, child.asProperty().getPropertyType(), parent.getDb(), // no data set necessary for
null);
// nested instances
} else if (child.asGroup() != null) {
return new OGroup((ODocument) value, child.asGroup(), parent.getDb());
} else {
throw new IllegalStateException("Field " + childName + " is associated neither with a property nor a group.");
}
}
}
// TODO objects that are not supported inside document
if (value instanceof ORecordBytes) {
// XXX should not be reached as every ORecordBytes should be
// contained in a wrapper
// TODO try conversion first?!
// object deserialization
ORecordBytes record = (ORecordBytes) value;
ByteArrayInputStream bytes = new ByteArrayInputStream(record.toStream());
try {
ObjectInputStream in = new ObjectInputStream(bytes) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
Class<?> result = resolved.get(desc.getName());
if (result == null) {
result = OsgiUtils.loadClass(desc.getName(), null);
if (resolved.size() > 200) {
resolved.entrySet().iterator().remove();
}
resolved.put(desc.getName(), result);
}
return result;
}
};
return in.readObject();
} catch (Exception e) {
throw new IllegalStateException("Could not deserialize field value.", e);
}
}
return value;
}
Aggregations