Search in sources :

Example 1 with OGroup

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) OInstance(eu.esdihumboldt.hale.common.instance.orient.OInstance) OGroup(eu.esdihumboldt.hale.common.instance.orient.OGroup) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ParseException(org.locationtech.jts.io.ParseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)1 OGroup (eu.esdihumboldt.hale.common.instance.orient.OGroup)1 OInstance (eu.esdihumboldt.hale.common.instance.orient.OInstance)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 ParseException (org.locationtech.jts.io.ParseException)1