db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util BrokerHelper.java
Date Wed, 23 Apr 2003 16:01:12 GMT
brj         2003/04/23 09:01:12

  Modified:    src/java/org/apache/ojb/broker/util BrokerHelper.java
  Log:
  convert autoincremented values to sql as well.
  fix by ron gallagher
  
  Revision  Changes    Path
  1.20      +318 -332  db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- BrokerHelper.java	21 Apr 2003 07:55:57 -0000	1.19
  +++ BrokerHelper.java	23 Apr 2003 16:01:12 -0000	1.20
  @@ -78,7 +78,6 @@
   */
   //#endif
   
  -
   /**
    *
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  @@ -86,335 +85,322 @@
    */
   public class BrokerHelper
   {
  -    public static final String REPOSITORY_NAME_SEPARATOR = "#";
  -
  -    private PersistenceBroker broker;
  +	public static final String REPOSITORY_NAME_SEPARATOR = "#";
  +	private PersistenceBroker broker;
   
  -    public BrokerHelper(PersistenceBroker broker)
  -    {
  -        this.broker = broker;
  -    }
  -
  -    /**
  -     * splits up the name string and extract db url,
  -     * user name and password and build a new PBKey
  -     * instance - the token '#' is used to separate
  -     * the substrings.
  -     * @throws PersistenceBrokerException if given name was <code>null</code>
  -     */
  -    public static PBKey extractAllTokens(String name)
  -    {
  -        if (name == null)
  -        {
  -            throw new PersistenceBrokerException("Could not extract PBKey, given argument
is 'null'");
  -        }
  -        String user = null;
  -        String passwd = null;
  -        StringTokenizer tok = new StringTokenizer(name, REPOSITORY_NAME_SEPARATOR);
  -        String dbName = tok.nextToken();
  -        if (tok.hasMoreTokens())
  -        {
  -            user = tok.nextToken();
  -            if (user != null && user.trim().equals(""))
  -            {
  -                user = null;
  -            }
  -        }
  -        if (tok.hasMoreTokens())
  -        {
  -            if (user != null) passwd = tok.nextToken();
  -        }
  -        if (user != null && passwd == null)
  -        {
  -            passwd = "";
  -        }
  -        PBKey key = new PBKey(dbName, user, passwd);
  -        return key;
  -    }
  -
  -    /**
  -     * Check if the user of the given PBKey was <code>null</code>, if so we
try to
  -     * get user/password from the jdbc-connection-descriptor matching the given
  -     * PBKey.getAlias().
  -     */
  -    public static PBKey crossCheckPBKey(PBKey key)
  -    {
  -        if (key.getUser() == null)
  -        {
  -            PBKey defKey = MetadataManager.getInstance().
  -                    connectionRepository().getStandardPBKeyForJcdAlias(key.getAlias());
  -            if (defKey != null)
  -            {
  -                return defKey;
  -            }
  -        }
  -        return key;
  -    }
  -
  -    public static boolean isRunningInServerMode()
  -    {
  -//        return ((PersistenceBrokerConfiguration) PersistenceBrokerFactory.
  -//                getConfigurator().getConfigurationFor(null)).isRunningInServerMode();
  -        return false;
  -    }
  -
  -
  -
  -    // ***************************************************************************
  -    // methods from ClassDescriptor
  -    // ***************************************************************************
  -
  -
  -    /**
  -     * returns an Array with an Objects PK VALUES if convertToSql is true, any
  -     * associated java-to-sql conversions are applied. If the Object is a Proxy
  -     * or a VirtualProxy NO conversion is necessary.
  -     *
  -     * @param objectOrProxy
  -     * @param convertToSql
  -     * @return Object[]
  -     * @throws PersistenceBrokerException
  -     */
  -    public Object[] getKeyValues(ClassDescriptor cld, Object objectOrProxy, boolean convertToSql)
  -            throws PersistenceBrokerException
  -    {
  -        /*
  -        arminw
  -        TODO: Check it out. Because the isProxyClass method is costly and most objects
  -        aren't proxies, I add a instanceof check before. Is every Proxy a instance of Proxy?
  -        */
  -        if ((objectOrProxy instanceof Proxy) && Proxy.isProxyClass(objectOrProxy.getClass()))
  -        {
  -            // return getKeyValuesForProxy((Proxy) objectOrProxy);
  -            IndirectionHandler handler;
  -            handler = (IndirectionHandler) Proxy.getInvocationHandler((Proxy) objectOrProxy);
  -            return handler.getIdentity().getPrimaryKeyValues();
  -        }
  -        else if (objectOrProxy instanceof VirtualProxy)
  -        {
  -            // return getKeyValuesForProxy((VirtualProxy) objectOrProxy);
  -            IndirectionHandler handler;
  -            handler = (IndirectionHandler) VirtualProxy.getIndirectionHandler((VirtualProxy)
objectOrProxy);
  -            return handler.getIdentity().getPrimaryKeyValues();
  -        }
  -        else
  -        {
  -            // return getKeyValuesForObject(broker, cld, objectOrProxy, convertToSql);
  -
  -            FieldDescriptor[] pkFields = cld.getPkFields();
  -            Object[] result = new Object[pkFields.length];
  -            for (int i = 0; i < result.length; i++)
  -            {
  -                FieldDescriptor fmd = pkFields[i];
  -                PersistentField f = fmd.getPersistentField();
  -                Object cv = f.get(objectOrProxy);
  -
  -                // handle autoincrement attributes if not filled
  -                if (fmd.isAutoIncrement())
  -                {
  -                    // getAutoIncrementValue returns a value that already
  -                    // has a field conversion run on it
  -                    cv = getAutoIncrementValue(fmd, objectOrProxy, cv);
  -                }
  -                else if (convertToSql)
  -                {
  -                    // BRJ : apply type and value mapping
  -                    cv = fmd.getFieldConversion().javaToSql(cv);
  -                }
  -                result[i] = cv;
  -            }
  -            return result;
  -        }
  -    }
  -
  -    /**
  -     * Return key Values of an Identity
  -     * @param cld
  -     * @param oid
  -     * @return Object[]
  -     * @throws PersistenceBrokerException
  -     */
  -    public Object[] getKeyValues(ClassDescriptor cld, Identity oid)
  -            throws PersistenceBrokerException
  -    {
  -        return getKeyValues(cld, oid, true);
  -    }
  -
  -    /**
  -     * Return key Values of an Identity
  -     * @param cld
  -     * @param oid
  -     * @param convertToSql
  -     * @return Object[]
  -     * @throws PersistenceBrokerException
  -     */
  -    public Object[] getKeyValues(ClassDescriptor cld, Identity oid, boolean convertToSql)
  -            throws PersistenceBrokerException
  -    {
  -        FieldDescriptor[] pkFields = cld.getPkFields();
  -        Object[] result = new Object[pkFields.length];
  -        Object[] pkValues = oid.getPrimaryKeyValues();
  -
  -        try
  -        {
  -            for (int i = 0; i < result.length; i++)
  -            {
  -                FieldDescriptor fmd = pkFields[i];
  -                Object cv = pkValues[i];
  -
  -                if (convertToSql)
  -                {
  -                    // BRJ : apply type and value mapping
  -                    cv = fmd.getFieldConversion().javaToSql(cv);
  -                }
  -                result[i] = cv;
  -            }
  -        }
  -        catch (Exception e)
  -        {
  -            throw new PersistenceBrokerException("Could not generate primary key values
for given Identity " +
  -                    SystemUtils.LINE_SEPARATOR + oid + ", exception was " + e, e);
  -        }
  -        return result;
  -    }
  -
  -
  -    /**
  -     * returns an Array with an Objects PK VALUES, with any java-to-sql
  -     * FieldConversion applied. If the Object is a Proxy or a VirtualProxy NO
  -     * conversion is necessary.
  -     *
  -     * @param objectOrProxy
  -     * @return Object[]
  -     * @throws PersistenceBrokerException
  -     */
  -    public Object[] getKeyValues(ClassDescriptor cld, Object objectOrProxy)
  -            throws PersistenceBrokerException
  -    {
  -        return getKeyValues(cld, objectOrProxy, true);
  -    }
  -
  -
  -    /**
  -     * Get an autoincremented value that has already
  -     * had a field conversion run on it.
  -     * @throws MetadataException if there is an erros accessing obj field values
  -     * @deprecated
  -     */
  -    protected Object getAutoIncrementValue(FieldDescriptor fmd, Object obj, Object cv)
  -    {
  -        if ((cv == null) || ((cv instanceof Number) && (((Number) cv).intValue()
== 0)))
  -        {
  -            PersistentField f = fmd.getPersistentField();
  -            Object result = cv;
  -            try
  -            {
  -                Object currentValue = f.get(obj);
  -                if ((currentValue == null) || ((cv instanceof Number) && ((Number)
currentValue).intValue() == 0))
  -                {
  -                    // lookup SeqMan for a value matching db column an fieldconversion
  -                    result = broker.serviceSequenceManager().getUniqueValue(fmd);
  -                    // reflect autoincrement value back into object
  -                    f.set(obj, result);
  -                }
  -                return result;
  -            }
  -            catch (MetadataException e)
  -            {
  -                throw new PersistenceBrokerException(
  -                    "Error while trying to autoincrement field " +
  -                    f.getDeclaringClass() + "#" + f.getName(), e);
  -            }
  -            catch (SequenceManagerException e)
  -            {
  -                throw new PersistenceBrokerException("Could not get key value", e);
  -            }
  -        }
  -        else
  -        {
  -            return cv;
  -        }
  -    }
  -
  -    /**
  -     * returns an Array with an Objects NON-PK VALUES (READ/WRITE only)
  -     * @throws MetadataException if there is an erros accessing o field values
  -     */
  -    public Object[] getNonKeyRwValues(ClassDescriptor cld, Object o)
  -            throws PersistenceBrokerException
  -    {
  -        FieldDescriptor[] fields = cld.getNonPkRwFields();
  -        Object[] result = new Object[fields.length];
  -
  -        for (int i = 0; i < result.length; i++)
  -        {
  -            FieldDescriptor fmd = fields[i];
  -            PersistentField f = fmd.getPersistentField();
  -            Object cv = f.get(o);
  -
  -            // handle autoincrement attributes if not filled
  -            if (fmd.isAutoIncrement())
  -            {
  -                // getAutoIncrementValue returns a value that already
  -                // has a field conversion run on it
  -                cv = getAutoIncrementValue(fmd, o, cv);
  -            }
  -            else
  -            {
  -                // apply type and value conversion
  -                cv = fmd.getFieldConversion().javaToSql(cv);
  -            }
  -
  -            result[i] = cv;
  -        }
  -        return result;
  -    }
  -
  -    /**
  -     * returns an array containing values for all the Objects attribute (READ/WRITE only)
  -     * @throws MetadataException if there is an erros accessing obj field values
  -     */
  -    public Object[] getAllRwValues(ClassDescriptor cld, Object obj) throws PersistenceBrokerException
  -    {
  -        FieldDescriptor[] fields = cld.getAllRwFields();
  -        Object[] result = new Object[fields.length];
  -
  -        for (int i = 0; i < fields.length; i++)
  -        {
  -            FieldDescriptor fmd = fields[i];
  -            PersistentField f = fmd.getPersistentField();
  -            Object cv = f.get(obj);
  -
  -            // handle autoincrement attributes if not filled
  -            if (fmd.isAutoIncrement())
  -            {
  -                // getAutoIncrementValue returns a value that already
  -                // has a field conversion run on it
  -                cv = getAutoIncrementValue(fmd, obj, cv);
  -            }
  -            else
  -            {
  -                // apply type and value mapping
  -                cv = fmd.getFieldConversion().javaToSql(cv);
  -            }
  -
  -            result[i] = cv;
  -        }
  -        return result;
  -    }
  -
  -
  -    public static String buildMessageString(Object obj, Object value, Field field)
  -    {
  -        String eol = SystemUtils.LINE_SEPARATOR;
  -        StringBuffer buf = new StringBuffer();
  -        buf.
  -                append(eol + "object class[ " + (obj != null ? obj.getClass().getName()
: null)).
  -                append(eol + "target field: " + field.getName()).
  -                append(eol + "target field type: " + (field != null ? field.getType() :
null)).
  -                append(eol + "object value class: " + (value != null ? value.getClass().getName()
: null)).
  -                append(eol + "object value: " + (value != null ? value : null)).
  -                append("]");
  -        return buf.toString();
  -    }
  +	public BrokerHelper(PersistenceBroker broker)
  +	{
  +		this.broker = broker;
  +	}
  +
  +	/**
  +	 * splits up the name string and extract db url,
  +	 * user name and password and build a new PBKey
  +	 * instance - the token '#' is used to separate
  +	 * the substrings.
  +	 * @throws PersistenceBrokerException if given name was <code>null</code>
  +	 */
  +	public static PBKey extractAllTokens(String name)
  +	{
  +		if (name == null)
  +		{
  +			throw new PersistenceBrokerException("Could not extract PBKey, given argument is 'null'");
  +		}
  +		String user = null;
  +		String passwd = null;
  +		StringTokenizer tok = new StringTokenizer(name, REPOSITORY_NAME_SEPARATOR);
  +		String dbName = tok.nextToken();
  +		if (tok.hasMoreTokens())
  +		{
  +			user = tok.nextToken();
  +			if (user != null && user.trim().equals(""))
  +			{
  +				user = null;
  +			}
  +		}
  +		if (tok.hasMoreTokens())
  +		{
  +			if (user != null)
  +				passwd = tok.nextToken();
  +		}
  +		if (user != null && passwd == null)
  +		{
  +			passwd = "";
  +		}
  +		PBKey key = new PBKey(dbName, user, passwd);
  +		return key;
  +	}
  +
  +	/**
  +	 * Check if the user of the given PBKey was <code>null</code>, if so we try
to
  +	 * get user/password from the jdbc-connection-descriptor matching the given
  +	 * PBKey.getAlias().
  +	 */
  +	public static PBKey crossCheckPBKey(PBKey key)
  +	{
  +		if (key.getUser() == null)
  +		{
  +			PBKey defKey = MetadataManager.getInstance().connectionRepository().getStandardPBKeyForJcdAlias(key.getAlias());
  +			if (defKey != null)
  +			{
  +				return defKey;
  +			}
  +		}
  +		return key;
  +	}
  +
  +	public static boolean isRunningInServerMode()
  +	{
  +		//        return ((PersistenceBrokerConfiguration) PersistenceBrokerFactory.
  +		//                getConfigurator().getConfigurationFor(null)).isRunningInServerMode();
  +		return false;
  +	}
  +
  +	// ***************************************************************************
  +	// methods from ClassDescriptor
  +	// ***************************************************************************
  +
  +	/**
  +	 * returns an Array with an Objects PK VALUES if convertToSql is true, any
  +	 * associated java-to-sql conversions are applied. If the Object is a Proxy
  +	 * or a VirtualProxy NO conversion is necessary.
  +	 *
  +	 * @param objectOrProxy
  +	 * @param convertToSql
  +	 * @return Object[]
  +	 * @throws PersistenceBrokerException
  +	 */
  +	public Object[] getKeyValues(ClassDescriptor cld, Object objectOrProxy, boolean convertToSql)
throws PersistenceBrokerException
  +	{
  +		/*
  +		arminw
  +		TODO: Check it out. Because the isProxyClass method is costly and most objects
  +		aren't proxies, I add a instanceof check before. Is every Proxy a instance of Proxy?
  +		*/
  +		if ((objectOrProxy instanceof Proxy) && Proxy.isProxyClass(objectOrProxy.getClass()))
  +		{
  +			// return getKeyValuesForProxy((Proxy) objectOrProxy);
  +			IndirectionHandler handler;
  +			handler = (IndirectionHandler) Proxy.getInvocationHandler((Proxy) objectOrProxy);
  +			return handler.getIdentity().getPrimaryKeyValues();
  +		}
  +		else if (objectOrProxy instanceof VirtualProxy)
  +		{
  +			// return getKeyValuesForProxy((VirtualProxy) objectOrProxy);
  +			IndirectionHandler handler;
  +			handler = (IndirectionHandler) VirtualProxy.getIndirectionHandler((VirtualProxy) objectOrProxy);
  +			return handler.getIdentity().getPrimaryKeyValues();
  +		}
  +		else
  +		{
  +			// return getKeyValuesForObject(broker, cld, objectOrProxy, convertToSql);
  +
  +			FieldDescriptor[] pkFields = cld.getPkFields();
  +			Object[] result = new Object[pkFields.length];
  +			for (int i = 0; i < result.length; i++)
  +			{
  +				FieldDescriptor fd = pkFields[i];
  +				PersistentField f = fd.getPersistentField();
  +				Object cv = f.get(objectOrProxy);
  +
  +				// handle autoincrement attributes if not filled
  +				if (fd.isAutoIncrement())
  +				{
  +					// getAutoIncrementValue returns a value that is
  +					// properly typed for the java-world.  This value
  +					// needs to be converted to it's corresponding 
  +					// sql type so that the entire result array contains
  +					// objects that are properly typed for sql.
  +					cv = getAutoIncrementValue(fd, objectOrProxy, cv);
  +					if (convertToSql)
  +					{
  +						cv = fd.getFieldConversion().javaToSql(cv);
  +					}
  +				}
  +				else if (convertToSql)
  +				{
  +					// BRJ : apply type and value mapping
  +					cv = fd.getFieldConversion().javaToSql(cv);
  +				}
  +				result[i] = cv;
  +			}
  +			return result;
  +		}
  +	}
  +
  +	/**
  +	 * Return key Values of an Identity
  +	 * @param cld
  +	 * @param oid
  +	 * @return Object[]
  +	 * @throws PersistenceBrokerException
  +	 */
  +	public Object[] getKeyValues(ClassDescriptor cld, Identity oid) throws PersistenceBrokerException
  +	{
  +		return getKeyValues(cld, oid, true);
  +	}
  +
  +	/**
  +	 * Return key Values of an Identity
  +	 * @param cld
  +	 * @param oid
  +	 * @param convertToSql
  +	 * @return Object[]
  +	 * @throws PersistenceBrokerException
  +	 */
  +	public Object[] getKeyValues(ClassDescriptor cld, Identity oid, boolean convertToSql)
throws PersistenceBrokerException
  +	{
  +		FieldDescriptor[] pkFields = cld.getPkFields();
  +		Object[] result = new Object[pkFields.length];
  +		Object[] pkValues = oid.getPrimaryKeyValues();
  +
  +		try
  +		{
  +			for (int i = 0; i < result.length; i++)
  +			{
  +				FieldDescriptor fd = pkFields[i];
  +				Object cv = pkValues[i];
  +
  +				if (convertToSql)
  +				{
  +					// BRJ : apply type and value mapping
  +					cv = fd.getFieldConversion().javaToSql(cv);
  +				}
  +				result[i] = cv;
  +			}
  +		}
  +		catch (Exception e)
  +		{
  +			throw new PersistenceBrokerException(
  +				"Could not generate primary key values for given Identity " + SystemUtils.LINE_SEPARATOR
+ oid + ", exception was " + e,
  +				e);
  +		}
  +		return result;
  +	}
  +
  +	/**
  +	 * returns an Array with an Objects PK VALUES, with any java-to-sql
  +	 * FieldConversion applied. If the Object is a Proxy or a VirtualProxy NO
  +	 * conversion is necessary.
  +	 *
  +	 * @param objectOrProxy
  +	 * @return Object[]
  +	 * @throws PersistenceBrokerException
  +	 */
  +	public Object[] getKeyValues(ClassDescriptor cld, Object objectOrProxy) throws PersistenceBrokerException
  +	{
  +		return getKeyValues(cld, objectOrProxy, true);
  +	}
  +
  +	/**
  +	 * Get an autoincremented value that has already
  +	 * had a field conversion run on it.
  +	 * <p>
  +	 * The data type of the value that is returned by this method is
  +	 * compatible with the java-world.  The return value has <b>NOT</b>
  +	 * been run through a field conversion and converted to a corresponding
  +	 * sql-type.
  +	 * 
  +	 * @throws MetadataException if there is an erros accessing obj field values
  +	 * @deprecated
  +	 */
  +	protected Object getAutoIncrementValue(FieldDescriptor fd, Object obj, Object cv)
  +	{
  +		if ((cv == null) || ((cv instanceof Number) && (((Number) cv).intValue() == 0)))
  +		{
  +			PersistentField f = fd.getPersistentField();
  +			Object result = cv;
  +			try
  +			{
  +				Object currentValue = f.get(obj);
  +				if ((currentValue == null) || ((cv instanceof Number) && ((Number) currentValue).intValue()
== 0))
  +				{
  +					// lookup SeqMan for a value matching db column an fieldconversion
  +					result = broker.serviceSequenceManager().getUniqueValue(fd);
  +					// reflect autoincrement value back into object
  +					f.set(obj, result);
  +				}
  +				return result;
  +			}
  +			catch (MetadataException e)
  +			{
  +				throw new PersistenceBrokerException(
  +					"Error while trying to autoincrement field " + f.getDeclaringClass() + "#" + f.getName(),
  +					e);
  +			}
  +			catch (SequenceManagerException e)
  +			{
  +				throw new PersistenceBrokerException("Could not get key value", e);
  +			}
  +		}
  +		else
  +		{
  +			return cv;
  +		}
  +	}
  +
  +	/**
  +	 * Get the values of the fields for an obj
  +	 * @param fields
  +	 * @param obj
  +	 * @throws PersistenceBrokerException
  +	 */
  +	protected Object[] getValues(FieldDescriptor[] fields, Object obj) throws PersistenceBrokerException
  +	{
  +		Object[] result = new Object[fields.length];
  +
  +		for (int i = 0; i < result.length; i++)
  +		{
  +			FieldDescriptor fd = fields[i];
  +			PersistentField f = fd.getPersistentField();
  +			Object cv = f.get(obj);
  +
  +			// handle autoincrement attributes if not filled
  +			if (fd.isAutoIncrement())
  +			{
  +				// getAutoIncrementValue returns a value that is
  +				// properly typed for the java-world.  This value
  +				// needs to be converted to it's corresponding 
  +				// sql type so that the entire result array contains
  +				// objects that are properly typed for sql.
  +				cv = getAutoIncrementValue(fd, obj, cv);
  +			}
  +
  +			// apply type and value conversion
  +			result[i] = fd.getFieldConversion().javaToSql(cv);
  +		}
  +		return result;
  +	}
  +
  +	/**
  +	 * returns an Array with an Objects NON-PK VALUES (READ/WRITE only)
  +	 * @throws MetadataException if there is an erros accessing o field values
  +	 */
  +	public Object[] getNonKeyRwValues(ClassDescriptor cld, Object obj) throws PersistenceBrokerException
  +	{
  +		return getValues(cld.getNonPkRwFields(), obj);
  +	}
  +
  +	/**
  +	 * returns an array containing values for all the Objects attribute (READ/WRITE only)
  +	 * @throws MetadataException if there is an erros accessing obj field values
  +	 */
  +	public Object[] getAllRwValues(ClassDescriptor cld, Object obj) throws PersistenceBrokerException
  +	{
  +		return getValues(cld.getAllRwFields(), obj);
  +	}
  +
  +	public static String buildMessageString(Object obj, Object value, Field field)
  +	{
  +		String eol = SystemUtils.LINE_SEPARATOR;
  +		StringBuffer buf = new StringBuffer();
  +		buf
  +			.append(eol + "object class[ " + (obj != null ? obj.getClass().getName() : null))
  +			.append(eol + "target field: " + field.getName())
  +			.append(eol + "target field type: " + (field != null ? field.getType() : null))
  +			.append(eol + "object value class: " + (value != null ? value.getClass().getName() :
null))
  +			.append(eol + "object value: " + (value != null ? value : null))
  +			.append("]");
  +		return buf.toString();
  +	}
   }
  
  
  

Mime
View raw message