if (ec != ec.getApiAdapter().getExecutionContext(newValue))
{
throw new NucleusUserException(LOCALISER.msg("RDBMS.SCO.Map.WriteValueInvalidWithDifferentPM"), ec.getApiAdapter().getIdForObject(newValue));
}
ObjectProvider vsm = ec.findObjectProvider(newValue);
// Ensure the current owner field is loaded, and replace with new value
if (ownerFieldNumber >= 0)
{
ec.getApiAdapter().isLoaded(vsm, ownerFieldNumber);
Object oldOwner = vsm.provideField(ownerFieldNumber);
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
if (ec.getManageRelations())
{
ec.getRelationshipManager(vsm).relationChange(ownerFieldNumber, oldOwner, newOwner);
}
}
else
{
updateValueFk(sm, newValue, newOwner);
}
// Ensure the current key field is loaded, and replace with new value
ec.getApiAdapter().isLoaded(vsm, keyFieldNumber);
Object oldKey = vsm.provideField(keyFieldNumber);
vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
if (ec.getManageRelations())
{
ec.getRelationshipManager(vsm).relationChange(keyFieldNumber, oldKey, newKey);
}
}
else
{
/*
* The new value is not yet persistent.
*
* Update its owner and key fields to the appropriate values and
* *then* make it persistent. Making the changes before DB
* insertion avoids an unnecessary UPDATE allows the owner
* and/or key fields to be non-nullable.
*/
ec.persistObjectInternal(newValue, new FieldValues()
{
public void fetchFields(ObjectProvider vsm)
{
if (ownerFieldNumber >= 0)
{
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
}
vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
JavaTypeMapping externalFKMapping = valueTable.getExternalMapping(ownerMemberMetaData, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
if (externalFKMapping != null)
{
// Set the owner in the value object where appropriate
vsm.setAssociatedValue(externalFKMapping, sm.getObject());
}
}
public void fetchNonLoadedFields(ObjectProvider sm)
{
}
public FetchPlan getFetchPlanForLoading()
{
return null;
}
}, ObjectProvider.PC);
/* // TODO Think about removing this since we set the associated value here
if (ownerFieldNumber < 0)
{
updateValueFk(sm, newValue, newOwner);
}*/
}
}
else
{
// Value is stored in the key
ExecutionContext ec = sm.getExecutionContext();
final Object newOwner = sm.getObject();
if (ec.getApiAdapter().isPersistent(newKey))
{
/*
* The new key is already persistent.
*
* "Put" the new key in the map by updating its owner and value
* fields to the appropriate values. This is done with the same
* methods the PC itself would use if the application code
* modified the fields. It should result in no actual database
* activity if the fields were already set to the right values.
*/
if (ec != ec.getApiAdapter().getExecutionContext(newKey))
{
throw new NucleusUserException(LOCALISER.msg("056060"),
ec.getApiAdapter().getIdForObject(newKey));
}
ObjectProvider vsm = ec.findObjectProvider(newKey);
// Ensure the current owner field is loaded, and replace with new key
if (ownerFieldNumber >= 0)
{
ec.getApiAdapter().isLoaded(vsm, ownerFieldNumber);
Object oldOwner = vsm.provideField(ownerFieldNumber);
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
if (ec.getManageRelations())
{
ec.getRelationshipManager(vsm).relationChange(ownerFieldNumber, oldOwner, newOwner);
}
}
else
{
updateKeyFk(sm, newKey, newOwner);
}
// Ensure the current value field is loaded, and replace with new value
ec.getApiAdapter().isLoaded(vsm, valueFieldNumber);
oldValue = vsm.provideField(valueFieldNumber); // TODO Should we update the local variable ?
vsm.replaceFieldMakeDirty(valueFieldNumber, newValue);
if (ec.getManageRelations())
{
ec.getRelationshipManager(vsm).relationChange(valueFieldNumber, oldValue, newValue);
}
}
else
{
/*
* The new key is not yet persistent.
*
* Update its owner and key fields to the appropriate values and
* *then* make it persistent. Making the changes before DB
* insertion avoids an unnecessary UPDATE allows the owner
* and/or key fields to be non-nullable.
*/
final Object newValueObj = newValue;
ec.persistObjectInternal(newKey, new FieldValues()
{
public void fetchFields(ObjectProvider vsm)
{
if (ownerFieldNumber >= 0)
{
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
}
vsm.replaceFieldMakeDirty(valueFieldNumber, newValueObj);
JavaTypeMapping externalFKMapping =
valueTable.getExternalMapping(ownerMemberMetaData, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
if (externalFKMapping != null)
{
// Set the owner in the value object where appropriate
vsm.setAssociatedValue(externalFKMapping, sm.getObject());
}
}
public void fetchNonLoadedFields(ObjectProvider sm)
{
}