diff --git a/src/main/java/de/avatic/taric/controller/TariffController.java b/src/main/java/de/avatic/taric/controller/TariffController.java index 3cd7cd0..791e78d 100644 --- a/src/main/java/de/avatic/taric/controller/TariffController.java +++ b/src/main/java/de/avatic/taric/controller/TariffController.java @@ -20,7 +20,7 @@ public class TariffController { @GetMapping("") public ResponseEntity getTariffRate(@RequestParam String hsCode, @RequestParam String countryCode) { - return tariffService.importTariffs( hsCode, countryCode).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + return tariffService.getCustomPref( hsCode, countryCode).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } } diff --git a/src/main/java/de/avatic/taric/error/ErrorController.java b/src/main/java/de/avatic/taric/error/ErrorController.java index 1c09da0..a8c138c 100644 --- a/src/main/java/de/avatic/taric/error/ErrorController.java +++ b/src/main/java/de/avatic/taric/error/ErrorController.java @@ -19,4 +19,13 @@ public class ErrorController { dto.setMessage("Invalid argument"); return new ResponseEntity<>(dto, HttpStatus.BAD_REQUEST); } + + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler + public ResponseEntity handleException(InternalError ex) { + var dto = new ErrorDTO(); + dto.setField(ex.getArg()); + dto.setMessage("Internal error"); + return new ResponseEntity<>(dto, HttpStatus.INTERNAL_SERVER_ERROR); + } } diff --git a/src/main/java/de/avatic/taric/error/InternalError.java b/src/main/java/de/avatic/taric/error/InternalError.java new file mode 100644 index 0000000..a3fae23 --- /dev/null +++ b/src/main/java/de/avatic/taric/error/InternalError.java @@ -0,0 +1,14 @@ +package de.avatic.taric.error; + +import lombok.Getter; + +@Getter +public class InternalError extends RuntimeException { + + private final String arg; + + public InternalError(String arg) { + super(); + this.arg = arg; + } +} diff --git a/src/main/java/de/avatic/taric/repository/MeasureRepository.java b/src/main/java/de/avatic/taric/repository/MeasureRepository.java index cf13101..6665902 100644 --- a/src/main/java/de/avatic/taric/repository/MeasureRepository.java +++ b/src/main/java/de/avatic/taric/repository/MeasureRepository.java @@ -1,6 +1,7 @@ package de.avatic.taric.repository; import java.time.LocalDate; +import java.util.Optional; import org.springframework.data.jdbc.repository.query.Query; import org.springframework.data.repository.CrudRepository; @@ -11,5 +12,5 @@ import de.avatic.taric.model.Measure; public interface MeasureRepository extends CrudRepository { - + Optional getMeasureByShortDesc(String pref); } diff --git a/src/main/java/de/avatic/taric/service/TariffService.java b/src/main/java/de/avatic/taric/service/TariffService.java index 10f42d1..b45ba49 100644 --- a/src/main/java/de/avatic/taric/service/TariffService.java +++ b/src/main/java/de/avatic/taric/service/TariffService.java @@ -2,16 +2,13 @@ package de.avatic.taric.service; import de.avatic.taric.error.ArgumentException; -import de.avatic.taric.model.AppliedMeasure; -import de.avatic.taric.model.Geo; -import de.avatic.taric.model.GeoGroup; -import de.avatic.taric.model.Nomenclature; +import de.avatic.taric.model.*; import de.avatic.taric.repository.ImportRepository; +import de.avatic.taric.repository.MeasureRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.*; -import java.util.stream.Collectors; @Service @Slf4j @@ -20,14 +17,16 @@ public class TariffService { private final GeoService geoService; private final NomenclatureService nomenclatureService; private final ImportRepository importRepository; + private final MeasureRepository measureRepository; - public TariffService(GeoService geoService, NomenclatureService nomenclatureService, ImportRepository importRepository) { + public TariffService(GeoService geoService, NomenclatureService nomenclatureService, ImportRepository importRepository, MeasureRepository measureRepository) { this.geoService = geoService; this.nomenclatureService = nomenclatureService; this.importRepository = importRepository; + this.measureRepository = measureRepository; } - public Optional importTariffs(String hsCode, String countryCode) { + public Optional getCustomPref(String hsCode, String countryCode) { var geoGroups = geoService.getGeoGroupByCountryCode(countryCode); var geo = geoService.getGeo(countryCode); @@ -35,15 +34,19 @@ public class TariffService { var nomenclature = nomenclatureService.getNomenclature(hsCode); var cascade = nomenclatureService.getNomenclatureCascade(hsCode); + var customAppl = measureRepository.getMeasureByShortDesc("APPL"); + if (nomenclature.isEmpty() || !nomenclature.get().getIsLeaf()) throw new ArgumentException("hsCode"); if (geo.isEmpty()) throw new ArgumentException("countryCode"); + if (customAppl.isEmpty()) throw new InternalError("APPL not found"); + for (Nomenclature n : cascade) { var applMeasures = findAppliedMeasureByGeo(n, geo.get()); if (!applMeasures.isEmpty()) { - var tariff = findTariff(applMeasures); + var tariff = findTariff(applMeasures, customAppl.get()); if (tariff.isPresent()) return tariff; @@ -56,7 +59,7 @@ public class TariffService { var applMeasures = findAppliedMeasureByGeoGroup(n, geo.get(), geoGroups); if (!applMeasures.isEmpty()) { - var tariff = findTariff(applMeasures); + var tariff = findTariff(applMeasures, customAppl.get()); if (tariff.isPresent()) return tariff; @@ -67,14 +70,18 @@ public class TariffService { } - private Optional findTariff(Collection measures) { + private Optional findTariff(Collection measures, Measure appl) { - List percentages = measures.stream().map(AppliedMeasure::getAmount) - .filter(str -> str != null && str.trim().matches("\\d+\\.\\d+\\s*%")) - .map(str -> Double.parseDouble(str.trim().replace("%", "").trim())/100) + var code = appl.getMeasureCode(); + + + List filteredMeasures = measures.stream() + .filter(meas -> meas.getAmount() != null && meas.getAmount().trim().matches("\\d+\\.\\d+\\s*%")) .toList(); - if (!percentages.isEmpty()) return Optional.of(percentages.getFirst()); + AppliedMeasure customTariff = filteredMeasures.stream().filter(meas -> meas.getMeasure().getId().equals(appl.getId())).findAny().orElse(filteredMeasures.isEmpty() ? null : filteredMeasures.getFirst()); + + if (!filteredMeasures.isEmpty()) return Optional.of(customTariff).map(meas -> Double.parseDouble(meas.getAmount().trim().replace("%", "").trim())/100); return Optional.empty(); }