Just to close off this thread... in 1.10.0-SNAPSHOT there is built-in
support for this feature... just implement Timetstampable [1]

Cheers
Dan

[1] http://isis.apache.org/guides/rg.html#_rg_classes_roles

On 28 September 2015 at 04:01, Stephen Cameron <steve.cameron...@gmail.com>
wrote:

> Hi Dan,
>
> I tried this and its not correct, I get the open and close methods being
> called over and over whenever I open and close an object in the UI. but the
> jdo listener method preStore (InstanceLifecycleEvent event) never gets
> called.
>
> I pictured open()  and close() being called just once as the DOMAIN service
> singleton is created by Isis and then it listens on the JDO events as each
> entity goes through its lifecycle.
>
>  I will put this aside as its not the main priority. I'll read up and
> understand the jdo events to find an answer, this must be close to correct.
>
>
> On Wed, Sep 16, 2015 at 4:02 PM, Dan Haywood <d...@haywood-associates.co.uk
> >
> wrote:
>
> > Hi Steve,
> >
> > Although there isn't any direct support for this, it's should be
> relatively
> > easy to do by using the underlying JDO API.
> >
> > As a quick code sketch:
> >
> > public interface CreateTrackingEntity {
> >     void setCreatedBy(String createdBy);
> >     void setCreatedOn(DateTime createdOn);
> > }
> >
> > public interface ModifyTrackingEntity {
> >     void setModifiedBy(String username);
> >     void setModifiedOn(DateTime modifiedOn);
> > }
> >
> >
> > Your entity should implement one or both of the above.
> >
> > Then, define a service such as:
> >
> > @RequestScoped
> > @DomainService(nature=NatureOfService.DOMAIN)
> > public class UpdateableEntityServices implements
> > javax.jdo.listener.StoreLifecycleListener {
> >
> >     @PostConstruct
> >     public void open() {
> >
> >
> >
> isisJdoSupport.getJdoPersistenceManager().addInstanceLifecycleListener(this);
> >     }
> >
> >     @PreDestroy
> >     public void close() {
> >
> >
> >
> isisJdoSupport.getJdoPersistenceManager().removeInstanceLifecycleListener(this);
> >     }
> >
> >     @Programmatic
> >     public void preStore (InstanceLifecycleEvent event) {
> >
> >         final Object pi = event.getPersistentInstance();
> >
> >         if(pi instanceof org.datanucleus.enhancement.Persistable) {
> >             boolean isPersistent =
> > ((org.datanucleus.enhancement.Persistable)pi).dnIsPersistent();
> >
> >             if(!isPersistent) {
> >                 if(pi instanceof CreateTrackingEntity) {
> >
> >  ((CreateTrackingEntity)pi).setCreatedBy(container.getUserName());
> >
> >  ((CreateTrackingEntity)pi).setCreatedOn(clockService.nowAsDateTime());
> >                 }
> >             } else {
> >                 if(pi instanceof ModifyTrackingEntity) {
> >
> >  ((ModifyTrackingEntity)pi).setModifiedBy(container.getUserName());
> >
> >  ((ModifyTrackingEntity)pi).setModifedOn(clockService.nowAsDateTime());
> >                 }
> >             }
> >         }
> >     }
> >
> >     @Programmatic
> >     public void postStore (InstanceLifecycleEvent event) {
> >         // no-op
> >     }
> >
> >     @Inject
> >     private DomainObjectContainer container;
> >
> >     @Inject
> >     private ClockService clockService;
> >
> >     @Inject
> >     private IsisJdoSupport isisJdoSupport;
> > }
> >
> >
> >
> > ~~~~~~~~~~~~
> > There is actually a ticket in JIRA for this [1], so I'll formalize this
> as
> > a service in Isis 1.10.0.
> >
> > HTH
> > Dan
> >
> > [1] https://issues.apache.org/jira/browse/ISIS-867
> >
> >
> > On 16 September 2015 at 05:18, Stephen Cameron <
> steve.cameron...@gmail.com
> > >
> > wrote:
> >
> > > Hi,
> > >
> > > Could someone please assist me in adding this capability, to automate
> the
> > > creation and update of values in these standard fields
> > >
> > > created_by
> > > created_on
> > > modified_by
> > > modified_on
> > >
> > > That is I need to set the first two on creating a new object, and the
> > last
> > > two on modifying an object.
> > >
> > > Thanks
> > > Steve Cameron
> > >
> >
>

Reply via email to