At 08:47 AM 5/30/99 -0800, [EMAIL PROTECTED] wrote:
>
>look at CSpAnyObject, it takes a Serializable in its constructor.
>
>you can use CSpAnyObject.get() and cast to your class type when
>you want to pull it out.
>
>-Chip

This is a good idea, but be careful.  

If you use CSpAnyObject for simple values, you might disable ND from using
the value.

I had a conversion routine to build a CSpValue for any basic java Object. I
was mapping the Object type to the CSpValue type
as closely as possible, e.g., Double -> CSpDouble.

It seemed  that I could cut out a lot of 'instanceof' testing by throwing
everything into CSpAnyObjects.

Here's how something worked before the change:

1.  I have a String value that I want to set as a dynamic criterion.
2.  I call my utility method getCSpValue to return a CSpValue.
3.  Based on the argument being a String, it returns a CSpString.
4.  I addDynamicCriterion(..) to a dataobject using the CSpString as the
argument
5.  I execute the DO.  ND goes to create the sql and decides it needs a
numeric value for this column.  It executes doubleValue on the CSpString
and the string parses into a number with no problems.

Here's what happened (I think) after the change:
1.  I have a String value that I want to set as a dynamic criterion.
2.  I call my utility method getCSpValue to return a CSpValue.
3.  Based on the argument not being null, and not already being a CSpValue,
it returns a CSpAnyObject.
4.  I addDynamicCriterion(..) to a dataobject using the CSpAnyObject as the
argument
5.  I execute the DO.  ND goes to create the sql and decides it needs a
numeric value for this column.  It attempts to execute doubleValue on the
CSpAnyObject.  The CSpAnyObject issues a log message (and an Exception??)
that it doesn't support doubleValue.  The execution of the DO fails.


I guess this makes sense.  The CSpAnyObject doesn't know anything about the
object that it contains other than the fact that it is Serializable, so how
would it know how to pull a double or other numeric value out of it.

Immediately below is the code that caused the problem.  Below that is the
code I am using, which is basically the same as I have been using all
along, except that now CSpAnyObject is used if the argument does not match
any of the standard CSpValue classes.

-- Curt Springer, Team ND

(bum code)

//**************************************************************************
***
    /**
        * This utility function converts non-proprietary java Objects into
        * equivalent
        * NetDynamics CSpValues:
        * <BR><BR>
        * If the passed Object is a CSpValue, it is returned 'as is'.
        * If the passed Object is null, a CSpNull is returned
        * 
        * Otherwise, a CSpAnyObject is created to contain the object, if it is
Serializable
        * Otherwise, an Exception is thrown
    * <BR><BR>
    * Note that if a Serializable Vector, Hashtable, or other complex
Serializable contains non-Serializable
    * non-transient components, there could be runtime errors.
    *
        * @param caller  handle to calling object for logging (because this is
static)
    * @param  value - The java Serializable to be converted
        * @return  CSpValue - The equivalent CSpValue.
        * @exception Exception
    *
    */

//**************************************************************************
***
public static CSpValue getCSpValue(HMSLog caller, Object value)
                                                                                       
                 throws Exception
//********************************************************************
//* Get value from buffer.  Return it as a CSpValue.
//********************************************************************
        {
        try
                {
                        if (value == null)
                                {
                                return new CSpNull();
                                }//if (value == null)
                        if (value instanceof CSpValue)
                                {
                                return((CSpValue) value);       //return 'as is'
                                }       //if (value instanceof CSpValue)
                        if (value instanceof Serializable)
                                {
                                return new CSpAnyObject((Serializable) value);
                                }//if (value instanceof Serializable)
                        throw (new Exception(value.getClass().getName()+" is not 
Serializable,
can not convert to CSpValue"));
                }       //try
        catch (Exception ex)
                {
                        caller.writeLog(caller,"getCSpValue(): " +ex);
                        throw(ex);
                }       //catch
        }       //public static CSpValue getCSpValue(HMSLog caller, Object value)
                                                                                
//throws Exception

(production code)

//**************************************************************************
***
    /**
        * This utility function converts non-proprietary java Objects into
        * equivalent
        * NetDynamics CSpValues:
        * <BR><BR>
        * If the passed Object is a CSpValue, it is returned 'as is'.
        * If the passed Object is null, a CSpNull is returned
    * <BR><BR>
        * The following java Objects are converted:
    * <br>
        * <BR>String -> CSpString
        * <BR>Integer -> CSpInteger
        * <BR>Double -> CSpDouble

        * <BR>Float -> CSpFloat
        * <BR>Long -> CSpLong
        * <BR>Boolean -> CSpBoolean
        * <BR>Date -> CSpDatetime
        * <BR>Calendar -> CSpDatetime
        * <BR>Vector -> CSpVector (getCSpValue will also be applied to each member)
        * <br>Hashtable -> CSpHashTable (getCSpValue will also be applied to each
member)
        * <BR>HMSNull -> CSpNull
        * <br>Character -> CSpCharacter
        * <BR><BR>
        * Otherwise, if the Object is Serializable, create a CSpAnyObject.
        * If not Serializable, throw an Exception.
        *<br><br>
        * Note, it is preferable to convert simple java Objects to the direct
CSpValue equivalents,
        * as it seems that ND does not support numeric conversions on CSpAnyObject
(for example,
        * pulling out a numeric version to build a sql 'where' clause.
    *
        * @param caller  handle to calling object for logging (because this is
static)
    * @param  value - The java Object to be converted
        * @return  CSpValue - The equivalent CSpValue.
        * @exception Exception
    *
    */

//**************************************************************************
***
public static CSpValue getCSpValue(HMSLog caller, Object value)
                                                                                       
                 throws Exception
//********************************************************************
//* Get value from buffer.  Return it as a CSpValue.
//********************************************************************
        {
        try
                {
                        if (value instanceof CSpValue)
                                {
                                return((CSpValue) value);       //return 'as is'
                                }       //if (value instanceof CSpValue)
                        if (value instanceof String)
                                {
                                return(new CSpString((String) value));
                                }       //if (value instanceof String)

                        if (value instanceof Integer)
                                {
                                return(new CSpInteger(((Integer) value).intValue()));
                                }       //if (value instanceof Integer)

                        if (value instanceof Double)
                                {
                                return(new CSpDouble(((Double) value).doubleValue()));
                                }       //if (value instanceof Double)

                        if (value instanceof Float)
                                {
                                return(new CSpFloat(((Float) value).floatValue()));
                                }       //if (value instanceof Float)

                        if (value instanceof Long)
                                {
                                return(new CSpLong(((Long) value).longValue()));
                                }       //if (value instanceof Long)

                        if (value instanceof Boolean)
                                {
                                return(new CSpBoolean(((Boolean) 
value).booleanValue()));
                                }       //if (value instanceof Boolean)

                        if (value instanceof Date)
                                {
                                    //this now deprecated:
                                return(new CSpDatetime((Date) value));
                                }       //if (value instanceof Date)

                        if (value instanceof Calendar)
                                {
                                return(new CSpDatetime((Calendar) value));
                                }       //if (value instanceof Calendar)

                        if ((value == null)|(value instanceof HMSNull))
                                {
                                return(new CSpNull());
                                }       //((value == null)|(value instanceof HMSNull))

                        if (value instanceof Vector)
                                {
                                CSpVector v = new CSpVector();
                                //now convert the contents into CSpValues:
                                for (Enumeration e= ((Vector) 
value).elements();e.hasMoreElements();)
                                {
                                  Object nextvalue = e.nextElement();
                                  v.put(getCSpValue(caller, nextvalue));

                                }//for (Enumeration e= ((Vector) 
value).elements();e.hasMoreElements;)
                                return(v);
                                }       //if (value instanceof Vector)

                        if (value instanceof Hashtable)
                                {
                                CSpHashTable h = new CSpHashTable();
                                //now convert the contents into CSpValues:
                                Enumeration k= ((Hashtable) value).keys();
                                for (Enumeration e= ((Hashtable) 
value).elements();e.hasMoreElements();)
                                {
                                  Object key = k.nextElement();
                                  Object nextvalue = e.nextElement();
                                  h.put(key.toString(), getCSpValue(caller, 
nextvalue));

                                }//for (Enumeration e= ((Hashtable) 
value).elements();e.hasMoreElements;)
                                return(h);
                                }       //if (value instanceof Hashtable)
                                
                        if (value instanceof Character)
                                {
                                return(new CSpCharacter(((Character) 
value).charValue()));
                                }//if (value instanceof Character)
                        
                        if (value instanceof Serializable)
                                {
                                return(new CSpAnyObject(((Serializable) value)));
                                }//if (value instanceof Serializable)
                                
                        throw (new Exception("could not create CSpValue from class
"+value.getClass().getName())); //fall-through

                }       //try
        catch (Exception ex)
                {
                        caller.writeLog(caller,"getCSpValue(): " +ex);
                        throw(ex);
                }       //catch
        }       //public static CSpValue getCSpValue(HMSLog caller, Object value)
                                                                                
//throws Exception




>
>[EMAIL PROTECTED] wrote:
>>Hi,
>>
>>  I would like to know whether there are any class for wrapping any java
object into CSpValue object?? I had to create a new class
>>object extending CSpValue and had to map the member variables to the java
object, which is roundabout way.
>>
>>Regards,
>>Swami.
>
>_________________________________________________________________________
>
>For help in using, subscribing, and unsubscribing to the discussion
>forums, please go to: http://www.netdynamics.com/support/visitdevfor.html
>
>For dire need help, email: [EMAIL PROTECTED]
> 
_________________________________________________________________________

For help in using, subscribing, and unsubscribing to the discussion
forums, please go to: http://www.netdynamics.com/support/visitdevfor.html

For dire need help, email: [EMAIL PROTECTED]

Reply via email to