if (OObjectSerializationThreadLocal.INSTANCE.get().containsKey(identityRecord))
return (T) OObjectSerializationThreadLocal.INSTANCE.get().get(identityRecord);
OObjectSerializationThreadLocal.INSTANCE.get().put(identityRecord, iProxiedPojo);
OProperty schemaProperty;
final Class<?> pojoClass = iPojo.getClass();
final OClass schemaClass = iRecord.getSchemaClass();
// CHECK FOR ID BINDING
final Field idField = getIdField(pojoClass);
if (idField != null) {
Object id = getFieldValue(idField, iPojo);
if (id != null) {
// FOUND
if (id instanceof ORecordId) {
ORecordInternal.setIdentity(iRecord, (ORecordId) id);
} else if (id instanceof Number) {
// TREATS AS CLUSTER POSITION
((ORecordId) iRecord.getIdentity()).clusterId = schemaClass.getDefaultClusterId();
((ORecordId) iRecord.getIdentity()).clusterPosition = OClusterPositionFactory.INSTANCE.valueOf(((Number) id).longValue());
} else if (id instanceof String)
((ORecordId) iRecord.getIdentity()).fromString((String) id);
else if (id.getClass().equals(Object.class))
ORecordInternal.setIdentity(iRecord, (ORecordId) id);
else
OLogManager.instance().warn(OObjectSerializerHelper.class,
"@Id field has been declared as %s while the supported are: ORID, Number, String, Object", id.getClass());
}
if (iRecord.getIdentity().isValid() && iRecord.getIdentity().isPersistent())
iRecord.reload();
}
// CHECK FOR VERSION BINDING
final Field vField = getVersionField(pojoClass);
boolean versionConfigured = false;
if (vField != null) {
versionConfigured = true;
Object ver = getFieldValue(vField, iPojo);
if (ver != null) {
// FOUND
final ORecordVersion version = iRecord.getRecordVersion();
if (ver instanceof ORecordVersion) {
version.copyFrom((ORecordVersion) ver);
} else if (ver instanceof Number) {
if (version instanceof OSimpleVersion)
// TREATS AS CLUSTER POSITION
version.setCounter(((Number) ver).intValue());
else
OLogManager
.instance()
.warn(OObjectEntitySerializer.class,
"@Version field can't be declared as Number in distributed mode. Should be one of following: String, Object, ORecordVersion");
} else if (ver instanceof String) {
version.getSerializer().fromString((String) ver, version);
} else if (ver.getClass().equals(Object.class))
version.copyFrom((ORecordVersion) ver);
else
OLogManager.instance().warn(OObjectSerializerHelper.class,
"@Version field has been declared as %s while the supported are: Number, String, Object", ver.getClass());
}
}
if (db.isMVCC() && !versionConfigured && db.getTransaction() instanceof OTransactionOptimistic)
throw new OTransactionException(
"Cannot involve an object of class '"
+ pojoClass
+ "' in an Optimistic Transaction commit because it does not define @Version or @OVersion and therefore cannot handle MVCC");
String fieldName;
Object fieldValue;
// CALL BEFORE MARSHALLING
invokeCallback(pojoClass, iPojo, iRecord, OBeforeSerialization.class);
Class<?> currentClass = pojoClass;
while (!currentClass.equals(Object.class) && classes.contains(pojoClass)) {
for (Field p : currentClass.getDeclaredFields()) {
if (Modifier.isStatic(p.getModifiers()) || Modifier.isNative(p.getModifiers()) || Modifier.isTransient(p.getModifiers())
|| p.getType().isAnonymousClass())
continue;
fieldName = p.getName();
List<String> classTransientFields = transientFields.get(pojoClass);
if ((idField != null && fieldName.equals(idField.getName()) || (vField != null && fieldName.equals(vField.getName())) || (classTransientFields != null && classTransientFields
.contains(fieldName))))
continue;
fieldValue = getFieldValue(p, iPojo);
if (fieldValue != null && fieldValue.getClass().isAnonymousClass())
continue;
if (isSerializedType(p))
fieldValue = serializeFieldValue(p.getType(), fieldValue);
schemaProperty = schemaClass != null ? schemaClass.getProperty(fieldName) : null;
OType fieldType = schemaProperty != null ? schemaProperty.getType() : getTypeByClass(currentClass, fieldName);
if (fieldValue != null) {
if (isEmbeddedObject(p)) {
// AUTO CREATE SCHEMA CLASS
if (iRecord.getSchemaClass() == null) {