如何在java中处理模型验证和一致性检查

时间:2010-01-08 13:24:25

标签: java validation data-modeling

我的问题最好用一些代码说明。请浏览代码并在最后阅读问题。

enum PropertySourceEnum {
    DEFAULT,
    CALCULATED,
    USER,
    UNKNOWN
}

enum PropertyValidityEnum {
    ERROR,
    WARNING,
    OK
}

class WorkerProperty <T>{
    private T value;
    private PropertyValidity validity;
    private PropertySourceEnum source;

    public WorkerProperty(T value) {
        this.value = value;
        this.source = PropertySourceEnum.UNKNOWN;
        this.validity = new PropertyValidity(PropertyValidityEnum.WARNING,
                "Unverified");
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public PropertySourceEnum getSource() {
        return this.source;
    }

    public void setSource(PropertySourceEnum source) {
        this.source = source;
    }

    public PropertyValidity getValidity() {
        return this.validity;
    }

    public void setValidity(PropertyValidity validity) {
        this.validity = validity;
    }

}

class PropertyValidity {
    private final PropertyValidityEnum state;
    private final String message;

    public PropertyValidity(PropertyValidityEnum state, 
            String message) {
        this.state = state;
        this.message = message;
    }
}

class DefaultingService {
    // Using reflection iterate through all the properties
    // and if property source is UNKNOWN or was previous DEFAULTED
    // then see if the database has an appropriate value for it
    // set it to that value and set its source to "DEFAULTED"
}

interface Rule {
    public void fire(Worker w);
}
class ValidationWorkerAge implements Rule {

    @Override
    public void fire(Worker w) {
        // is the worker's age > 16 < 85
        // if age is not set set the validity to ERROR, "required field" 
        int age = (int) w.age.value;
        if (age <= 16) {
            PropertyValidity pv = new PropertyValidity(PropertyValidityEnum.ERROR,
                    "Worker too young");
            w.age.setValidity(pv); 
        } else if (age > 75) {
            PropertyValidity pv = new PropertyValidity(PropertyValidityEnum.ERROR,
            "Worker too old");
            w.age.setValidity(pv); 
        } else {
            PropertyValidity pv = new PropertyValidity(PropertyValidityEnum.OK,
            "OK");
            w.age.setValidity(pv); 
        }

    }

}

class ValidationService {
    // Has a Tree of validation rules and traverses the tree fire()ing each rule
    // along the way

}

class CheckWorkerAgeConsistency implements Rule {

    @Override
    public void fire(Worker w) {
        // if both age and dateOfBirth are set
        // Check that worker age = today().year - dateofBirth.years()
        // or dateofBirth.years() = today().year - age

        // if only DateOfBith is set then calculate age and mark its source as CALCULATED
        // if only age is set then calculate DateOfBith and mark its source as CALCULATED
        // if neither is set do nothing
    }

}


class ConsistencyService {
    // Has a Tree of consistency rules and traverses the tree fire()ing each rule
    // along the way
}

// ---------------------- //工人实施

class Worker1 {
    WorkerProperty<Long> id;
    WorkerProperty<String> name;
    WorkerProperty<String> surname;
    WorkerProperty<Integer> dateOfBirth;
    WorkerProperty<Integer> age;
}

class Manager1 extends Worker1 {

}

// plus other types of worker

// ---------------------- //替代工人实施

class Worker2 {
    Long id;
    PropertyValidity id_validity;
    PropertySourceEnum id_source;

    String name;
    PropertyValidity name_validity;
    PropertySourceEnum name_source;

    String surname;
    PropertyValidity surname_validity;
    PropertySourceEnum surname_source;

    Integer dateOfBirth;
    PropertyValidity dateOfBirth_validity;
    PropertySourceEnum dateOfBirth_source;

    Integer age;
    PropertyValidity age_validity;
    PropertySourceEnum age_source;
}

class Manager2 extends Worker2 {

}

// plus other types of worker

我的问题是:
鉴于上述情况,应该如何实施工人。我应该如类Worker1所示实现Worker,还是应该实现Worker,如Worker2所示? 谢谢和亲切的问候

1 个答案:

答案 0 :(得分:2)

您可能需要考虑使用JSR 303 Bean Validation进行验证。 Hibernate Validator是其中的一个实现。

当然,使用JSR 303的一些想法会很好,例如而不是Worker1中的WorkerProperty容器或Worker2中的PropertyValidity,您可以考虑使用Annotations。