You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Glen Stampoultzis <gs...@iinet.net.au> on 2003/11/05 10:48:23 UTC

Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for answers.]

Any chance of seeing some sample code snippets?

Regards,

Glen

At 08:18 PM 5/11/2003, you wrote:
>Hi,
>
>This is *exactly* the way I've been handling my Hibernate objects, and
>it works wonderfully.
>
>Just my 2c.
>
>   - John


Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/

RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a questforanswers.]

Posted by John Meredith <ps...@t-online.de>.
Thanks for the explanation Tsvetelin, cleared things up :-)

Rgds,

  - John

On Wed, 2003-11-05 at 16:55, tsvetelin wrote:
> Hi John,
> 
> You don't need to encode the class name in your JDO squeezer because this is
> handled by JDO implementations.
> Here are 2 cases that are possible.
> 
> The first one is when the JDO implementation define your JDO identity class.
> In this case the JDO implementation use its identity class. This class
> contains the primary keys for your object (in the most cases it is a int or
> long value) and the class name of corresponded data object. When you take
> the string presentation of the identity instance it encode class name. So
> the JDO implementation know persistence class name for each instance of
> identity object. For example I am using the Kodo JDO and their identity
> class is: com.solarmetric.kodo.util.ObjectIds$Id. It contains 2 properties.
> long and persistence class
> 
> The second case is when you define the identity class for particular
> persistent class in package.jdo file. For more information please see the
> JDO metadata(<!ATTLIST class identity-type (application|datastore|none)
> 'datastore'> and <!ATTLIST class objectid-class CDATA #IMPLIED> ). In this
> case the JDO implementation know which is associated persistent class for
> particular identity object.
> 
> 
> I hope that this will be useful.
> 
> Tsvetelin
> 
> -----Original Message-----
> From: John Meredith [mailto:psynix@t-online.de]
> Sent: Wednesday, November 05, 2003 4:48 PM
> To: Tapestry users
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a
> questforanswers.]
> 
> 
> Possibly a stupid question (and somewhat OT), do you have an Adaptor for
> each JDO and if not, how do you get from the oid back to the object
> without distinguishing between your ModelObjects? Or does JDO encode
> this into the oid?
> 
> I generally use integer sequences for my iod (provided by the DB), which
> is why I've had to encode the classname as well.
> 
> Just interested in how other people are doing this :-)
> 
>   - John
> 
> On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> > Yeah that's pretty much how the JDO squeezer is written.  My only change
> is
> > that my persitance capable classes all implement a common interface
> (always
> > found this usefull) defining a getId() method which I use in the Squeezer.
> >
> >     /**
> >      *  Squeezes the {@link is.symbian.framework.model.ModelObject} data
> to
> > it's oid string
> >      */
> >     public String squeeze(DataSqueezer squeezer, Object data) {
> >         is.symbian.framework.model.ModelObject jdoObject =
> > (is.symbian.framework.model.ModelObject) data;
> >         return PREFIX + jdoObject.getObjectId().toString();
> >     }
> >
> >     /**
> >      * Un-squeezes a jdo object using it's oid string to locate it.
> >      */
> >     public Object unsqueeze(DataSqueezer squeezer, String string) {
> >         PersistenceManager pm = JDOSupport.getPM();
> >         if (string.length() == PREFIX.length()) return "";   // Probably a
> > problem
> >         Object o = pm.newObjectIdInstance(null,
> > string.substring(PREFIX.length()));
> >         return pm.getObjectById(o, true);
> >     }
> >
> > Cheers
> > Ryan Moffat
> >
> > ----- Original Message -----
> > From: "John Meredith" <ps...@t-online.de>
> > To: "Tapestry users" <ta...@jakarta.apache.org>
> > Sent: Wednesday, November 05, 2003 10:29 AM
> > Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> > foranswers.]
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> --
> John Meredith <ps...@t-online.de>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
John Meredith <ps...@t-online.de>

RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a questforanswers.]

Posted by tsvetelin <ts...@rushmore-digital.com>.
Hi John,

You don't need to encode the class name in your JDO squeezer because this is
handled by JDO implementations.
Here are 2 cases that are possible.

The first one is when the JDO implementation define your JDO identity class.
In this case the JDO implementation use its identity class. This class
contains the primary keys for your object (in the most cases it is a int or
long value) and the class name of corresponded data object. When you take
the string presentation of the identity instance it encode class name. So
the JDO implementation know persistence class name for each instance of
identity object. For example I am using the Kodo JDO and their identity
class is: com.solarmetric.kodo.util.ObjectIds$Id. It contains 2 properties.
long and persistence class

The second case is when you define the identity class for particular
persistent class in package.jdo file. For more information please see the
JDO metadata(<!ATTLIST class identity-type (application|datastore|none)
'datastore'> and <!ATTLIST class objectid-class CDATA #IMPLIED> ). In this
case the JDO implementation know which is associated persistent class for
particular identity object.


I hope that this will be useful.

Tsvetelin

-----Original Message-----
From: John Meredith [mailto:psynix@t-online.de]
Sent: Wednesday, November 05, 2003 4:48 PM
To: Tapestry users
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a
questforanswers.]


Possibly a stupid question (and somewhat OT), do you have an Adaptor for
each JDO and if not, how do you get from the oid back to the object
without distinguishing between your ModelObjects? Or does JDO encode
this into the oid?

I generally use integer sequences for my iod (provided by the DB), which
is why I've had to encode the classname as well.

Just interested in how other people are doing this :-)

  - John

On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> Yeah that's pretty much how the JDO squeezer is written.  My only change
is
> that my persitance capable classes all implement a common interface
(always
> found this usefull) defining a getId() method which I use in the Squeezer.
>
>     /**
>      *  Squeezes the {@link is.symbian.framework.model.ModelObject} data
to
> it's oid string
>      */
>     public String squeeze(DataSqueezer squeezer, Object data) {
>         is.symbian.framework.model.ModelObject jdoObject =
> (is.symbian.framework.model.ModelObject) data;
>         return PREFIX + jdoObject.getObjectId().toString();
>     }
>
>     /**
>      * Un-squeezes a jdo object using it's oid string to locate it.
>      */
>     public Object unsqueeze(DataSqueezer squeezer, String string) {
>         PersistenceManager pm = JDOSupport.getPM();
>         if (string.length() == PREFIX.length()) return "";   // Probably a
> problem
>         Object o = pm.newObjectIdInstance(null,
> string.substring(PREFIX.length()));
>         return pm.getObjectById(o, true);
>     }
>
> Cheers
> Ryan Moffat
>
> ----- Original Message -----
> From: "John Meredith" <ps...@t-online.de>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, November 05, 2003 10:29 AM
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> foranswers.]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
--
John Meredith <ps...@t-online.de>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest foranswers.]

Posted by John Meredith <ps...@t-online.de>.
Possibly a stupid question (and somewhat OT), do you have an Adaptor for
each JDO and if not, how do you get from the oid back to the object
without distinguishing between your ModelObjects? Or does JDO encode
this into the oid?

I generally use integer sequences for my iod (provided by the DB), which
is why I've had to encode the classname as well.

Just interested in how other people are doing this :-)

  - John

On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> Yeah that's pretty much how the JDO squeezer is written.  My only change is
> that my persitance capable classes all implement a common interface (always
> found this usefull) defining a getId() method which I use in the Squeezer.
> 
>     /**
>      *  Squeezes the {@link is.symbian.framework.model.ModelObject} data to
> it's oid string
>      */
>     public String squeeze(DataSqueezer squeezer, Object data) {
>         is.symbian.framework.model.ModelObject jdoObject =
> (is.symbian.framework.model.ModelObject) data;
>         return PREFIX + jdoObject.getObjectId().toString();
>     }
> 
>     /**
>      * Un-squeezes a jdo object using it's oid string to locate it.
>      */
>     public Object unsqueeze(DataSqueezer squeezer, String string) {
>         PersistenceManager pm = JDOSupport.getPM();
>         if (string.length() == PREFIX.length()) return "";   // Probably a
> problem
>         Object o = pm.newObjectIdInstance(null,
> string.substring(PREFIX.length()));
>         return pm.getObjectById(o, true);
>     }
> 
> Cheers
> Ryan Moffat
> 
> ----- Original Message -----
> From: "John Meredith" <ps...@t-online.de>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, November 05, 2003 10:29 AM
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> foranswers.]
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
-- 
John Meredith <ps...@t-online.de>

Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest foranswers.]

Posted by Ryan Moffat <ry...@hotmail.com>.
Yeah that's pretty much how the JDO squeezer is written.  My only change is
that my persitance capable classes all implement a common interface (always
found this usefull) defining a getId() method which I use in the Squeezer.

    /**
     *  Squeezes the {@link is.symbian.framework.model.ModelObject} data to
it's oid string
     */
    public String squeeze(DataSqueezer squeezer, Object data) {
        is.symbian.framework.model.ModelObject jdoObject =
(is.symbian.framework.model.ModelObject) data;
        return PREFIX + jdoObject.getObjectId().toString();
    }

    /**
     * Un-squeezes a jdo object using it's oid string to locate it.
     */
    public Object unsqueeze(DataSqueezer squeezer, String string) {
        PersistenceManager pm = JDOSupport.getPM();
        if (string.length() == PREFIX.length()) return "";   // Probably a
problem
        Object o = pm.newObjectIdInstance(null,
string.substring(PREFIX.length()));
        return pm.getObjectById(o, true);
    }

Cheers
Ryan Moffat

----- Original Message -----
From: "John Meredith" <ps...@t-online.de>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Wednesday, November 05, 2003 10:29 AM
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
foranswers.]


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for answers.]

Posted by James Iry <ja...@hotmail.com>.
Put the optimistic lock "version" or "timestamp" into a hidden field on your
form after the squeezed object field.  On form submit, the object is
reloaded because of the squeeze/unsqueeze protocol.  Then the
version/timestamp is written into the object.  When you actually try to
save, your persistence tool should check the version/timestamp and throw an
optimistic lock exception if the version/timestamp submitted from the web
doesn't match the one in the database.  Just be sure to catch and handle
that exception in your form submission code.

-----Original Message-----
From: Glen Stampoultzis [mailto:gstamp@iinet.net.au] 
Sent: Sunday, November 09, 2003 3:30 AM
To: Tapestry users
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for
answers.]


Sorry for the rather late reply.

Just a couple of questions:

1. It seems as though this will not work for composite keys or keys that 
are not ints.  Is that right?
2. If you reload the object don't you loose optimistic locking?

To explain what I mean with point 2 an example:

1. Web page loads hibernate object with up-to-date data from the database 
and displays the results to the screen.
2. External changes to the record representing that object occur. 3. User
edits fields and submits form. 4. Hibernate object is squeezed and
unsqueezed causing a reload of the 
object from the db.
5. Tapestry writes the modified fields into the object.

Now because the object is being reloaded (in 4) doesn't that mean hibernate 
can't detect that there's been a modification to the record?

I could easily be wrong as I'm new to hibernate and tapestry.

Regards,

Glen


At 09:29 PM 5/11/2003, you wrote:
>Sure (see below).
>
>I played around with the classname, because I couldn't think of a 
>better way of distinguishing the different Hibernate objects. I'd love 
>to know if there's a more appropriate way to to that.
>
>To tell Tapestry about your shiny new Adaptor, you'll also need to 
>override createDataSqueezer() in your Engine, something like:
>
>public DataSqueezer createDataSqueezer() {
>         return new DataSqueezer(getResourceResolver(),
>                 new ISqueezeAdaptor[] {new HibernateObjectAdaptor()}); 
>}
>
>HibernateObject is just an interface defining getId() method and is 
>implemented by all my Hibernate objects.
>
>Not sure how whether this will come out wrapped and looking shocking, 
>but I hope this helps. Suggestions/Improvements welcome.
>
>   - John
>
>----------------------------------
>import java.util.StringTokenizer;
>
>import net.sf.hibernate.HibernateException;
>import net.sf.hibernate.Session;
>
>import org.apache.commons.logging.Log;
>import org.apache.commons.logging.LogFactory;
>import org.apache.tapestry.ApplicationRuntimeException;
>import org.apache.tapestry.util.io.DataSqueezer;
>import org.apache.tapestry.util.io.ISqueezeAdaptor;
>
>import de.tuerkas.resin.Persistence; // Open session in view import 
>de.tuerkas.tmarket.model.HibernateObject;
>
>/**
>  *  Squeezes a HibernateObject
>  *
>  *  @author John Meredith
>  *  @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03 
>18:06:56 johnm Exp $
>  *
>  **/
>public class HibernateObjectAdaptor implements ISqueezeAdaptor {
>         private static Log log = 
>LogFactory.getLog(HibernateObjectAdaptor.class);
>
>         // TODO: Investigate why PREFIX must be one character.
>         private static final String PREFIX = "H";
>
>         private static final String BASE_PACKAGE =
> "de.tuerkas.tmarket.model";
>
>         public void register(DataSqueezer squeezer) {
>                 squeezer.register(PREFIX, HibernateObject.class, this);
>         }
>
>         public String squeeze(DataSqueezer squeezer, Object data) {
>                 HibernateObject ho = (HibernateObject) data;
>
>                 String fullClassname = ho.getClass().getName();
>                 int dotx = fullClassname.indexOf(".",
BASE_PACKAGE.length());
>                 String className = fullClassname.substring(dotx + 1);
>
>                 String result = PREFIX + "|" + className + "|" + 
>ho.getId().toString();
>
>                 if (log.isDebugEnabled())
>                         log.debug("Squeezed: " + result);
>
>                 return result;
>         }
>
>         public Object unsqueeze(DataSqueezer squeezer, String string) {
>                 if (log.isDebugEnabled())
>                         log.debug("Unsqueezing: " + string);
>
>                 StringTokenizer st = new StringTokenizer(string, "|");
>                 st.nextElement();
>                 String objClassName = BASE_PACKAGE + "." + (String)
> st.nextElement();
>                 Long objId = new Long((String) st.nextElement());
>
>                 Session sess = null;
>                 HibernateObject result = null;
>                 try {
>                         sess = Persistence.getSession();
>                         result = (HibernateObject)
> sess.load(Class.forName(objClassName),
>objId);
>                 } catch (ClassNotFoundException cnfe) {
>                         log.error("Cannot find class '" + objClassName + 
> "' for id value " +
>objId);
>                         throw new ApplicationRuntimeException("Unable to 
> find class", cnfe);
>                 } catch (HibernateException he) {
>                         // TODO: Could be handled better
>                         throw new ApplicationRuntimeException("Problems 
> with database
>access", he);
>                 }
>
>                 return result;
>         }
>}
>
>On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> > Any chance of seeing some sample code snippets?
> >
> > Regards,
> >
> > Glen
> >
> > At 08:18 PM 5/11/2003, you wrote:
> > >Hi,
> > >
> > >This is *exactly* the way I've been handling my Hibernate objects, 
> > >and it works wonderfully.
> > >
> > >Just my 2c.
> > >
> > >   - John
> >
> >
> > Glen Stampoultzis
> > gstamp@iinet.net.au http://members.iinet.net.au/~gstamp/glen/
>--
>John Meredith <ps...@t-online.de>


Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for answers.]

Posted by Glen Stampoultzis <gs...@iinet.net.au>.
Sorry for the rather late reply.

Just a couple of questions:

1. It seems as though this will not work for composite keys or keys that 
are not ints.  Is that right?
2. If you reload the object don't you loose optimistic locking?

To explain what I mean with point 2 an example:

1. Web page loads hibernate object with up-to-date data from the database 
and displays the results to the screen.
2. External changes to the record representing that object occur.
3. User edits fields and submits form.
4. Hibernate object is squeezed and unsqueezed causing a reload of the 
object from the db.
5. Tapestry writes the modified fields into the object.

Now because the object is being reloaded (in 4) doesn't that mean hibernate 
can't detect that there's been a modification to the record?

I could easily be wrong as I'm new to hibernate and tapestry.

Regards,

Glen


At 09:29 PM 5/11/2003, you wrote:
>Sure (see below).
>
>I played around with the classname, because I couldn't think of a better
>way of distinguishing the different Hibernate objects. I'd love to know
>if there's a more appropriate way to to that.
>
>To tell Tapestry about your shiny new Adaptor, you'll also need to
>override createDataSqueezer() in your Engine, something like:
>
>public DataSqueezer createDataSqueezer() {
>         return new DataSqueezer(getResourceResolver(),
>                 new ISqueezeAdaptor[] {new HibernateObjectAdaptor()});
>}
>
>HibernateObject is just an interface defining getId() method and is
>implemented by all my Hibernate objects.
>
>Not sure how whether this will come out wrapped and looking shocking,
>but I hope this helps. Suggestions/Improvements welcome.
>
>   - John
>
>----------------------------------
>import java.util.StringTokenizer;
>
>import net.sf.hibernate.HibernateException;
>import net.sf.hibernate.Session;
>
>import org.apache.commons.logging.Log;
>import org.apache.commons.logging.LogFactory;
>import org.apache.tapestry.ApplicationRuntimeException;
>import org.apache.tapestry.util.io.DataSqueezer;
>import org.apache.tapestry.util.io.ISqueezeAdaptor;
>
>import de.tuerkas.resin.Persistence; // Open session in view
>import de.tuerkas.tmarket.model.HibernateObject;
>
>/**
>  *  Squeezes a HibernateObject
>  *
>  *  @author John Meredith
>  *  @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03 18:06:56
>johnm Exp $
>  *
>  **/
>public class HibernateObjectAdaptor implements ISqueezeAdaptor {
>         private static Log log =
>LogFactory.getLog(HibernateObjectAdaptor.class);
>
>         // TODO: Investigate why PREFIX must be one character.
>         private static final String PREFIX = "H";
>
>         private static final String BASE_PACKAGE = 
> "de.tuerkas.tmarket.model";
>
>         public void register(DataSqueezer squeezer) {
>                 squeezer.register(PREFIX, HibernateObject.class, this);
>         }
>
>         public String squeeze(DataSqueezer squeezer, Object data) {
>                 HibernateObject ho = (HibernateObject) data;
>
>                 String fullClassname = ho.getClass().getName();
>                 int dotx = fullClassname.indexOf(".", BASE_PACKAGE.length());
>                 String className = fullClassname.substring(dotx + 1);
>
>                 String result = PREFIX + "|" + className + "|" +
>ho.getId().toString();
>
>                 if (log.isDebugEnabled())
>                         log.debug("Squeezed: " + result);
>
>                 return result;
>         }
>
>         public Object unsqueeze(DataSqueezer squeezer, String string) {
>                 if (log.isDebugEnabled())
>                         log.debug("Unsqueezing: " + string);
>
>                 StringTokenizer st = new StringTokenizer(string, "|");
>                 st.nextElement();
>                 String objClassName = BASE_PACKAGE + "." + (String) 
> st.nextElement();
>                 Long objId = new Long((String) st.nextElement());
>
>                 Session sess = null;
>                 HibernateObject result = null;
>                 try {
>                         sess = Persistence.getSession();
>                         result = (HibernateObject) 
> sess.load(Class.forName(objClassName),
>objId);
>                 } catch (ClassNotFoundException cnfe) {
>                         log.error("Cannot find class '" + objClassName + 
> "' for id value " +
>objId);
>                         throw new ApplicationRuntimeException("Unable to 
> find class", cnfe);
>                 } catch (HibernateException he) {
>                         // TODO: Could be handled better
>                         throw new ApplicationRuntimeException("Problems 
> with database
>access", he);
>                 }
>
>                 return result;
>         }
>}
>
>On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> > Any chance of seeing some sample code snippets?
> >
> > Regards,
> >
> > Glen
> >
> > At 08:18 PM 5/11/2003, you wrote:
> > >Hi,
> > >
> > >This is *exactly* the way I've been handling my Hibernate objects, and
> > >it works wonderfully.
> > >
> > >Just my 2c.
> > >
> > >   - John
> >
> >
> > Glen Stampoultzis
> > gstamp@iinet.net.au
> > http://members.iinet.net.au/~gstamp/glen/
>--
>John Meredith <ps...@t-online.de>


Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/

Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for answers.]

Posted by John Meredith <ps...@t-online.de>.
Sure (see below).

I played around with the classname, because I couldn't think of a better
way of distinguishing the different Hibernate objects. I'd love to know
if there's a more appropriate way to to that.

To tell Tapestry about your shiny new Adaptor, you'll also need to
override createDataSqueezer() in your Engine, something like:

public DataSqueezer createDataSqueezer() {
	return new DataSqueezer(getResourceResolver(),
		new ISqueezeAdaptor[] {new HibernateObjectAdaptor()});
}

HibernateObject is just an interface defining getId() method and is
implemented by all my Hibernate objects.

Not sure how whether this will come out wrapped and looking shocking,
but I hope this helps. Suggestions/Improvements welcome.

  - John

----------------------------------
import java.util.StringTokenizer;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.util.io.DataSqueezer;
import org.apache.tapestry.util.io.ISqueezeAdaptor;

import de.tuerkas.resin.Persistence; // Open session in view
import de.tuerkas.tmarket.model.HibernateObject;

/**
 *  Squeezes a HibernateObject 
 *
 *  @author John Meredith
 *  @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03 18:06:56
johnm Exp $
 *
 **/
public class HibernateObjectAdaptor implements ISqueezeAdaptor {
	private static Log log =
LogFactory.getLog(HibernateObjectAdaptor.class);

	// TODO: Investigate why PREFIX must be one character. 
	private static final String PREFIX = "H";
	
	private static final String BASE_PACKAGE = "de.tuerkas.tmarket.model";

	public void register(DataSqueezer squeezer) {
		squeezer.register(PREFIX, HibernateObject.class, this);
	}

	public String squeeze(DataSqueezer squeezer, Object data) {
		HibernateObject ho = (HibernateObject) data;

		String fullClassname = ho.getClass().getName();
		int dotx = fullClassname.indexOf(".", BASE_PACKAGE.length());
		String className = fullClassname.substring(dotx + 1); 

		String result = PREFIX + "|" + className + "|" +
ho.getId().toString();
		
		if (log.isDebugEnabled())
			log.debug("Squeezed: " + result);
		
		return result;
	}

	public Object unsqueeze(DataSqueezer squeezer, String string) {
		if (log.isDebugEnabled())
			log.debug("Unsqueezing: " + string);

		StringTokenizer st = new StringTokenizer(string, "|");
		st.nextElement();
		String objClassName = BASE_PACKAGE + "." + (String) st.nextElement();
		Long objId = new Long((String) st.nextElement());
		
		Session sess = null;
		HibernateObject result = null;
		try {
			sess = Persistence.getSession();
			result = (HibernateObject) sess.load(Class.forName(objClassName),
objId);
		} catch (ClassNotFoundException cnfe) {
			log.error("Cannot find class '" + objClassName + "' for id value " +
objId);
			throw new ApplicationRuntimeException("Unable to find class", cnfe);
		} catch (HibernateException he) {
			// TODO: Could be handled better
			throw new ApplicationRuntimeException("Problems with database
access", he);
		}
		
		return result;
	}
}

On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> Any chance of seeing some sample code snippets?
> 
> Regards,
> 
> Glen
> 
> At 08:18 PM 5/11/2003, you wrote:
> >Hi,
> >
> >This is *exactly* the way I've been handling my Hibernate objects, and
> >it works wonderfully.
> >
> >Just my 2c.
> >
> >   - John
> 
> 
> Glen Stampoultzis
> gstamp@iinet.net.au
> http://members.iinet.net.au/~gstamp/glen/
-- 
John Meredith <ps...@t-online.de>