com.webbfontaine.valuewebb.model.validators.HSCValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.model.validators.HSCValidator.java

Source

package com.webbfontaine.valuewebb.model.validators;

import com.webbfontaine.valuewebb.action.pricedb.mp.MarketPriceHome;
import com.webbfontaine.valuewebb.action.tt.TtGenHome;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.constants.Constants;
import com.webbfontaine.valuewebb.model.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.Validator;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import javax.faces.context.FacesContext;
import java.util.Arrays;

/**
 * Copyrights 2002-2013 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * Developer: nigiyan
 * Date: 18/07/2013
 */

public class HSCValidator implements Validator<HSCode> {

    private static final Log LOGGER = Logging.getLog(HSCValidator.class);
    private String[] invalids;

    @Override
    public boolean isValid(Object value) {
        String hsCode = (String) value;
        boolean isValid = false;

        if (StringUtils.isEmpty(hsCode)) {
            isValid = true;
        } else {
            HSCFinder hscFinder = new HSCFinder(hsCode);

            if (invalids != null && Arrays.asList(invalids).contains(hsCode)) {
                isValid = false;
            } else {

                if (hsCode.length() == 10) {
                    isValid = !hscFinder.findAll().isEmpty();
                } else {
                    if (FacesContext.getCurrentInstance() != null
                            && ((String) Component.getInstance("currentPage", ScopeType.PAGE, true))
                                    .contains("MarketResearch")) {
                        isValid = checkHSCodeForMP(hsCode);
                    }
                }
            }
        }

        return isValid;
    }

    private static boolean checkHSCodeForMP(String hsCode) {
        boolean exists = false;
        if (hsCode.length() >= 6
                && !"4".equals(((MarketPriceHome) Component.getInstance(MarketPriceHome.class, false)).getInstance()
                        .getVmRuleCode())) {
            exists = new HSCFinder(hsCode).countStartWith() != 0;
        }
        return exists;
    }

    @Override
    public void initialize(HSCode parameters) {
        invalids = parameters.invalids();
    }
}