Search in sources :

Example 1 with OObjectLazyMap

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

the class OObjectSerializerHelper method multiValueToStream.

private static Object multiValueToStream(final Object iMultiValue, OType iType, final OEntityManager iEntityManager, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final ODocument iRecord, final boolean iSaveOnlyDirty) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (iType == null) {
        if (sourceValues.size() == 0)
            return iMultiValue;
        // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
        final Object firstValue = sourceValues.iterator().next();
        if (firstValue == null)
            return iMultiValue;
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDSET))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDLIST))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ArrayList<Object>();
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Collection<Object>) result).add(typeToStream(o, linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new HashMap<Object, Object>();
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Entry(java.util.Map.Entry) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) List(java.util.List) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Example 2 with OObjectLazyMap

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

the class OObjectEntitySerializer method multiValueToStream.

@SuppressWarnings("unchecked")
private static Object multiValueToStream(final Object iMultiValue, OType iType, final ODatabaseObject db, final ODocument iRecord) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (sourceValues.size() == 0)
        return iMultiValue;
    // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
    final Object firstValue = sourceValues.iterator().next();
    if (firstValue == null)
        return iMultiValue;
    if (iType == null) {
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (isToSerialize(firstValue.getClass()))
            result = new HashSet<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDSET)) || OType.isSimpleType(firstValue))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (isToSerialize(firstValue.getClass()))
            result = new ArrayList<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDLIST)) || OType.isSimpleType(firstValue))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ORecordLazyList(iRecord);
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        if (firstValue instanceof List)
            linkedType = OType.EMBEDDEDLIST;
        else if (firstValue instanceof Set)
            linkedType = OType.EMBEDDEDSET;
        else if (firstValue instanceof Map)
            linkedType = OType.EMBEDDEDMAP;
        else
            linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Set<Object>) result).add(typeToStream(o, linkedType, db, null));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, db, null));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (isToSerialize(firstValue.getClass()))
                result = new HashMap<Object, Object>();
            else if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new ORecordLazyMap(iRecord);
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, db, null));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) HashSet(java.util.HashSet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Entry(java.util.Map.Entry) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) Collection(java.util.Collection) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) List(java.util.List) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashSet(java.util.HashSet)

Example 3 with OObjectLazyMap

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

the class OObjectProxyMethodHandler method manageMapSave.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object manageMapSave(final Object self, final Field f, Map<?, ?> value, final boolean customSerialization) {
    final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
    if (customSerialization) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
        value = new OObjectCustomSerializerMap<TYPE>(OObjectEntitySerializer.getSerializedType(f), doc, map, (Map<Object, Object>) value);
    } else if (genericType != null && genericType.isEnum()) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
        value = new OObjectEnumLazyMap(genericType, doc, map, (Map<Object, Object>) value);
    } else if (!(value instanceof OObjectLazyMultivalueElement)) {
        OType type = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName()) ? OType.EMBEDDEDMAP : OType.LINKMAP;
        if (doc.fieldType(f.getName()) != type)
            doc.field(f.getName(), doc.field(f.getName()), type);
        Map<Object, OIdentifiable> docMap = doc.field(f.getName(), type);
        if (docMap == null) {
            if (OType.EMBEDDEDMAP == type)
                docMap = new OTrackedMap<OIdentifiable>(doc);
            else
                docMap = new ORecordLazyMap(doc);
            setDocFieldValue(f.getName(), docMap, type);
        }
        value = new OObjectLazyMap(self, docMap, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
    }
    return value;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashMap(java.util.HashMap) OType(com.orientechnologies.orient.core.metadata.schema.OType) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) TYPE(com.orientechnologies.orient.core.hook.ORecordHook.TYPE) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Example 4 with OObjectLazyMap

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

the class OObjectProxyMethodHandler method getValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object getValue(final Object self, final String fieldName, final boolean idOrVersionField, Object value, final boolean iIgnoreLoadedFields) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (!idOrVersionField) {
        if (value == null) {
            if (!iIgnoreLoadedFields && loadedFields.containsKey(fieldName) && loadedFields.get(fieldName).compareTo(doc.getVersion()) == 0) {
                return null;
            } else {
                final Object docValue = getDocFieldValue(self, fieldName);
                if (docValue != null) {
                    value = lazyLoadField(self, fieldName, docValue, value);
                }
            }
        } else {
            if (((value instanceof Collection<?> || value instanceof Map<?, ?>) && !(value instanceof OObjectLazyMultivalueElement)) || value.getClass().isArray()) {
                final Class<?> genericMultiValueType = OReflectionHelper.getGenericMultivalueType(OObjectEntitySerializer.getField(fieldName, self.getClass()));
                if (genericMultiValueType == null || !OReflectionHelper.isJavaType(genericMultiValueType)) {
                    final Field f = OObjectEntitySerializer.getField(fieldName, self.getClass());
                    if (OObjectEntitySerializer.isSerializedType(f) && !(value instanceof OObjectLazyCustomSerializer)) {
                        value = manageSerializedCollections(self, fieldName, value);
                    } else if (genericMultiValueType != null && genericMultiValueType.isEnum() && !(value instanceof OObjectLazyEnumSerializer)) {
                        value = manageEnumCollections(self, f.getName(), genericMultiValueType, value);
                    } else {
                        value = manageObjectCollections(self, fieldName, value);
                    }
                } else {
                    final Object docValue = getDocFieldValue(self, fieldName);
                    if (docValue == null) {
                        if (value.getClass().isArray()) {
                            OClass schemaClass = doc.getSchemaClass();
                            OProperty schemaProperty = null;
                            if (schemaClass != null)
                                schemaProperty = schemaClass.getProperty(fieldName);
                            doc.field(fieldName, OObjectEntitySerializer.typeToStream(value, schemaProperty != null ? schemaProperty.getType() : null, getDatabase(), doc));
                        } else
                            doc.field(fieldName, value);
                    } else if (!loadedFields.containsKey(fieldName)) {
                        value = manageArrayFieldObject(OObjectEntitySerializer.getField(fieldName, self.getClass()), self, docValue);
                        Method setMethod = getSetMethod(self.getClass().getSuperclass(), getSetterFieldName(fieldName), value);
                        setMethod.invoke(self, value);
                    } else if ((value instanceof Set || value instanceof Map) && loadedFields.get(fieldName).compareTo(doc.getVersion()) < 0 && !OReflectionHelper.isJavaType(genericMultiValueType)) {
                        if (value instanceof Set)
                            value = new OObjectLazySet(self, (Set<OIdentifiable>) docValue, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName));
                        else
                            value = new OObjectLazyMap(self, (Map<Object, OIdentifiable>) docValue, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), fieldName));
                        final Method setMethod = getSetMethod(self.getClass().getSuperclass(), getSetterFieldName(fieldName), value);
                        setMethod.invoke(self, value);
                    }
                }
            } else if (!loadedFields.containsKey(fieldName) || loadedFields.get(fieldName).compareTo(doc.getVersion()) < 0) {
                final Object docValue = getDocFieldValue(self, fieldName);
                if (docValue != null && !docValue.equals(value)) {
                    value = lazyLoadField(self, fieldName, docValue, value);
                }
            }
        }
    }
    return value;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) 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) OObjectLazyCustomSerializer(com.orientechnologies.orient.object.serialization.OObjectLazyCustomSerializer) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) Method(java.lang.reflect.Method) OObjectLazyEnumSerializer(com.orientechnologies.orient.object.enumerations.OObjectLazyEnumSerializer) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Field(java.lang.reflect.Field) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Aggregations

ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)4 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)4 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)3 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)3 OType (com.orientechnologies.orient.core.metadata.schema.OType)3 Set (java.util.Set)3 ProxyObject (javassist.util.proxy.ProxyObject)3 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OObjectEnumLazyMap (com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap)2 OObjectCustomSerializerMap (com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2