if (OSerializationThreadLocal.INSTANCE.get().contains(identityRecord))
return iRecord;
OSerializationThreadLocal.INSTANCE.get().add(identityRecord);
OProperty schemaProperty;
final Class<?> pojoClass = iPojo.getClass();
final List<Field> properties = getClassFields(pojoClass);
// CHECK FOR ID BINDING
final String idFieldName = fieldIds.get(pojoClass);
if (idFieldName != null) {
Object id = getFieldValue(iPojo, idFieldName);
if (id != null) {
// FOUND
if (id instanceof ORecordId) {
iRecord.setIdentity((ORecordId) id);
} else if (id instanceof Number) {
// TREATS AS CLUSTER POSITION
((ORecordId) iRecord.getIdentity()).clusterId = schemaClass.getDefaultClusterId();
((ORecordId) iRecord.getIdentity()).clusterPosition = ((Number) id).longValue();
} else if (id instanceof String)
((ORecordId) iRecord.getIdentity()).fromString((String) id);
else if (id.getClass().equals(Object.class))
iRecord.setIdentity((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());
}
}
// CHECK FOR VERSION BINDING
final String vFieldName = fieldVersions.get(pojoClass);
boolean versionConfigured = false;
if (vFieldName != null) {
versionConfigured = true;
Object ver = getFieldValue(iPojo, vFieldName);
if (ver != null) {
// FOUND
if (ver instanceof Number) {
// TREATS AS CLUSTER POSITION
iRecord.setVersion(((Number) ver).intValue());
} else if (ver instanceof String)
iRecord.setVersion(Integer.parseInt((String) ver));
else if (ver.getClass().equals(Object.class))
iRecord.setVersion((Integer) ver);
else
OLogManager.instance().warn(OObjectSerializerHelper.class,
"@Version field has been declared as %s while the supported are: Number, String, Object", ver.getClass());
}
}
if (!versionConfigured && iRecord.getDatabase().getTransaction() instanceof OTransactionOptimistic)
throw new OTransactionException("Can't involve an object of class '" + pojoClass
+ "' in an Optimistic Transaction commit because it doesn't define @Version or @OVersion and therefore can't handle MVCC");
// SET OBJECT CLASS
iRecord.setClassName(schemaClass != null ? schemaClass.getName() : null);
String fieldName;
Object fieldValue;
// CALL BEFORE MARSHALLING
invokeCallback(iPojo, iRecord, OBeforeSerialization.class);
for (Field p : properties) {
fieldName = p.getName();
if (fieldName.equals(idFieldName) || fieldName.equals(vFieldName))
continue;
fieldValue = serializeFieldValue(iPojo, fieldName, getFieldValue(iPojo, fieldName));
schemaProperty = schemaClass != null ? schemaClass.getProperty(fieldName) : null;
fieldValue = typeToStream(fieldValue, schemaProperty != null ? schemaProperty.getType() : null, iEntityManager,
iObj2RecHandler, db, iSaveOnlyDirty);
iRecord.field(fieldName, fieldValue);
}