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();
}
}
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;
}
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());
}
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());
}
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;
}
Aggregations