Skip to content

Instantly share code, notes, and snippets.

@arturozv
Created June 11, 2015 07:48
Show Gist options
  • Save arturozv/f9084111bb3365dc0f5d to your computer and use it in GitHub Desktop.
Save arturozv/f9084111bb3365dc0f5d to your computer and use it in GitHub Desktop.
Spring data mongodb custom auditing with AbstractMongoEventListener
import org.joda.time.Instant;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
public class MongoSynchronizedListener extends AbstractMongoEventListener<Object> {
/**
* Called in MongoTemplate insert, insertList and save operations before the
* object is converted to a DBObject using a MongoConveter.
*/
@Override
public void onBeforeConvert(Object obj) {
if (MongoSynchronized.class.isAssignableFrom(obj.getClass())) {
MongoSynchronized csys = (MongoSynchronized) obj;
csys.setRevision(csys.getRevision() != null ? csys.getRevision() + 1l : 0l);
csys.setModified(Instant.now().getMillis());
csys.setDeleted(null);
if (csys.getBirth() == null) {
csys.setBirth(Instant.now().getMillis());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment