use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method getMappingClass.
/**
* Accessor for the mapping class for the specified type.
* Provides special handling for interface types and for classes that are being embedded in a field.
* Refers others to its mapping manager lookup.
* @param javaType Class to query
* @param serialised Whether the field is serialised
* @param embedded Whether the field is embedded
* @param colmds Metadata for column(s) (optional)
* @param fieldName The full field name (for logging only)
* @return The mapping class for the class
*/
protected MappingConverterDetails getMappingClass(Class javaType, boolean serialised, boolean embedded, ColumnMetaData[] colmds, String fieldName) {
ApiAdapter api = storeMgr.getApiAdapter();
if (api.isPersistable(javaType)) {
// Persistence Capable field
if (serialised) {
// Serialised PC field
return new MappingConverterDetails(SerialisedPCMapping.class);
} else if (embedded) {
// Embedded PC field
return new MappingConverterDetails(EmbeddedPCMapping.class);
} else {
// PC field
return new MappingConverterDetails(PersistableMapping.class);
}
}
if (javaType.isInterface() && !storeMgr.getMappingManager().isSupportedMappedType(javaType.getName())) {
// Interface field
if (serialised) {
// Serialised Interface field
return new MappingConverterDetails(SerialisedReferenceMapping.class);
} else if (embedded) {
// Embedded interface field - just default to an embedded PCMapping!
return new MappingConverterDetails(EmbeddedPCMapping.class);
} else {
// Interface field
return new MappingConverterDetails(InterfaceMapping.class);
}
}
if (javaType == java.lang.Object.class) {
// Object field
if (serialised) {
// Serialised Object field
return new MappingConverterDetails(SerialisedReferenceMapping.class);
} else if (embedded) {
// Embedded Object field - do we ever want to support this ? I think not ;-)
throw new NucleusUserException(Localiser.msg("041042", fieldName)).setFatal();
} else {
// Object field as reference to PC object
return new MappingConverterDetails(ObjectMapping.class);
}
}
if (javaType.isArray()) {
// Array field
if (api.isPersistable(javaType.getComponentType())) {
// Array of PC objects
return new MappingConverterDetails(ArrayMapping.class);
} else if (javaType.getComponentType().isInterface() && !storeMgr.getMappingManager().isSupportedMappedType(javaType.getComponentType().getName())) {
// Array of interface objects
return new MappingConverterDetails(ArrayMapping.class);
} else if (javaType.getComponentType() == java.lang.Object.class) {
// Array of Object reference objects
return new MappingConverterDetails(ArrayMapping.class);
}
// Other array types will be caught by the default mappings
}
// Find a suitable mapping for the type and column definition (doesn't allow for serialised setting)
MappingConverterDetails mcd = getDefaultJavaTypeMapping(javaType, colmds);
if (mcd == null || mcd.mappingClass == null) {
Class superClass = javaType.getSuperclass();
while (superClass != null && !superClass.getName().equals(ClassNameConstants.Object) && (mcd == null || mcd.mappingClass == null)) {
mcd = getDefaultJavaTypeMapping(superClass, colmds);
superClass = superClass.getSuperclass();
}
}
if (mcd == null) {
if (storeMgr.getMappingManager().isSupportedMappedType(javaType.getName())) {
// "supported" type yet no FCO mapping !
throw new NucleusUserException(Localiser.msg("041001", fieldName, javaType.getName()));
}
// start in this class
Class superClass = javaType;
while (superClass != null && !superClass.getName().equals(ClassNameConstants.Object) && (mcd == null || mcd.mappingClass == null)) {
Class[] interfaces = superClass.getInterfaces();
for (int i = 0; i < interfaces.length && (mcd == null || mcd.mappingClass == null); i++) {
mcd = getDefaultJavaTypeMapping(interfaces[i], colmds);
}
superClass = superClass.getSuperclass();
}
if (mcd == null) {
if (!serialised) {
TypeConverter converter = storeMgr.getNucleusContext().getTypeManager().getAutoApplyTypeConverterForType(javaType);
if (converter != null) {
// Fall back to the auto-apply converter for this member type
return new MappingConverterDetails(TypeConverterMapping.class, converter);
}
}
// Treat as serialised TODO If !serialised should we throw an exception?
NucleusLogger.PERSISTENCE.warn("Trying to find mapping for type " + javaType.getName() + " but none found, and only serialised available! Falling back to SerialisedMapping");
return new MappingConverterDetails(SerialisedMapping.class);
}
}
return mcd;
}
use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.
the class DatastoreIdMapping method setObject.
/**
* Mutator for the OID in the datastore
* @param ec ExecutionContext
* @param ps The Prepared Statement
* @param param Param numbers in the PreparedStatement for this object
* @param value The OID value to use
*/
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] param, Object value) {
if (value == null) {
getDatastoreMapping(0).setObject(ps, param[0], null);
} else {
ApiAdapter api = ec.getApiAdapter();
Object id;
if (api.isPersistable(value)) {
id = api.getIdForObject(value);
if (id == null) {
if (ec.isInserting(value)) {
// Object is in the process of being inserted, but has no id yet so provide a null for now
// The "NotYetFlushedException" is caught by ParameterSetter and processed as an update being required.
getDatastoreMapping(0).setObject(ps, param[0], null);
throw new NotYetFlushedException(value);
}
// Object is not persist, nor in the process of being made persistent
ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
ec.flushInternal(false);
}
id = api.getIdForObject(value);
} else {
id = value;
}
Object idKey = IdentityUtils.getTargetKeyForDatastoreIdentity(id);
try {
// Try as a Long
getDatastoreMapping(0).setObject(ps, param[0], idKey);
} catch (Exception e) {
// Must be a String
getDatastoreMapping(0).setObject(ps, param[0], idKey.toString());
}
}
}
use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.
the class AppIDObjectIdFieldManager method storeObjectField.
/**
* Method to store an object in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeObjectField(int fieldNumber, Object value) {
ApiAdapter api = ec.getApiAdapter();
if (api.isPersistable(value)) {
api.copyKeyFieldsFromIdToObject(value, new AppIdObjectIdFieldConsumer(api, this), api.getIdForObject(value));
} else {
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setObject(ec, statement, getParamsForField(mapping), value);
}
}
use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method setObjectAsValue.
/**
* Method to set an object reference (FK) in the datastore.
* @param ec The ExecutionContext
* @param ps The Prepared Statement
* @param param The parameter ids in the statement
* @param value The value to put in the statement at these ids
* @param ownerOP ObjectProvider for the owner object
* @param ownerFieldNumber Field number of this PC object in the owner
* @throws NotYetFlushedException Just put "null" in and throw "NotYetFlushedException", to be caught by ParameterSetter and will signal to the
* PC object being inserted that it needs to inform this object when it is inserted.
*/
private void setObjectAsValue(ExecutionContext ec, PreparedStatement ps, int[] param, Object value, ObjectProvider ownerOP, int ownerFieldNumber) {
Object id;
ApiAdapter api = ec.getApiAdapter();
if (!api.isPersistable(value)) {
throw new NucleusException(Localiser.msg("041016", value.getClass(), value)).setFatal();
}
ObjectProvider valueOP = ec.findObjectProvider(value);
try {
ClassLoaderResolver clr = ec.getClassLoaderResolver();
// Check if the field is attributed in the datastore
boolean hasDatastoreAttributedPrimaryKeyValues = hasDatastoreAttributedPrimaryKeyValues(ec.getMetaDataManager(), storeMgr, clr);
boolean inserted = false;
if (ownerFieldNumber >= 0) {
// Field mapping : is this field of the related object present in the datastore?
inserted = storeMgr.isObjectInserted(valueOP, ownerFieldNumber);
} else if (mmd == null) {
// Identity mapping : is the object inserted far enough to be considered of this mapping type?
inserted = storeMgr.isObjectInserted(valueOP, type);
}
if (valueOP != null) {
if (ec.getApiAdapter().isDetached(value) && valueOP.getReferencedPC() != null && ownerOP != null && mmd != null) {
// Still detached but started attaching so replace the field with what will be the attached
// Note that we have "fmd != null" here hence omitting any M-N relations where this is a join table
// mapping
ownerOP.replaceFieldMakeDirty(ownerFieldNumber, valueOP.getReferencedPC());
}
if (valueOP.isWaitingToBeFlushedToDatastore()) {
try {
// Related object is not yet flushed to the datastore so flush it so we can set the FK
valueOP.flush();
} catch (NotYetFlushedException nfe) {
// Could not flush it, maybe it has a relation to this object! so set as null TODO check nullability
if (ownerOP != null) {
ownerOP.updateFieldAfterInsert(value, ownerFieldNumber);
}
setObjectAsNull(ec, ps, param);
return;
}
}
} else {
if (ec.getApiAdapter().isDetached(value)) {
// Field value is detached and not yet started attaching, so attach
Object attachedValue = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
if (attachedValue != value && ownerOP != null) {
// Replace the field value if using copy-on-attach
ownerOP.replaceFieldMakeDirty(ownerFieldNumber, attachedValue);
// Work from attached value now that it is attached
value = attachedValue;
}
valueOP = ec.findObjectProvider(value);
}
}
// 5) the value is the same object as we are inserting anyway and has its identity set
if (inserted || !ec.isInserting(value) || (!hasDatastoreAttributedPrimaryKeyValues && (this.mmd != null && this.mmd.isPrimaryKey())) || (!hasDatastoreAttributedPrimaryKeyValues && ownerOP == valueOP && api.getIdForObject(value) != null)) {
// The PC is either already inserted, or inserted down to the level we need, or not inserted at all,
// or the field is a PK and identity not attributed by the datastore
// Object either already exists, or is not yet being inserted.
id = api.getIdForObject(value);
// Check if the persistable object exists in this datastore
boolean requiresPersisting = false;
if (ec.getApiAdapter().isDetached(value) && ownerOP != null) {
// Detached object so needs attaching
if (ownerOP.isInserting()) {
// we can just return the value now and attach later (in InsertRequest)
if (!ec.getBooleanProperty(PropertyNames.PROPERTY_ATTACH_SAME_DATASTORE)) {
if (ec.getObjectFromCache(api.getIdForObject(value)) != null) {
// Object is in cache so exists for this datastore, so no point checking
} else {
try {
Object obj = ec.findObject(api.getIdForObject(value), true, false, value.getClass().getName());
if (obj != null) {
// Make sure this object is not retained in cache etc
ObjectProvider objOP = ec.findObjectProvider(obj);
if (objOP != null) {
ec.evictFromTransaction(objOP);
}
ec.removeObjectFromLevel1Cache(api.getIdForObject(value));
}
} catch (NucleusObjectNotFoundException onfe) {
// Object doesn't yet exist
requiresPersisting = true;
}
}
}
} else {
requiresPersisting = true;
}
} else if (id == null) {
// Transient object, so we need to persist it
requiresPersisting = true;
} else {
ExecutionContext pcEC = ec.getApiAdapter().getExecutionContext(value);
if (pcEC != null && ec != pcEC) {
throw new NucleusUserException(Localiser.msg("041015"), id);
}
}
if (requiresPersisting) {
// This PC object needs persisting (new or detached) to do the "set"
if (mmd != null && !mmd.isCascadePersist() && !ec.getApiAdapter().isDetached(value)) {
// Related PC object not persistent, but cant do cascade-persist so throw exception
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("007006", mmd.getFullFieldName()));
}
throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), value);
}
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("007007", mmd != null ? mmd.getFullFieldName() : null));
}
try {
Object pcNew = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
if (hasDatastoreAttributedPrimaryKeyValues) {
ec.flushInternal(false);
}
id = api.getIdForObject(pcNew);
if (ec.getApiAdapter().isDetached(value) && ownerOP != null && mmd != null) {
// Update any detached reference to refer to the attached variant
ownerOP.replaceFieldMakeDirty(ownerFieldNumber, pcNew);
RelationType relationType = mmd.getRelationType(clr);
if (relationType == RelationType.MANY_TO_ONE_BI) {
// TODO Update the container to refer to the attached object
if (NucleusLogger.PERSISTENCE.isInfoEnabled()) {
NucleusLogger.PERSISTENCE.info("PCMapping.setObject : object " + ownerOP.getInternalObjectId() + " has field " + ownerFieldNumber + " that is 1-N bidirectional." + " Have just attached the N side so should really update the reference in the 1 side collection" + " to refer to this attached object. Not yet implemented");
}
} else if (relationType == RelationType.ONE_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
ObjectProvider relatedOP = ec.findObjectProvider(pcNew);
relatedOP.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), ownerOP.getObject());
}
}
} catch (NotYetFlushedException e) {
setObjectAsNull(ec, ps, param);
throw new NotYetFlushedException(value);
}
}
if (valueOP != null) {
valueOP.setStoringPC();
}
// If the field doesn't map to any datastore fields (e.g remote FK), omit the set process
if (getNumberOfDatastoreMappings() > 0) {
if (IdentityUtils.isDatastoreIdentity(id)) {
Object idKey = IdentityUtils.getTargetKeyForDatastoreIdentity(id);
try {
// Try as a Long
getDatastoreMapping(0).setObject(ps, param[0], idKey);
} catch (Exception e) {
// Must be a String
getDatastoreMapping(0).setObject(ps, param[0], idKey.toString());
}
} else {
boolean fieldsSet = false;
if (IdentityUtils.isSingleFieldIdentity(id) && javaTypeMappings.length > 1) {
Object key = IdentityUtils.getTargetKeyForSingleFieldIdentity(id);
AbstractClassMetaData keyCmd = ec.getMetaDataManager().getMetaDataForClass(key.getClass(), clr);
if (keyCmd != null && keyCmd.getIdentityType() == IdentityType.NONDURABLE) {
// Embedded ID - Make sure these are called starting at lowest first, in order
// We cannot just call OP.provideFields with all fields since that does last first
ObjectProvider keyOP = ec.findObjectProvider(key);
int[] fieldNums = keyCmd.getAllMemberPositions();
FieldManager fm = new AppIDObjectIdFieldManager(param, ec, ps, javaTypeMappings);
for (int i = 0; i < fieldNums.length; i++) {
keyOP.provideFields(new int[] { fieldNums[i] }, fm);
}
fieldsSet = true;
}
}
if (!fieldsSet) {
// Copy PK fields from identity to the object
FieldManager fm = new AppIDObjectIdFieldManager(param, ec, ps, javaTypeMappings);
api.copyKeyFieldsFromIdToObject(value, new AppIdObjectIdFieldConsumer(api, fm), id);
}
}
}
} else {
if (valueOP != null) {
valueOP.setStoringPC();
}
if (getNumberOfDatastoreMappings() > 0) {
// Object is in the process of being inserted so we cant use its id currently and we need to store
// a foreign key to it (which we cant yet do). Just put "null" in and throw "NotYetFlushedException",
// to be caught by ParameterSetter and will signal to the PC object being inserted that it needs
// to inform this object when it is inserted.
setObjectAsNull(ec, ps, param);
throw new NotYetFlushedException(value);
}
}
} finally {
if (valueOP != null) {
valueOP.unsetStoringPC();
}
}
}
use of org.datanucleus.api.ApiAdapter in project datanucleus-rdbms by datanucleus.
the class MultiPersistableMapping method setObject.
/**
* Sets the specified positions in the PreparedStatement associated with this field, and value.
* If the number of positions in "pos" is not the same as the number of datastore mappings then it is assumed
* that we should only set the positions for the real implementation FK; this happens where we have a statement
* like "... WHERE IMPL1_ID_OID = ? AND IMPL2_ID_OID IS NULL" so we need to filter on the other implementations
* being null and only want to input parameter(s) for the real implementation of "value".
* @param ec execution context
* @param ps a datastore object that executes statements in the database
* @param pos The position(s) of the PreparedStatement to populate
* @param value the value stored in this field
* @param ownerOP the owner ObjectProvider
* @param ownerFieldNumber the owner absolute field number
*/
public void setObject(ExecutionContext ec, PreparedStatement ps, int[] pos, Object value, ObjectProvider ownerOP, int ownerFieldNumber) {
boolean setValueFKOnly = false;
if (pos != null && pos.length < getNumberOfDatastoreMappings()) {
setValueFKOnly = true;
}
// Make sure that this field has a sub-mapping appropriate for the specified value
int javaTypeMappingNumber = getMappingNumberForValue(ec, value);
if (value != null && javaTypeMappingNumber == -1) {
// TODO Change this to a multiple field mapping localised message
throw new ClassCastException(Localiser.msg("041044", mmd != null ? mmd.getFullFieldName() : "", getType(), value.getClass().getName()));
}
if (value != null) {
ApiAdapter api = ec.getApiAdapter();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
// Make sure the value is persisted if it is persistable in its own right
if (!ec.isInserting(value)) {
// Object either already exists, or is not yet being inserted.
Object id = api.getIdForObject(value);
// Check if the persistable exists in this datastore
boolean requiresPersisting = false;
if (ec.getApiAdapter().isDetached(value) && ownerOP != null) {
// Detached object that needs attaching (or persisting if detached from a different datastore)
requiresPersisting = true;
} else if (id == null) {
// Transient object, so we need to persist it
requiresPersisting = true;
} else {
ExecutionContext valueEC = api.getExecutionContext(value);
if (valueEC != null && ec != valueEC) {
throw new NucleusUserException(Localiser.msg("041015"), id);
}
}
if (requiresPersisting) {
// The object is either not yet persistent or is detached and so needs attaching
Object pcNew = ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
ec.flushInternal(false);
id = api.getIdForObject(pcNew);
if (ec.getApiAdapter().isDetached(value) && ownerOP != null) {
// Update any detached reference to refer to the attached variant
ownerOP.replaceFieldMakeDirty(ownerFieldNumber, pcNew);
if (mmd != null) {
RelationType relationType = mmd.getRelationType(clr);
if (relationType == RelationType.ONE_TO_ONE_BI) {
ObjectProvider relatedSM = ec.findObjectProvider(pcNew);
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Allow for multiple related fields
relatedSM.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), ownerOP.getObject());
} else if (relationType == RelationType.MANY_TO_ONE_BI) {
// TODO Update the container element with the attached variant
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug("PCMapping.setObject : object " + ownerOP.getInternalObjectId() + " has field " + ownerFieldNumber + " that is 1-N bidirectional - should really update the reference in the relation. Not yet supported");
}
}
}
}
}
if (getNumberOfDatastoreMappings() <= 0) {
// If the field doesn't map to any datastore fields, omit the set process
return;
}
}
}
if (pos == null) {
return;
}
ObjectProvider op = (value != null ? ec.findObjectProvider(value) : null);
try {
if (op != null) {
op.setStoringPC();
}
int n = 0;
NotYetFlushedException notYetFlushed = null;
for (int i = 0; i < javaTypeMappings.length; i++) {
// Set the PreparedStatement positions for this implementation mapping
int[] posMapping;
if (setValueFKOnly) {
// Only using first "pos" value(s)
n = 0;
} else if (n >= pos.length) {
// store all implementations to the same columns, so we reset the index
n = 0;
}
if (javaTypeMappings[i].getReferenceMapping() != null) {
posMapping = new int[javaTypeMappings[i].getReferenceMapping().getNumberOfDatastoreMappings()];
} else {
posMapping = new int[javaTypeMappings[i].getNumberOfDatastoreMappings()];
}
for (int j = 0; j < posMapping.length; j++) {
posMapping[j] = pos[n++];
}
try {
if (javaTypeMappingNumber == -2 || (value != null && javaTypeMappingNumber == i)) {
// This mapping is where the value is to be stored, or using persistent interfaces
javaTypeMappings[i].setObject(ec, ps, posMapping, value);
} else if (!setValueFKOnly) {
// Set null for this mapping, since the value is null or is for something else
javaTypeMappings[i].setObject(ec, ps, posMapping, null);
}
} catch (NotYetFlushedException e) {
notYetFlushed = e;
}
}
if (notYetFlushed != null) {
throw notYetFlushed;
}
} finally {
if (op != null) {
op.unsetStoringPC();
}
}
}
Aggregations