use of org.datanucleus.store.valuegenerator.ValueGenerator in project datanucleus-core by datanucleus.
the class AbstractStoreManager method getValueGeneratorForMember.
protected synchronized ValueGenerator getValueGeneratorForMember(ClassLoaderResolver clr, AbstractClassMetaData cmd, int absoluteFieldNumber) {
String memberKey = valueGenerationMgr.getMemberKey(cmd, absoluteFieldNumber);
// Check if we have a ValueGenerator already created for this member
ValueGenerator generator = valueGenerationMgr.getValueGeneratorForMemberKey(memberKey);
if (generator != null) {
// Return the ValueGenerator already registered against this member "key"
return generator;
}
// No ValueGenerator registered for this memberKey, so need to determine which to use and create it as required.
String fieldName = null;
ValueGenerationStrategy strategy = null;
String sequence = null;
String valueGeneratorName = null;
if (absoluteFieldNumber >= 0) {
// real field
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
fieldName = mmd.getFullFieldName();
strategy = mmd.getValueStrategy();
sequence = mmd.getSequence();
valueGeneratorName = mmd.getValueGeneratorName();
} else {
// datastore-identity surrogate field
fieldName = cmd.getFullClassName() + " (datastore id)";
strategy = cmd.getIdentityMetaData().getValueStrategy();
sequence = cmd.getIdentityMetaData().getSequence();
valueGeneratorName = cmd.getIdentityMetaData().getValueGeneratorName();
}
String strategyName = strategy.toString();
if (strategy.equals(ValueGenerationStrategy.CUSTOM)) {
// Using a "custom" generator
strategyName = strategy.getCustomName();
} else if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
strategyName = getValueGenerationStrategyForNative(cmd, absoluteFieldNumber);
strategy = ValueGenerationStrategy.getIdentityStrategy(strategyName);
}
// Check for this strategy being a "unique" ValueGenerator, and created if not yet present
generator = valueGenerationMgr.getUniqueValueGeneratorByName(strategyName);
if (generator != null) {
// "unique" ValueGenerator already defined for this strategy, so register it against the member
valueGenerationMgr.registerValueGeneratorForMemberKey(memberKey, generator);
return generator;
}
// Must be "datastore" specific generator so use plugin mechanism to create one and register against this member "key"
// Set up the default properties available for all value generators
// Extract any metadata-based generation information keyed by the "valueGeneratorName"
TableGeneratorMetaData tableGeneratorMetaData = null;
SequenceMetaData sequenceMetaData = null;
if (valueGeneratorName != null) {
if (strategy == ValueGenerationStrategy.INCREMENT) {
tableGeneratorMetaData = getMetaDataManager().getMetaDataForTableGenerator(clr, valueGeneratorName);
if (tableGeneratorMetaData == null) {
throw new NucleusUserException(Localiser.msg("038005", fieldName, valueGeneratorName));
}
} else if (strategy == ValueGenerationStrategy.SEQUENCE) {
sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, valueGeneratorName);
if (sequenceMetaData == null) {
throw new NucleusUserException(Localiser.msg("038006", fieldName, valueGeneratorName));
}
}
} else if (strategy == ValueGenerationStrategy.SEQUENCE && sequence != null) {
// TODO Allow for package name of this class prefix for the sequence name
sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, sequence);
if (sequenceMetaData == null) {
// No <sequence> defining the datastore sequence name, so fallback to this name directly in the datastore
NucleusLogger.VALUEGENERATION.info("Member " + fieldName + " has been specified to use sequence '" + sequence + "' but there is no <sequence> specified in the MetaData. Falling back to use a sequence in the datastore with this name directly.");
}
}
Properties props = getPropertiesForValueGenerator(cmd, absoluteFieldNumber, clr, sequenceMetaData, tableGeneratorMetaData);
return valueGenerationMgr.createAndRegisterValueGenerator(memberKey, strategyName, props);
}
use of org.datanucleus.store.valuegenerator.ValueGenerator in project datanucleus-core by datanucleus.
the class AbstractStoreManager method getValueGenerationStrategyValue.
/* (non-Javadoc)
* @see org.datanucleus.store.StoreManager#getStrategyValue(org.datanucleus.ClassLoaderResolver, org.datanucleus.metadata.AbstractClassMetaData, int)
*/
public Object getValueGenerationStrategyValue(ExecutionContext ec, AbstractClassMetaData cmd, int absoluteFieldNumber) {
// Get the ValueGenerator for this member
ValueGenerator generator = getValueGeneratorForMember(ec.getClassLoaderResolver(), cmd, absoluteFieldNumber);
// Get the next value from the ValueGenerator
Object oid = getNextValueForValueGenerator(generator, ec);
// Do any necessary conversion of the value to the precise member type
AbstractMemberMetaData mmd = null;
String fieldName = null;
ValueGenerationStrategy strategy = null;
if (absoluteFieldNumber >= 0) {
// real field
mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
fieldName = mmd.getFullFieldName();
strategy = mmd.getValueStrategy();
} else {
// datastore-identity surrogate field
fieldName = cmd.getFullClassName() + " (datastore id)";
strategy = cmd.getIdentityMetaData().getValueStrategy();
}
if (mmd != null) {
// replace the value of the id, but before convert the value to the field type if needed
try {
Object convertedValue = TypeConversionHelper.convertTo(oid, mmd.getType());
if (convertedValue == null) {
throw new NucleusException(Localiser.msg("038003", mmd.getFullFieldName(), oid)).setFatal();
}
oid = convertedValue;
} catch (NumberFormatException nfe) {
throw new NucleusUserException("Value strategy created value=" + oid + " type=" + oid.getClass().getName() + " but field is of type " + mmd.getTypeName() + ". Use a different strategy or change the type of the field " + mmd.getFullFieldName());
}
}
if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) {
NucleusLogger.VALUEGENERATION.debug(Localiser.msg("038002", fieldName, strategy, generator.getClass().getName(), oid));
}
return oid;
}
Aggregations