use of com.orientechnologies.orient.object.db.OObjectLazySet 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.object.db.OObjectLazySet 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;
}
Aggregations