Hi Chetan,
The Wrapper actually shouldn't extend sampleorder, but accept one and
create one as such:
/**
* A wrapper class, used by a special FieldHandler for marshalling and
unmarshalling
* SampleOrder classes properly with use of the type element.
*
*/
public class SampleOrderWrapper {
private String _type = null;
private SampleOrder _sampleOrder = null;
//-- Constructors --/
public SampleOrderWrapper() {
_sampleOrder = new SampleOrder();
}
public SampleOrderWrapper(SampleOrder sampleOrder) {
_sampleOrder = sampleOrder;
}
/**
* Accessor for attribute sparte
*/
public String getSparte() {
return _sampleOrder.getSparte();
}
/**
* Mutator for attribute sparte
*/
public void setSparte(String sparte) {
_sampleOrder.setSparte(sparte);
}
/**
* Returns the type attribute for the sample order
*/
public String getType() {
return _type;
}
/**
* Sets the type attribute for the sample order
*/
public void setType(java.lang.String newType) {
type=newType;
}
/**
* Method used by FieldHandler to obtain the internal SampleOrder
*/
public SampleOrder createSampleOrder() {
return _sampleOrder;
}
}
The FieldHandler will then look as follows (assuming Foo is the class
that contains
the SampleOrder):
package my.package;
import org.exolab.castor.mapping.FieldHandler;
import org.exolab.castor.mapping.ValidityException;
/**
* The FieldHandler for the "sampleorder" field of the Foo class.
**/
public class SampleOrderHandler
implements org.exolab.castor.mapping.FieldHandler
{
/**
* Creates a new SampleOrderHandler for the Foo class
**/
public SampleOrderHandler() {
super();
}
/**
* Returns the value of the field from the object.
*
* @param object The object
* @return The value of the field
* @throws IllegalStateException The Java object has changed and
* is no longer supported by this handler, or the handler is not
* compatiable with the Java object
*/
public Object getValue( Object object )
throws IllegalStateException
{
SampleOrder sampleOrder = ((Foo)object).getSampleOrder();
if (sampleOrder == null) return null;
return new SampleOrderWrapper(sampleOrder);
}
/**
* Sets the value of the field on the object.
*
* @param object The object
* @param value The new value
* @throws IllegalStateException The Java object has changed and
* is no longer supported by this handler, or the handler is not
* compatiable with the Java object
* @thorws IllegalArgumentException The value passed is not of
* a supported type
*/
public void setValue( Object object, Object value )
throws IllegalStateException, IllegalArgumentException
{
SampleOrder sampleOrder =
((SampleOrderWrapper)value).createSampleOrder();
((Foo)object).setSampleOrder(sampleOrder);
}
/**
* Sets the value of the field to a default value.
* <p>
* Reference fields are set to null, primitive fields are set to
* their default value, collection fields are emptied of all
* elements.
*
* @param object The object
* @throws IllegalStateException The Java object has changed and
* is no longer supported by this handler, or the handler is not
* compatiable with the Java object
*/
public void resetValue( Object object )
throws IllegalStateException, IllegalArgumentException
{
((Foo)object).setSampleOrder((SampleOrder)null);
}
/**
* @deprecated No longer supported
*/
public void checkValidity( Object object )
throws ValidityException, IllegalStateException
{
//-- add validation check if desired
}
/**
* Creates a new instance of the object described by this field.
*
* @param parent The object for which the field is created
* @return A new instance of the field's value
* @throws IllegalStateException This field is a simple type and
* cannot be instantiated
*/
public Object newInstance( Object parent )
throws IllegalStateException
{
return new SampleOrderWrapper();
}
} //-- class: SampleOrderHandler
In the mapping file specify the type as SampleOrderWrapper and use the
handler as such:
<field name="sampleOrder" type="SampleOrderWrapper"
handler="SampleOrderHandler"/>
Hope that helps.
--Keith
Chetan Rathi wrote:
>
> Hello Keith,
>
> I would to like to have an sample code for the solution u mentioned
> i.e using a combination of special fieldhandler and the wrapper class.
>
> The following are the sample classes as suggested by you:
>
> The sample order class:
>
> public class sampleorder
> {
> protected java.lang.String sparte;
> public sampleorder() { }
> /** Accessor for attribute sparte */
> public java.lang.String getSparte() {
> return sparte;
> }
> /** Mutator for attribute sparte */
> public void setSparte(java.lang.String newSparte) {
> sparte = newSparte;
>
> }
> }
>
>
>
> the wrapper class:
>
> public class sampleorderWrapper extends sampleorder
> {
> protected java.lang.String sparte;
> protected java.lang.String type;
> public sampleorderWrapper() { }
>
> /** Accessor for attribute sparte */
> public java.lang.String getSparte() {
> return super.sparte;
> }
> /** Mutator for attribute sparte */
> public void setSparte(java.lang.String newSparte) {
> super.sparte = newSparte;
> }
> /** Accessor for attribute sparte */
> public java.lang.String getType() {
> return type;
> }
> /** Mutator for attribute sparte */
> public void setType(java.lang.String newType) {
> type=newType;
> }
>
> }
>
>
>
> I would like to have further inputs from you in terms of the code to
> be put in the fieldhandler class and the relevant changes to be made
> in the mapping file:
>
> Kindly provide me with the same.
>
> Thankyou,
>
> Regds,
>
> Chetan Rathi
>
>
>
>
> >From: "Chetan Rathi"
> >Reply-To: [EMAIL PROTECTED]
> >To: [EMAIL PROTECTED]
> >Subject: Re: [castor-dev] Functionality of create-method attribute
> >Date: Tue, 14 May 2002 15:53:51 +0000
> >
>
> ----------------------------------------------------------------------
> Chat with friends online, try MSN Messenger: Click Here
>
> ---------------------------------------------------------------
>
> Subject: Re: [castor-dev] Functionality of create-method attribute
> Date: Tue, 14 May 2002 15:53:51 +0000
> From: "Chetan Rathi" <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
>
> Hello Keith,
>
> Was working on the concept as suggested by you..
>
> But am not getting it right..
>
> Could you provide with a sample example showing the mapping file,
> calling the fieldHandler class which in turn operates on the
> attributes as mentioned in the wrapper class...
>
> A working sample example would be a great help.
>
> Thanking oyu,
>
> Regds,
>
> Chetan
>
>
>
> Chetan Rathi
> Augsburger Str.336
> 70327 Stuttgart
> Tel no.0711-7861-3148(off.)
> Handy.no.015055564671
>
> >From: Keith Visco
> >Reply-To: [EMAIL PROTECTED]
> >To: [EMAIL PROTECTED]
> >Subject: Re: [castor-dev] Functionality of create-method attribute
> >Date: Wed, 08 May 2002 02:17:04 -0500
> >
> >
> >Chetan,
> >
> >You'll need to use a combination of a Wrapper class as well as a
> special
> >FieldHandler.
> >
> >The Wrapper class will be the main class used by Castor for
> >unmarshalling and marshalling of the AuftragParter.
> >
> >The FieldHandler will "unwrap" the wrapper class during unmarshalling
> >and the "wrap" the AuftragParter during marshalling.
> >
> >So something like:
> >
> >public class AuftragPartnerWrapper {
> >
> > ...
> >
> > public String getName() {
> > return _name;
> > }
> >
> > public String getType() {
> > return _type;
> > }
> >
> > public void setType(String type) {
> > _type = type;
> > }
> > ...
> >
> >}
> >
> >Unpon unmarshalling, your special FieldHandler
> (FieldHandler#setValue)
> >can then use the AuftragPartnerWrapper to create the proper class
> based
> >on the type value, and set this in the Auftragposition class.
> >
> >During Marshalling, the FieldHandler (FieldHandler#getValue) will get
> >the proper class from the Auftragposition class and wrap it inside of
> >AuftragPartnerWrapper and return it to Castor for marshalling.
> >
> >Hope that helps,
> >
> >--Keith
> >
> >Chetan Rathi wrote:
> > >
> > > Hello Keith,
> > >
> > > Kindly respond for this forwarded query....
> > >
> > > Regds,
> > >
> > > Chetan Rathi
> > > >From: "Chetan Rathi"
> > > >Reply-To: [EMAIL PROTECTED]
> > > >To: [EMAIL PROTECTED]
> > > >Subject: [castor-dev] Functionality of create-method attribute
> > > >Date: Tue, 07 May 2002 07:00:47 +0000
> > > >
> > >
> > >
> ----------------------------------------------------------------------
> > > MSN Photos is the easiest way to share and print your photos:
> Click
> > > Here
> > >
> > > Hello Keith,
> > >
> > > There is the requirement of altering the XML document so generated
> in
> > > terms of adding additional attribute for better understanding of
> the
> > > same by the other interfaces interacting with the this XML
> document
> > > example the COI Archival system.
> > >
> > > The following document shows the XML document generated using the
> > > Mapping option representing the underlying java classes:
> > >
> > >
> > >
> > > HU
> > >
> > >
> > >
> > > SCHELLING
> > > JOCHEN
> > >
> > >
> > > SCHELLING
> > > JOCHEN
> > >
> > >
> > >
> > > The above is to be represented as:
> > >
> > >
> > > HU
> > > > > PartnerOID="0000108G100TKKS4O3N0590">
> > > SCHELLING
> > > JOCHEN
> > >
> > > > > type="Organisation" PartnerOID="0000108G100TKKS4O3N0590">
> > > SCHELLING
> > >
> > > JOCHEN
> > >
> > >
> > >
> > > Thus the requirement is adding an additional attribute tpye
> > > which wud in turn tell the marshalling tool to instantiate
> > > the object of the person or Organisation class.
> > >
> > > The Auftragsposition has a one to many relationship with
> > > Partner which are of types Person and Orgnaisation.
> > >
> > > Auftag is bascially an order and Auftragposition is the
> > > order postion for your information.
> > >
> > > So a given auftragpostion can have many partner and the same
> > > is to be represented in the above mentioned form.
> > >
> > > I was probing into the field Handler functionality perhaps
> > > using the create-method attribute...whcih wud allow me to
> > > use my code to be run to generate the above mentioned
> > > output, thus being called through the mapping file so
> > > written.
> > >
> > > Hopefully , i make myself clear.
> > >
> > > Also send me an example showing the usage of the create-method and
> its
> > > relevance to the above mentioned problem.
> > > Can it be done using Castor??
> > > Regds,
> > > Chetan Rathi
> > >
> > >
> ----------------------------------------------------------------------
> > > Join the world�s largest e-mail service with MSN Hotmail. Click
> Here
> > >
> > > ---------------------------------------------------------------
> > >
> > > Subject: [castor-dev] Example for functionality of create-method
> attribute
> > > Date: Fri, 03 May 2002 10:44:17 +0000
> > > From: "Chetan Rathi"
> > > Reply-To: [EMAIL PROTECTED]
> > > To: [EMAIL PROTECTED]
> > >
> > >
> > >
> > > Hello Keith,
> > >
> > > There is the requirement of altering the XML document so generated
> in
> > > terms of adding additional attribute for better understanding of
> the
> > > same by the other interfaces interacting with the this XML
> document
> > > example the COI Archival system.
> > >
> > > The following document shows the XML document generated using the
> > > Mapping option representing the underlying java classes:
> > >
> > >
> > >
> > > HU
> > >
> > > SCHELLING
> > > JOCHEN
> > >
> > >
> > > SCHELLING
> > > JOCHEN
> > >
> > >
> > >
> > > The above is to be represented as:
> > >
> > >
> > > HU
> > > > > PartnerOID="0000108G100TKKS4O3N0590">
> > > SCHELLING
> > > JOCHEN
> > >
> > > > > type="Organisation" PartnerOID="0000108G100TKKS4O3N0590">
> > > SCHELLING
> > >
> > > JOCHEN
> > >
> > >
> > >
> > > Thus the requirement is adding an additional attribute tpye
> > > which wud in turn tell the marshalling tool to instantiate
> > > the object of the person or Organisation class.
> > >
> > > The Auftragsposition has a one to many relationship with
> > > Partner which are of types Person and Orgnaisation.
> > >
> > > Auftag is bascially an order and Auftragposition is the
> > > order postion for your information.
> > >
> > > So a given auftragpostion can have many partner and the same
> > > is to be represented in the above mentioned form.
> > >
> > > I was probing into the field Handler functionality perhaps
> > > using the create-method attribute...whcih wud allow me to
> > > use my code to be run to generate the above mentioned
> > > output, thus being called through the mapping file so
> > > written.
> > >
> > > Hopefully , i make myself clear.
> > >
> > > Also send me an example showing the usage of the create-method and
> its
> > > relevance to the above mentioned problem.
> > > Can it be done using Castor??
> > > Regds,
> > > Chetan Rathi
> > >
> > >
> > >
> ----------------------------------------------------------------------
> > > Get your FREE download of MSN Explorer at http://explorer.msn.com.
> > > ----------------------------------------------------------- If you
> > > wish to unsubscribe from this mailing, send mail to
> > > [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
> >
> >-----------------------------------------------------------
> >If you wish to unsubscribe from this mailing, send mail to
> >[EMAIL PROTECTED] with a subject of:
> > unsubscribe castor-dev
>
> ----------------------------------------------------------------------
> Chat with friends online, try MSN Messenger: Click Here
> ----------------------------------------------------------- If you
> wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev