Author: tchemit Date: 2013-05-28 09:39:21 +0200 (Tue, 28 May 2013) New Revision: 1018 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/1018 Log: refs #2414: [IMP/EXP] - Export des donn?\195?\169es de campagne : format g?\195?\169n?\195?\169rique (finaliser) Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/CatchExportModel.java trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java Modified: trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java =================================================================== --- trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java 2013-05-27 21:50:15 UTC (rev 1017) +++ trunk/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntities.java 2013-05-28 07:39:21 UTC (rev 1018) @@ -32,6 +32,10 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.common.collect.Sets; +import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; +import fr.ifremer.tutti.persistence.entities.data.SampleCategoryEnum; +import fr.ifremer.tutti.persistence.entities.data.SortedUnsortedEnum; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType; @@ -94,6 +98,30 @@ } }; + public static final Predicate<SpeciesBatch> IS_VRAC_SPECIES_BATCH = new Predicate<SpeciesBatch>() { + + public boolean apply(SpeciesBatch input) { + return SampleCategoryEnum.sortedUnsorted == input.getSampleCategoryType() && + SortedUnsortedEnum.SORTED.getFieldValue().equals(input.getSampleCategoryValue()); + } + }; + + public static final Predicate<BenthosBatch> IS_VRAC_BENTHOS = new Predicate<BenthosBatch>() { + + public boolean apply(BenthosBatch input) { + return SampleCategoryEnum.sortedUnsorted == input.getSampleCategoryType() && + SortedUnsortedEnum.SORTED.getFieldValue().equals(input.getSampleCategoryValue()); + } + }; + + public static boolean isVracSpeciesBatch(SpeciesBatch speciesBatch) { + return IS_VRAC_SPECIES_BATCH.apply(speciesBatch); + } + + public static boolean isVracBenthosBatch(BenthosBatch benthosBatch) { + return IS_VRAC_BENTHOS.apply(benthosBatch); + } + /** Logger. */ private static final Log log = LogFactory.getLog(TuttiEntities.class); @@ -316,18 +344,6 @@ } } -// public static <B extends TuttiEntity> B newEntity(B entity) { -// return newEntity((Class<B>) entity.getClass()); -// } - -// public static <B extends TuttiEntity> B newEntity(Class<B> type) { -// try { -// return type.newInstance(); -// } catch (Exception e) { -// throw Throwables.propagate(e); -// } -// } - public static <K, V> void fillEntries(Map<K, V> map, Collection<K> keys, Function<K, V> function) { Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/CatchExportModel.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/CatchExportModel.java 2013-05-27 21:50:15 UTC (rev 1017) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/CatchExportModel.java 2013-05-28 07:39:21 UTC (rev 1018) @@ -24,6 +24,7 @@ * #L% */ +import fr.ifremer.tutti.persistence.entities.TuttiEntities; import fr.ifremer.tutti.persistence.entities.data.BatchContainer; import fr.ifremer.tutti.persistence.entities.data.BenthosBatch; import fr.ifremer.tutti.persistence.entities.data.BenthosBatchFrequency; @@ -112,7 +113,8 @@ FishingOperation operation, BatchContainer<SpeciesBatch> rootSpeciesBatch, BatchContainer<BenthosBatch> rootBenthosBatch, - Float catchRaisingFactor) { + Float speciesCatchRaisingFactor, + Float benthosCatchRaisingFactor) { for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) { CatchExportRow row = new CatchExportRow(); @@ -120,9 +122,18 @@ row.setFishingOperation(operation); row.setSpecies(speciesBatch.getSpecies()); - prepareRows(persistenceService, row, rows, - speciesBatch, - catchRaisingFactor); + if (TuttiEntities.isVracSpeciesBatch(speciesBatch)) { + Float speciesTotalWeight = speciesBatch.getSampleCategoryWeight(); + if (speciesTotalWeight == null) { + speciesTotalWeight = speciesBatch.getSampleCategoryComputedWeight(); + } + prepareSortedRows(persistenceService, row, rows, + speciesBatch, + speciesCatchRaisingFactor / speciesTotalWeight); + } else { + prepareUnsortedRows(row, rows, speciesBatch); + } + } for (BenthosBatch benthosBatch : rootBenthosBatch.getChildren()) { CatchExportRow row = new CatchExportRow(); @@ -130,36 +141,36 @@ row.setFishingOperation(operation); row.setSpecies(benthosBatch.getSpecies()); - prepareRows(persistenceService, row, rows, - benthosBatch, - catchRaisingFactor); + if (TuttiEntities.isVracBenthosBatch(benthosBatch)) { + Float speciesTotalWeight = benthosBatch.getSampleCategoryWeight(); + if (speciesTotalWeight == null) { + speciesTotalWeight = benthosBatch.getSampleCategoryComputedWeight(); + } + prepareSortedRows(persistenceService, row, rows, + benthosBatch, + benthosCatchRaisingFactor / speciesTotalWeight); + } else { + prepareUnsortedRows(row, rows, benthosBatch); + } } } - public void prepareRows(PersistenceService persistenceService, - CatchExportRow currentRow, - List<CatchExportRow> rows, - SpeciesBatch speciesBatch, - Float catchRaisingFactor) { + protected void prepareSortedRows(PersistenceService persistenceService, + CatchExportRow currentRow, + List<CatchExportRow> rows, + SpeciesBatch speciesBatch, + Float catchRaisingFactor) { - ExportSampleCategory<Serializable> sampleCategory = - new ExportSampleCategory<Serializable>(); - SampleCategoryEnum sampleCategoryType = speciesBatch.getSampleCategoryType(); - Caracteristic caracteristic = sampleCategoryToCaracteristics.get(sampleCategoryType); - sampleCategory.setCategoryCaracteristic(caracteristic); - sampleCategory.setCategoryType(sampleCategoryType); - sampleCategory.setCategoryValue(speciesBatch.getSampleCategoryValue()); - sampleCategory.setCategoryWeight(speciesBatch.getSampleCategoryWeight()); - sampleCategory.setSampleWeight(speciesBatch.getWeight()); + prepareBatch(currentRow, + speciesBatch.getSampleCategoryType(), + speciesBatch.getSampleCategoryValue(), + speciesBatch.getSampleCategoryWeight(), + speciesBatch.getSampleCategoryComputedWeight(), + speciesBatch.getWeight(), + speciesBatch.getNumber(), + speciesBatch.getComputedNumber(), + speciesBatch.getRankOrder()); - sampleCategory.setComputedWeight(speciesBatch.getSampleCategoryComputedWeight()); - sampleCategory.setNumber(speciesBatch.getNumber()); - sampleCategory.setComputedNumber(speciesBatch.getComputedNumber()); - sampleCategory.setWeightOrVolType(WEIGHT_OR_VOL_TYPE); - sampleCategory.setRankOrder(speciesBatch.getRankOrder()); - - currentRow.addCategory(sampleCategory); - if (speciesBatch.isChildBatchsEmpty()) { // on a leaf, get frequencies @@ -167,119 +178,211 @@ List<SpeciesBatchFrequency> speciesBatchFrequency = persistenceService.getAllSpeciesBatchFrequency(speciesBatch.getId()); - if (CollectionUtils.isEmpty(speciesBatchFrequency) || - speciesBatchFrequency.get(0).getWeight()==null) { + speciesBatchFrequency.get(0).getWeight() == null) { // no frequency or frequencies with no weight - // simple denombrement + CatchExportRow row = currentRow.copy(); - Float batchWeight = speciesBatch.getSampleCategoryWeight(); + // Get reference weight - if (batchWeight == null) { - batchWeight = speciesBatch.getSampleCategoryComputedWeight(); - } - Float referenceWeight = speciesBatch.getWeight(); if (referenceWeight == null) { - referenceWeight = batchWeight; + referenceWeight = speciesBatch.getSampleCategoryWeight(); } + if (referenceWeight == null) { + referenceWeight = speciesBatch.getSampleCategoryComputedWeight(); + } + computeRaisingFactor(row, catchRaisingFactor, - batchWeight, referenceWeight); rows.add(row); } else { + + // there is some frequencies with weight + for (SpeciesBatchFrequency batchFrequency : speciesBatchFrequency) { CatchExportRow row = currentRow.copy(); row.setSpeciesFrequency(batchFrequency); rows.add(row); - Float batchWeight = speciesBatch.getComputedWeight(); - Float referenceWeight = batchFrequency.getWeight(); computeRaisingFactor(row, catchRaisingFactor, - batchWeight, referenceWeight); } } - } else { + // Yet another sample category to walk through + for (SpeciesBatch childBatch : speciesBatch.getChildBatchs()) { - prepareRows(persistenceService, currentRow, rows, childBatch, catchRaisingFactor); + prepareSortedRows(persistenceService, + currentRow, + rows, + childBatch, + catchRaisingFactor); } } } - protected void computeRaisingFactor(CatchExportRow row, - float catchRaisingFactor, - float batchWeight, - float referenceWeight) { - float raisingFactor = catchRaisingFactor * (batchWeight / referenceWeight); + protected void prepareUnsortedRows(CatchExportRow currentRow, + List<CatchExportRow> rows, + SpeciesBatch speciesBatch) { - row.setReferenceWeight(referenceWeight); - row.setRaisingFactor(raisingFactor); + prepareBatch(currentRow, + speciesBatch.getSampleCategoryType(), + speciesBatch.getSampleCategoryValue(), + speciesBatch.getSampleCategoryWeight(), + speciesBatch.getSampleCategoryComputedWeight(), + speciesBatch.getWeight(), + speciesBatch.getNumber(), + speciesBatch.getComputedNumber(), + speciesBatch.getRankOrder()); + + CatchExportRow row = currentRow.copy(); + row.setReferenceWeight(speciesBatch.getSampleCategoryWeight()); + row.setRaisingFactor(1.f); + rows.add(row); } - public void prepareRows(PersistenceService persistenceService, - CatchExportRow currentRow, - List<CatchExportRow> rows, - BenthosBatch benthosBatch, - Float catchRaisingFactor) { + protected void prepareSortedRows(PersistenceService persistenceService, + CatchExportRow currentRow, + List<CatchExportRow> rows, + BenthosBatch benthosBatch, + Float catchRaisingFactor) { - ExportSampleCategory<Serializable> sampleCategory = - new ExportSampleCategory<Serializable>(); - SampleCategoryEnum sampleCategoryType = benthosBatch.getSampleCategoryType(); - Caracteristic caracteristic = sampleCategoryToCaracteristics.get(sampleCategoryType); - sampleCategory.setCategoryCaracteristic(caracteristic); - sampleCategory.setCategoryType(sampleCategoryType); - sampleCategory.setCategoryValue(benthosBatch.getSampleCategoryValue()); - sampleCategory.setCategoryWeight(benthosBatch.getSampleCategoryWeight()); - sampleCategory.setSampleWeight(benthosBatch.getWeight()); + prepareBatch(currentRow, + benthosBatch.getSampleCategoryType(), + benthosBatch.getSampleCategoryValue(), + benthosBatch.getSampleCategoryWeight(), + benthosBatch.getSampleCategoryComputedWeight(), + benthosBatch.getWeight(), + benthosBatch.getNumber(), + benthosBatch.getComputedNumber(), + benthosBatch.getRankOrder()); - sampleCategory.setComputedWeight(benthosBatch.getSampleCategoryComputedWeight()); - sampleCategory.setNumber(benthosBatch.getNumber()); - sampleCategory.setComputedNumber(benthosBatch.getComputedNumber()); - sampleCategory.setWeightOrVolType(WEIGHT_OR_VOL_TYPE); - sampleCategory.setRankOrder(benthosBatch.getRankOrder()); - - currentRow.addCategory(sampleCategory); - if (benthosBatch.isChildBatchsEmpty()) { // on a leaf, get frequencies - List<BenthosBatchFrequency> speciesBatchFrequency = + List<BenthosBatchFrequency> benthosBatchFrequency = persistenceService.getAllBenthosBatchFrequency(benthosBatch.getId()); - if (CollectionUtils.isEmpty(speciesBatchFrequency)) { + if (CollectionUtils.isEmpty(benthosBatchFrequency) || + benthosBatchFrequency.get(0).getWeight() == null) { - // no frequency - // simple denombrement - rows.add(currentRow.copy()); + // no frequency or frequencies with no weight + + CatchExportRow row = currentRow.copy(); + + // Get reference weight + + Float referenceWeight = benthosBatch.getWeight(); + + if (referenceWeight == null) { + referenceWeight = benthosBatch.getSampleCategoryWeight(); + } + + if (referenceWeight == null) { + referenceWeight = benthosBatch.getSampleCategoryComputedWeight(); + } + + computeRaisingFactor(row, + catchRaisingFactor, + referenceWeight); + rows.add(row); } else { - for (BenthosBatchFrequency batchFrequency : speciesBatchFrequency) { + + // there is some frequencies with weight + + for (BenthosBatchFrequency batchFrequency : benthosBatchFrequency) { CatchExportRow row = currentRow.copy(); row.setBenthosFrequency(batchFrequency); rows.add(row); + + Float referenceWeight = batchFrequency.getWeight(); + + computeRaisingFactor(row, catchRaisingFactor, referenceWeight); } } } else { + // Yet another sample category to walk through + for (BenthosBatch childBatch : benthosBatch.getChildBatchs()) { - prepareRows(persistenceService, currentRow, rows, childBatch, catchRaisingFactor); + prepareSortedRows(persistenceService, + currentRow, + rows, + childBatch, + catchRaisingFactor); } } } + protected void prepareUnsortedRows(CatchExportRow currentRow, + List<CatchExportRow> rows, + BenthosBatch benthosBatch) { + + prepareBatch(currentRow, + benthosBatch.getSampleCategoryType(), + benthosBatch.getSampleCategoryValue(), + benthosBatch.getSampleCategoryWeight(), + benthosBatch.getSampleCategoryComputedWeight(), + benthosBatch.getWeight(), + benthosBatch.getNumber(), + benthosBatch.getComputedNumber(), + benthosBatch.getRankOrder()); + + CatchExportRow row = currentRow.copy(); + row.setReferenceWeight(benthosBatch.getSampleCategoryWeight()); + row.setRaisingFactor(1.f); + rows.add(row); + } + + protected void computeRaisingFactor(CatchExportRow row, + float catchRaisingFactor, + float referenceWeight) { + float raisingFactor = referenceWeight / catchRaisingFactor; + row.setReferenceWeight(referenceWeight); + row.setRaisingFactor(raisingFactor); + } + + protected void prepareBatch(CatchExportRow currentRow, + SampleCategoryEnum sampleCategoryType, + Serializable sampleCategoryValue, + Float sampleCategoryWeight, + Float sampleCategoryComputedWeight, + Float batchWeight, + Integer batchNumber, + Integer batchComputedNumber, + Integer batchRankOrder + ) { + ExportSampleCategory<Serializable> sampleCategory = + new ExportSampleCategory<Serializable>(); + Caracteristic caracteristic = sampleCategoryToCaracteristics.get(sampleCategoryType); + sampleCategory.setCategoryCaracteristic(caracteristic); + sampleCategory.setCategoryType(sampleCategoryType); + sampleCategory.setCategoryValue(sampleCategoryValue); + sampleCategory.setCategoryWeight(sampleCategoryWeight); + sampleCategory.setSampleWeight(batchWeight); + + sampleCategory.setComputedWeight(sampleCategoryComputedWeight); + sampleCategory.setNumber(batchNumber); + sampleCategory.setComputedNumber(batchComputedNumber); + sampleCategory.setWeightOrVolType(WEIGHT_OR_VOL_TYPE); + sampleCategory.setRankOrder(batchRankOrder); + + currentRow.addCategory(sampleCategory); + } + protected void addSampleCategory(String headerPrefix, String categoryName) { newNullableColumnForExport(headerPrefix, categoryName + "." + ExportSampleCategory.PROPERTY_CATEGORY_VALUE, TuttiCsvUtil.CARACTERISTIC_VALUE_FORMATTER); newNullableColumnForExport("Num_Ordre_" + headerPrefix + "_H2", categoryName + "." + ExportSampleCategory.PROPERTY_RANK_ORDER, TuttiCsvUtil.INTEGER); Modified: trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java =================================================================== --- trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java 2013-05-27 21:50:15 UTC (rev 1017) +++ trunk/tutti-service/src/main/java/fr/ifremer/tutti/service/export/TuttiExportService.java 2013-05-28 07:39:21 UTC (rev 1018) @@ -511,20 +511,22 @@ rootSpeciesBatch, rootBenthosBatch); - Float catchRaisingFactor; + Float totalWeight = catchBatch.getCatchTotalWeight(); if (totalWeight == null) { totalWeight = catchBatch.getCatchTotalComputedWeight(); } - Float totalSortedWeight = catchBatch.getCatchTotalSortedComputedWeight(); - Float totalUnsortedWeight = catchBatch.getCatchTotalUnsortedComputedWeight(); - catchRaisingFactor = totalWeight / (totalSortedWeight + totalUnsortedWeight); + Float totalSpeciesWeight = catchBatch.getSpeciesTotalSampleSortedComputedWeight(); + Float totalBenthosWeight = catchBatch.getBenthosTotalSampleSortedComputedWeight(); + Float speciesCatchRaisingFactor =(totalWeight-totalUnsortedWeight) / (totalSpeciesWeight); + Float benthosCatchRaisingFactor =(totalWeight-totalUnsortedWeight) / (totalBenthosWeight); + List<CatchExportRow> rows = Lists.newArrayList(); exportContext.catchModel.prepareRows(persistenceService, rows, @@ -532,7 +534,8 @@ operation, rootSpeciesBatch, rootBenthosBatch, - catchRaisingFactor); + speciesCatchRaisingFactor, + benthosCatchRaisingFactor); exportContext.catchExport.write(rows, exportContext.catchWriter); }