Java Code Examples for org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent#getSource()

The following examples show how to use org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent#getSource() . 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: MongoEventListener.java    From microservice-recruit with Apache License 2.0 7 votes vote down vote up
@Override
public void onBeforeConvert(BeforeConvertEvent<Object> event) {
    final Object source = event.getSource();
    ReflectionUtils.doWithFields(source.getClass(), field -> {
        ReflectionUtils.makeAccessible(field);
        // 如果字段添加了我们自定义的AutoValue注解
        if (field.isAnnotationPresent(AutoValue.class) && field.get(source) instanceof Number
                && field.getLong(source) == 0) {
            // 判断注解的字段是否为number类型且值是否等于0.如果大于0说明有ID不需要生成ID
            // 设置自增ID
            field.set(source, IdUtil.getNextIdAndUpdate(source.getClass().getSimpleName(), mongo));
            log.debug("Collections ID IS=======================" + source);
        }
        if (field.isAnnotationPresent(CreateTime.class) && field.get(source) == null) {
            field.set(source, LocalDateTime.now());
        }
        if (field.isAnnotationPresent(UpdateTime.class)) {
            field.set(source, LocalDateTime.now());

        }
    });
}
 
Example 2
Source File: MongoEventListener.java    From microservice-recruit with Apache License 2.0 6 votes vote down vote up
@Override
public void onBeforeConvert(BeforeConvertEvent<Object> event) {
    final Object source = event.getSource();
    ReflectionUtils.doWithFields(source.getClass(), field -> {
        ReflectionUtils.makeAccessible(field);
        // 如果字段添加了我们自定义的AutoValue注解
        if (field.isAnnotationPresent(AutoValue.class) && field.get(source) instanceof Number
                && field.getLong(source) == 0) {
            // 判断注解的字段是否为number类型且值是否等于0.如果大于0说明有ID不需要生成ID
            // 设置自增ID
            field.set(source, IdUtil.getNextIdAndUpdate(source.getClass().getSimpleName(), mongo));
            log.debug("Collections ID IS=======================" + source);
        }
        if (field.isAnnotationPresent(CreateTime.class) && field.get(source) == null) {
            field.set(source, LocalDateTime.now());
        }
        if (field.isAnnotationPresent(UpdateTime.class)) {
            field.set(source, LocalDateTime.now());

        }
    });
}
 
Example 3
Source File: DateCreatorMongoEventListener.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Override
public void onBeforeConvert(BeforeConvertEvent<AbstractDocument> event) {
    super.onBeforeConvert(event);

    AbstractDocument document = event.getSource();

    LocalDateTime d = LocalDateTime.now();
    // set created date if necessary
    if (document.getCreatedDate() == null) {
        document.setCreatedDate(d);
    }

    // update last updated date
    document.setLastUpdate(d);
}
 
Example 4
Source File: UserCascadeSaveMongoEventListener.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void onBeforeConvert(final BeforeConvertEvent<Object> event) {
    final Object source = event.getSource();
    if ((source instanceof User) && (((User) source).getEmailAddress() != null)) {
        mongoOperations.save(((User) source).getEmailAddress());
    }
}
 
Example 5
Source File: CascadeSaveMongoEventListener.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onBeforeConvert(final BeforeConvertEvent<Object> event) {
    final Object source = event.getSource();
    ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
}