com.webbfontaine.valuewebb.model.validators.tt.TINValidator.java Source code

Java tutorial

Introduction

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

Source

package com.webbfontaine.valuewebb.model.validators.tt;

import com.webbfontaine.valuewebb.action.rimm.RefSelect;
import com.webbfontaine.valuewebb.action.tt.TtGenHome;
import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.rimm.CompanyData;
import com.webbfontaine.valuewebb.model.rimm.CompanyType;
import com.webbfontaine.valuewebb.model.util.Utils;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.Validator;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import javax.faces.application.FacesMessage;

import static com.webbfontaine.valuewebb.model.constants.Constants.CI;

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

public class TINValidator implements Validator<Tin> {

    private static final Log LOGGER = Logging.getLog(TINValidator.class);

    private CompanyType companyType;

    @Override
    public boolean isValid(Object value) {
        if (Utils.currentPage().contains("TtGenList")) {
            return true;
        }

        String tin = (String) value;

        if (StringUtils.isEmpty(tin)) {
            return true; // mandatory value check is not done by this validator
        }

        CompanyData company = RefSelect.getInstance().loadCompany(companyType, tin);

        boolean isValid = false;
        if (company != null) {
            if (companyType == CompanyType.IMPORTER) {
                setImpData(company);
            }
            if (companyType == CompanyType.APPLICANT) {
                setAppDisabledData(company);
            }
            isValid = true;

            //checkIfBlocked(company);      todo: Remove this method if TIN blocked checking is really unnecessary (Marina)
        }

        if (!isValid) {
            LOGGER.debug("Company inexistent {}", value);
        }

        return isValid;
    }

    private static void setAppDisabledData(CompanyData company) {
        TtGen ttGen = TtGenHome.getComponentInstance(false).getInstance();
        ttGen.setAppNam(company.getNam());
        ttGen.setAppAdr(company.getAdr());
        ttGen.setAppCty(company.getCountry());
        ttGen.setAppCity(company.getCity());
        ttGen.setAppId(company.getId());
    }

    private static void setImpData(CompanyData company) {
        TtGen ttGen = TtGenHome.getComponentInstance(false).getInstance();

        ttGen.setImpNam(company.getNam());
        ttGen.setImpAdr(company.getAdr());
        ttGen.setImpCty(company.getCountry());
        ttGen.setImpCity(company.getCity());
        ttGen.setImpTradeLevel(company.getTradeLevel());
        ttGen.setImpEmail(company.getEmail());
        ttGen.setImpId(company.getId());
    }

    @Override
    public void initialize(Tin parameters) {
        companyType = parameters.companyType();
    }

    private void checkIfBlocked(CompanyData company) {
        String uiFieldId;

        switch (companyType) {
        case APPLICANT:
            uiFieldId = "appTin";
            break;
        case IMPORTER:
            uiFieldId = "impTin";
            break;
        default:
            throw new RuntimeException("Unknown Company type: " + companyType);
        }

        if (CI.equals(Utils.getAppCountry()) && !company.getStatus()) {
            ErrorHandling.addFacesMessage(uiFieldId, "TIN is blocked by customs", FacesMessage.SEVERITY_ERROR);
        }
    }
}