Search in sources :

Example 1 with OObjectLazyList

use of com.orientechnologies.orient.object.db.OObjectLazyList in project orientdb by orientechnologies.

the class OObjectProxyMethodHandler method manageCollectionSave.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object manageCollectionSave(final Object self, final Field f, Collection<?> value, final boolean customSerialization, final boolean isFieldUpdate) {
    final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
    if (customSerialization) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectCustomSerializerList(OObjectEntitySerializer.getSerializedType(f), doc, new ArrayList<Object>(), (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectCustomSerializerSet(OObjectEntitySerializer.getSerializedType(f), doc, set, (Set<Object>) value);
        }
    } else if (genericType != null && genericType.isEnum()) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectEnumLazyList(genericType, doc, list, (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectEnumLazySet(genericType, doc, set, (Set<Object>) value);
        }
    } else if (!(value instanceof OObjectLazyMultivalueElement)) {
        boolean embedded = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName());
        if (value instanceof List) {
            OType type = embedded ? OType.EMBEDDEDLIST : OType.LINKLIST;
            List<OIdentifiable> docList = doc.field(f.getName(), type);
            if (docList == null) {
                if (embedded)
                    docList = new OTrackedList<OIdentifiable>(doc);
                else
                    docList = new ORecordLazyList(doc);
                setDocFieldValue(f.getName(), docList, type);
            } else if (isFieldUpdate) {
                docList.clear();
            }
            value = new OObjectLazyList(self, docList, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        } else if (value instanceof Set) {
            OType type = embedded ? OType.EMBEDDEDSET : OType.LINKSET;
            Set<OIdentifiable> docSet = doc.field(f.getName(), type);
            if (docSet == null) {
                if (embedded)
                    docSet = new OTrackedSet<OIdentifiable>(doc);
                else
                    docSet = new ORecordLazySet(doc);
                setDocFieldValue(f.getName(), docSet, type);
            } else if (isFieldUpdate) {
                docSet.clear();
            }
            value = new OObjectLazySet(self, docSet, (Set<?>) value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        }
    }
    if (!((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).isLazyLoading())
        ((OObjectLazyMultivalueElement) value).detach(false);
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) List(java.util.List) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject)

Example 2 with OObjectLazyList

use of com.orientechnologies.orient.object.db.OObjectLazyList in project orientdb by orientechnologies.

the class OObjectSerializerHelper method convertInObject.

public static Object convertInObject(final Object iPojo, final String iField, final Object iValue, final Class<?> parameterType) {
    // New conversion method working with OLazyObjectList
    if (!(iValue instanceof OObjectLazyList<?>))
        return OType.convert(iValue, parameterType);
    List<Object> aSubList = null;
    try {
        final Field aField = OObjectSerializerHelper.getField(iPojo, iField);
        final Class<?> listClass = aField.getType();
        final ParameterizedType aType = (ParameterizedType) aField.getGenericType();
        final Class<?> objectClass = (Class<?>) aType.getActualTypeArguments()[0];
        final OObjectLazyList<?> aList = (OObjectLazyList<?>) iValue;
        // Instantiation of the list
        if (listClass.isInterface()) {
            aSubList = new ArrayList<Object>();
        } else {
            aSubList = (List<Object>) listClass.newInstance();
        }
        for (final Object value : aList) {
            if (value instanceof ODocument) {
                final ODocument aDocument = (ODocument) value;
                aSubList.add(OObjectSerializerHelper.convertDocumentInType(aDocument, objectClass));
            } else {
                aSubList.add(value);
            }
        }
    } catch (Exception e) {
        OLogManager.instance().error(null, "Error on convertInObject()", e);
    }
    return aSubList;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) OObjectNotDetachedException(com.orientechnologies.orient.object.db.OObjectNotDetachedException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OObjectLazyList (com.orientechnologies.orient.object.db.OObjectLazyList)2 OException (com.orientechnologies.common.exception.OException)1 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)1 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)1 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)1 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)1 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OObjectLazySet (com.orientechnologies.orient.object.db.OObjectLazySet)1 OObjectNotDetachedException (com.orientechnologies.orient.object.db.OObjectNotDetachedException)1 OObjectEnumLazyList (com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList)1 OObjectEnumLazySet (com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet)1