Created
June 11, 2015 07:48
-
-
Save arturozv/f9084111bb3365dc0f5d to your computer and use it in GitHub Desktop.
Spring data mongodb custom auditing with AbstractMongoEventListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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