Package org.jpox.store.rdbms.query

Source Code of org.jpox.store.rdbms.query.RDBMSQueryUtils

/**********************************************************************
Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.jpox.store.rdbms.query;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import org.jpox.ObjectManager;
import org.jpox.metadata.AbstractClassMetaData;
import org.jpox.metadata.DiscriminatorMetaData;
import org.jpox.metadata.DiscriminatorStrategy;
import org.jpox.store.mapped.DatastoreClass;
import org.jpox.store.mapped.expression.MetaDataStringLiteral;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.query.QueryUtils;

/**
* Utilities for use in queries specific to RDBMS.
* @version $Revision: 1.1 $
*/
public class RDBMSQueryUtils extends QueryUtils
{
    /**
     * Convenience method that takes a result set that contains a discriminator column and returns
     * the class name that it represents.
     * TODO This is RDBMS specific so consider moving it to an RDBMSQueryUtils in the future.
     * @param table Primary table of the select (so we can identify the primary class and discriminator range)
     * @param rs The result set
     * @param om Object Manager
     * @return The class name for the object represented in the current row
     */
    public static String getClassNameFromDiscriminatorResultSetRow(DatastoreClass table, ResultSet rs, ObjectManager om)
    {
        String rowClassName = null;

        JavaTypeMapping discriminatorMapping = table.getDiscriminatorMapping(true);
        DiscriminatorMetaData dismd = table.getDiscriminatorMetaData();
        if (discriminatorMapping != null && dismd.getStrategy() != DiscriminatorStrategy.NONE)
        {
            try
            {
                String discriminatorColName = discriminatorMapping.getDataStoreMapping(0).getDatastoreField().getIdentifier().getIdentifier();
                //TODO use discriminatorMapping.getObject()
                String discriminatorValue = rs.getString(discriminatorColName);
                if (dismd.getStrategy() == DiscriminatorStrategy.CLASS_NAME)
                {
                    rowClassName = discriminatorValue;
                }
                else if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                {
                    // Check the main class type for the table
                    String className = table.getType();
                    if (dismd.getValue().equals(discriminatorValue))
                    {
                        rowClassName = className;
                    }
                    else
                    {
                        // Go through all possible subclasses to find one with this value
                        Iterator iterator = om.getStoreManager().getSubClassesForClass(table.getType(), true, om.getClassLoaderResolver()).iterator();
                        while (iterator.hasNext())
                        {
                            className = (String)iterator.next();
                            AbstractClassMetaData classCmd = om.getMetaDataManager().getMetaDataForClass(className, om.getClassLoaderResolver());
                            if (discriminatorValue.equals(classCmd.getInheritanceMetaData().getDiscriminatorMetaData().getValue()))
                            {
                                rowClassName = className;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                // Do nothing
            }
        }
        return rowClassName;
    }

    /**
     * Convenience method that takes a result set that contains a JPOXMETADATA column and returns
     * the class name.
     * TODO This is RDBMS specific so consider moving it to an RDBMSQueryUtils in the future.
     * @param rs The result set
     * @return The class name for the object represented in the current row
     */
    public static String getClassNameFromJPOXMetaDataResultSetRow(ResultSet rs)
    {
        try
        {
            return rs.getString(MetaDataStringLiteral.QUERY_META_DATA).trim();
        }
        catch (SQLException sqle)
        {
            return null;
        }
    }
}
TOP

Related Classes of org.jpox.store.rdbms.query.RDBMSQueryUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.