All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cybermkd.mongo.validation.ExistValidator Maven / Gradle / Ivy

The newest version!
package com.cybermkd.mongo.validation;

import com.cybermkd.mongo.constraints.Exist;
import com.cybermkd.mongo.kit.MongoQuery;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 创建人:T-baby
 * 创建日期: 16/8/3
 * 文件描述:
 */
public class ExistValidator implements ConstraintValidator {

    private boolean value = false;

    private String collectionName = "";

    private String key = "";

    @Override
    public void initialize(Exist exist) {

        value = exist.value();

        collectionName = exist.collectionName();

        key = exist.key();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        if (value) {
            return exist(s);
        }

        return !exist(s);

    }

    private boolean exist(String s) {

        MongoQuery query = new MongoQuery().use(collectionName);

        if (key.equals("id") || key.equals("_id")) {
            query.byId(s);
        } else {
            query.eq(key, s);
        }

        return query.exist();

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy