org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent Java Examples

The following examples show how to use org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateIdListener.java    From moserp with Apache License 2.0 7 votes vote down vote up
@Override
public void onApplicationEvent(MongoMappingEvent<?> event) {
    if (!(event instanceof BeforeSaveEvent)) {
        return;
    }
    if (!IdentifiableEntity.class.isAssignableFrom(event.getSource().getClass())) {
        return;
    }
    IdentifiableEntity source = (IdentifiableEntity) event.getSource();
    if (source.getId() != null) {
        return;
    }
    DBObject dbObject = event.getDBObject();
    String id = sequenceService.getNextIt(event.getSource().getClass());
    source.setId(id);
    dbObject.put("_id", id);
}
 
Example #2
Source File: BeforeSaveListener.java    From microservices-event-sourcing with Apache License 2.0 6 votes vote down vote up
@Override
public void onBeforeSave(BeforeSaveEvent<BaseEntity> event) {
    Date date = Date.from(Instant.now());
    if(event.getSource().getCreatedAt() == null)
        event.getSource().setCreatedAt(date);
    event.getSource().setLastModified(date);
    super.onBeforeSave(event);
}
 
Example #3
Source File: CachedEncryptionEventListener.java    From spring-data-mongodb-encrypt with Apache License 2.0 5 votes vote down vote up
@Override
public void onBeforeSave(BeforeSaveEvent event) {
    Document document = event.getDocument();

    Node node = node(event.getSource().getClass());
    if (node == Node.EMPTY) return;

    try {
        cryptFields(document, node, new Encoder());
    } catch (Exception e) {
        Object id = document.get("_id");
        throw new DocumentCryptException(event.getCollectionName(), id, e);
    }
}
 
Example #4
Source File: ReflectionEncryptionEventListener.java    From spring-data-mongodb-encrypt with Apache License 2.0 5 votes vote down vote up
@Override
public void onBeforeSave(BeforeSaveEvent event) {
    Document document = event.getDocument();
    try {
        cryptFields(document, event.getSource().getClass(), new Encoder());
    } catch (Exception e) {
        Object id = document.get("_id");
        throw new DocumentCryptException(event.getCollectionName(), id, e);
    }
}
 
Example #5
Source File: BeforeSaveListener.java    From spring-cloud-event-sourcing-example with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBeforeSave(BeforeSaveEvent<BaseEntity> event) {

    Date timestamp = new Date();

    // Add a timestamp to the created date if it does not yet exist
    if (event.getSource().getCreatedAt() == null)
        event.getSource().setCreatedAt(timestamp);

    // Update the timestamp to the current time
    event.getSource().setLastModified(timestamp);

    super.onBeforeSave(event);
}