Tony CHEMIT pushed to branch develop at ultreiaio / ird-t3 Commits: a8ec9c67 by Tony CHEMIT at 2018-03-20T13:14:06Z [N2][N3] Gestion des catégories de poids -10/+10kg dans la stratification N2 et N3 (See #263) - nettoyage de code et ajout de FIXME... - - - - - 8 changed files: - t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratum.java - t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratumLoader.java - t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2SampleStratum.java - t3-actions/src/main/java/fr/ird/t3/actions/data/level2/Level2Action.java - t3-actions/src/main/java/fr/ird/t3/actions/data/level3/L3SampleStratum.java - t3-actions/src/test/java/fr/ird/t3/actions/ActionResumeTest.java - t3-domain/src/main/java/fr/ird/t3/actions/stratum/SampleStratum.java - t3-domain/src/main/java/fr/ird/t3/models/WeightCompositionAggregateModel.java Changes: ===================================== t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratum.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratum.java +++ b/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratum.java @@ -8,12 +8,12 @@ * it under the terms of the GNU Affero 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 Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% @@ -37,6 +37,8 @@ import fr.ird.t3.models.WeightCompositionModel; import fr.ird.t3.models.WeightCompositionModelHelper; import fr.ird.t3.services.DecoratorService; import fr.ird.t3.services.T3ServiceContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.Collection; @@ -55,6 +57,7 @@ import static org.nuiton.i18n.I18n.l; */ public class L2CatchStratum extends CatchStratum<Level2Configuration, Level2Action> { + private static final Log log = LogFactory.getLog(L2CatchStratum.class); /** * All species used by all weight categories found in all catches * for this stratum. @@ -100,13 +103,38 @@ public class L2CatchStratum extends CatchStratum<Level2Configuration, Level2Acti */ private WeightCompositionAggregateModel inputModelForSpeciesToFix; - L2CatchStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, Collection<Species> speciesToFix) { + private L2CatchStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, Collection<Species> speciesToFix) { super(stratumConfiguration, speciesToFix); weightCategoriesForSpecies = HashMultimap.create(); inputModelForAllSpecies = new WeightCompositionAggregateModel(); outputModelForAllSpecies = new WeightCompositionAggregateModel(); } + static L2CatchStratum newCatchStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, + List<WeightCategoryTreatment> weightCategories, + Collection<Species> species, + Level2Action action) throws Exception { + L2CatchStratum catchStratum = new L2CatchStratum(stratumConfiguration, species); + T3ServiceContext serviceContext = action.getServiceContext(); + catchStratum.init(serviceContext, weightCategories, action); + // get the total weight of the catch stratum + float catchStratumWeight = catchStratum.getTotalCatchWeightForSpeciesToFix(); + if (catchStratumWeight == 0) { + // no catch in this stratum, skip it + String message = l(serviceContext.getLocale(), "t3.level2.message.noCatch.in.stratum"); + log.info(message); + action.addInfoMessage(message); + // let's nullify the catch stratum (make it no more available) + catchStratum = null; + } else { + // log it + String message = catchStratum.logCatchStratum(action.getDecoratorService()); + log.info(message); + action.addInfoMessage(message); + } + return catchStratum; + } + @Override public void close() throws IOException { super.close(); @@ -130,11 +158,13 @@ public class L2CatchStratum extends CatchStratum<Level2Configuration, Level2Acti Integer nbZones = e.getValue(); if (activity.isCorrectedElementaryCatchNotEmpty()) { + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie for (CorrectedElementaryCatch aCatch : activity.getCorrectedElementaryCatch()) { weightCategoriesForSpecies.put(aCatch.getWeightCategoryTreatment(), aCatch.getSpecies()); } } + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie Multimap<WeightCategoryTreatment, CorrectedElementaryCatch> correctedElementaryCatchesByCategory = ActivityTopiaDao.groupByWeightCategoryTreatment(activity.getCorrectedElementaryCatch()); ===================================== t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratumLoader.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratumLoader.java +++ b/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2CatchStratumLoader.java @@ -25,8 +25,10 @@ import fr.ird.t3.actions.stratum.CatchStratumLoader; import fr.ird.t3.actions.stratum.StratumConfiguration; import fr.ird.t3.entities.data.Activity; import fr.ird.t3.entities.data.ActivityTopiaDao; +import fr.ird.t3.entities.data.CorrectedElementaryCatch; import fr.ird.t3.entities.data.Trip; import fr.ird.t3.entities.reference.Vessel; +import fr.ird.t3.entities.reference.WeightCategoryTreatment; import fr.ird.t3.services.ioc.InjectDAO; import java.util.HashMap; @@ -71,6 +73,7 @@ public class L2CatchStratumLoader extends CatchStratumLoader<Level2Configuration Map<Activity, Integer> result = new HashMap<>(); if (activityIds != null && activityIds.size() > 0) { Set<Vessel> possibleVessels = configuration.getPossibleCatchVessels(); + WeightCategoryTreatment weightCategoryTreatment = configuration.getWeightCategoryTreatment(); for (Map.Entry<String, Integer> e : activityIds.entrySet()) { String activityId = e.getKey(); // get activity @@ -84,6 +87,14 @@ public class L2CatchStratumLoader extends CatchStratumLoader<Level2Configuration // recheck activity have some catches. Preconditions.checkState(activity.isCorrectedElementaryCatchNotEmpty(), String.format("Can not accept an activity (%s) with no catch", activity.getTopiaId())); + if (weightCategoryTreatment != null) { + // weight category is in stratum, check there is catch using this category + boolean foundWeightCategory = activity.getCorrectedElementaryCatch().stream().map(CorrectedElementaryCatch::getWeightCategoryTreatment).anyMatch(c -> c.equals(weightCategoryTreatment)); + if (!foundWeightCategory) { + // reject - the stratum weight category not found for this activity + continue; + } + } result.put(activity, e.getValue()); } } ===================================== t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2SampleStratum.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2SampleStratum.java +++ b/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/L2SampleStratum.java @@ -26,11 +26,12 @@ import fr.ird.t3.entities.data.Activity; import fr.ird.t3.entities.data.ActivityTopiaDao; import fr.ird.t3.entities.data.SetSpeciesFrequency; import fr.ird.t3.entities.reference.Species; +import fr.ird.t3.entities.reference.WeightCategory; import fr.ird.t3.entities.reference.WeightCategorySample; +import fr.ird.t3.entities.reference.WeightCategoryTreatment; import fr.ird.t3.models.WeightCompositionAggregateModel; import fr.ird.t3.models.WeightCompositionModelHelper; import fr.ird.t3.services.T3ServiceContext; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.persistence.TopiaException; @@ -38,6 +39,7 @@ import org.nuiton.topia.persistence.TopiaException; import java.io.IOException; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -81,12 +83,22 @@ public class L2SampleStratum extends SampleStratum<Level2Configuration, Level2Ac */ private float sampleStratumTotalWeight; - L2SampleStratum(StratumConfiguration<Level2Configuration> configuration, Collection<Species> speciesToFix, float catchStratumTotalWeight) { + private L2SampleStratum(StratumConfiguration<Level2Configuration> configuration, Collection<Species> speciesToFix, float catchStratumTotalWeight) { super(configuration, speciesToFix); this.catchStratumTotalWeight = catchStratumTotalWeight; modelsForAllSpecies = new WeightCompositionAggregateModel(); } + static L2SampleStratum newSampleStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, + List<WeightCategoryTreatment> weightCategories, + float totalCatchWeight, + Collection<Species> species, + Level2Action action) throws Exception { + L2SampleStratum sampleStratum = new L2SampleStratum(stratumConfiguration, species, totalCatchWeight); + sampleStratum.init(action.getServiceContext(), weightCategories, action); + return sampleStratum; + } + @Override public void close() throws IOException { super.close(); @@ -97,18 +109,14 @@ public class L2SampleStratum extends SampleStratum<Level2Configuration, Level2Ac @Override protected L2SampleStratumLoader newLoader() { int oceanCode = getConfiguration().getZone().getOcean().getCode(); - L2SampleStratumLoader result; switch (oceanCode) { case 1: - result = new L2SampleStratumLoaderAtlantic(this); - break; + return new L2SampleStratumLoaderAtlantic(this); case 2: - result = new L2SampleStratumLoaderIndian(this); - break; + return new L2SampleStratumLoaderIndian(this); default: throw new IllegalStateException("Not implemented for ocean with code " + oceanCode); } - return result; } public WeightCompositionAggregateModel getModelsForSpeciesToFix() { @@ -137,7 +145,7 @@ public class L2SampleStratum extends SampleStratum<Level2Configuration, Level2Ac ActivityTopiaDao.fillWeightsFromSetSpeciesCatWeight(activity, weights, null); // obtain the set species frequencies for the current activity Collection<SetSpeciesFrequency> setSpeciesFrequencies = activity.getSetSpeciesFrequency(); - if (CollectionUtils.isNotEmpty(setSpeciesFrequencies)) { + if (activity.isSetSpeciesFrequencyNotEmpty()) { // compute sample count for this activity int newCount = computeSampleCount(setSpeciesFrequencies, species); // merge it with final total count @@ -145,24 +153,25 @@ public class L2SampleStratum extends SampleStratum<Level2Configuration, Level2Ac } } // add all weights to model - for (WeightCategorySample weightCategoryTreatment : weights.keySet()) { - Map<Species, Float> speciesFloatMap = weights.get(weightCategoryTreatment); - modelsForAllSpecies.addModel(weightCategoryTreatment, speciesFloatMap); + for (Map.Entry<WeightCategorySample, Map<Species, Float>> e : weights.entrySet()) { + Map<Species, Float> speciesFloatMap = e.getValue(); + WeightCategory weightCategorySample = e.getKey(); + modelsForAllSpecies.addModel(weightCategorySample, speciesFloatMap); } // recompute the weight model for species to fix modelsForSpeciesToFix = modelsForAllSpecies.extractForSpecies(species); // recompute the total sample weight (for species to fix) sampleStratumTotalWeight = modelsForSpeciesToFix.getTotalModel().getTotalWeight(); - addMergedActivitesCount(activities.size()); + addMergedActivitiesCount(activities.size()); log.info(String.format("sampleStratumTotalCount = %d / sampleStratumTotalWeight = %f", getSampleStratumTotalCount(), getSampleStratumTotalWeight())); } - private int computeSampleCount(Collection<SetSpeciesFrequency> newDatas, Collection<Species> speciesToFix) { + private int computeSampleCount(Collection<SetSpeciesFrequency> setSpeciesFrequencies, Collection<Species> speciesToFix) { int newCount = 0; - for (SetSpeciesFrequency newData : newDatas) { - Species species = newData.getSpecies(); + for (SetSpeciesFrequency setSpeciesFrequency : setSpeciesFrequencies) { + Species species = setSpeciesFrequency.getSpecies(); if (speciesToFix.contains(species)) { - newCount += newData.getNumber(); + newCount += setSpeciesFrequency.getNumber(); } } return newCount; ===================================== t3-actions/src/main/java/fr/ird/t3/actions/data/level2/Level2Action.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/Level2Action.java +++ b/t3-actions/src/main/java/fr/ird/t3/actions/data/level2/Level2Action.java @@ -51,6 +51,7 @@ import fr.ird.t3.models.WeightCompositionAggregateModel; import fr.ird.t3.models.WeightCompositionModel; import fr.ird.t3.models.WeightCompositionModelHelper; import fr.ird.t3.services.DecoratorService; +import fr.ird.t3.services.T3ServiceContext; import fr.ird.t3.services.ZoneStratumService; import fr.ird.t3.services.ioc.InjectDAO; import fr.ird.t3.services.ioc.InjectEntitiesById; @@ -113,7 +114,7 @@ public class Level2Action extends T3Action<Level2Configuration> { * * @since 1.3 */ - private Collection<L2StratumResult> stratumsResult; + private Collection<L2StratumResult> stratumResultSet; @InjectDAO(entityType = Activity.class) private ActivityTopiaDao activityDAO; @InjectDAO(entityType = WeightCategoryTreatment.class) @@ -179,7 +180,7 @@ public class Level2Action extends T3Action<Level2Configuration> { this.possibleCatchVessels = vesselDAO.getPossibleCatchVessels(catchFleets); // get possible vessels for sample stratum this.possibleSampleVessels = vesselDAO.getPossibleSampleVessels(sampleFleets, sampleFlags); - this.stratumsResult = new LinkedHashSet<>(); + this.stratumResultSet = new LinkedHashSet<>(); this.inputCatchModelForAllSpecies = new WeightCompositionAggregateModel(); this.outputCatchModelForAllSpecies = new WeightCompositionAggregateModel(); } @@ -278,7 +279,7 @@ public class Level2Action extends T3Action<Level2Configuration> { activityCache); try { L2StratumResult result = doExecuteStratum(stratumConfiguration, weightCategories, stratumIndex, usedActivityIds); - stratumsResult.add(result); + stratumResultSet.add(result); } finally { flushTransaction("After stratum " + stratumIndex); } @@ -319,7 +320,7 @@ public class Level2Action extends T3Action<Level2Configuration> { addInfoMessage(message); addInfoMessage("=============================================================================================="); // compute the catch stratum - try (L2CatchStratum catchStratum = newCatchStratum(stratumConfiguration, weightCategories)) { + try (L2CatchStratum catchStratum = L2CatchStratum.newCatchStratum(stratumConfiguration, weightCategories, species, this)) { incrementsProgression(); if (catchStratum == null) { // no catch in this stratum @@ -328,7 +329,7 @@ public class Level2Action extends T3Action<Level2Configuration> { incrementsProgression(); } else { // compute sample stratum - try (L2SampleStratum sampleStratum = newSampleStratum(stratumConfiguration, weightCategories, catchStratum.getTotalCatchWeightForSpeciesToFix())) { + try (L2SampleStratum sampleStratum = L2SampleStratum.newSampleStratum(stratumConfiguration, weightCategories, catchStratum.getTotalCatchWeightForSpeciesToFix(), species, this)) { incrementsProgression(); // get the substitution level for the sample stratum Integer level = sampleStratum.getSubstitutionLevel(); @@ -349,7 +350,7 @@ public class Level2Action extends T3Action<Level2Configuration> { // int nbZones = e.getValue(); // is activity was already treated ? boolean newActivity = usedActivityIds.add(activity.getTopiaId()); - doExecuteActivityInCatchStratum(catchStratum, + doExecuteActivityInCatchStratumWithoutSampleStratum(catchStratum, activity, activityIndex++, nbActivities, @@ -408,35 +409,104 @@ public class Level2Action extends T3Action<Level2Configuration> { private Collection<ZoneStratumAware> getZones(SchoolType schoolType) { return oceanContext.values().stream().flatMap(o -> o.getZones(schoolType).stream()).distinct().collect(Collectors.toList()); } +// +// private L2CatchStratum newCatchStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, +// List<WeightCategoryTreatment> weightCategories) throws Exception { +// L2CatchStratum catchStratum = new L2CatchStratum(stratumConfiguration, species); +// catchStratum.init(serviceContext, weightCategories, this); +// // get the total weight of the catch stratum +// float catchStratumWeight = catchStratum.getTotalCatchWeightForSpeciesToFix(); +// if (catchStratumWeight == 0) { +// // no catch in this stratum, skip it +// String message = l(locale, "t3.level2.message.noCatch.in.stratum"); +// log.info(message); +// addInfoMessage(message); +// // let's nullify the catch stratum (make it no more available) +// catchStratum = null; +// } else { +// // log it +// String message = catchStratum.logCatchStratum(getDecoratorService()); +// log.info(message); +// addInfoMessage(message); +// } +// return catchStratum; +// } + +// private L2SampleStratum newSampleStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, +// List<WeightCategoryTreatment> weightCategories, +// float totalCatchWeight) throws Exception { +// L2SampleStratum sampleStratum = new L2SampleStratum(stratumConfiguration, species, totalCatchWeight); +// sampleStratum.init(serviceContext, weightCategories, this); +// return sampleStratum; +// } + + private void doExecuteActivityInCatchStratumWithoutSampleStratum(L2CatchStratum catchStratum, + Activity activity, + int activityIndex, + int nbActivities, + boolean deleteOldData) { - private L2CatchStratum newCatchStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, - List<WeightCategoryTreatment> weightCategories) throws Exception { - L2CatchStratum catchStratum = new L2CatchStratum(stratumConfiguration, species); - catchStratum.init(serviceContext, weightCategories, this); - // get the total weight of the catch stratum - float catchStratumWeight = catchStratum.getTotalCatchWeightForSpeciesToFix(); - if (catchStratumWeight == 0) { - // no catch in this stratum, skip it - String message = l(locale, "t3.level2.message.noCatch.in.stratum"); - log.info(message); - addInfoMessage(message); - // let's nullify the catch stratum (make it no more available) - catchStratum = null; - } else { - // log it - String message = catchStratum.logCatchStratum(getDecoratorService()); - log.info(message); - addInfoMessage(message); + String activityStr = String.format("%s (%s)", decorate(activity), decorate(activity.getTrip(), DecoratorService.WITH_ID)); + String message = l(locale, "t3.level2.message.start.activity", activityIndex, nbActivities, activityStr, 1); + log.info(message); + addInfoMessage(message); + if (deleteOldData) { + // delete old data for this activity + log.info(String.format("Delete previous level2 data of %s", activityStr)); + activity.deleteComputedDataLevel2(); + } + // build a model for getting total of each weight category + try (WeightCompositionAggregateModel catchWeightModelForAllSpecies = new WeightCompositionAggregateModel()) { + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie + ActivityTopiaDao.fillWeightsFromCatchesWeight(activity, catchWeightModelForAllSpecies, 1); + try (WeightCompositionAggregateModel catchWeightModelForSpeciesToFix = catchWeightModelForAllSpecies.extractForSpecies(species)) { + // log catches weight + message = logCatchWeight(getDecoratorService(), catchWeightModelForAllSpecies, catchWeightModelForSpeciesToFix); + log.info(message); + addInfoMessage(message); + } + try (WeightCompositionAggregateModel model = new WeightCompositionAggregateModel()) { + ActivityTopiaDao.fillWeightsFromSetSpeciesCatWeight(activity, null, model); + try (WeightCompositionAggregateModel compositionModel = model.extractForSpecies(species)) { + String activityResume = logActivityCatchStratum(model, compositionModel, getDecoratorService()); + message = l(locale, "t3.level2.message.activity.with.sample.useOwn.composition", activityResume); + log.info(message); + addInfoMessage(message); + } + } + // apply composition model to activity catches + applySampleSpecificCompositionWithoutSampleStratum(catchStratum, activity); } - return catchStratum; } - private L2SampleStratum newSampleStratum(StratumConfiguration<Level2Configuration> stratumConfiguration, - List<WeightCategoryTreatment> weightCategories, - float totalCatchWeight) throws Exception { - L2SampleStratum sampleStratum = new L2SampleStratum(stratumConfiguration, species, totalCatchWeight); - sampleStratum.init(serviceContext, weightCategories, this); - return sampleStratum; + private void applySampleSpecificCompositionWithoutSampleStratum(L2CatchStratum catchStratum, Activity activity) { + try (WeightCompositionAggregateModel correctedCatchWeightModel = new WeightCompositionAggregateModel()) { + // Get all corrected catch group by weight category + Multimap<WeightCategoryTreatment, CorrectedElementaryCatch> correctedElementaryCatches = + ActivityTopiaDao.groupByWeightCategoryTreatment(activity.getCorrectedElementaryCatch()); + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie + for (WeightCategoryTreatment weightCategory : correctedElementaryCatches.keySet()) { + Map<Species, Float> weights = new HashMap<>(); + // Apply on all existing corrected catch weight + for (CorrectedElementaryCatch aCatch : correctedElementaryCatches.get(weightCategory)) { + Species speciesToUse = aCatch.getSpecies(); + // the corrected catch weight to add to the row + float correctedCatchWeight = aCatch.getCatchWeight(); + // hold added value (for logs) + weights.put(speciesToUse, correctedCatchWeight); + aCatch.setCorrectedCatchWeight(correctedCatchWeight); + aCatch.setCorrectedFlag(false); + } + // add corrected weights used for this category + correctedCatchWeightModel.addModel(weightCategory, weights); + } + // log corrected catches weight + String message = logCorrectedCatchWeight(correctedCatchWeightModel, getDecoratorService()); + log.info(message); + addInfoMessage(message); + // add corrected catch weight done for this activity in stratum output model + catchStratum.addActivityOutputModel(correctedCatchWeightModel); + } } private void doExecuteActivityInCatchStratum(L2CatchStratum catchStratum, @@ -457,14 +527,14 @@ public class Level2Action extends T3Action<Level2Configuration> { } // build a model for getting total of each weight category WeightCompositionAggregateModel catchWeightModelForAllSpecies = new WeightCompositionAggregateModel(); + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie ActivityTopiaDao.fillWeightsFromCatchesWeight(activity, catchWeightModelForAllSpecies, nbZones); WeightCompositionAggregateModel catchWeightModelForSpeciesToFix = catchWeightModelForAllSpecies.extractForSpecies(species); // log catches weight message = logCatchWeight(getDecoratorService(), catchWeightModelForAllSpecies, catchWeightModelForSpeciesToFix); log.info(message); addInfoMessage(message); - // obtain the sample composition model to use for the activity (can come from catch if it has some samples, - // otherwise use the catch stratum one) + // obtain the sample composition model to use for the activity (can come from catch if it has some samples, otherwise use the catch stratum one) boolean activityWithSample = catchStratum.isActivityWithSample(activity); boolean useAllSamplesOfStratum = getConfiguration().isUseAllSamplesOfStratum(); WeightCompositionAggregateModel compositionModel; @@ -498,161 +568,98 @@ public class Level2Action extends T3Action<Level2Configuration> { } } - private void doExecuteActivityInCatchStratum(L2CatchStratum catchStratum, - Activity activity, - int activityIndex, - int nbActivities, - boolean deleteOldData) { - - String activityStr = String.format("%s (%s)", decorate(activity), decorate(activity.getTrip(), DecoratorService.WITH_ID)); - String message = l(locale, "t3.level2.message.start.activity", activityIndex, nbActivities, activityStr, 1); - log.info(message); - addInfoMessage(message); - if (deleteOldData) { - // delete old data for this activity - log.info(String.format("Delete previous level2 data of %s", activityStr)); - activity.deleteComputedDataLevel2(); - } - // build a model for getting total of each weight category - WeightCompositionAggregateModel catchWeightModelForAllSpecies = new WeightCompositionAggregateModel(); - ActivityTopiaDao.fillWeightsFromCatchesWeight(activity, catchWeightModelForAllSpecies, 1); - WeightCompositionAggregateModel catchWeightModelForSpeciesToFix = catchWeightModelForAllSpecies.extractForSpecies(species); - // log catches weight - message = logCatchWeight(getDecoratorService(), catchWeightModelForAllSpecies, catchWeightModelForSpeciesToFix); - log.info(message); - addInfoMessage(message); - WeightCompositionAggregateModel model = new WeightCompositionAggregateModel(); - ActivityTopiaDao.fillWeightsFromSetSpeciesCatWeight(activity, null, model); - WeightCompositionAggregateModel compositionModel = model.extractForSpecies(species); - String activityResume = logActivityCatchStratum(model, compositionModel, getDecoratorService()); - message = l(locale, "t3.level2.message.activity.with.sample.useOwn.composition", activityResume); - log.info(message); - addInfoMessage(message); - // apply composition model to activity catches - applySampleSpecificComposition(catchStratum, activity); - catchWeightModelForAllSpecies.close(); - catchWeightModelForSpeciesToFix.close(); - compositionModel.close(); - } - - private void applySampleSpecificComposition(L2CatchStratum catchStratum, Activity activity) { - WeightCompositionAggregateModel correctedCatchWeightModel = new WeightCompositionAggregateModel(); - // Get all corrected catch group by weight category - Multimap<WeightCategoryTreatment, CorrectedElementaryCatch> correctedElementaryCatches = - ActivityTopiaDao.groupByWeightCategoryTreatment(activity.getCorrectedElementaryCatch()); - for (WeightCategoryTreatment weightCategory : correctedElementaryCatches.keySet()) { - Map<Species, Float> weights = new HashMap<>(); - // Apply on all existing corrected catch weight - for (CorrectedElementaryCatch aCatch : correctedElementaryCatches.get(weightCategory)) { - Species speciesToUse = aCatch.getSpecies(); - // the corrected catch weight to add to the row - float correctedCatchWeight = aCatch.getCatchWeight(); - // hold added value (for logs) - weights.put(speciesToUse, correctedCatchWeight); - aCatch.setCorrectedCatchWeight(correctedCatchWeight); - aCatch.setCorrectedFlag(false); - } - // add corrected weights used for this category - correctedCatchWeightModel.addModel(weightCategory, weights); - } - // log corrected catches weight - String message = logCorrectedCatchWeight(correctedCatchWeightModel, getDecoratorService()); - log.info(message); - addInfoMessage(message); - // add corrected catch weight done for this activity in stratum output model - catchStratum.addActivityOutputModel(correctedCatchWeightModel); - } - private void applySampleSpecificComposition(L2CatchStratum catchStratum, Activity activity, int nbZones, WeightCompositionAggregateModel sampleCompositionModel, WeightCompositionAggregateModel correctedCatchesForSpeciesToFix) { - WeightCompositionAggregateModel correctedCatchWeightModel = new WeightCompositionAggregateModel(); - // Get all corrected catch group by weight category - Multimap<WeightCategoryTreatment, CorrectedElementaryCatch> correctedElementaryCatches = - ActivityTopiaDao.groupByWeightCategoryTreatment(activity.getCorrectedElementaryCatch()); - for (WeightCategoryTreatment weightCategory : correctedElementaryCatches.keySet()) { - Map<Species, Float> weights = new HashMap<>(); - // get composition model to use - WeightCompositionModel model = sampleCompositionModel.getModel(weightCategory); - // set of species still to fix - Set<Species> speciesToFix; - // set of species still to fix - Set<Species> speciesStillToFix; - // get total weight for species to fix from correctedCatches - float totalWeight; - if (model == null) { - // this means there is no specific composition for this weight category to apply for species to fix - // says there is no species to fix (so all catches will be copied to correctedCatchWeight (with no fixedFlag) - speciesToFix = speciesStillToFix = Collections.emptySet(); - totalWeight = 0f; - } else { - // found a specific composition to apply for this weight category - speciesToFix = model.getSpecies(); - speciesStillToFix = new HashSet<>(speciesToFix); - totalWeight = correctedCatchesForSpeciesToFix.getModel(weightCategory).getTotalWeight(); - } - // divide the total weight to used by the number of zones of this activity - totalWeight = totalWeight / nbZones; - // Apply on all existing corrected catch weight - for (CorrectedElementaryCatch aCatch : correctedElementaryCatches.get(weightCategory)) { - Species speciesToUse = Objects.requireNonNull(aCatch.getSpecies()); - // flag (was fixed or not) - boolean toFix; - // the corrected catch weight to add to the row - float correctedCatchWeight; - if (model != null && speciesToFix.contains(speciesToUse)) { - // species to fix - toFix = true; - float weightRate = model.getWeightRate(speciesToUse); - // new corrected catch weight - correctedCatchWeight = totalWeight * weightRate; - // species no more to treat - speciesStillToFix.remove(speciesToUse); + try (WeightCompositionAggregateModel correctedCatchWeightModel = new WeightCompositionAggregateModel()) { + //FIXME-263 si la categorie de poids est dans la strate alors on ne doit que traiter cette catégorie + // Get all corrected catch group by weight category + Multimap<WeightCategoryTreatment, CorrectedElementaryCatch> correctedElementaryCatches = + ActivityTopiaDao.groupByWeightCategoryTreatment(activity.getCorrectedElementaryCatch()); + for (WeightCategoryTreatment weightCategory : correctedElementaryCatches.keySet()) { + Map<Species, Float> weights = new HashMap<>(); + // get composition model to use + WeightCompositionModel model = sampleCompositionModel.getModel(weightCategory); + // set of species still to fix + Set<Species> speciesToFix; + // set of species still to fix + Set<Species> speciesStillToFix; + // get total weight for species to fix from correctedCatches + float totalWeight; + if (model == null) { + // this means there is no specific composition for this weight category to apply for species to fix + // says there is no species to fix (so all catches will be copied to correctedCatchWeight (with no fixedFlag) + speciesToFix = speciesStillToFix = Collections.emptySet(); + totalWeight = 0f; } else { - // nothing to fix, just propagate old value - toFix = false; - correctedCatchWeight = aCatch.getCatchWeight(); + // found a specific composition to apply for this weight category + speciesToFix = model.getSpecies(); + speciesStillToFix = new HashSet<>(speciesToFix); + totalWeight = correctedCatchesForSpeciesToFix.getModel(weightCategory).getTotalWeight(); } - // hold added value (for logs) - weights.put(speciesToUse, correctedCatchWeight); - // Add to old value (if any) - Float oldCorrectedCatchWeight = aCatch.getCorrectedCatchWeight(); - if (oldCorrectedCatchWeight == null) { - oldCorrectedCatchWeight = 0f; + // divide the total weight to used by the number of zones of this activity + totalWeight = totalWeight / nbZones; + // Apply on all existing corrected catch weight + for (CorrectedElementaryCatch aCatch : correctedElementaryCatches.get(weightCategory)) { + Species speciesToUse = Objects.requireNonNull(aCatch.getSpecies()); + // flag (was fixed or not) + boolean toFix; + // the corrected catch weight to add to the row + float correctedCatchWeight; + if (model != null && speciesToFix.contains(speciesToUse)) { + // species to fix + toFix = true; + float weightRate = model.getWeightRate(speciesToUse); + // new corrected catch weight + correctedCatchWeight = totalWeight * weightRate; + // species no more to treat + speciesStillToFix.remove(speciesToUse); + } else { + // nothing to fix, just propagate old value + toFix = false; + correctedCatchWeight = aCatch.getCatchWeight(); + } + // hold added value (for logs) + weights.put(speciesToUse, correctedCatchWeight); + // Add to old value (if any) + Float oldCorrectedCatchWeight = aCatch.getCorrectedCatchWeight(); + if (oldCorrectedCatchWeight == null) { + oldCorrectedCatchWeight = 0f; + } + aCatch.setCorrectedCatchWeight(oldCorrectedCatchWeight + correctedCatchWeight); + aCatch.setCorrectedFlag(toFix); } - aCatch.setCorrectedCatchWeight(oldCorrectedCatchWeight + correctedCatchWeight); - aCatch.setCorrectedFlag(toFix); - } - // Creates new corrected catch weight records for all species still to fix - // Means they are in samples but not in catches - for (Species speciesToUse : speciesStillToFix) { - // weight rate of the species - float weightRate = model == null ? 1f : model.getWeightRate(speciesToUse); - // corrected catch weight - float correctedCatchWeight = totalWeight * weightRate; - // create new record - CorrectedElementaryCatch aCatch = correctedElementaryCatchDAO.create(); - aCatch.setSpecies(speciesToUse); - aCatch.setWeightCategoryTreatment(weightCategory); - aCatch.setCatchWeight(0f); - aCatch.setCorrectedFlag(true); - aCatch.setCorrectedCatchWeight(correctedCatchWeight); - // hold added value (for logs) - weights.put(speciesToUse, correctedCatchWeight); - // add it to activity - activity.addCorrectedElementaryCatch(aCatch); + // Creates new corrected catch weight records for all species still to fix + // Means they are in samples but not in catches + for (Species speciesToUse : speciesStillToFix) { + // weight rate of the species + float weightRate = model == null ? 1f : model.getWeightRate(speciesToUse); + // corrected catch weight + float correctedCatchWeight = totalWeight * weightRate; + // create new record + CorrectedElementaryCatch aCatch = correctedElementaryCatchDAO.create(); + aCatch.setSpecies(speciesToUse); + aCatch.setWeightCategoryTreatment(weightCategory); + aCatch.setCatchWeight(0f); + aCatch.setCorrectedFlag(true); + aCatch.setCorrectedCatchWeight(correctedCatchWeight); + // hold added value (for logs) + weights.put(speciesToUse, correctedCatchWeight); + // add it to activity + activity.addCorrectedElementaryCatch(aCatch); + } + // add corrected weights used for this category + correctedCatchWeightModel.addModel(weightCategory, weights); } - // add corrected weights used for this category - correctedCatchWeightModel.addModel(weightCategory, weights); + // log corrected catches weight + String message = logCorrectedCatchWeight(correctedCatchWeightModel, getDecoratorService()); + log.info(message); + addInfoMessage(message); + // add corrected catch weight done for this activity in stratum output model + catchStratum.addActivityOutputModel(correctedCatchWeightModel); } - // log corrected catches weight - String message = logCorrectedCatchWeight(correctedCatchWeightModel, getDecoratorService()); - log.info(message); - addInfoMessage(message); - // add corrected catch weight done for this activity in stratum output model - catchStratum.addActivityOutputModel(correctedCatchWeightModel); } // ------------------------------------------------------------------------- @@ -681,13 +688,13 @@ public class Level2Action extends T3Action<Level2Configuration> { @SuppressWarnings("unused") public int getNbStrataFixed() { - return stratumsResult.size(); + return stratumResultSet.size(); } @SuppressWarnings({"unused", "WeakerAccess"}) public Integer[] getAllSubstitutionLevels() { Set<Integer> levels = new HashSet<>(); - for (L2StratumResult stratumResult : stratumsResult) { + for (L2StratumResult stratumResult : stratumResultSet) { levels.add(stratumResult.getSubstitutionLevel()); } List<Integer> result = new ArrayList<>(levels); @@ -703,7 +710,7 @@ public class Level2Action extends T3Action<Level2Configuration> { @SuppressWarnings("unused") public Collection<L2StratumResult> getStratumResult(int level) { Set<L2StratumResult> singleResult = new LinkedHashSet<>(); - for (L2StratumResult stratumResult : stratumsResult) { + for (L2StratumResult stratumResult : stratumResultSet) { if (level == stratumResult.getSubstitutionLevel()) { singleResult.add(stratumResult); } @@ -717,7 +724,7 @@ public class Level2Action extends T3Action<Level2Configuration> { Integer[] levels = getAllSubstitutionLevels(); for (Integer level : levels) { Set<L2StratumResult> singleResult = new LinkedHashSet<>(); - for (L2StratumResult stratumResult : stratumsResult) { + for (L2StratumResult stratumResult : stratumResultSet) { if (level == stratumResult.getSubstitutionLevel()) { singleResult.add(stratumResult); } @@ -752,7 +759,7 @@ public class Level2Action extends T3Action<Level2Configuration> { @SuppressWarnings("unused") public int getMaximumSizeForStratum() { int result = 0; - for (L2StratumResult stratumResult : stratumsResult) { + for (L2StratumResult stratumResult : stratumResultSet) { result = Math.max(result, stratumResult.getLibelle().length() + 1); } return result; @@ -796,8 +803,8 @@ public class Level2Action extends T3Action<Level2Configuration> { modelForSpeciestoFix); } - public void setStratumsResult(Set<L2StratumResult> stratumsResult) { - this.stratumsResult = stratumsResult; + public void setStratumResultSet(Set<L2StratumResult> stratumResultSet) { + this.stratumResultSet = stratumResultSet; } public void setInputCatchModelForAllSpecies(WeightCompositionAggregateModel inputCatchModelForAllSpecies) { @@ -807,4 +814,8 @@ public class Level2Action extends T3Action<Level2Configuration> { public void setOutputCatchModelForAllSpecies(WeightCompositionAggregateModel outputCatchModelForAllSpecies) { this.outputCatchModelForAllSpecies = outputCatchModelForAllSpecies; } + + T3ServiceContext getServiceContext() { + return serviceContext; + } } ===================================== t3-actions/src/main/java/fr/ird/t3/actions/data/level3/L3SampleStratum.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/actions/data/level3/L3SampleStratum.java +++ b/t3-actions/src/main/java/fr/ird/t3/actions/data/level3/L3SampleStratum.java @@ -181,7 +181,7 @@ public class L3SampleStratum extends SampleStratum<Level3Configuration, Level3Ac speciesCount.put(species, oldCount + newCount); } } - addMergedActivitesCount(activities.size()); + addMergedActivitiesCount(activities.size()); if (log.isInfoEnabled()) { log.info(String.format("sampleStratumTotalWeight = %s", getSampleStratumTotalWeight())); ===================================== t3-actions/src/test/java/fr/ird/t3/actions/ActionResumeTest.java ===================================== --- a/t3-actions/src/test/java/fr/ird/t3/actions/ActionResumeTest.java +++ b/t3-actions/src/test/java/fr/ird/t3/actions/ActionResumeTest.java @@ -746,7 +746,7 @@ public class ActionResumeTest<C extends T3ActionConfiguration, A extends T3Actio stratumResult.setNbActivities(5); stratumsResult.add(stratumResult); - action.setStratumsResult(stratumsResult); + action.setStratumResultSet(stratumsResult); action.setInputCatchModelForAllSpecies(new WeightCompositionAggregateModel()); action.setOutputCatchModelForAllSpecies(new WeightCompositionAggregateModel()); ===================================== t3-domain/src/main/java/fr/ird/t3/actions/stratum/SampleStratum.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/actions/stratum/SampleStratum.java +++ b/t3-domain/src/main/java/fr/ird/t3/actions/stratum/SampleStratum.java @@ -27,7 +27,6 @@ import fr.ird.t3.entities.reference.WeightCategoryTreatment; import fr.ird.t3.services.IOCService; import fr.ird.t3.services.T3ServiceContext; import fr.ird.t3.services.ioc.InjectDAO; -import org.nuiton.topia.persistence.TopiaException; import java.util.Collection; import java.util.List; @@ -60,10 +59,13 @@ public abstract class SampleStratum<C extends LevelConfigurationWithStratum, A e */ private Integer substitutionLevel; + protected SampleStratum(StratumConfiguration<C> configuration, Collection<Species> speciesToFix) { + super(configuration, speciesToFix); + } + protected abstract SampleStratumLoader<C, A, S> newLoader(); - protected abstract String logSampleStratumLevel(int substitutionLevel, - A messager); + protected abstract String logSampleStratumLevel(int substitutionLevel, A messager); /** * Merge the given {@code activities} sample data in the stratum result. @@ -81,24 +83,15 @@ public abstract class SampleStratum<C extends LevelConfigurationWithStratum, A e * * @param serviceContext service context * @param activities the activities to merge - * @throws TopiaException if any database problem while loading data */ - protected abstract void mergeNewActivities(T3ServiceContext serviceContext, - Set<Activity> activities) throws TopiaException; - - protected SampleStratum(StratumConfiguration<C> configuration, - Collection<Species> speciesToFix) { - super(configuration, speciesToFix); - } + protected abstract void mergeNewActivities(T3ServiceContext serviceContext, Set<Activity> activities); - protected final void addMergedActivitesCount(int nb) { + protected final void addMergedActivitiesCount(int nb) { nbMergedActivities += nb; } @Override - public void init(T3ServiceContext serviceContext, - List<WeightCategoryTreatment> weightCategories, - A messager) throws Exception { + public void init(T3ServiceContext serviceContext, List<WeightCategoryTreatment> weightCategories, A messager) throws Exception { SampleStratumLoader<C, A, S> stratumLoader = newLoader(); ===================================== t3-domain/src/main/java/fr/ird/t3/models/WeightCompositionAggregateModel.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/models/WeightCompositionAggregateModel.java +++ b/t3-domain/src/main/java/fr/ird/t3/models/WeightCompositionAggregateModel.java @@ -85,8 +85,7 @@ public class WeightCompositionAggregateModel implements Closeable { } public void addModel(WeightCompositionAggregateModel modelToMerge) { - for (WeightCompositionModel compositionModel : - modelToMerge.getModel().values()) { + for (WeightCompositionModel compositionModel : modelToMerge.getModel().values()) { addModel(compositionModel); } } View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/a8ec9c67461949609c21522a8c4e96014... --- View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/a8ec9c67461949609c21522a8c4e96014... You're receiving this email because of your account on gitlab.com.