SWIFTValidationUtil.java

package com.tradecloud.domain.letterofcredit;

import com.tradecloud.domain.exception.BaseBusinessException;
import com.tradecloud.domain.model.ordermanagement.PurchaseOrder;
import com.tradecloud.domain.party.Bank;
import com.tradecloud.domain.party.ServiceProvider;
import com.tradecloud.domain.party.base.Company;
import com.tradecloud.domain.supplier.OrganisationalUnitSupplier;
import org.apache.commons.lang.StringUtils;

public class SWIFTValidationUtil {

    public static final String swiftSet = "[ \r\na-zA-Z0-9\\?\\(\\)\\.,:'\\+\\-/]*";
    private static final String BENEFICIARY = "beneficiary";
    private static final String ADVISING_BANK = "Advising Bank";
    private static final String LOGISTICS_SERVICE_PROVIDER = "Logistics Service Provider";
    private static final String APPLICANT = "applicant";
    private static final String ISSUING_BANK = "Issuing bank";

    public static void validateLetterOfCredit(LetterOfCredit letterOfCredit) {

        validate("Bankcharges Description", letterOfCredit.getBankChargesDescription());
        validate("Instructions To Issuing Bank", letterOfCredit.getInstructionsToIssuingBank());
        validate("Special Conditions", letterOfCredit.getSpecialConditions());
        validate("Shipping Mark", letterOfCredit.getShippingMark());
        validate("Documents", letterOfCredit.getResolvedDocuments());
        validate("Conditions", letterOfCredit.getResolvedConditions());
        // JBR
        //validate("Goods Description", letterOfCredit.getPurchaseOrder().getGoodsDescription());
        validate("Port of Docking", (letterOfCredit.getShippingInfo().getPlaceOfDischarge() == null)
                ? "" : letterOfCredit.getShippingInfo().getPlaceOfDischarge().getName());
        validate("Port of Docking", (letterOfCredit.getShippingInfo().getPlaceOfLoading() == null)
                ? "" : letterOfCredit.getShippingInfo().getPlaceOfLoading().getName());
        validate("Named Place", (letterOfCredit.getShippingInfo().getNamedPlace() == null)
                ? "" : letterOfCredit.getShippingInfo().getNamedPlace().getName());
        //validatePurchaseOrder(letterOfCredit.getPurchaseOrder());
        //validateApplicant(letterOfCredit.getApplicant());

        ServiceProvider logisticsServiceProvider;
        if ((logisticsServiceProvider = letterOfCredit.getShippingInfo().getFreightForwarder()) != null) {
            // JBR
            //validateOrganisation(logisticsServiceProvider.getOrganisation(), LOGISTICS_SERVICE_PROVIDER);
        }
        validateBeneficiary(letterOfCredit.getBeneficiary());
    }

    private static void validateApplicant(LocalLCApplicant applicant) {
        if (applicant != null) {
            // JBR
            //validateOrganisation(applicant.getOrganisation(), APPLICANT);
            validateBank(applicant.getIssuingBank(), ISSUING_BANK);
        }
    }

    private static void validatePurchaseOrder(PurchaseOrder purchaseOrder) {
        if (purchaseOrder != null) {
            // JBR
            //validate("Application Reference", (purchaseOrder.getApplicationReference() == null) ? "" : purchaseOrder.getApplicationReference());
        }
    }

    private static void validateBeneficiary(OrganisationalUnitSupplier beneficiary) {
        if (beneficiary != null) {
            validate("Supplier Reference", beneficiary.getExternalReference());
            validate("Supplier Expiry Place", beneficiary.getPlaceOfExpiry());
            validateLocalLCBeneficiary(beneficiary, BENEFICIARY);
            validateBank(beneficiary.getAdvisingBank(), ADVISING_BANK);
        }
    }

    private static void validateBank(Bank localBank, String prefix) {

        if (localBank != null) {
            // JBR
            //validateOrganisation(localBank.getOrganisation(), prefix);
        }
    }

    private static void validateOrganisation(Company organisation, String prefix) {

        String foreword = (prefix == null) ? "" : prefix + " ";
        if (organisation != null) {
            validate(foreword + "Name", organisation.getName());
            if (organisation.getPhysicalAddress() != null) {
                validate(foreword + " address line 1", organisation.getPhysicalAddress().getAddressLine1());
                validate(foreword + "address line 2", organisation.getPhysicalAddress().getAddressLine2());
                validate(foreword + "address line 3", organisation.getPhysicalAddress().getAddressLine3());
                validate(foreword + "city", organisation.getPhysicalAddress().getCity().getCode());
                if (organisation.getPhysicalAddress().getCountry() != null) {
                    // JBR
                    //validate(foreword + "country name", organisation.getPhysicalAddress().getCountry().getDescription());
                    validate(foreword + "country code", organisation.getPhysicalAddress().getCountry().getCode());
                }
            }
        }
    }

    private static void validateLocalLCBeneficiary(OrganisationalUnitSupplier lcBeneficiary, String prefix) {

        String foreword = (prefix == null) ? "" : prefix + " ";
        if (lcBeneficiary != null) {
            validate(foreword + "Name", lcBeneficiary.getName());
            if (lcBeneficiary.getPhysicalAddress() != null) {
                validate(foreword + " address line 1", lcBeneficiary.getPhysicalAddress().getAddressLine1());
                validate(foreword + "address line 2", lcBeneficiary.getPhysicalAddress().getAddressLine2());
                validate(foreword + "address line 3", lcBeneficiary.getPhysicalAddress().getAddressLine3());
                validate(foreword + "city", lcBeneficiary.getPhysicalAddress().getCity().getCode());
                if (lcBeneficiary.getPhysicalAddress().getCountry() != null) {
                    // JBR
                    //validate(foreword + "country name", lcBeneficiary.getPhysicalAddress().getCountry().getDescription());
                    validate(foreword + "country code", lcBeneficiary.getPhysicalAddress().getCountry().getCode());
                }
            }
        }
    }

    protected static void validate(String description, String value) {

        if (!StringUtils.isEmpty(value)) {
            if (!value.matches(swiftSet)) {
                throw new BaseBusinessException("SWIFTValidationException.description:" + description);
            }
        }
    }

}