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

cn.allbs.hj212.validator.T212MapFieldMissingValidator Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package cn.allbs.hj212.validator;


import cn.allbs.hj212.enums.HjDataFlag;
import cn.allbs.hj212.model.verify.DataElement;
import cn.allbs.hj212.model.verify.T212Map;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author ChenQi
 */
public class T212MapFieldMissingValidator
        implements ConstraintValidator {

    private FieldMissing fieldMissing;


    @Override
    public void initialize(FieldMissing fieldMissing) {
        this.fieldMissing = fieldMissing;
    }

    @Override
    public boolean isValid(T212Map value, ConstraintValidatorContext constraintValidatorContext) {
        Map result = value;

        Stream stream = Stream.of(DataElement.values())
                .filter(DataElement::isRequired);
        if (result.containsKey(DataElement.Flag.name())) {
            String f = result.get(DataElement.Flag.name());
            int flag = Integer.valueOf(f);
            if (HjDataFlag.D.isMarked(flag)) {
                stream = Stream.concat(stream, Stream.of(DataElement.PNO, DataElement.PNUM));
            }
        }

        Optional missing = stream
                .filter(e -> !result.containsKey(e.name()))
                .findFirst();
        if (missing.isPresent()) {
            return false;
        }
        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy