Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package nb;
import java.time.LocalDateTime;
import org.apache.cayenne.BaseDataObject;
import org.apache.cayenne.annotation.PostAdd;
import org.apache.cayenne.annotation.PreUpdate;
import jambalaya.interfaces.DateTimeStampedCreation;
import jambalaya.interfaces.DateTimeStampedModification;
/**
* Looks at all DataObjects before they are committed to the DB. If they implement "DateTimestamped",
* the creationDate() and modificationDate() attributes will be updated accordingly.
*/
public class DateTimestampedListener {
@PostAdd( { BaseDataObject.class } )
public void handleAdd( BaseDataObject object ) {
if( object instanceof DateTimeStampedCreation ) {
((DateTimeStampedCreation)object).setCreationDate( LocalDateTime.now() );
}
if( object instanceof DateTimeStampedModification ) {
((DateTimeStampedModification)object).setModificationDate( LocalDateTime.now() );
}
}
@PreUpdate( { BaseDataObject.class } )
public void handleUpdate( BaseDataObject object ) {
if( object instanceof DateTimeStampedModification ) {
((DateTimeStampedModification)object).setModificationDate( LocalDateTime.now() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment