This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository observe. See http://git.codelutin.com/observe.git commit 1163ce4c15e14be824f5e42150c2891556e22e16 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Aug 28 12:27:59 2015 +0200 Mise en place de consolidation des données sur les marées Seine Reste à finaliser le contenu des résultats See #7488 --- .../ConsolidateActivitySeineDataResult.java | 59 ++ .../ConsolidateTripSeineDataRequest.java | 25 + .../ConsolidateTripSeineDataResult.java | 45 ++ .../consolidate/ConsolidationDataService.java | 21 + .../service/ConsolidationDataServiceTopia.java | 788 +++++++++++++++++++++ 5 files changed, 938 insertions(+) diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateActivitySeineDataResult.java b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateActivitySeineDataResult.java new file mode 100644 index 0000000..0298edd --- /dev/null +++ b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateActivitySeineDataResult.java @@ -0,0 +1,59 @@ +package fr.ird.observe.services.service.actions.consolidate; + +import java.util.Date; + +/** + * Pour retourner le résultat de la consolidation d'une activité de type seine. + * + * Created on 28/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ConsolidateActivitySeineDataResult { + + protected String schoolTypeChanged; + + public void registerLengthWeightParameterNotFound(String speciesId, String oceanId, String sexId, Date routeDate) { + + } + + public void registerTargetLengthLengthWasComputed(String targetLengthId, Float length) { + + } + + public void registerTargetLengthWeightWasComputed(String targetLengthId, Float weight) { + + } + + public void registerNonTargetLengthLengthWasComputed(String nonTargetLengthId, Float length) { + + } + + public void registerNonTargetLengthWeightWasComputed(String nonTargetLengthId, Float weight) { + + } + + public void registerNonTargetCatchMeanLengthWasComputed(String nonTargetCatchId, Float meanLength, String computeValueSource) { + + } + + public void registerNonTargetCatchMeanWeightWasComputed(String nonTargetCatchId, Float meanWeight, String computeValueSource) { + + } + + public void registerNonTargetCatchCatchWeightWasComputed(String nonTargetCatchId, Float catchWeight, String computeValueSource) { + + } + + public void registerNonTargetCatchTotalCountWasComputed(String nonTargetCatchId, Integer totalCount, String computeValueSource) { + + } + + public void setSchoolTypeChanged(String schoolTypeChanged) { + this.schoolTypeChanged = schoolTypeChanged; + } + + public String getSchoolTypeChanged() { + return schoolTypeChanged; + } +} diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataRequest.java b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataRequest.java new file mode 100644 index 0000000..5205e33 --- /dev/null +++ b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataRequest.java @@ -0,0 +1,25 @@ +package fr.ird.observe.services.service.actions.consolidate; + +import com.google.common.collect.ImmutableSet; + +/** + * Pour configurer l'opération de consolidation. + * + * Created on 28/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @see ConsolidationDataService#consolidateTripSeine(String) + */ +public class ConsolidateTripSeineDataRequest { + + protected ImmutableSet<String> tripSeineIds; + + public ImmutableSet<String> getTripSeineIds() { + return tripSeineIds; + } + + public void setTripSeineIds(ImmutableSet<String> tripSeineIds) { + this.tripSeineIds = tripSeineIds; + } + +} diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataResult.java b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataResult.java new file mode 100644 index 0000000..ff9c6a1 --- /dev/null +++ b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidateTripSeineDataResult.java @@ -0,0 +1,45 @@ +package fr.ird.observe.services.service.actions.consolidate; + +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Pour retourner le résultat de la consolidation d'une marée de type seine. + * + * Created on 28/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @see ConsolidationDataService#consolidateTripSeine(String) + */ +public class ConsolidateTripSeineDataResult { + + protected boolean noActivityFound; + + protected boolean tripWasModified; + + protected final Set<ConsolidateActivitySeineDataResult> activitySeineDataResults = new LinkedHashSet<>(); + + public boolean isNoActivityFound() { + return noActivityFound; + } + + public void setNoActivityFound(boolean noActivityFound) { + this.noActivityFound = noActivityFound; + } + + public boolean isTripWasModified() { + return tripWasModified; + } + + public void setTripWasModified(boolean tripWasModified) { + this.tripWasModified = tripWasModified; + } + + public void addActivityResult(ConsolidateActivitySeineDataResult activitySeineDataResult) { + activitySeineDataResults.add(activitySeineDataResult); + } + + public Set<ConsolidateActivitySeineDataResult> getActivitySeineDataResults() { + return activitySeineDataResults; + } +} diff --git a/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidationDataService.java b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidationDataService.java new file mode 100644 index 0000000..b76ead7 --- /dev/null +++ b/observe-services-api/src/main/java/fr/ird/observe/services/service/actions/consolidate/ConsolidationDataService.java @@ -0,0 +1,21 @@ +package fr.ird.observe.services.service.actions.consolidate; + +import com.google.common.collect.ImmutableSet; + +/** + * Le service pour effectuer les consolidations de données, i.e + * de pouvoir effectuer remplir certains champs à partir de champs observés. + * + * Created on 28/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public interface ConsolidationDataService { + + /** + * @param consolidateTripSeineDataRequest la demande de consolidation + * @return le résultat de la consolidation de données pour chaque marée. + */ + ImmutableSet<ConsolidateTripSeineDataResult> consolidateTripSeines(ConsolidateTripSeineDataRequest consolidateTripSeineDataRequest); + +} diff --git a/observe-services-topia/src/main/java/fr/ird/observe/services/service/ConsolidationDataServiceTopia.java b/observe-services-topia/src/main/java/fr/ird/observe/services/service/ConsolidationDataServiceTopia.java new file mode 100644 index 0000000..13ef1a9 --- /dev/null +++ b/observe-services-topia/src/main/java/fr/ird/observe/services/service/ConsolidationDataServiceTopia.java @@ -0,0 +1,788 @@ +package fr.ird.observe.services.service; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import fr.ird.observe.entities.LengthWeightComputable; +import fr.ird.observe.entities.constants.seine.NonTargetCatchComputedValueSource; +import fr.ird.observe.entities.constants.seine.SchoolType; +import fr.ird.observe.entities.referentiel.LengthWeightParameter; +import fr.ird.observe.entities.referentiel.LengthWeightParameterTopiaDao; +import fr.ird.observe.entities.referentiel.LengthWeightParameters; +import fr.ird.observe.entities.referentiel.Ocean; +import fr.ird.observe.entities.referentiel.Sex; +import fr.ird.observe.entities.referentiel.Species; +import fr.ird.observe.entities.seine.ActivitySeine; +import fr.ird.observe.entities.seine.NonTargetCatch; +import fr.ird.observe.entities.seine.NonTargetCatches; +import fr.ird.observe.entities.seine.NonTargetLength; +import fr.ird.observe.entities.seine.NonTargetSample; +import fr.ird.observe.entities.seine.Route; +import fr.ird.observe.entities.seine.SetSeine; +import fr.ird.observe.entities.seine.SetSeines; +import fr.ird.observe.entities.seine.TargetLength; +import fr.ird.observe.entities.seine.TargetSample; +import fr.ird.observe.entities.seine.TripSeine; +import fr.ird.observe.services.ObserveServiceTopia; +import fr.ird.observe.services.dto.seine.TripSeineDto; +import fr.ird.observe.services.service.actions.consolidate.ConsolidateActivitySeineDataResult; +import fr.ird.observe.services.service.actions.consolidate.ConsolidateTripSeineDataRequest; +import fr.ird.observe.services.service.actions.consolidate.ConsolidateTripSeineDataResult; +import fr.ird.observe.services.service.actions.consolidate.ConsolidationDataService; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.beans.BeanMonitor; + +import java.util.Collection; +import java.util.Date; +import java.util.Set; + +/** + * Created on 28/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ConsolidationDataServiceTopia extends ObserveServiceTopia implements ConsolidationDataService { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ConsolidationDataServiceTopia.class); + +// private static final String MESSAGE_FORMAT_3 = "[%1$s] %2$s calculé : %3$s (%4$s:%5$s, %6$s:%7$s)"; + + class ConsolidationActivitySeineDataContext { + + protected TripSeine tripSeine; + + protected Route route; + + protected ActivitySeine activity; + + protected LengthWeightParameterTopiaDao lengthWeightParameterTopiaDao; + + protected ConsolidateActivitySeineDataResult consolidateActivitySeineDataResult; + + BeanMonitor targetLengthMonitor; + + BeanMonitor nonTargetLengthMonitor; + + BeanMonitor nonTargetCatchMonitor; + + public Ocean getOcean() { + return tripSeine.getOcean(); + } + + public Date getRouteDate() { + return route.getDate(); + } + + public SetSeine getSetSeine() { + return activity.getSetSeine(); + } + + public LengthWeightParameter findLengthWeightParameter(Species species, Sex sex) { + Ocean ocean = getOcean(); + Date routeDate = getRouteDate(); + LengthWeightParameter lengthWeightParameter = LengthWeightParameters.findLengthWeightParameter(getTopiaPersistenceContext(), species, ocean, sex, routeDate); + if (lengthWeightParameter == null) { + + //FIXME On devrait retourner une erreur plutôt + consolidateActivitySeineDataResult.registerLengthWeightParameterNotFound(species.getTopiaId(), ocean.getTopiaId(), sex == null ? null : sex.getTopiaId(), routeDate); + + } + return lengthWeightParameter; + } + + public void setSchoolTypeChanged(SchoolType schoolTypeChanged) { + consolidateActivitySeineDataResult.setSchoolTypeChanged(schoolTypeChanged.name()); + } + + public void watchTargetLength(TargetLength targetLength) { + targetLengthMonitor.setBean(targetLength); + } + + public void watchNonTargetLenght(NonTargetLength nonTargetLength) { + nonTargetLengthMonitor.setBean(nonTargetLength); + } + + public void flushTargetLength() { + if (targetLengthMonitor.wasModified()) { + TargetLength targetLength = (TargetLength) targetLengthMonitor.getBean(); + Set<String> modifiedProperties = Sets.newHashSet(targetLengthMonitor.getModifiedProperties()); + if (modifiedProperties.contains(TargetLength.PROPERTY_LENGTH)) { + consolidateActivitySeineDataResult.registerTargetLengthLengthWasComputed(targetLength.getTopiaId(), targetLength.getLength()); + } + if (modifiedProperties.contains(TargetLength.PROPERTY_WEIGHT)) { + consolidateActivitySeineDataResult.registerTargetLengthWeightWasComputed(targetLength.getTopiaId(), targetLength.getWeight()); + } + } + targetLengthMonitor.setBean(null); + } + + public void flushNonTargetLength() { + if (nonTargetLengthMonitor.wasModified()) { + NonTargetLength nonTargetLength = (NonTargetLength) nonTargetLengthMonitor.getBean(); + Set<String> modifiedProperties = Sets.newHashSet(nonTargetLengthMonitor.getModifiedProperties()); + if (modifiedProperties.contains(NonTargetLength.PROPERTY_LENGTH)) { + consolidateActivitySeineDataResult.registerNonTargetLengthLengthWasComputed(nonTargetLength.getTopiaId(), nonTargetLength.getLength()); + } + if (modifiedProperties.contains(NonTargetLength.PROPERTY_WEIGHT)) { + consolidateActivitySeineDataResult.registerNonTargetLengthWeightWasComputed(nonTargetLength.getTopiaId(), nonTargetLength.getWeight()); + } + } + nonTargetLengthMonitor.setBean(null); + } + + public void watchNonTargetCatch(NonTargetCatch nonTargetCatch) { + nonTargetCatchMonitor.setBean(nonTargetCatch); + } + + public void flushNonTargetCatch() { + if (nonTargetCatchMonitor.wasModified()) { + NonTargetCatch nonTargetCatch = (NonTargetCatch) nonTargetCatchMonitor.getBean(); + Set<String> modifiedProperties = Sets.newHashSet(nonTargetCatchMonitor.getModifiedProperties()); + if (modifiedProperties.contains(NonTargetCatch.PROPERTY_MEAN_LENGTH)) { + consolidateActivitySeineDataResult.registerNonTargetCatchMeanLengthWasComputed(nonTargetCatch.getTopiaId(), nonTargetCatch.getMeanLength(), nonTargetCatch.getMeanLengthComputedSource().name()); + } + if (modifiedProperties.contains(NonTargetCatch.PROPERTY_MEAN_WEIGHT)) { + consolidateActivitySeineDataResult.registerNonTargetCatchMeanWeightWasComputed(nonTargetCatch.getTopiaId(), nonTargetCatch.getMeanWeight(), nonTargetCatch.getMeanWeightComputedSource().name()); + } + if (modifiedProperties.contains(NonTargetCatch.PROPERTY_CATCH_WEIGHT)) { + consolidateActivitySeineDataResult.registerNonTargetCatchCatchWeightWasComputed(nonTargetCatch.getTopiaId(), nonTargetCatch.getCatchWeight(), nonTargetCatch.getCatchWeightComputedSource().name()); + } + if (modifiedProperties.contains(NonTargetCatch.PROPERTY_TOTAL_COUNT)) { + consolidateActivitySeineDataResult.registerNonTargetCatchTotalCountWasComputed(nonTargetCatch.getTopiaId(), nonTargetCatch.getTotalCount(), nonTargetCatch.getTotalCountComputedSource().name()); + } + } + nonTargetCatchMonitor.setBean(null); + } + } + + @Override + public ImmutableSet<ConsolidateTripSeineDataResult> consolidateTripSeines(ConsolidateTripSeineDataRequest consolidateTripSeineDataRequest) { + + ImmutableSet.Builder<ConsolidateTripSeineDataResult> resultBuilder = new ImmutableSet.Builder<>(); + for (String tripSeineId : consolidateTripSeineDataRequest.getTripSeineIds()) { + ConsolidateTripSeineDataResult consolidateTripSeineDataResult = consolidateTripSeine(tripSeineId); + resultBuilder.add(consolidateTripSeineDataResult); + } + return resultBuilder.build(); + + } + + protected ConsolidateTripSeineDataResult consolidateTripSeine(String tripSeineId) { + + TripSeine tripSeine = loadEntity(TripSeineDto.class, TripSeine.class, tripSeineId); + + ConsolidateTripSeineDataResult computationResult = new ConsolidateTripSeineDataResult(); + + BeanMonitor targetLengthMonitor = new BeanMonitor( + TargetLength.PROPERTY_LENGTH, + TargetLength.PROPERTY_LENGTH_SOURCE, + TargetLength.PROPERTY_WEIGHT, + TargetLength.PROPERTY_WEIGHT_SOURCE); + + BeanMonitor nonTargetSampleMonitor = new BeanMonitor( + NonTargetLength.PROPERTY_LENGTH, + NonTargetLength.PROPERTY_LENGTH_SOURCE, + NonTargetLength.PROPERTY_WEIGHT, + NonTargetLength.PROPERTY_WEIGHT_SOURCE); + + BeanMonitor nonTargetCatchMonitor = new BeanMonitor( + NonTargetCatch.PROPERTY_MEAN_LENGTH, + NonTargetCatch.PROPERTY_MEAN_LENGTH_COMPUTED_SOURCE, + NonTargetCatch.PROPERTY_MEAN_WEIGHT, + NonTargetCatch.PROPERTY_MEAN_WEIGHT_COMPUTED_SOURCE, + NonTargetCatch.PROPERTY_CATCH_WEIGHT, + NonTargetCatch.PROPERTY_CATCH_WEIGHT_COMPUTED_SOURCE, + NonTargetCatch.PROPERTY_TOTAL_COUNT, + NonTargetCatch.PROPERTY_TOTAL_COUNT_COMPUTED_SOURCE + ); + + LengthWeightParameterTopiaDao lengthWeightParameterDao = getTopiaPersistenceContext().getLengthWeightParameterDao(); + + for (Route route : tripSeine.getRoute()) { + + for (ActivitySeine activitySeine : route.getActivitySeine()) { + + ConsolidateActivitySeineDataResult consolidateActivitySeineDataResult = new ConsolidateActivitySeineDataResult(); + ConsolidationActivitySeineDataContext activityContext = new ConsolidationActivitySeineDataContext(); + activityContext.tripSeine = tripSeine; + activityContext.route = route; + activityContext.activity = activitySeine; + activityContext.lengthWeightParameterTopiaDao = lengthWeightParameterDao; + activityContext.consolidateActivitySeineDataResult = consolidateActivitySeineDataResult; + activityContext.targetLengthMonitor = targetLengthMonitor; + activityContext.nonTargetLengthMonitor = nonTargetSampleMonitor; + activityContext.nonTargetCatchMonitor = nonTargetCatchMonitor; + computationResult.addActivityResult(consolidateActivitySeineDataResult); + + consolidateActivitySeine(activityContext); + + } + + } + + // on devra mettre a jour en base la maree +// messager1.sendMessage(t("observe.message.consolidate.maree.need.update", +// mareeStr)); + + return computationResult; + } + + + protected void consolidateActivitySeine(ConsolidationActivitySeineDataContext activityContext) { + + ActivitySeine activity = activityContext.activity; + + if (log.isInfoEnabled()) { + log.info("Start consolidate activity: " + activity.getTopiaId()); + } + + SetSeine setSeine = activity.getSetSeine(); + SchoolType oldTypeBanc = setSeine.getSchoolType(); + SchoolType newTypeBanc = activity.getSchoolType(); + if (oldTypeBanc == null || oldTypeBanc != newTypeBanc) { + + // le type de banc a changé, on doit sauver l'activité + setSeine.setSchoolType(newTypeBanc); + activityContext.setSchoolTypeChanged(newTypeBanc); + + } + + if (!setSeine.isTargetSampleEmpty()) { + + // des echantillons thons trouves + for (TargetSample targetSample : setSeine.getTargetSample()) { + if (!targetSample.isTargetLengthEmpty()) { + for (TargetLength targetLength : targetSample.getTargetLength()) { + + activityContext.watchTargetLength(targetLength); + + updateLengthWeightAble(activityContext, + targetLength.getSpecies(), + null, /* pas de sexe precise */ + targetLength); + + activityContext.flushTargetLength(); + + } + } + } + } + + if (!setSeine.isNonTargetSampleEmpty()) { + + // des echantillons faunes trouves + for (NonTargetSample nonTargetSample : setSeine.getNonTargetSample()) { + + if (!nonTargetSample.isNonTargetLengthEmpty()) { + for (NonTargetLength nonTargetLength : nonTargetSample.getNonTargetLength()) { + + activityContext.watchNonTargetLenght(nonTargetLength); + + updateLengthWeightAble(activityContext, + nonTargetLength.getSpecies(), + nonTargetLength.getSex(), + nonTargetLength); + + activityContext.flushNonTargetLength(); + + } + } + } + } + + if (!setSeine.isNonTargetCatchEmpty()) { + + // des captures (ou rejets) faunes trouves + for (NonTargetCatch nonTargetCatch : setSeine.getNonTargetCatch()) { + + activityContext.watchNonTargetCatch(nonTargetCatch); + + // suppression de tous les champs précédemment calculés + + if (nonTargetCatch.isCatchWeightComputed()) { + nonTargetCatch.setCatchWeight(null); + nonTargetCatch.setCatchWeightComputedSource(null); + } + + if (nonTargetCatch.isTotalCountComputed()) { + nonTargetCatch.setTotalCount(null); + nonTargetCatch.setTotalCountComputedSource(null); + } + + if (nonTargetCatch.isMeanWeightComputed()) { + nonTargetCatch.setMeanWeight(null); + nonTargetCatch.setMeanWeightComputedSource(null); + } + + if (nonTargetCatch.isMeanLengthComputed()) { + nonTargetCatch.setMeanLength(null); + nonTargetCatch.setMeanLengthComputedSource(null); + } + + updateNonTargetCatch(activityContext, nonTargetCatch); + + activityContext.flushNonTargetCatch(); + + } + } + + } + + protected void updateNonTargetCatch(ConsolidationActivitySeineDataContext activityContext, NonTargetCatch nonTargetCatch) { + + Species species = nonTargetCatch.getSpecies(); + + // récupération du référentiel + LengthWeightParameter lengthWeightParameter = activityContext.findLengthWeightParameter(species, null /* pas de sexe spécifié*/); + + // -- Cas n°1 (calcul uniquement à partir des relations taille - poids) + updateNonTargetCatchByLengthWeightRelation(nonTargetCatch, lengthWeightParameter); + + if (NonTargetCatches.allNonTargetCatchDataFilled(nonTargetCatch)) { + + // tout est rempli, plus rien à faire + return; + } + + // répération des échantillon de cette espèce sur les calée + + Collection<NonTargetLength> nonTargetLengths = SetSeines.getNonTargetLengths(activityContext.getSetSeine(), species); + +// SetSeine setSeine = activityContext.getSetSeine(); +// +// if (!setSeine.isNonTargetSampleEmpty()) { +// nonTargetLengths = Collections2.filter(setSeine.getNonTargetSample().iterator().next().getNonTargetLength(), new Predicate<NonTargetLength>() { +// +// @Override +// public boolean apply(NonTargetLength input) { +// return species.equals(input.getSpecies()); +// } +// }); +// } + + if (nonTargetCatch.getCatchWeight() != null || nonTargetCatch.getTotalCount() != null) { + + // -- Cas n°2 (pas de taille / poids moyen mais au moins un des deux taille / poids) + computeNonTargetCatchMeanLength(nonTargetCatch, nonTargetLengths, lengthWeightParameter); + } + + if (NonTargetCatches.allNonTargetCatchDataFilled(nonTargetCatch)) { + + // tout est rempli, plus rien à faire + return; + } + + // -- Cas n°3 (pas de nombre estimé) + + if (nonTargetCatch.getTotalCount() == null) { + + computeNonTargetCatchNombreEstime(nonTargetCatch, nonTargetLengths, lengthWeightParameter); + + } + + if (NonTargetCatches.allNonTargetCatchDataFilled(nonTargetCatch)) { + + // tout est rempli, plus rien à faire + return; + } + + // -- Cas n°4 (pas de poids moyen, taille moyenne) + + computeNonTargetCatchMeanValues(nonTargetCatch, lengthWeightParameter); + } + + protected void updateNonTargetCatchByLengthWeightRelation(NonTargetCatch nonTargetCatch, + LengthWeightParameter lengthWeightParameter) { + + // calcul via le paramétrage taille - poids + updateLengthWeightAble(nonTargetCatch, lengthWeightParameter); + + // calcule l'un des trois champs poids estimé - nbEstime - poids moyen + updateNonTargetCatchPoidsEstimeNbEstimePoidsMoyen(nonTargetCatch); + + // on ressaye d'appliquer la relation taille - poids au cas où une des + // trois valeurs précédentes a été calculée, on pourrait peut-être + // ainsi en déduire via le paramétrage la taille moyenne + updateLengthWeightAble(nonTargetCatch, lengthWeightParameter); + + } + + protected void computeNonTargetCatchMeanLength(NonTargetCatch nonTargetCatch, + Collection<NonTargetLength> samples, + LengthWeightParameter lengthWeightParameter) { + + Float meanLength = nonTargetCatch.getMeanLength(); + + if (meanLength == null) { + + // on essaye de calculer la taille moyenne à partir des échantillons + + NonTargetCatchComputedValueSource computedSource = null; + + if (CollectionUtils.isNotEmpty(samples)) { + + // on calcul la taille moyenne à partir des échantillons + float totalLength = 0f; + int totalCount = 0; + for (NonTargetLength sample : samples) { + + Integer count = sample.getCount(); + Float length = sample.getLength(); + + if (count != null && length != null) { + totalCount += count; + totalLength += length * count; + } + } + + if (totalCount != 0) { + + meanLength = totalLength / totalCount; + + computedSource = NonTargetCatchComputedValueSource.FROM_SAMPLE; + } + } + + if (meanLength == null && lengthWeightParameter != null) { + + // on prend directement la valeur fournie par le référentiel + + meanLength = lengthWeightParameter.getMeanLength(); + computedSource = NonTargetCatchComputedValueSource.FROM_REFERENTIEL; + } + + if (meanLength != null) { + + // la taille moyenne a pu etre calculee, on la pousse alors + nonTargetCatch.setMeanLength(meanLength); + nonTargetCatch.setMeanLengthComputedSource(computedSource); + + } + + } + + if (meanLength != null) { + + // on peut aussi relancer la calcul du cas n°1 + updateNonTargetCatchByLengthWeightRelation(nonTargetCatch, lengthWeightParameter); + + } + + } + + protected void computeNonTargetCatchNombreEstime(NonTargetCatch nonTargetCatch, + Collection<NonTargetLength> samples, + LengthWeightParameter lengthWeightParameter) { + + + Integer totalCount = nonTargetCatch.getTotalCount(); + + if (totalCount == null) { + + if (CollectionUtils.isNotEmpty(samples)) { + + // on calcul la nombre d'individus à partir des échantillons + totalCount = 0; + for (NonTargetLength sample : samples) { + + Integer count = sample.getCount(); + + if (count != null) { + totalCount += count; + } + } + + if (totalCount != 0) { + + nonTargetCatch.setTotalCount(totalCount); + nonTargetCatch.setTotalCountComputedSource(NonTargetCatchComputedValueSource.FROM_SAMPLE); + + + } + } + + } + + if (nonTargetCatch.getMeanWeight() != null || nonTargetCatch.getMeanLength() != null) { + + // on peut aussi relancer la calcul du cas n°1 + updateNonTargetCatchByLengthWeightRelation(nonTargetCatch, lengthWeightParameter); + } + + } + + protected void computeNonTargetCatchMeanValues(NonTargetCatch nonTargetCatch, LengthWeightParameter lengthWeightParameter) { + + if (lengthWeightParameter != null) { + + if (nonTargetCatch.getMeanLength() == null) { + nonTargetCatch.setMeanLength(lengthWeightParameter.getMeanLength()); + nonTargetCatch.setMeanLengthComputedSource(NonTargetCatchComputedValueSource.FROM_REFERENTIEL); + } + + if (nonTargetCatch.getMeanWeight() == null) { + nonTargetCatch.setMeanWeight(lengthWeightParameter.getMeanWeight()); + nonTargetCatch.setMeanWeightComputedSource(NonTargetCatchComputedValueSource.FROM_REFERENTIEL); + } + + if (nonTargetCatch.getMeanWeight() != null || nonTargetCatch.getMeanLength() != null) { + + // on peut aussi relancer la calcul du cas n°1 + updateNonTargetCatchByLengthWeightRelation(nonTargetCatch, lengthWeightParameter); + } + + } + } + + protected void updateNonTargetCatchPoidsEstimeNbEstimePoidsMoyen(NonTargetCatch nonTargetCatch) { + + Float meanWeight = nonTargetCatch.getMeanWeight(); + Float catchWeight = nonTargetCatch.getCatchWeight(); + Integer totalCount = nonTargetCatch.getTotalCount(); + +// String entityLabel = getNonTargetCatcheLabel(); + + if (catchWeight == null && totalCount != null && meanWeight != null) { + + // calcul le weight poids à partir de nb estime et du poids moyen + catchWeight = meanWeight * (float) totalCount / 1000; + nonTargetCatch.setCatchWeight(catchWeight); + nonTargetCatch.setCatchWeightComputedSource(NonTargetCatchComputedValueSource.FROM_DATA); +// String message = String.format(MESSAGE_FORMAT_3, +// entityLabel, +// t("observe.common.catchWeight"), +// catchWeight, +// t("observe.common.meanWeight"), +// meanWeight, +// t("observe.common.totalCount"), +// totalCount); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + } + + if (totalCount == null && catchWeight != null && meanWeight != null) { + + // calcul le nb estime à partir du poids estime et du poids moyen + totalCount = (int) ((float) 1000 * catchWeight / meanWeight); + nonTargetCatch.setTotalCount(totalCount); + nonTargetCatch.setTotalCountComputedSource(NonTargetCatchComputedValueSource.FROM_DATA); +// String message = String.format(MESSAGE_FORMAT_3, +// entityLabel, +// t("observe.common.totalCount"), +// totalCount, +// t("observe.common.meanWeight"), +// meanWeight, +// t("observe.common.catchWeight"), +// catchWeight); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + } + + if (meanWeight == null && totalCount != null && totalCount != 0 && catchWeight != null && + !NonTargetCatchComputedValueSource.FROM_SAMPLE.equals(nonTargetCatch.getTotalCountComputedSource())) { + + // calcul le poids moyen à partir de nb estime et du poids estime + // uniquement si le nombre estimé ne vient pas des échantillons (voir http://forge.codelutin.com/issues/4670) + + meanWeight = catchWeight * (float) 1000 / (float) totalCount; + nonTargetCatch.setMeanWeight(meanWeight); + nonTargetCatch.setMeanWeightComputedSource(NonTargetCatchComputedValueSource.FROM_DATA); +// String message = String.format(MESSAGE_FORMAT_3, +// entityLabel, +// t("observe.common.meanWeight"), +// meanWeight, +// t("observe.common.totalCount"), +// totalCount, +// t("observe.common.catchWeight"), +// catchWeight); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + } + } + + protected void updateLengthWeightAble(ConsolidationActivitySeineDataContext activityContext, + Species species, + Sex sex, + LengthWeightComputable lengthWeightComputable) { + +// String entityLabel = getSpeciesLabel(); + + Float weight = lengthWeightComputable.getWeight(); + Float length = lengthWeightComputable.getLength(); + + boolean computeWeight = false; + boolean computeLength = false; + + if (weight == null && length != null) { + + // on essaye de calculer le poids + computeWeight = true; + } + + if (length == null && weight != null) { + + // on essaye de calcule la taille + computeLength = true; + } + + if (!computeLength && !computeWeight) { + + // rien a calculer + return; + } + + // recherche du parametrage adequate + LengthWeightParameter lengthWeightParameter = activityContext.findLengthWeightParameter(species, sex); + + if (lengthWeightParameter == null) { + + // aucun parametrage connu + +// String message = t("observe.message.consolidate.no.parametrage.found", +// t(entityLabel), +// decorate(species) +// ); +// if (log.isWarnEnabled()) { +// log.warn(message); +// } + return; + } + + if (computeLength) { + Float newLength = lengthWeightParameter.computeLength(weight); + if (newLength != null) { + +// // la taille a ete calculee +// String message = +// "[" + t(entityLabel) + "] " + +// t("observe.message.consolidate.computed.taille", +// newLength, +// weight, +// lengthWeightParameter.getWeightLengthFormula(), +// lengthWeightParameter.getCoefficients() +// ); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + lengthWeightComputable.setLength(newLength); + lengthWeightComputable.setLengthSource(true); + return; + } + + // la taille n'a pas ete changee, on peut quitter car il est impossible + // de calculer et la taille et le poids... + return; + } + + // on cherche obligatoirement a calculer le poids + Float newWeight = lengthWeightParameter.computeWeight(length); + if (newWeight != null) { + + // le poids a ete calcule +// String message = "[" + t(entityLabel) + "] " + +// t("observe.message.consolidate.computed.weight", +// newWeight, +// length, +// lengthWeightParameter.getLengthWeightFormula(), +// lengthWeightParameter.getCoefficients()); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + lengthWeightComputable.setWeight(newWeight); + lengthWeightComputable.setWeightSource(true); + } + } + + protected void updateLengthWeightAble(LengthWeightComputable lengthWeightComputable, + LengthWeightParameter lengthWeightParameter) { + +// String entityLabel = getSpeciesLabel(); + + Float weight = lengthWeightComputable.getWeight(); + boolean computeWeight = false; + boolean computeLength = false; + Float length = lengthWeightComputable.getLength(); + + if (weight == null && length != null) { + + // on essaye de calculer le poids + computeWeight = true; + } + + if (length == null && weight != null) { + + // on essaye de calcule la taille + computeLength = true; + } + + if (!computeLength && !computeWeight) { + + // rien a calculer + return; + } + + if (lengthWeightParameter == null) { + + // aucun parametrage connu + +// String message = t("observe.message.consolidate.no.parametrage.found", +// t(entityLabel), +// decorate(species) +// ); +// if (log.isWarnEnabled()) { +// log.warn(message); +// } + return; + } + + if (computeLength) { + Float newLength = lengthWeightParameter.computeLength(weight); + if (newLength != null) { + + // la taille a ete calculee +// String message = +// "[" + t(entityLabel) + "] " + +// t("observe.message.consolidate.computed.taille", +// newLength, +// weight, +// lengthWeightParameter.getWeightLengthFormula(), +// lengthWeightParameter.getCoefficients() +// ); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + lengthWeightComputable.setLength(newLength); + lengthWeightComputable.setLengthSource(true); + return; + } + + // la taille n'a pas ete changee, on peut quitter car il est impossible + // de calculer et la taille et le poids... + return; + } + + // on cherche obligatoirement a calculer le poids + Float newWeight = lengthWeightParameter.computeWeight(length); + if (newWeight != null) { + + // le poids a ete calcule +// String message = "[" + t(entityLabel) + "] " + +// t("observe.message.consolidate.computed.weight", +// newWeight, +// length, +// lengthWeightParameter.getLengthWeightFormula(), +// lengthWeightParameter.getCoefficients()); +// if (log.isInfoEnabled()) { +// log.info(message); +// } + lengthWeightComputable.setWeight(newWeight); + lengthWeightComputable.setWeightSource(true); + } + } + +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.