Tony CHEMIT pushed to branch develop-9.0.x at ultreiaio / ird-observe Commits: 4666462e by Tony Chemit at 2023-03-07T14:25:26+01:00 update pom - - - - - b18fcd29 by Tony Chemit at 2023-03-07T15:21:54+01:00 Revue du code de l'action de consolidation (Marché local) - - - - - 6 changed files: - core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/AtomicConsolidateAction.java - + core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateActions.java - core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateContext.java - core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateEngine.java - core/services/test/src/main/java/fr/ird/observe/services/service/data/ps/ConsolidateDataServiceFixtures.java - pom.xml Changes: ===================================== core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/AtomicConsolidateAction.java ===================================== @@ -53,17 +53,26 @@ public interface AtomicConsolidateAction<C extends ConsolidateContext<E>, E exte default boolean execute(C context, E data) { if (test(context, data)) { accept(context, data); - String fieldName = fieldName(); - context.getModification(fieldName).ifPresent(modification -> log.info(String.format("[%s] property '%s.%s' has changed [old value: %s - new value: %s] (by action %s)", - data.getTopiaId(), - context.dataType().getSimpleName(), - fieldName, - modification.getOldValue(), - modification.getNewValue(), - this))); + logModification(context, data); return true; } return false; } + + default void logModification(C context, E data) { + String fieldName = fieldName(); + logModification(context, data, fieldName); + } + + default void logModification(C context, E data, String fieldName) { + context.getModification(fieldName).ifPresent(modification -> log.info(String.format("[%s] property '%s.%s' has changed [old value: %s - new value: %s] (by action %s)", + data.getTopiaId(), + context.dataType().getSimpleName(), + fieldName, + modification.getOldValue(), + modification.getNewValue(), + this))); + } + } ===================================== core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateActions.java ===================================== @@ -0,0 +1,140 @@ +package fr.ird.observe.consolidation.data.ps.localmarket; + +/*- + * #%L + * ObServe Core :: API :: Dto Consolidation + * %% + * Copyright (C) 2008 - 2023 IRD, Ultreia.io + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import fr.ird.observe.consolidation.AtomicConsolidateAction; +import fr.ird.observe.dto.ProtectedIdsPs; +import fr.ird.observe.dto.data.ps.localmarket.BatchDto; +import fr.ird.observe.dto.data.ps.localmarket.BatchWeightComputedValueSource; +import fr.ird.observe.dto.referential.common.SpeciesReference; +import fr.ird.observe.dto.referential.ps.localmarket.BatchWeightTypeReference; +import io.ultreia.java4all.lang.Numbers; + +import java.util.Date; +import java.util.Optional; + +/** + * Created on 24/02/2023. + * + * @author Tony Chemit - dev@tchemit.fr + * @since 9.0.26 + */ +public enum BatchConsolidateActions implements AtomicConsolidateAction<BatchConsolidateContext, BatchDto> { + + + ComputeWeightFromSpeciesMeanWeight() { + @Override + public boolean test(BatchConsolidateContext context, BatchDto dto) { + if (dto.getCount() == null) { + // this method requires count + return false; + } + BatchWeightTypeReference batchWeightType = dto.getPackaging().getBatchWeightType(); + return batchWeightType != null && ProtectedIdsPs.PS_LOCAL_MARKET_BATCH_WEIGHT_TYPE_SINGLE_SPECIES_INDIVIDUAL.equals(batchWeightType.getId()); + } + + @Override + public void accept(BatchConsolidateContext context, BatchDto dto) { + + // espèce unité + // P = poids moyen de l'espèce * batch.count / 1000 + // ocean = océan de débarquement de la marée = ps_common.trip.ocean + //lengthweightparameter.startdate <= trip.enddate <= lengthweightparameter.enddate + //sex = 0 - indéterminé (fr.ird.referential.common.Sex#1239832686121#0.0) + //sizemeasuretype = type de mesure par défaut de l'espèce = common.species.sizemeasuretype + // Si aucun poids moyen n'est trouvé P = NULL. + + SpeciesReference species = dto.getSpecies(); + String sizeMeasureTypeId = species.getSizeMeasureTypeId(); + BatchConsolidateRequest request = context.getRequest(); + String oceanId = request.getOceanId(); + Date date = request.getDate(); + boolean failIfLengthWeightParameterNotFound = request.isFailIfLengthWeightParameterNotFound(); + Optional<Float> optionalMeanWeight = context.getRtpMeanWeightFinder().get(species.getId(), + "fr.ird.referential.common.Sex#1239832686121#0.0", + oceanId, + sizeMeasureTypeId, + date, + failIfLengthWeightParameterNotFound, + context::registerLengthWeightParameterNotFound); + if (optionalMeanWeight.isPresent()) { + Float meanWeight = optionalMeanWeight.get(); + dto.setWeight(Numbers.roundFourDigits(meanWeight * dto.getCount() / 1000)); + dto.setWeightComputedSource(BatchWeightComputedValueSource.fromSpeciesMeanWeight); + } else { + dto.setWeight(null); + dto.setWeightComputedSource(null); + } + } + + }, + ComputeWeightFromPackagingMeanWeight() { + @Override + public boolean test(BatchConsolidateContext context, BatchDto dto) { + if (dto.getCount() == null) { + // this method requires count + return false; + } + BatchWeightTypeReference batchWeightType = dto.getPackaging().getBatchWeightType(); + return batchWeightType != null && ProtectedIdsPs.PS_LOCAL_MARKET_BATCH_WEIGHT_TYPE_PACKAGING_NOT_WEIGHTED.equals(batchWeightType.getId()); + } + + @Override + public void accept(BatchConsolidateContext context, BatchDto dto) { + // Paquet/Conditionnement non pesé + // P = batch.packaging.meanWeight * batch.count (ou null si pas de packaging.meanWeight) + Float meanWeight = dto.getPackaging().getMeanWeight(); + if (meanWeight == null) { + dto.setWeight(null); + dto.setWeightComputedSource(null); + } else { + dto.setWeight(Numbers.roundFourDigits(dto.getCount() * meanWeight)); + dto.setWeightComputedSource(BatchWeightComputedValueSource.fromPackagingMeanWeight); + } + } + }, + ResetWeightValues() { + @Override + public boolean test(BatchConsolidateContext context, BatchDto dto) { + return true; + } + + @Override + public void accept(BatchConsolidateContext context, BatchDto dto) { + dto.setWeight(null); + dto.setWeightComputedSource(null); + } + }; + + @Override + public String fieldName() { + return BatchDto.PROPERTY_WEIGHT; + } + + + @Override + public void logModification(BatchConsolidateContext context, BatchDto data) { + AtomicConsolidateAction.super.logModification(context, data); + AtomicConsolidateAction.super.logModification(context, data, BatchDto.PROPERTY_WEIGHT_COMPUTED_SOURCE); + } +} ===================================== core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateContext.java ===================================== @@ -22,10 +22,11 @@ package fr.ird.observe.consolidation.data.ps.localmarket; * #L% */ -import fr.ird.observe.decoration.DecoratorService; +import fr.ird.observe.consolidation.ConsolidateContext; import fr.ird.observe.dto.ToolkitIdModifications; import fr.ird.observe.dto.data.ps.localmarket.BatchDto; import io.ultreia.java4all.bean.monitor.JavaBeanMonitor; +import io.ultreia.java4all.decoration.Decorator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -39,24 +40,36 @@ import java.util.Set; * @author Tony Chemit - dev@tchemit.fr * @since 9.0.26 */ -public class BatchConsolidateContext implements AutoCloseable { +public class BatchConsolidateContext implements ConsolidateContext<BatchDto> { private static final Logger log = LogManager.getLogger(BatchConsolidateContext.class); + private final GetOptionalRtpMeanWeight rtpMeanWeightFinder; private final JavaBeanMonitor monitor; + private final Decorator decorator; + private BatchConsolidateRequest request; + private BatchDto batch; private String lengthWeightParameterNotFound; - public BatchConsolidateContext(JavaBeanMonitor monitor, BatchDto bean) { + public BatchConsolidateContext(GetOptionalRtpMeanWeight rtpMeanWeightFinder, JavaBeanMonitor monitor, Decorator decorator) { + this.rtpMeanWeightFinder = rtpMeanWeightFinder; this.monitor = Objects.requireNonNull(monitor); - this.monitor.setBean(Objects.requireNonNull(bean)); + this.decorator = decorator; } - public Optional<ToolkitIdModifications> build(DecoratorService decoratorService) { - BatchDto bean = (BatchDto) monitor.getBean(); + public void watch(BatchConsolidateRequest request) { + this.request = Objects.requireNonNull(request); + this.batch = Objects.requireNonNull(request.getBatch()); + monitor.setBean(batch); + } + + public Optional<ToolkitIdModifications> build() { if (monitor.wasModified()) { - decoratorService.installDecorator(bean); - return monitor.toModifications(modifications -> new ToolkitIdModifications(bean, modifications, lengthWeightParameterNotFound == null ? null : Set.of(lengthWeightParameterNotFound))); + batch.registerDecorator(decorator); + Optional<ToolkitIdModifications> result = monitor.toModifications(modifications -> new ToolkitIdModifications(batch, modifications, lengthWeightParameterNotFound == null ? null : Set.of(lengthWeightParameterNotFound))); + result.ifPresent(m -> m.reset(batch)); + return result; } else if (lengthWeightParameterNotFound != null) { - return Optional.of(new ToolkitIdModifications(bean, Set.of(), Set.of(lengthWeightParameterNotFound))); + return Optional.of(new ToolkitIdModifications(batch, Set.of(), Set.of(lengthWeightParameterNotFound))); } return Optional.empty(); } @@ -68,8 +81,27 @@ public class BatchConsolidateContext implements AutoCloseable { } } - @Override - public void close() { + public BatchConsolidateRequest getRequest() { + return request; + } + + public GetOptionalRtpMeanWeight getRtpMeanWeightFinder() { + return rtpMeanWeightFinder; + } + + public void clear() { + this.batch = null; + this.request = null; monitor.setBean(null); } + + @Override + public JavaBeanMonitor monitor() { + return monitor; + } + + @Override + public Class<BatchDto> dataType() { + return BatchDto.class; + } } ===================================== core/api/dto-consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/localmarket/BatchConsolidateEngine.java ===================================== @@ -23,19 +23,12 @@ package fr.ird.observe.consolidation.data.ps.localmarket; */ import fr.ird.observe.decoration.DecoratorService; -import fr.ird.observe.dto.ProtectedIdsPs; import fr.ird.observe.dto.ToolkitIdModifications; import fr.ird.observe.dto.data.ps.localmarket.BatchDto; -import fr.ird.observe.dto.data.ps.localmarket.BatchWeightComputedValueSource; -import fr.ird.observe.dto.referential.common.SpeciesReference; -import fr.ird.observe.dto.referential.ps.localmarket.BatchWeightTypeReference; import io.ultreia.java4all.bean.monitor.JavaBeanMonitor; -import io.ultreia.java4all.lang.Numbers; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.Date; -import java.util.Objects; import java.util.Optional; /** @@ -48,98 +41,41 @@ public class BatchConsolidateEngine { private static final Logger log = LogManager.getLogger(BatchConsolidateEngine.class); - private final GetOptionalRtpMeanWeight rtpMeanWeightFinder; - private final DecoratorService decoratorService; - private final JavaBeanMonitor monitor; + private final BatchConsolidateContext context; public BatchConsolidateEngine(GetOptionalRtpMeanWeight rtpMeanWeightFinder, DecoratorService decoratorService) { - this.rtpMeanWeightFinder = Objects.requireNonNull(rtpMeanWeightFinder); - this.decoratorService = Objects.requireNonNull(decoratorService); - this.monitor = new JavaBeanMonitor(BatchDto.PROPERTY_WEIGHT, BatchDto.PROPERTY_WEIGHT_COMPUTED_SOURCE); + JavaBeanMonitor monitor = new JavaBeanMonitor(BatchDto.PROPERTY_WEIGHT, BatchDto.PROPERTY_WEIGHT_COMPUTED_SOURCE); + this.context = new BatchConsolidateContext(rtpMeanWeightFinder, monitor, decoratorService.getDecoratorByType(BatchDto.class)); } public Optional<ToolkitIdModifications> consolidate(BatchConsolidateRequest request) { BatchDto dto = request.getBatch(); - try (BatchConsolidateContext context = new BatchConsolidateContext(monitor, dto)) { - consolidateWeight(context, - dto, - request.getOceanId(), - request.getDate(), - request.isFailIfLengthWeightParameterNotFound()); - Optional<ToolkitIdModifications> result = context.build(decoratorService); + + // the only thing we can consolidate is the batch.weight + if (dto.getWeight() != null && dto.getWeightComputedSource() == null) { + // the value was set by user, do not change any thing + return Optional.empty(); + } + context.watch(request); + + try { + if (!BatchConsolidateActions.ComputeWeightFromSpeciesMeanWeight.execute(context, dto)) { + if (!BatchConsolidateActions.ComputeWeightFromPackagingMeanWeight.execute(context, dto)) { + BatchConsolidateActions.ResetWeightValues.execute(context, dto); + } + } + Optional<ToolkitIdModifications> result = context.build(); result.ifPresent(r -> { if (r.withModifications()) { log.info(String.format("Found some modifications on batch: %s", request.getBatch().getId())); } if (r.withWarnings()) { - log.info(String.format("Found some warnings on batch: %s", request.getBatch().getId())); + log.warn(String.format("Found some warnings on batch: %s", request.getBatch().getId())); } }); return result; - } - } - - private void consolidateWeight(BatchConsolidateContext context, - BatchDto dto, - String oceanId, - Date date, - boolean failIfLengthWeightParameterNotFound) { - - Float weight = dto.getWeight(); - if (weight != null && dto.getWeightComputedSource() == null) { - // the value was set by user, do not change any thing - return; - } - // always reset computed source state - dto.setWeightComputedSource(null); - // always reset weight since it was computed - dto.setWeight(null); - BatchWeightTypeReference batchWeightType = dto.getPackaging().getBatchWeightType(); - Integer count = dto.getCount(); - switch (batchWeightType.getId()) { - case ProtectedIdsPs.PS_LOCAL_MARKET_BATCH_WEIGHT_TYPE_WEIGHING: - // pesée poids direct - // P = batch.weight - // donc rien à faire - //FIXME Si il faut repousser le poids! - dto.setWeight(weight); - break; - case ProtectedIdsPs.PS_LOCAL_MARKET_BATCH_WEIGHT_TYPE_SINGLE_SPECIES_INDIVIDUAL: - // espèce unité - // P = poids moyen de l'espèce * batch.count / 1000 - // ocean = océan de débarquement de la marée = ps_common.trip.ocean - //lengthweightparameter.startdate <= trip.enddate <= lengthweightparameter.enddate - //sex = 0 - indéterminé (fr.ird.referential.common.Sex#1239832686121#0.0) - //sizemeasuretype = type de mesure par défaut de l'espèce = common.species.sizemeasuretype - // Si aucun poids moyen n'est trouvé P = NULL. - - if (count != null) { - SpeciesReference species = dto.getSpecies(); - String sizeMeasureTypeId = species.getSizeMeasureTypeId(); - Optional<Float> optionalMeanWeight = rtpMeanWeightFinder.get(species.getId(), - "fr.ird.referential.common.Sex#1239832686121#0.0", - oceanId, - sizeMeasureTypeId, - date, - failIfLengthWeightParameterNotFound, - context::registerLengthWeightParameterNotFound); - optionalMeanWeight.ifPresent(meanWeight -> { - dto.setWeight(Numbers.roundFourDigits(meanWeight * count / 1000)); - dto.setWeightComputedSource(BatchWeightComputedValueSource.fromSpeciesMeanWeight); - }); - } - break; - case ProtectedIdsPs.PS_LOCAL_MARKET_BATCH_WEIGHT_TYPE_PACKAGING_NOT_WEIGHTED: - // Paquet/Conditionnement non pesé - // P = batch.packaging.meanWeight * batch.count (ou null si pas de packaging.meanWeight) - if (count != null) { - Float meanWeight = dto.getPackaging().getMeanWeight(); - if (meanWeight != null) { - dto.setWeight(Numbers.roundFourDigits(count * meanWeight)); - dto.setWeightComputedSource(BatchWeightComputedValueSource.fromPackagingMeanWeight); - } - } - break; + } finally { + context.clear(); } } } ===================================== core/services/test/src/main/java/fr/ird/observe/services/service/data/ps/ConsolidateDataServiceFixtures.java ===================================== @@ -32,10 +32,14 @@ import fr.ird.observe.consolidation.data.ps.observation.ActivityConsolidateResul import fr.ird.observe.decoration.DecoratorService; import fr.ird.observe.dto.ToolkitIdModifications; import fr.ird.observe.dto.data.ps.localmarket.BatchDto; +import fr.ird.observe.dto.data.ps.localmarket.BatchWeightComputedValueSource; import fr.ird.observe.dto.data.ps.localmarket.TripBatchDto; import fr.ird.observe.dto.form.Form; import fr.ird.observe.dto.referential.ReferentialLocale; +import fr.ird.observe.dto.referential.common.SpeciesReference; +import fr.ird.observe.dto.referential.ps.localmarket.PackagingReference; import fr.ird.observe.services.ObserveServicesProvider; +import io.ultreia.java4all.util.Dates; import org.junit.Assert; import java.net.URL; @@ -126,11 +130,75 @@ public class ConsolidateDataServiceFixtures extends GeneratedConsolidateDataServ List<BatchDto> children = form.getObject().getChildren(); Assert.assertNotNull(children); Assert.assertEquals(2, children.size()); - request.setOceanId("fr.ird.referential.common.Ocean#1239832686151#0.17595105505051245"); + request.setOceanId("fr.ird.referential.common.Ocean#1239832686152#0.8325731048817705"); request.setBatch(children.get(0)); ToolkitIdModifications actual = service.consolidateLocalmarketBatch(request); + //FIXME:Test Get a real dto Assert.assertNull(actual); + + { + + BatchDto createDto = new BatchDto(); + createDto.setId("fr.ird.data.ps.localmarket.Batch#1617103690104#0.1"); + createDto.setCount(5); + createDto.setPackaging(servicesProvider.getReferenceService().loadReferential(PackagingReference.class, "fr.ird.referential.ps.localmarket.Packaging#1464000000000#001")); + createDto.setSpecies(servicesProvider.getReferenceService().loadReferential(SpeciesReference.class, "fr.ird.referential.common.Species#1560863653582#0.22356459159799613")); + createDto.setDate(Dates.createDate(30, 3, 2021)); + request.setBatch(createDto); + request.setDate(createDto.getDate()); + + actual = service.consolidateLocalmarketBatch(request); + + Assert.assertNotNull(actual); + } + { + + BatchDto createDto = new BatchDto(); + createDto.setId("fr.ird.data.ps.localmarket.Batch#1617103690104#0.2"); + createDto.setCount(5); + createDto.setPackaging(servicesProvider.getReferenceService().loadReferential(PackagingReference.class, "fr.ird.referential.ps.localmarket.Packaging#1464000000000#039")); + createDto.setSpecies(servicesProvider.getReferenceService().loadReferential(SpeciesReference.class, "fr.ird.referential.common.Species#1560863653582#0.22356459159799613")); + createDto.setDate(Dates.createDate(30, 3, 2021)); + request.setBatch(createDto); + request.setDate(createDto.getDate()); + + actual = service.consolidateLocalmarketBatch(request); + + Assert.assertNotNull(actual); + } + { + + BatchDto createDto = new BatchDto(); + createDto.setId("fr.ird.data.ps.localmarket.Batch#1617103690104#0.3"); + createDto.setWeightComputedSource(BatchWeightComputedValueSource.fromPackagingMeanWeight); + createDto.setPackaging(servicesProvider.getReferenceService().loadReferential(PackagingReference.class, "fr.ird.referential.ps.localmarket.Packaging#1464000000000#022")); + createDto.setSpecies(servicesProvider.getReferenceService().loadReferential(SpeciesReference.class, "fr.ird.referential.common.Species#1560863653582#0.22356459159799613")); + createDto.setDate(Dates.createDate(30, 3, 2021)); + request.setBatch(createDto); + request.setDate(createDto.getDate()); + + actual = service.consolidateLocalmarketBatch(request); + + Assert.assertNotNull(actual); + } + + { + + BatchDto createDto = new BatchDto(); + createDto.setId("fr.ird.data.ps.localmarket.Batch#1617103690104#0.4"); + createDto.setWeightComputedSource(BatchWeightComputedValueSource.fromSpeciesMeanWeight); + createDto.setPackaging(servicesProvider.getReferenceService().loadReferential(PackagingReference.class, "fr.ird.referential.ps.localmarket.Packaging#1464000000000#022")); + createDto.setSpecies(servicesProvider.getReferenceService().loadReferential(SpeciesReference.class, "fr.ird.referential.common.Species#1560863653582#0.22356459159799613")); + createDto.setDate(Dates.createDate(30, 3, 2021)); + createDto.setWeight(100.0f); + request.setBatch(createDto); + request.setDate(createDto.getDate()); + + actual = service.consolidateLocalmarketBatch(request); + + Assert.assertNotNull(actual); + } } @Override ===================================== pom.xml ===================================== @@ -23,7 +23,7 @@ <parent> <groupId>io.ultreia.maven</groupId> <artifactId>pom</artifactId> - <version>2023.9</version> + <version>2023.10</version> </parent> <groupId>fr.ird.observe</groupId> <artifactId>ird-observe</artifactId> View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/17b40e5fbe56c2bedb1e8036b... -- View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/-/compare/17b40e5fbe56c2bedb1e8036b... You're receiving this email because of your account on gitlab.com.
participants (1)
-
Tony CHEMIT (@tchemit)