Search in sources :

Example 1 with VerhoeffCheckDigit

use of org.apache.commons.validator.routines.checkdigit.VerhoeffCheckDigit in project midpoint by Evolveum.

the class WebComponentUtil method isSubscriptionIdCorrect.

public static boolean isSubscriptionIdCorrect(String subscriptionId) {
    if (StringUtils.isEmpty(subscriptionId)) {
        return false;
    }
    if (!NumberUtils.isDigits(subscriptionId)) {
        return false;
    }
    if (subscriptionId.length() < 11) {
        return false;
    }
    String subscriptionType = subscriptionId.substring(0, 2);
    boolean isTypeCorrect = false;
    for (SubscriptionType type : SubscriptionType.values()) {
        if (type.getSubscriptionType().equals(subscriptionType)) {
            isTypeCorrect = true;
            break;
        }
    }
    if (!isTypeCorrect) {
        return false;
    }
    String substring1 = subscriptionId.substring(2, 4);
    String substring2 = subscriptionId.substring(4, 6);
    try {
        if (Integer.parseInt(substring1) < 1 || Integer.parseInt(substring1) > 12) {
            return false;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yy");
        String currentYear = dateFormat.format(Calendar.getInstance().getTime());
        if (Integer.parseInt(substring2) < Integer.parseInt(currentYear)) {
            return false;
        }
        String expDateStr = subscriptionId.substring(2, 6);
        dateFormat = new SimpleDateFormat("MMyy");
        Date expDate = dateFormat.parse(expDateStr);
        Calendar expireCalendarValue = Calendar.getInstance();
        expireCalendarValue.setTime(expDate);
        expireCalendarValue.add(Calendar.MONTH, 1);
        Date currentDate = new Date(System.currentTimeMillis());
        if (expireCalendarValue.getTime().before(currentDate) || expireCalendarValue.getTime().equals(currentDate)) {
            return false;
        }
    } catch (Exception ex) {
        return false;
    }
    VerhoeffCheckDigit checkDigit = new VerhoeffCheckDigit();
    return checkDigit.isValid(subscriptionId);
}
Also used : SubscriptionType(com.evolveum.midpoint.gui.api.SubscriptionType) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SimpleDateFormat(java.text.SimpleDateFormat) VerhoeffCheckDigit(org.apache.commons.validator.routines.checkdigit.VerhoeffCheckDigit) InvocationTargetException(java.lang.reflect.InvocationTargetException) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException)

Aggregations

SubscriptionType (com.evolveum.midpoint.gui.api.SubscriptionType)1 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 VerhoeffCheckDigit (org.apache.commons.validator.routines.checkdigit.VerhoeffCheckDigit)1