Search in sources :

Example 1 with DatastoreIdentityMetaData

use of org.datanucleus.metadata.DatastoreIdentityMetaData in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method getPropertiesForValueGenerator.

/**
 * Method to return the properties to pass to the ValueGenerator for the specified field.
 * @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) {
    Properties properties = new Properties();
    properties.setProperty(ValueGenerator.PROPERTY_CLASS_NAME, cmd.getFullClassName());
    properties.setProperty(ValueGenerator.PROPERTY_ROOT_CLASS_NAME, cmd.getBaseAbstractClassMetaData().getFullClassName());
    if (cmd.getCatalog() != null) {
        properties.setProperty(ValueGenerator.PROPERTY_CATALOG_NAME, cmd.getCatalog());
    } else if (!StringUtils.isWhitespace(catalogName)) {
        properties.setProperty(ValueGenerator.PROPERTY_CATALOG_NAME, catalogName);
    }
    if (cmd.getSchema() != null) {
        properties.setProperty(ValueGenerator.PROPERTY_SCHEMA_NAME, cmd.getSchema());
    } else if (!StringUtils.isWhitespace(schemaName)) {
        properties.setProperty(ValueGenerator.PROPERTY_SCHEMA_NAME, schemaName);
    }
    AbstractMemberMetaData mmd = null;
    ValueGenerationStrategy strategy = null;
    String sequence = null;
    Map<String, String> extensions = null;
    if (absoluteFieldNumber >= 0) {
        // real field
        mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
        properties.setProperty(ValueGenerator.PROPERTY_FIELD_NAME, mmd.getFullFieldName());
        strategy = mmd.getValueStrategy();
        if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
            strategy = ValueGenerationStrategy.getIdentityStrategy(getValueGenerationStrategyForNative(mmd));
        }
        sequence = mmd.getSequence();
        if (sequence != null) {
            properties.setProperty(ValueGenerator.PROPERTY_SEQUENCE_NAME, sequence);
        }
        extensions = mmd.getExtensions();
        if (extensions != null && extensions.size() > 0) {
            properties.putAll(extensions);
        }
    } else {
        // datastore-identity surrogate field
        // always use the root IdentityMetaData since the root class defines the identity
        DatastoreIdentityMetaData idmd = cmd.getBaseDatastoreIdentityMetaData();
        strategy = idmd.getValueStrategy();
        if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
            strategy = ValueGenerationStrategy.getIdentityStrategy(getValueGenerationStrategyForNative(cmd));
        }
        sequence = idmd.getSequence();
        if (sequence != null) {
            properties.setProperty(ValueGenerator.PROPERTY_SEQUENCE_NAME, sequence);
        }
        extensions = idmd.getExtensions();
        if (extensions != null && extensions.size() > 0) {
            properties.putAll(extensions);
        }
    }
    if (strategy != ValueGenerationStrategy.INCREMENT || tablegenmd == null) {
        // Get base table with the required field
        DatastoreClass tbl = getDatastoreClass(cmd.getBaseAbstractClassMetaData().getFullClassName(), clr);
        if (tbl == null) {
            tbl = getTableForStrategy(cmd, absoluteFieldNumber, clr);
        }
        JavaTypeMapping m = null;
        if (mmd != null) {
            m = tbl.getMemberMapping(mmd);
            if (m == null) {
                // Field not mapped in root table so use passed-in table
                tbl = getTableForStrategy(cmd, absoluteFieldNumber, clr);
                m = tbl.getMemberMapping(mmd);
            }
        } else {
            m = tbl.getIdMapping();
        }
        StringBuilder columnsName = new StringBuilder();
        for (int i = 0; i < m.getNumberOfColumnMappings(); i++) {
            if (i > 0) {
                columnsName.append(",");
            }
            columnsName.append(m.getColumnMapping(i).getColumn().getIdentifier().toString());
        }
        properties.setProperty(ValueGenerator.PROPERTY_TABLE_NAME, tbl.getIdentifier().toString());
        properties.setProperty(ValueGenerator.PROPERTY_COLUMN_NAME, columnsName.toString());
    }
    if (strategy == ValueGenerationStrategy.INCREMENT) {
        addValueGenerationPropertiesForIncrement(properties, tablegenmd);
    } else if (strategy == ValueGenerationStrategy.SEQUENCE) {
        addValueGenerationPropertiesForSequence(properties, seqmd);
    }
    return properties;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ValueGenerationStrategy(org.datanucleus.metadata.ValueGenerationStrategy) MacroString(org.datanucleus.util.MacroString) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) Properties(java.util.Properties) DatastoreIdentityMetaData(org.datanucleus.metadata.DatastoreIdentityMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

Properties (java.util.Properties)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 DatastoreIdentityMetaData (org.datanucleus.metadata.DatastoreIdentityMetaData)1 ValueGenerationStrategy (org.datanucleus.metadata.ValueGenerationStrategy)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1 MacroString (org.datanucleus.util.MacroString)1