/*
|
* 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;
|
}
|
}
|
}
|