Kenmegne
2025-12-10 e9d80d486b912144b59ebd5939d4837105b37b99
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
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.queryadhoc.validators;
 
import com.megatim.queryadhoc.exceptions.ConnectionParameterException;
import com.megatim.queryadhoc.model.ConnectionParameter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 *
 * @author ASUS
 */
public class ConnectionParameterValidator {
 
    public void checkConnectionParameter(ConnectionParameter connectionParameter) throws ConnectionParameterException {
 
        List<String> errors = Arrays.asList(checkPort(connectionParameter.getPort()),
                checkDataBaseName(connectionParameter.getDatabaseName()),
                checkUserName(connectionParameter.getUserName()),
                checkIpAddress(connectionParameter.getIpAddress()));
        String message = errors.stream().filter(e -> !e.isEmpty()).collect(Collectors.joining("\n"));
 
        if (!message.isEmpty()) {
            throw new ConnectionParameterException(message);
        }
    }
 
    private String checkPort(int port) {
        if (port < 0 && port > 65535) {
            return "La valeur du port est invalide";
        }
        return "";
    }
 
    private String checkDataBaseName(String dataBaseName) {
        if (dataBaseName == null || dataBaseName.isEmpty()) {
            return "Le nom de la base de données ne peut être vide ou nul";
        }
        return "";
    }
 
    private String checkUserName(String userName) {
        if (userName == null || userName.isEmpty()) {
            return "Le nom de l'utilisateur ne peut être vide ou nul";
        }
        return "";
    }
 
    private String checkIpAddress(String ipAddress) {
        if (ipAddress == null || ipAddress.isEmpty()) {
            return "Adresse IP Invalide";
        }
        String[] tab = ipAddress.split("\\.");
 
        if (tab.length != 4 || !checkIpAdressPart(tab)) {
            return "Adresse IP Invalide";
        }
        return "";
    }
 
    private boolean checkIpAdressPart(String[] tab) {
        try {
            int part1 = Integer.parseInt(tab[0]);
            int part2 = Integer.parseInt(tab[1]);
            int part3 = Integer.parseInt(tab[2]);
            int part4 = Integer.parseInt(tab[3]);
            return !(part1 < 0 || part1 > 255
                    || part2 < 0 || part2 > 255
                    || part3 < 0 || part3 > 255
                    || part4 < 0 || part4 > 255);
        } catch (Exception ex) {
            return false;
        }
    }
}