Schedule1Part7Helper.java

package com.tradecloud.dto.duties;

import com.tradecloud.domain.duties.DutySchedule;
import com.tradecloud.domain.duties.DutyUnit;
import com.tradecloud.domain.duties.Schedule1Part7;
import com.tradecloud.tariffing.domain.BaseTariff;
import com.tradecloud.tariffing.domain.SugarTax;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Pattern;

public class Schedule1Part7Helper {

    public static void populateSchedule1Part7(DutySchedule dutySchedule, BaseTariff baseTariff) {

        Schedule1Part7 schedule1Part7 = (Schedule1Part7) dutySchedule;
        setSchedule1Part7Duties(schedule1Part7, baseTariff);
    }

    private static void setSchedule1Part7Duties(Schedule1Part7 schedule1Part7, BaseTariff baseTariff) {

        if (baseTariff instanceof SugarTax) {
            SugarTax sugarTax = (SugarTax) baseTariff;
            String dutyDescription = sugarTax.getRateOfDuty().toLowerCase();

            // 2,21C/GRAM OF THE SUGAR CONTENT THAT EXCEEDS 4G/100ML
            String centPerContentExceed = "^\\d+\\,\\d+c\\/gram\\.*.*exceeds\\s\\d+g\\/\\d+ml$";
            if (Pattern.matches(centPerContentExceed, dutyDescription)) {
                String centValue = dutyDescription.split("/")[0];

                centValue = centValue.replace("c", "");
                schedule1Part7.getDutyRate().getOtherDuty1().setValue(
                        new BigDecimal(TariffGeneralHelper.stringToDouble(centValue)).setScale(5, RoundingMode.HALF_DOWN));
                schedule1Part7.getDutyRate().getOtherDuty1().setUnit(DutyUnit.MASS_G);

                String gramPerMl = dutyDescription.split("exceeds")[1];
                String gram = gramPerMl.split("/")[0];
                String ml = gramPerMl.split("/")[1];

                BigDecimal gramValue = new BigDecimal(gram.trim().replaceAll("g", ""));
                BigDecimal mlValue = new BigDecimal(ml.trim().replaceAll("ml", ""));

                schedule1Part7.getDutyRate().getOtherDuty2().setAdditionalValue(gramValue);
                schedule1Part7.getDutyRate().getOtherDuty3().setAdditionalValue(mlValue);
            }

            schedule1Part7.setCalculationMethod(TariffGeneralHelper.getCalculationMethod(dutyDescription));
        }
    }
}