if (iLinked == null)
// NULL REFERENCE
return null;
OIdentifiable resultRid = null;
ORID rid;
if (iLinked instanceof ORID) {
// JUST THE REFERENCE
rid = (ORID) iLinked;
if (rid.isNew()) {
// SAVE AT THE FLY AND STORE THE NEW RID
final ORecord<?> record = rid.getRecord();
if (record.getDatabase().getTransaction().isActive()) {
final OTransactionRecordEntry recordEntry = record.getDatabase().getTransaction().getRecordEntry(rid);
if (recordEntry != null)
// GET THE CLUSTER SPECIFIED
record.getDatabase().save((ORecordInternal<?>) record, recordEntry.clusterName);
else
// USE THE DEFAULT CLUSTER
record.getDatabase().save((ORecordInternal<?>) record);
} else
record.getDatabase().save((ORecordInternal<?>) record);
rid = record.getIdentity();
}
} else {
if (!(iLinked instanceof ORecordInternal<?>)) {
// NOT RECORD: TRY TO EXTRACT THE DOCUMENT IF ANY
final String boundDocumentField = OObjectSerializerHelper.getDocumentBoundField(iLinked.getClass());
if (boundDocumentField != null)
iLinked = OObjectSerializerHelper.getFieldValue(iLinked, boundDocumentField);
}
if (!(iLinked instanceof ORecordInternal<?>))
throw new IllegalArgumentException("Invalid object received. Expected a record but received type="
+ iLinked.getClass().getName() + " and value=" + iLinked);
// RECORD
ORecordInternal<?> iLinkedRecord = (ORecordInternal<?>) iLinked;
rid = iLinkedRecord.getIdentity();
if (rid.isNew() || iLinkedRecord.isDirty()) {
if (iLinkedRecord.getDatabase() == null && iParentRecord != null)
// OVERWRITE THE DATABASE TO THE SAME OF THE PARENT ONE
iLinkedRecord.setDatabase(iParentRecord.getDatabase());
if (iLinkedRecord instanceof ODocument) {
final OClass schemaClass = ((ODocument) iLinkedRecord).getSchemaClass();
iLinkedRecord.getDatabase().save(iLinkedRecord,
schemaClass != null ? iLinkedRecord.getDatabase().getClusterNameById(schemaClass.getDefaultClusterId()) : null);
} else
// STORE THE TRAVERSED OBJECT TO KNOW THE RECORD ID. CALL THIS VERSION TO AVOID CLEAR OF STACK IN THREAD-LOCAL
iLinkedRecord.getDatabase().save(iLinkedRecord);
iLinkedRecord.getDatabase().registerUserObject(iLinkedRecord.getDatabase().getUserObjectByRecord(iLinkedRecord, null),
iLinkedRecord);
resultRid = iLinkedRecord;
}
if (iParentRecord != null && iParentRecord.getDatabase() instanceof ODatabaseRecord) {
final ODatabaseRecord db = iParentRecord.getDatabase();
if (!db.isRetainRecords())
// REPLACE CURRENT RECORD WITH ITS ID: THIS SAVES A LOT OF MEMORY
resultRid = iLinkedRecord.getIdentity();
}
}
if (rid.isValid())
rid.toString(buffer);
return resultRid;
}