use of org.datanucleus.metadata.ValueGenerationStrategy in project datanucleus-core by datanucleus.
the class AbstractStoreManager method getPropertiesForValueGenerator.
/**
* Method to return the properties to pass to the generator for the specified field.
* Will define the following properties "class-name", "root-class-name", "field-name" (if for a field),
* "sequence-name", "key-initial-value", "key-cache-size", "sequence-table-name", "sequence-schema-name",
* "sequence-catalog-name", "sequence-name-column-name", "sequence-nextval-column-name".
* In addition any extension properties on the respective field or datastore-identity are also passed through as properties.
* @param cmd MetaData for the class
* @param absoluteFieldNumber Number of the field (-1 = datastore identity)
* @param clr ClassLoader resolver
* @param seqmd Any sequence metadata
* @param tablegenmd Any table generator metadata
* @return The properties to use for this field
*/
protected Properties getPropertiesForValueGenerator(AbstractClassMetaData cmd, int absoluteFieldNumber, ClassLoaderResolver clr, SequenceMetaData seqmd, TableGeneratorMetaData tablegenmd) {
// Set up the default properties available for all value generators
Properties properties = new Properties();
AbstractMemberMetaData mmd = null;
ValueGenerationStrategy strategy = null;
String sequence = null;
Map<String, String> extensions = null;
if (absoluteFieldNumber >= 0) {
// real field
mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
strategy = mmd.getValueStrategy();
sequence = mmd.getSequence();
extensions = mmd.getExtensions();
} else {
// datastore-identity surrogate field
// always use the root IdentityMetaData since the root class defines the identity
IdentityMetaData idmd = cmd.getBaseIdentityMetaData();
strategy = idmd.getValueStrategy();
sequence = idmd.getSequence();
extensions = idmd.getExtensions();
}
properties.setProperty(ValueGenerator.PROPERTY_CLASS_NAME, cmd.getFullClassName());
properties.put(ValueGenerator.PROPERTY_ROOT_CLASS_NAME, cmd.getBaseAbstractClassMetaData().getFullClassName());
if (mmd != null) {
properties.setProperty(ValueGenerator.PROPERTY_FIELD_NAME, mmd.getFullFieldName());
}
if (sequence != null) {
properties.setProperty(ValueGenerator.PROPERTY_SEQUENCE_NAME, sequence);
}
// Add any extension properties
if (extensions != null) {
properties.putAll(extensions);
}
if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
String realStrategyName = getValueGenerationStrategyForNative(cmd, absoluteFieldNumber);
strategy = ValueGenerationStrategy.getIdentityStrategy(realStrategyName);
}
if (strategy == ValueGenerationStrategy.INCREMENT && tablegenmd != null) {
// User has specified a TableGenerator (JPA)
properties.put(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE, "" + tablegenmd.getInitialValue());
properties.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + tablegenmd.getAllocationSize());
if (tablegenmd.getTableName() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCETABLE_TABLE, tablegenmd.getTableName());
}
if (tablegenmd.getCatalogName() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCETABLE_CATALOG, tablegenmd.getCatalogName());
}
if (tablegenmd.getSchemaName() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCETABLE_SCHEMA, tablegenmd.getSchemaName());
}
if (tablegenmd.getPKColumnName() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCETABLE_NAME_COLUMN, tablegenmd.getPKColumnName());
}
if (tablegenmd.getPKColumnName() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCETABLE_NEXTVAL_COLUMN, tablegenmd.getValueColumnName());
}
if (tablegenmd.getPKColumnValue() != null) {
properties.put(ValueGenerator.PROPERTY_SEQUENCE_NAME, tablegenmd.getPKColumnValue());
}
} else if (strategy == ValueGenerationStrategy.INCREMENT && tablegenmd == null) {
if (!properties.containsKey(ValueGenerator.PROPERTY_KEY_CACHE_SIZE)) {
// Use default allocation size
properties.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + getIntProperty(PropertyNames.PROPERTY_VALUEGEN_INCREMENT_ALLOCSIZE));
}
} else if (strategy == ValueGenerationStrategy.SEQUENCE && seqmd != null) {
// User has specified a SequenceGenerator (JDO/JPA)
if (seqmd.getDatastoreSequence() != null) {
if (seqmd.getInitialValue() >= 0) {
properties.put(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE, "" + seqmd.getInitialValue());
}
if (seqmd.getAllocationSize() > 0) {
properties.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + seqmd.getAllocationSize());
} else {
// Use default allocation size
properties.put(ValueGenerator.PROPERTY_KEY_CACHE_SIZE, "" + getIntProperty(PropertyNames.PROPERTY_VALUEGEN_SEQUENCE_ALLOCSIZE));
}
properties.put(ValueGenerator.PROPERTY_SEQUENCE_NAME, "" + seqmd.getDatastoreSequence());
// Add on any extensions specified on the sequence
Map<String, String> seqExtensions = seqmd.getExtensions();
if (seqExtensions != null) {
properties.putAll(seqExtensions);
}
} else {
// JDO Factory-based sequence generation
// TODO Support this
}
}
return properties;
}
use of org.datanucleus.metadata.ValueGenerationStrategy 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