*
* @param value
*/
void writeEntity(Object value, DataOutput raf, WriteState state) throws IOException {
if (value instanceof ObjectId || value instanceof Persistable) {
ObjectId objId;
if (value instanceof Persistable)
objId = ((Persistable) value).getId();
else
objId = (ObjectId) value;
String sourceName = objId.source == null ? null : objId.source.getId();
Integer tableId = tableNameIds.get(sourceName);
if (tableId == null) {
raf.writeByte(HEADER_FOREIGN_REFERENCE);
writeString(raf, objId.toString());
} else {
if (objId.source == arraySource) {
switch (state) {
case HISTORY:
throw new IllegalStateException("Transaction instance can't be an array");
case FROZEN_VERSION:
case CHANGED:
Persistable object = value instanceof Persistable ? (Persistable) value : objId.getTarget();
writeObject(object, raf, state, null, null);
break;
case REFERENCE_IF_EXISTS:
if (objId.isPersisted()) {
raf.writeByte(HEADER_REFERENCE_ARRAY);
InternalId internalId = convertIdToInternalObject(objId);
raf.writeInt(internalId.tableId);
writeEntity(internalId.subObjectId, raf, null);
} else {
throw new IllegalStateException();
}
}
} else {
switch (state) {
case FROZEN_VERSION:
case CHANGED:
case HISTORY:
Persistable object = value instanceof Persistable ? (Persistable) value : objId.getTarget();
writeObject(object, raf, state, null, null);
break;
case REFERENCE_IF_EXISTS:
if (objId.isPersisted()) {
raf.writeByte(HEADER_REFERENCE);
InternalId internalId = convertIdToInternalObject(objId);
raf.writeInt(internalId.tableId);
writeEntity(internalId.subObjectId, raf, null);
} else {