Re: Custom describe() for use with BeanUtil.populate()

2002-05-26 Thread Duke Ronlund

Hi, 

Look to the comomns/beanutils source, either download it from the
commons project or from the cvs
http://cvs.apache.org/viewcvs.cgi/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java

I have done this in the past by just customising the populate/describe
methods to suit your my purposes.


Duke


On Tue, 2002-05-21 at 04:32, DHS Struts wrote:
> I was wondering if anyone had a custom describe method they would be willing 
> to share as an example of what Ted Husted describes in his "Struts Catalog" 
> in the 'Use a "populate" utility to exchange data with value objects' 
> section?
> 
> I understand the basic concept of needing to write my own describe() for 
> those beans that have Date attributes (because the BeanUtil.populate() can't 
> handle them), but I am not having much luck getting it to work.
> 
> If you have some code and are willing to share I would appreciate it. Also, 
> if you would like to see what I have attempted I can post that here as well.
> 
> Thank you in advance,
> 
> mark n.
> 
> _
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
> 
> 
> --
> To unsubscribe, e-mail:   
> For additional commands, e-mail: 
> 



--
To unsubscribe, e-mail:   
For additional commands, e-mail: 




Re: Custom describe() for use with BeanUtil.populate()

2002-05-21 Thread DHS Struts

Thank you Will, this is very cool. Much better than littering my form beans 
with desribe methods.

mark n.


>From: Will Jaynes <[EMAIL PROTECTED]>
>Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Custom describe() for use with BeanUtil.populate()
>Date: Tue, 21 May 2002 07:12:46 -0400
>
>You don't need your own describe method anymore. BeanUtils has added the
>capability to register conversion methods, to go from String to any
>arbitrary object. So for example, I have a MSCalendar object whose
>toString puts out mm/dd/ format, and can take that same format in a
>constructor.
>
>The MSConverter (attached) is mostly copied from one of Craig's
>converters in the Commons BeanUtils package.
>
>public void testStuff()
> {
> Map map = null;
> int i;
>
> // Register converters that enable conversion of String properties
> // to a designated class. String->MSCalendar
> ConvertUtils.register(new MSCalendarConverter(),
>   MSCalendar.class);
>
> EmployeeBean emp = new EmployeeBean();
> emp.setFirstName("Joe");
> emp.setLastName("Klein");
> emp.setBirthDate(new MSCalendar("1/1/1999"));
>
> EmployeeFormBean form = new EmployeeFormBean();
> try {
>
> // Copy all properties of an EmployeeBean to the corresponding
> // properties of an EmployeeFormBean.
> // This process uses the toString() method of each
> // property in EmployeeBean as the value to put into
> // the corresponding String property in the form.
> map = BeanUtils.describe(emp);
> BeanUtils.populate(form, map);
>
> // Conversely, copy all the properties from the form into
> // the corresponding properties of EmployeeBean, converting
> // from String to the taget object type.
> emp = new EmployeeBean();
> map = BeanUtils.describe(form);
> BeanUtils.populate(emp, map);
>
>  } catch(Exception ex) {
> fail("failure:"+ex.getStackTrace());
> }
> }
>}
>
>DHS Struts wrote:
>>I was wondering if anyone had a custom describe method they would be
>>willing to share as an example of what Ted Husted describes in his
>>"Struts Catalog" in the 'Use a "populate" utility to exchange data with
>>value objects' section?
>>
>>I understand the basic concept of needing to write my own describe() for
>>those beans that have Date attributes (because the BeanUtil.populate()
>>can't handle them), but I am not having much luck getting it to work.
>>
>>If you have some code and are willing to share I would appreciate it.
>>Also, if you would like to see what I have attempted I can post that
>>here as well.
>>
>>Thank you in advance,
>>
>>mark n.
>>
>>_
>>Chat with friends online, try MSN Messenger: http://messenger.msn.com
>>
>>
>>--
>>To unsubscribe, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>For additional commands, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>
>
>
>package edu.umich.umms.beanutils;
>import java.sql.Date;
>import org.apache.commons.beanutils.ConversionException;
>import org.apache.commons.beanutils.Converter;
>
>
>/**
>  * Standard {@link Converter} implementation that converts an incoming
>  * String into a edu.umich.umms.util.MSCalendar object, 
>optionally using a
>  * default value or throwing a {@link ConversionException} if a conversion
>  * error occurs.
>  *
>  */
>
>public final class MSCalendarConverter implements Converter {
>
>
> // --- 
>Constructors
>
>
> /**
>  * Create a {@link Converter} that will throw a {@link 
>ConversionException}
>  * if a conversion error occurs.
>  */
> public MSCalendarConverter() {
>
> this(null);
>
> }
>
>
> /**
>  * Create a {@link Converter} that will return the specified default 
>value
>  * if a conversion error occurs.
>  *
>  * @param defaultValue The default value to be returned
>  */
> public MSCalendarConverter(Object defaultValue) {
>
> this.defaultValue = defaultValue;
>
> }
>
>
> // - Instance 
>Variables
>
>
> /**
>  * The default value specified to our Constructor

Re: Custom describe() for use with BeanUtil.populate()

2002-05-21 Thread Will Jaynes

You don't need your own describe method anymore. BeanUtils has added the 
capability to register conversion methods, to go from String to any 
arbitrary object. So for example, I have a MSCalendar object whose 
toString puts out mm/dd/ format, and can take that same format in a 
constructor.

The MSConverter (attached) is mostly copied from one of Craig's 
converters in the Commons BeanUtils package.

public void testStuff()
 {
 Map map = null;
 int i;

 // Register converters that enable conversion of String properties
 // to a designated class. String->MSCalendar
 ConvertUtils.register(new MSCalendarConverter(),
   MSCalendar.class);

 EmployeeBean emp = new EmployeeBean();
 emp.setFirstName("Joe");
 emp.setLastName("Klein");
 emp.setBirthDate(new MSCalendar("1/1/1999"));

 EmployeeFormBean form = new EmployeeFormBean();
 try {

 // Copy all properties of an EmployeeBean to the corresponding
 // properties of an EmployeeFormBean.
 // This process uses the toString() method of each
 // property in EmployeeBean as the value to put into
 // the corresponding String property in the form.
 map = BeanUtils.describe(emp);
 BeanUtils.populate(form, map);

 // Conversely, copy all the properties from the form into
 // the corresponding properties of EmployeeBean, converting
 // from String to the taget object type.
 emp = new EmployeeBean();
 map = BeanUtils.describe(form);
 BeanUtils.populate(emp, map);

  } catch(Exception ex) {
 fail("failure:"+ex.getStackTrace());
 }
 }
}

DHS Struts wrote:
> I was wondering if anyone had a custom describe method they would be 
> willing to share as an example of what Ted Husted describes in his 
> "Struts Catalog" in the 'Use a "populate" utility to exchange data with 
> value objects' section?
> 
> I understand the basic concept of needing to write my own describe() for 
> those beans that have Date attributes (because the BeanUtil.populate() 
> can't handle them), but I am not having much luck getting it to work.
> 
> If you have some code and are willing to share I would appreciate it. 
> Also, if you would like to see what I have attempted I can post that 
> here as well.
> 
> Thank you in advance,
> 
> mark n.
> 
> _
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
> 
> 
> -- 
> To unsubscribe, e-mail:   
> 
> For additional commands, e-mail: 
> 
> 




package edu.umich.umms.beanutils;
import java.sql.Date;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;


/**
 * Standard {@link Converter} implementation that converts an incoming
 * String into a edu.umich.umms.util.MSCalendar object, optionally using a
 * default value or throwing a {@link ConversionException} if a conversion
 * error occurs.
 *
 */

public final class MSCalendarConverter implements Converter {


// --- Constructors


/**
 * Create a {@link Converter} that will throw a {@link ConversionException}
 * if a conversion error occurs.
 */
public MSCalendarConverter() {

this(null);

}


/**
 * Create a {@link Converter} that will return the specified default value
 * if a conversion error occurs.
 *
 * @param defaultValue The default value to be returned
 */
public MSCalendarConverter(Object defaultValue) {

this.defaultValue = defaultValue;

}


// - Instance Variables


/**
 * The default value specified to our Constructor, if any.
 */
private Object defaultValue = null;


// - Public Methods


/**
 * Convert the specified input object into an output object of the
 * specified type.
 *
 * @param type Data type to which this value should be converted
 * @param value The input value to be converted
 *
 * @exception ConversionException if conversion cannot be performed
 *  successfully
 */
public Object convert(Class type, Object value) {

if (value == null) {
if (defaultValue != null) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}

try {
return new edu.umich.umms.util.MSCalendar((String) value);
//return (Date.valueOf((String) value));
} catch (Exception e) {
if (defaultValue != null) {
return (defaultValue);
} else {
throw new ConversionException(e)

Re: Custom describe() for use with BeanUtil.populate()

2002-05-20 Thread Arron Bates

BeanUtils/PropertyUtils simply got moved to commons, and is maintained 
there.
Same classes, new home.


Arron.

Adam Hardy wrote:

> But BeanUtils has been deprecated - in the Javadocs it says "/At some 
> point after Struts 1.0 final, will be replaced by an equivalent class 
> in the Jakarta Commons Beanutils package./"
>
> I don't know whether this is still true.
>
>
> Adam
>
> DHS Struts wrote:
>
>> I was wondering if anyone had a custom describe method they would be 
>> willing to share as an example of what Ted Husted describes in his 
>> "Struts Catalog" in the 'Use a "populate" utility to exchange data 
>> with value objects' section?
>>
>> I understand the basic concept of needing to write my own describe() 
>> for those beans that have Date attributes (because the 
>> BeanUtil.populate() can't handle them), but I am not having much luck 
>> getting it to work.
>>
>> If you have some code and are willing to share I would appreciate it. 
>> Also, if you would like to see what I have attempted I can post that 
>> here as well.
>>
>> Thank you in advance,
>>
>> mark n.
>>
>> _
>> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>>
>>
>> -- 
>> To unsubscribe, e-mail: 
>> 
>> For additional commands, e-mail: 
>> 
>>
>>
>
>
>
> -- 
> To unsubscribe, e-mail: 
> 
> For additional commands, e-mail: 
> 
>
>



--
To unsubscribe, e-mail:   
For additional commands, e-mail: 




Re: Custom describe() for use with BeanUtil.populate()

2002-05-20 Thread Adam Hardy

But BeanUtils has been deprecated - in the Javadocs it says "/At some 
point after Struts 1.0 final, will be replaced by an equivalent class in 
the Jakarta Commons Beanutils package./"

I don't know whether this is still true.


Adam

DHS Struts wrote:

> I was wondering if anyone had a custom describe method they would be 
> willing to share as an example of what Ted Husted describes in his 
> "Struts Catalog" in the 'Use a "populate" utility to exchange data 
> with value objects' section?
>
> I understand the basic concept of needing to write my own describe() 
> for those beans that have Date attributes (because the 
> BeanUtil.populate() can't handle them), but I am not having much luck 
> getting it to work.
>
> If you have some code and are willing to share I would appreciate it. 
> Also, if you would like to see what I have attempted I can post that 
> here as well.
>
> Thank you in advance,
>
> mark n.
>
> _
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> -- 
> To unsubscribe, e-mail:   
> 
> For additional commands, e-mail: 
> 
>
>



--
To unsubscribe, e-mail:   
For additional commands, e-mail: