wangmengmeng
2024-12-24 24432a361d5c6bd6f3d8c008693e9f1155d62517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.dji.sdk.common;
 
import com.dji.sdk.annotations.CloudSDKVersion;
import com.dji.sdk.config.version.GatewayManager;
import com.dji.sdk.exception.CloudSDKErrorEnum;
import com.dji.sdk.exception.CloudSDKException;
import org.springframework.util.CollectionUtils;
 
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author sean
 * @version 1.7
 * @date 2023/5/23
 */
public class BaseModel {
 
    private final static Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
 
    public void valid() {
        this.valid(null);
    }
 
    public void checkProperty(String fieldName, GatewayManager gateway) {
        try {
            Field field = this.getClass().getDeclaredField(fieldName);
            CloudSDKVersion annotation = field.getDeclaredAnnotation(CloudSDKVersion.class);
            if (!gateway.isTypeSupport(annotation) || !gateway.isVersionSupport(annotation)) {
                throw new CloudSDKException(CloudSDKErrorEnum.DEVICE_PROPERTY_NOT_SUPPORT, fieldName);
            }
        } catch (NoSuchFieldException e) {
            throw new CloudSDKException(e);
        }
    }
 
    public void valid(GatewayManager gateway) {
        Set<ConstraintViolation<BaseModel>> violations = VALIDATOR.validate(this);
        if (null != gateway) {
            Set<String> names = new HashSet<>();
            violations = violations.stream().filter(violation ->
                    filterProperty(gateway, violation.getRootBeanClass(),
                        violation.getPropertyPath().toString().split("\\."), 0, true, names))
                    .collect(Collectors.toSet());
        }
 
        if (CollectionUtils.isEmpty(violations)) {
            return;
        }
        throw new CloudSDKException(CloudSDKErrorEnum.INVALID_PARAMETER, violations.stream()
                .map(violation -> violation.getPropertyPath().toString() + violation.getMessage() +
                        ", Current value is: " + violation.getInvalidValue())
                .collect(Collectors.joining("; ")));
 
    }
 
    private boolean filterProperty(GatewayManager gateway, Class clazz, String[] fields, int index, boolean isValid, Set<String> names) {
        if (!isValid || index == fields.length) {
            return isValid;
        }
        String name = String.join(".", Arrays.copyOf(fields, index + 1));
        if (names.contains(name)) {
            return false;
        }
        try {
            Field field = clazz.getDeclaredField(fields[index]);
            isValid = gateway.isPropertyValid(field.getAnnotation(CloudSDKVersion.class));
            if (!isValid) {
                names.add(name);
            }
            return filterProperty(gateway, field.getType(), fields, index + 1, isValid, names);
        } catch (NoSuchFieldException e) {
            throw new CloudSDKException(e);
        }
    }
}