Search in sources :

Example 1 with OObjectLazyMultivalueElement

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

the class OObjectProxyMethodHandler method updateLoadedFieldMap.

public void updateLoadedFieldMap(final Object proxiedObject, final boolean iReload) {
    final Set<String> fields = new HashSet<String>(loadedFields.keySet());
    for (String fieldName : fields) {
        try {
            if (iReload) {
                // FORCE POJO FIELD VALUE TO DEFAULT VALUE, WHICH CAN BE null, 0 or false
                final Field fieldToReset = OObjectEntitySerializer.getField(fieldName, proxiedObject.getClass());
                OObjectEntitySerializer.setFieldValue(fieldToReset, proxiedObject, getDefaultValueForField(fieldToReset));
            } else {
                final Object value = getValue(proxiedObject, fieldName, false, null);
                if (value instanceof OObjectLazyMultivalueElement) {
                    if (((OObjectLazyMultivalueElement<?>) value).getUnderlying() != doc.field(fieldName))
                        loadedFields.remove(fieldName);
                } else {
                    loadedFields.put(fieldName, doc.getVersion());
                }
            }
        } catch (IllegalArgumentException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (IllegalAccessException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (NoSuchMethodException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        } catch (InvocationTargetException e) {
            throw OException.wrapException(new OSerializationException("Error updating object after save of class " + proxiedObject.getClass()), e);
        }
    }
    if (iReload) {
        // RESET LOADED FIELDS, SO THE MUST BE RELOADED FROM DATABASE
        loadedFields.clear();
    }
}
Also used : Field(java.lang.reflect.Field) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 2 with OObjectLazyMultivalueElement

use of com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement 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 3 with OObjectLazyMultivalueElement

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

the class OObjectProxyMethodHandler method detachAll.

/**
   * Method that detaches all fields contained in the document to the given object
   *
   * @param self
   *          :- The object containing this handler instance
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws NoSuchMethodException
   */
public void detachAll(final Object self, final boolean nonProxiedInstance, final Map<Object, Object> alreadyDetached, final Map<Object, Object> lazyObjects) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Class<?> selfClass = self.getClass();
    for (String fieldName : doc.fieldNames()) {
        final Field field = OObjectEntitySerializer.getField(fieldName, selfClass);
        if (field != null) {
            Object value = getValue(self, fieldName, false, null, true);
            if (value instanceof OObjectLazyMultivalueElement) {
                ((OObjectLazyMultivalueElement<?>) value).detachAll(nonProxiedInstance, alreadyDetached, lazyObjects);
                if (nonProxiedInstance)
                    value = ((OObjectLazyMultivalueElement<?>) value).getNonOrientInstance();
            } else if (value instanceof Proxy) {
                OObjectProxyMethodHandler handler = (OObjectProxyMethodHandler) ((ProxyObject) value).getHandler();
                if (nonProxiedInstance) {
                    value = OObjectEntitySerializer.getNonProxiedInstance(value);
                }
                if (OObjectEntitySerializer.isFetchLazyField(self.getClass(), fieldName)) {
                    // just make a placeholder with only the id, so it can be fetched later (but not by orient internally)
                    // do not use the already detached map for this, that might mix up lazy and non-lazy objects
                    Object lazyValue = lazyObjects.get(handler.doc.getIdentity());
                    if (lazyValue != null) {
                        value = lazyValue;
                    } else {
                        OObjectEntitySerializer.setIdField(field.getType(), value, handler.doc.getIdentity());
                        lazyObjects.put(handler.doc.getIdentity(), value);
                    }
                } else {
                    Object detachedValue = alreadyDetached.get(handler.doc.getIdentity());
                    if (detachedValue != null) {
                        value = detachedValue;
                    } else {
                        ORID identity = handler.doc.getIdentity();
                        if (identity.isValid())
                            alreadyDetached.put(identity, value);
                        handler.detachAll(value, nonProxiedInstance, alreadyDetached, lazyObjects);
                    }
                }
            }
            OObjectEntitySerializer.setFieldValue(field, self, value);
        }
    }
    OObjectEntitySerializer.setIdField(selfClass, self, doc.getIdentity());
    OObjectEntitySerializer.setVersionField(selfClass, self, doc.getVersion());
}
Also used : Field(java.lang.reflect.Field) ProxyObject(javassist.util.proxy.ProxyObject) Proxy(javassist.util.proxy.Proxy) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) ORID(com.orientechnologies.orient.core.id.ORID)

Example 4 with OObjectLazyMultivalueElement

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

the class OObjectProxyMethodHandler method detach.

/**
   * Method that detaches all fields contained in the document to the given object
   *
   * @param self
   *          :- The object containing this handler instance
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws NoSuchMethodException
   */
public void detach(final Object self, final boolean nonProxiedInstance) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Class<?> selfClass = self.getClass();
    for (String fieldName : doc.fieldNames()) {
        Object value = getValue(self, fieldName, false, null, true);
        if (value instanceof OObjectLazyMultivalueElement) {
            ((OObjectLazyMultivalueElement<?>) value).detach(nonProxiedInstance);
            if (nonProxiedInstance)
                value = ((OObjectLazyMultivalueElement<?>) value).getNonOrientInstance();
        }
        OObjectEntitySerializer.setFieldValue(OObjectEntitySerializer.getField(fieldName, selfClass), self, value);
    }
    OObjectEntitySerializer.setIdField(selfClass, self, doc.getIdentity());
    OObjectEntitySerializer.setVersionField(selfClass, self, doc.getVersion());
}
Also used : OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject)

Example 5 with OObjectLazyMultivalueElement

use of com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement 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)

Aggregations

ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)7 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)7 ProxyObject (javassist.util.proxy.ProxyObject)7 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 Field (java.lang.reflect.Field)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)3 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)3 OObjectEnumLazyMap (com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap)3 OObjectCustomSerializerMap (com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)2 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 OObjectLazySet (com.orientechnologies.orient.object.db.OObjectLazySet)2 OObjectEnumLazySet (com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet)2