TariffGeneralHelper.java
package com.tradecloud.dto.duties;
import com.tradecloud.domain.duties.*;
import com.tradecloud.domain.item.ItemFieldCopyHelper;
import com.tradecloud.tariffing.domain.BaseTariff;
import com.tradecloud.tariffing.domain.Duty;
import com.tradecloud.tariffing.domain.Tariff;
import org.apache.log4j.Logger;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;
public class TariffGeneralHelper {
private final static Logger log = Logger.getLogger(TariffGeneralHelper.class);
public static void setBaseTariffCode(DutySchedule dutySchedule, BaseTariff baseTariff) {
if (baseTariff != null) {
log.info("Setting tariff code. Duty schedule class=" + dutySchedule.getClass().getSimpleName() + ". Tariff code=" + baseTariff.getCode());
TariffHeading tariffHeading = dutySchedule.getTariffHeading();
if (baseTariff.getControlDigit() != null && !baseTariff.getControlDigit().trim().isEmpty()) {
tariffHeading.setParts(baseTariff.getCode() + "/" + baseTariff.getControlDigit());
} else {
tariffHeading.setParts(baseTariff.getCode());
}
setTariffValid(dutySchedule);
}
}
public static void setTariffValid(DutySchedule dutySchedule) {
ItemFieldCopyHelper.determineTariffHeadingValidationStatus(dutySchedule);
if (!dutySchedule.isValidationDisabled()) {
dutySchedule.setValid(true);
}
}
public static double getAmount(String dutyDescription) {
dutyDescription = dutyDescription.toLowerCase();
String aaRandPerLiter = "r?\\d+,?.?\\d+c?/li\\saa";
if (dutyDescription.matches(aaRandPerLiter)) {
String value = dutyDescription.split("/li\\saa")[0].replace("r", "");
return stringToDouble(value);
} else if (dutyDescription.contains("c/li")) {
String cents = dutyDescription.split("c")[0];
return stringToDouble(cents) / 100;
} else if (dutyDescription.contains("c/kg")) {
String cents = dutyDescription.split("c")[0];
return stringToDouble(cents) / 100;
}
return 0;
}
public static double stringToDouble(String beforeMax) {
beforeMax = beforeMax.toLowerCase().replaceAll("free", "0");
return Double.parseDouble((beforeMax.split("\\.*c/\\S*")[0].trim()).replace(",", ".").replace("%", "").replaceAll(" ", ""));
}
public static DutyUnit getDutyUnit(String dutyDescription, DutyUnit defaultDutyUnit) {
dutyDescription = dutyDescription.toLowerCase();
if (dutyDescription.contains(("li aa"))) {
return DutyUnit.ABSOLUTE_ALCOHOL;
} else if (dutyDescription.contains(("li"))) {
return DutyUnit.VOLUME_L;
} else if (dutyDescription.equals(("l"))) {
return DutyUnit.VOLUME_L;
} else if (dutyDescription.contains("kg")) {
return DutyUnit.MASS_KG;
}
return defaultDutyUnit;
}
public static DutyUnit getDutyUnit(String dutyDescription) {
return getDutyUnit(dutyDescription, null);
}
// Turn 34,56% into 34.56
public static BigDecimal extractPercentage(String percentageStr) {
return new BigDecimal(percentageStr.replaceAll(",", ".").replaceAll("%", ""));
}
public static DutyCalculationMethod getCalculationMethod(String dutyDescription) {
dutyDescription = dutyDescription.toLowerCase();
String percent = "\\d+%$|\\d+%$|\\d+,\\d+%$";
String maximumOf = "with a maximum of";
String centPer = "\\S*\\.*c/\\S*\\s*\\S*";
String centPerUnit = "\\S*\\.*c/u\\S*\\s*\\S*";
String centPerKg = "\\d+c/kg";
String centPerLiterAA = "\\d+,?.?\\d+c/laa";
String randPerKgNet = "^r\\d+\\.\\d+/kg\\s+net$";
String centPerContentExceed = "^\\d+\\,\\d+c\\/gram\\.*.*exceeds\\s\\d+g\\/\\d+ml$";
if (Pattern.matches(centPerKg, dutyDescription) || Pattern.matches(percent, dutyDescription) || dutyDescription.contains("free")
|| dutyDescription.matches(centPerLiterAA) || Pattern.matches(centPerUnit, dutyDescription)) {
return DutyCalculationMethod.SINGLE;
}
if (Pattern.matches(randPerKgNet, dutyDescription)) {
return DutyCalculationMethod.SINGLE;
}
if (Pattern.matches(centPer, dutyDescription) && !Pattern.matches(centPerUnit, dutyDescription) && !dutyDescription.matches(centPerLiterAA)) {
return DutyCalculationMethod.SINGLE;
}
if (dutyDescription.contains(maximumOf) && !dutyDescription.contains("less")) {
return DutyCalculationMethod.WITH_MAX_OF;
}
if (dutyDescription.contains("or") && dutyDescription.contains("less")) {
return DutyCalculationMethod.OR_LESS;
}
if (dutyDescription.contains("or") && !dutyDescription.contains("less")) {
return DutyCalculationMethod.OR;
}
if (dutyDescription.contains(maximumOf) && dutyDescription.contains("less")) {
return DutyCalculationMethod.LESS_WITH_MAX_OF;
}
if (dutyDescription.matches(centPerContentExceed)) {
return DutyCalculationMethod.CONTENT_THAT_EXCEEDS;
}
return DutyCalculationMethod.SINGLE;
}
public static List<Map.Entry<String, Duty>> getDutiesAsList(Tariff tariff) {
if (tariff != null) {
List<Map.Entry<String, Duty>> duties = new ArrayList<Map.Entry<String, Duty>>();
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.GENERAL.name(), tariff.getGeneralDuty()));
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.SADC.name(), tariff.getSadcDuty()));
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.EU.name(), tariff.getEuDuty()));
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.EFTA.name(), tariff.getEftaDuty()));
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.MERCOSUR.name(), tariff.getMercosurDuty()));
duties.add(new AbstractMap.SimpleEntry(TradeAgreement.AFCFTA.name(), tariff.getAfCFTADuty()));
return duties;
} else {
return Collections.EMPTY_LIST;
}
}
public static Map.Entry<String, Duty> getDutyEntry(TradeAgreement agreement, List<Map.Entry<String, Duty>> duties) {
Map.Entry<String, Duty> stringDutyEntry = null;
if (agreement != null) {
for (int i = 0; i < duties.size(); i++) {
stringDutyEntry = duties.get(i);
if (stringDutyEntry.getKey().equals(agreement.name())) {
return stringDutyEntry;
}
}
}
return null;
}
}