Example usage for org.apache.commons.validator.routines.checkdigit VerhoeffCheckDigit isValid

List of usage examples for org.apache.commons.validator.routines.checkdigit VerhoeffCheckDigit isValid

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines.checkdigit VerhoeffCheckDigit isValid.

Prototype

@Override
public boolean isValid(String code) 

Source Link

Document

Validate the Verhoeff Check Digit for a code.

Usage

From source file:com.evolveum.midpoint.gui.api.util.WebComponentUtil.java

public static boolean isSubscriptionIdCorrect(String subscriptionId) {
    if (StringUtils.isEmpty(subscriptionId)) {
        return false;
    }/*from w ww  .  ja v  a 2 s .com*/
    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);
        Date currentDate = new Date(System.currentTimeMillis());
        if (expDate.before(currentDate)) {
            return false;
        }
    } catch (Exception ex) {
        return false;
    }
    VerhoeffCheckDigit checkDigit = new VerhoeffCheckDigit();
    if (checkDigit.isValid(subscriptionId)) {
        return true;
    }
    return false;
}