Search in sources :

Example 1 with DiscriminatorStrategy

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

the class SQLStatementHelper method getDiscriminatorValuesForMember.

/**
 * Method to return all possible discriminator values for the supplied class and its subclasses.
 * @param className Name of the class
 * @param discMapping The discriminator mapping
 * @param storeMgr StoreManager
 * @param clr ClassLoader resolver
 * @return The possible discriminator values
 */
public static List getDiscriminatorValuesForMember(String className, JavaTypeMapping discMapping, RDBMSStoreManager storeMgr, ClassLoaderResolver clr) {
    List discrimValues = new ArrayList();
    DiscriminatorStrategy strategy = discMapping.getTable().getDiscriminatorMetaData().getStrategy();
    if (strategy != DiscriminatorStrategy.NONE) {
        MetaDataManager mmgr = storeMgr.getMetaDataManager();
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(className, clr);
        discrimValues.add(cmd.getDiscriminatorValue());
        Collection<String> subclasses = storeMgr.getSubClassesForClass(className, true, clr);
        if (subclasses != null && subclasses.size() > 0) {
            Iterator<String> subclassesIter = subclasses.iterator();
            while (subclassesIter.hasNext()) {
                String subclassName = subclassesIter.next();
                AbstractClassMetaData subclassCmd = mmgr.getMetaDataForClass(subclassName, clr);
                discrimValues.add(subclassCmd.getDiscriminatorValue());
            }
        }
    }
    return discrimValues;
}
Also used : DiscriminatorStrategy(org.datanucleus.metadata.DiscriminatorStrategy) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) List(java.util.List) ArrayList(java.util.ArrayList) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 2 with DiscriminatorStrategy

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

the class BackingStoreHelper method populateElementDiscriminatorInStatement.

/**
 * Convenience method to populate the passed PreparedStatement with the value from the element
 * discriminator, optionally including all subclasses of the element type.
 * @param ec execution context
 * @param ps The PreparedStatement
 * @param jdbcPosition Position in JDBC statement to populate
 * @param includeSubclasses Whether to include subclasses
 * @param info The element information
 * @param clr ClassLoader resolver
 * @return The next position in the JDBC statement
 */
public static int populateElementDiscriminatorInStatement(ExecutionContext ec, PreparedStatement ps, int jdbcPosition, boolean includeSubclasses, ComponentInfo info, ClassLoaderResolver clr) {
    DiscriminatorStrategy strategy = info.getDiscriminatorStrategy();
    JavaTypeMapping discrimMapping = info.getDiscriminatorMapping();
    Class cls = clr.classForName(info.getClassName());
    if (!Modifier.isAbstract(cls.getModifiers())) {
        // Include element discriminator
        Object discVal = info.getAbstractClassMetaData().getDiscriminatorValue();
        if (discVal != null) {
            discrimMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, discrimMapping), discVal);
            jdbcPosition += discrimMapping.getNumberOfDatastoreMappings();
        }
    }
    // Include all subclasses
    if (includeSubclasses) {
        RDBMSStoreManager storeMgr = discrimMapping.getStoreManager();
        Collection<String> subclasses = storeMgr.getSubClassesForClass(info.getClassName(), true, clr);
        if (subclasses != null && subclasses.size() > 0) {
            Iterator<String> iter = subclasses.iterator();
            while (iter.hasNext()) {
                String subclass = iter.next();
                Class subcls = clr.classForName(subclass);
                if (!Modifier.isAbstract(subcls.getModifiers())) {
                    AbstractClassMetaData subclassCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(subclass, clr);
                    Object discVal = subclassCmd.getDiscriminatorValue();
                    if (strategy != DiscriminatorStrategy.NONE) {
                        discrimMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, discrimMapping), discVal);
                        jdbcPosition += discrimMapping.getNumberOfDatastoreMappings();
                    }
                }
            }
        }
    }
    return jdbcPosition;
}
Also used : DiscriminatorStrategy(org.datanucleus.metadata.DiscriminatorStrategy) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 DiscriminatorStrategy (org.datanucleus.metadata.DiscriminatorStrategy)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1