ProductFieldCopyHelper.java

package com.tradecloud.domain.product;

import com.tradecloud.domain.duties.*;
import com.tradecloud.domain.item.ItemInterface;
import com.tradecloud.domain.item.Product;
import org.apache.log4j.Logger;

import java.util.ArrayList;

/**
 *
 */
public class ProductFieldCopyHelper {

    private static Logger log = Logger.getLogger(ProductFieldCopyHelper.class);

    private static void copyTariffHeading(DutySchedule from, DutySchedule to) {
        TariffHeading fromTariffHeading = from.getTariffHeading();
        TariffHeading toTariffHeading = to.getTariffHeading();
        toTariffHeading.setPart1(fromTariffHeading.getPart1());
        toTariffHeading.setPart2(fromTariffHeading.getPart2());
        toTariffHeading.setPart3(fromTariffHeading.getPart3());
        toTariffHeading.setPart4(fromTariffHeading.getPart4());
        toTariffHeading.setPart5(fromTariffHeading.getPart5());
        to.setValid(from.isValid());
    }

    private static void copyS1P1A(Schedule1Part1A from, Schedule1Part1A to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
        to.setCalculationMethod(from.getCalculationMethod());
        to.setTradeAgreement(from.getTradeAgreement());
        to.setTradeAgreementOverridden(from.isTradeAgreementOverridden());
        to.setDutyDescription(from.getDutyDescription());
        to.setSupplementaryQty1(from.getSupplementaryQty1());
        to.setSupplementaryQty2(from.getSupplementaryQty2());
        to.setSupplementaryQty3(from.getSupplementaryQty3());
    }

    private static void copyS1P3E(Schedule1Part3E from, Schedule1Part3E to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
        to.setCalculationMethod(from.getCalculationMethod());
    }

    private static void copyS1P7(Schedule1Part7 from, Schedule1Part7 to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
        to.setCalculationMethod(from.getCalculationMethod());
    }

    private static void copyS1P2A(Schedule1Part2A from, Schedule1Part2A to) {
        copyDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
    }

    private static void copyS1P2B(Schedule1Part2B from, Schedule1Part2B to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
    }

    private static void copyS2P1(Schedule2Part1 from, Schedule2Part1 to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
    }

    private static void copyS2P2(Schedule2Part2 from, Schedule2Part2 to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
    }

    public static void copyDuties(ItemInterface from, ItemInterface to) {

        if (from.getSchedule1Part1A() != null) {
            initS1P1A(to);
            copyS1P1A(from.getSchedule1Part1A(), to.getSchedule1Part1A());
            if (from.isLineItem()) {
                to.getSchedule1Part1A().setSupplementaryQty2(from.getSchedule1Part1A().getSupplementaryQty2());
                to.getSchedule1Part1A().setSupplementaryQty3(from.getSchedule1Part1A().getSupplementaryQty3());
            }
            if (from != null && from.getSchedule1Part1A() != null && from.getSchedule1Part1A().getRulesOfOrigin() != null)
                to.getSchedule1Part1A().setRulesOfOrigin(new ArrayList<>(from.getSchedule1Part1A().getRulesOfOrigin()));
            if (from != null && from.getSchedule1Part1A() != null && from.getSchedule1Part1A().getRulesOfOriginAuthNumber() != null)
                to.getSchedule1Part1A().setRulesOfOriginAuthNumber(from.getSchedule1Part1A().getRulesOfOriginAuthNumber());
        }
        if (from.getSchedule1Part2A() != null) {
            initS1P2A(to);
            copyS1P2A(from.getSchedule1Part2A(), to.getSchedule1Part2A());
        }
        if (from.getSchedule1Part2B() != null) {
            initS1P2B(to);
            copyS1P2B(from.getSchedule1Part2B(), to.getSchedule1Part2B());
        }
        if (from.getSchedule1Part3E() != null) {
            initS1P3E(to);
            copyS1P3E(from.getSchedule1Part3E(), to.getSchedule1Part3E());
        }

        if (from.getSchedule1Part7() != null) {
            initS1P7(to);
            copyS1P7(from.getSchedule1Part7(), to.getSchedule1Part7());
        }

        if (from.getSchedule2Part1() != null) {
            initS2P1(to);
            copyS2P1(from.getSchedule2Part1(), to.getSchedule2Part1());
        }
        if (from.getSchedule2Part2() != null) {
            initS2P2(to);
            copyS2P2(from.getSchedule2Part2(), to.getSchedule2Part2());
        }
        if (from.getSchedule3Part1() != null) {
            initS3P1(to);
            copyS3P1(from.getSchedule3Part1(), to.getSchedule3Part1());
        }
        if (from.getSchedule4Part1() != null) {
            initS4P1(to);
            copyS4P1(from.getSchedule4Part1(), to.getSchedule4Part1());
        }

        to.getItacPermit().copy(from.getItacPermit());
    }

    private static void copyS3P1(Schedule3Part1 from, Schedule3Part1 to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
        to.setRebateCode(from.getRebateCode());
        to.setRebateOptions(from.getRebateOptions());
    }

    private static void copyS4P1(Schedule4Part1 from, Schedule4Part1 to) {
        copyTypedDutyRate(from.getDutyRate(), to.getDutyRate());
        copyTariffHeading(from, to);
        to.setVatExemption(from.isVatExemption());
        to.setRebateCode(from.getRebateCode());
        to.setRebateOptions(from.getRebateOptions());
    }

    /**
     * Determines whether the tariff heading in the schedule is eligible to be deemed as valid.
     * It delegates to {@code TariffHeading} for this decision and if it is then we set the duty schedule validationDisabled flag to false.
     * If it's not eligible then we set the duty schedule valid flag to false and the validationDisabled flag to true.
     * This method is synchronized in an attempt to rule out any race conditions that may be occuring when the network requests are very slow from
     * the UI to the server (as seen on sndtest08 by Mariesa).  There was some inconsistency with the valid checkbox and when it was being made
     * enabled/disabled.  The problem can't be created locally (so far).
     *
     * @param schedule the schedule whose tariff heading is being checked
     * @throws NullPointerException if schedule is null
     */
    public static synchronized void determineTariffHeadingValidationStatus(DutySchedule schedule) {
        if (schedule.eligibleForValidation()) {
            //log.debug("Schedude (" + schedule.getTariffHeading().getTariffHeading() + ") is eligible for validation");
            schedule.setValidationDisabled(false);
            schedule.setValid(true);
        } else {
            //log.debug("Schedude (" + schedule.getTariffHeading().getTariffHeading() + ") is NOT eligible for validation");
            schedule.setValid(false);
            schedule.setValidationDisabled(true);
        }
    }

    private static void initS1P1A(ItemInterface product) {
        if (product.getSchedule1Part1A() == null) {
            product.setSchedule1Part1A(new Schedule1Part1A());
        }
        initialiseSchedule(product.getSchedule1Part1A());
    }

    private static void initS1P3E(ItemInterface product) {
        if (product.getSchedule1Part3E() == null) {
            product.setSchedule1Part3E(new Schedule1Part3E());
        }
        initialiseSchedule(product.getSchedule1Part3E());
    }

    private static void initS1P7(ItemInterface product) {
        if (product.getSchedule1Part7() == null) {
            product.setSchedule1Part7(new Schedule1Part7());
        }
        initialiseSchedule(product.getSchedule1Part7());
    }

    private static void initS1P2A(ItemInterface product) {
        if (product.getSchedule1Part2A() == null) {
            product.setSchedule1Part2A(new Schedule1Part2A());
        }
        if (product.getSchedule1Part2A().getDutyRate() == null) {
            product.getSchedule1Part2A().setDutyRate(new DutyRate());
        }
        initialiseSchedule(product.getSchedule1Part2A());
    }

    private static void initS1P2B(ItemInterface product) {
        if (product.getSchedule1Part2B() == null) {
            product.setSchedule1Part2B(new Schedule1Part2B());
        }
        initialiseSchedule(product.getSchedule1Part2B());
    }

    private static void initS2P1(ItemInterface product) {
        if (product.getSchedule2Part1() == null) {
            product.setSchedule2Part1(new Schedule2Part1());
        }
        initialiseSchedule(product.getSchedule2Part1());
    }

    private static void initS2P2(ItemInterface product) {
        if (product.getSchedule2Part2() == null) {
            product.setSchedule2Part2(new Schedule2Part2());
        }
        initialiseSchedule(product.getSchedule2Part2());
    }

    private static void initS3P1(ItemInterface product) {
        if (product.getSchedule3Part1() == null) {
            product.setSchedule3Part1(new Schedule3Part1());
        }
        initialiseSchedule(product.getSchedule3Part1());
    }

    private static void initS4P1(ItemInterface product) {
        if (product.getSchedule4Part1() == null) {
            product.setSchedule4Part1(new Schedule4Part1());
        }
        initialiseSchedule(product.getSchedule4Part1());
    }

    /**
     * Handles common functionality for initialising a schedule for the UI.
     *
     * @param schedule the schedule to initialise
     */
    private static void initialiseSchedule(DutySchedule schedule) {
        initialiseOtherDuties(schedule.getDutyRate());
        determineTariffHeadingValidationStatus(schedule);
        if (schedule.getTariffHeading() == null) {
            schedule.setTariffHeading(new TariffHeading());
        }
    }

    /**
     * Initialises the {@code OtherDuty} fields in a duty rate.
     *
     * @param dutyRate the duty rate whose other duty fields need to be initialised
     */
    private static void initialiseOtherDuties(AbstractDutyRate dutyRate) {
        if (dutyRate.getOtherDuty1() == null) {
            dutyRate.setOtherDuty1(new OtherDuty());
        }
        if (dutyRate.getOtherDuty2() == null) {
            dutyRate.setOtherDuty2(new OtherDuty());
        }
        if (dutyRate.getOtherDuty3() == null) {
            dutyRate.setOtherDuty3(new OtherDuty());
        }
    }

    private static void copyOtherDuty(OtherDuty from, OtherDuty to) {
        to.setAdditionalValue(from.getAdditionalValue());
        to.setProof(from.getProof());
        to.setUnit(from.getUnit());
        to.setValue(from.getValue());
    }

    private static void copyTypedDutyRate(TypedDutyRate from, TypedDutyRate to) {
        to.setType(from.getType());
        to.setPercentage(from.getPercentage());
        to.setValue(from.getValue());

        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty1(), to.getOtherDuty1());
        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty2(), to.getOtherDuty2());
        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty3(), to.getOtherDuty3());
    }

    private static void copyDutyRate(DutyRate from, DutyRate to) {
        to.setValue(from.getValue());
        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty1(), to.getOtherDuty1());
        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty2(), to.getOtherDuty2());
        ProductFieldCopyHelper.copyOtherDuty(from.getOtherDuty3(), to.getOtherDuty3());
    }

    public static void copySABS(Product from, Product to) {
        if (from.getSabsTariff() != null) {
            to.setSabsTariff(from.getSabsTariff());
        }
        if (from.getSabsTariff2() != null) {
            to.setSabsTariff2(from.getSabsTariff2());
        }
    }
}