private static OIdentifiable linkToStream ( final StringBuilder buffer , final ORecordSchemaAware < ? > iParentRecord , Object iLinked ) { if ( iLinked == null ) // NULL REFERENCE return null ; OIdentifiable resultRid = null ; ORID rid ; final ODatabaseRecord database = ODatabaseRecordThreadLocal . INSTANCE . get ( ) ; if ( iLinked instanceof ORID ) { // JUST THE REFERENCE rid = ( ORID ) iLinked ; if ( rid . isValid ( ) && rid . isNew ( ) ) { // SAVE AT THE FLY AND STORE THE NEW RID final ORecord < ? > record = rid . getRecord ( ) ; if ( database . getTransaction ( ) . isActive ( ) ) { // USE THE DEFAULT CLUSTER database . save ( ( ORecordInternal < ? > ) record ) ; } else database . save ( ( ORecordInternal < ? > ) record ) ; if ( record != null ) rid = record . getIdentity ( ) ; resultRid = rid ; } } else { if ( iLinked instanceof String ) iLinked = new ORecordId ( ( String ) iLinked ) ; else if ( ! ( iLinked instanceof ORecordInternal < ? > ) ) { // NOT RECORD: TRY TO EXTRACT THE DOCUMENT IF ANY final String boundDocumentField = OObjectSerializerHelperManager . getInstance ( ) . getDocumentBoundField ( iLinked . getClass ( ) ) ; if ( boundDocumentField != null ) iLinked = OObjectSerializerHelperManager . getInstance ( ) . getFieldValue ( iLinked , boundDocumentField ) ; } if ( ! ( iLinked instanceof OIdentifiable ) ) throw new IllegalArgumentException ( "Invalid object received. Expected a OIdentifiable but received type=" + iLinked . getClass ( ) . getName ( ) + " and value=" + iLinked ) ; // RECORD ORecordInternal < ? > iLinkedRecord = ( ( OIdentifiable ) iLinked ) . getRecord ( ) ; rid = iLinkedRecord . getIdentity ( ) ; if ( rid . isNew ( ) || iLinkedRecord . isDirty ( ) ) { if ( iLinkedRecord instanceof ODocument ) { final OClass schemaClass = ( ( ODocument ) iLinkedRecord ) . getSchemaClass ( ) ; database . save ( iLinkedRecord , schemaClass != null ? database . 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 database . save ( iLinkedRecord ) ; final ODatabaseComplex < ? > dbOwner = database . getDatabaseOwner ( ) ; dbOwner . registerUserObjectAfterLinkSave ( iLinkedRecord ) ; resultRid = iLinkedRecord ; } if ( iParentRecord != null && database instanceof ODatabaseRecord ) { final ODatabaseRecord db = database ; 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 ; }
Serialize the link .