package misc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Utils {
    
    public static String sanitizeString(String input) {
    
        return input.replaceAll("[^\\p{L}\\p{Nd}]+", "");
    }
    
    public static boolean validateString(String str) {
    
        if (str.matches("\\A\\s*\\Z")) {
            return false;
        }
        
        return (str != null && !str.isEmpty());
    }
    
    public static boolean validateString(String str, int maxLength) {
    
        if (str.matches("\\A\\s*\\Z")) {
            return false;
        }
        
        return (str != null && str.length() <= maxLength);
    }
    
    public static boolean validateString(String str, int minLength, int maxLength) {
    
        if (str.matches("\\A\\s*\\Z")) {
            return false;
        }
        
        return (str != null && str.length() >= minLength && str.length() <= maxLength);
    }
    
    public static boolean validateInt(Integer num) {
    
        return (num != null && num > 0);
    }
    
    public static boolean validateInt(Integer num, int minValue) {
    
        return (num != null && num >= minValue);
    }
    
    public static boolean validateDate(String date) {
    
        if (!date
                .matches("(([1-9])|(0[1-9])|(1[0-2]))/(([1-9])|(0[1-9])|([1-2][0-9])|(3[0-1]))/((20){0,1}[1-9][0-9]) (([0-9])|([0-1][0-9])|(2[0-3])):(([0-9])|([0-5][0-9]))")) {
            return false;
        }
        return true;
    }
    
    public static boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
    
        ResultSetMetaData rsmd = rs.getMetaData();
        int columns = rsmd.getColumnCount();
        for (int x = 1; x <= columns; x++) {
            if (columnName.equals(rsmd.getColumnName(x))) {
                return true;
            }
        }
        return false;
    }
}
