isis-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Haywood <...@haywood-associates.co.uk>
Subject Re: Automatic created-by and modified-by property updates
Date Wed, 16 Sep 2015 06:02:49 GMT
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.62@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
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message