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

Java tutorial

Introduction

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

Source

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

import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.constants.Constants;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import org.apache.commons.lang3.StringUtils;

import javax.faces.application.FacesMessage;

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

public class PortValidator implements Constants {
    private TtGen ttGen;

    public PortValidator(TtGen ttGen) {
        this.ttGen = ttGen;
    }

    public void checkPortOfLoadingAndPlaceOfReceiptAgainstCountryOfSupply() {

        String errorMessage1 = "Port of loading does not match to the country of supply!";
        String errorMessage2 = "Country of supply cannot be empty while Port of Loading/Place of Receipt is not empty!";
        String errorMessage3 = "Place of receipt does not match to the country of supply!";

        String countryOfSupply = ttGen.getCtySupp() == null ? "" : ttGen.getCtySupp();
        String portOfLoading = ttGen.getTtTrans().getLoadPort();
        String placeOfReceipt = ttGen.getTtTrans().getPlaceOfReceipt();

        if ((!StringUtils.isEmpty(portOfLoading) || !StringUtils.isEmpty(placeOfReceipt))
                && StringUtils.isEmpty(countryOfSupply)) {
            ErrorHandling.addFacesMessage("ctySupp", errorMessage2, FacesMessage.SEVERITY_ERROR);
        } else {
            if (StringUtils.isEmpty(placeOfReceipt)) {
                if (!StringUtils.isEmpty(portOfLoading)
                        && !portOfLoading.substring(0, 2).toLowerCase().equals(countryOfSupply.toLowerCase())) {
                    ErrorHandling.addFacesMessage("loadPort", errorMessage1, FacesMessage.SEVERITY_ERROR);
                }
            } else {
                if (!placeOfReceipt.substring(0, 2).toLowerCase().equals(countryOfSupply.toLowerCase())) {
                    ErrorHandling.addFacesMessage("placeOfReceipt", errorMessage3, FacesMessage.SEVERITY_ERROR);
                }
            }
        }
    }
}