Search in sources :

Example 6 with IdentityMetaData

use of org.datanucleus.metadata.IdentityMetaData 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;
}
Also used : ValueGenerationStrategy(org.datanucleus.metadata.ValueGenerationStrategy) Properties(java.util.Properties) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentityMetaData(org.datanucleus.metadata.IdentityMetaData)

Aggregations

IdentityMetaData (org.datanucleus.metadata.IdentityMetaData)6 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)2 FetchGroupMemberMetaData (org.datanucleus.metadata.FetchGroupMemberMetaData)2 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)2 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)2 IndexMetaData (org.datanucleus.metadata.IndexMetaData)2 InheritanceMetaData (org.datanucleus.metadata.InheritanceMetaData)2 InvalidClassMetaDataException (org.datanucleus.metadata.InvalidClassMetaDataException)2 InvalidMetaDataException (org.datanucleus.metadata.InvalidMetaDataException)2 JoinMetaData (org.datanucleus.metadata.JoinMetaData)2 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)2 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)2 BigInteger (java.math.BigInteger)1 Properties (java.util.Properties)1