This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit 11bae16b4b4318842ed0cc14c12ac59bb13e47bc Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Feb 3 19:13:23 2015 +0100 introduction d'un objet context de l'import --- .../tutti/service/bigfin/BigfinImportContext.java | 357 +++++++++++++++++++++ 1 file changed, 357 insertions(+) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/BigfinImportContext.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/BigfinImportContext.java new file mode 100644 index 0000000..e295f10 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/BigfinImportContext.java @@ -0,0 +1,357 @@ +package fr.ifremer.tutti.service.bigfin; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import fr.ifremer.tutti.persistence.entities.data.BatchContainer; +import fr.ifremer.tutti.persistence.entities.data.CatchBatch; +import fr.ifremer.tutti.persistence.entities.data.FishingOperation; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs; +import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol; +import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; +import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue; +import fr.ifremer.tutti.persistence.entities.referential.Species; +import fr.ifremer.tutti.service.PersistenceService; +import fr.ifremer.tutti.service.bigfin.csv.BigfinDataRow; +import fr.ifremer.tutti.service.bigfin.csv.SpeciesOrSpeciesBatch; +import fr.ifremer.tutti.service.bigfin.signs.Sign; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.t; + +/** + * Created on 2/3/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.13 + */ +public class BigfinImportContext { + + /** Logger. */ + private static final Log log = LogFactory.getLog(BigfinImportContext.class); + + protected final FishingOperation operation; + + protected final CatchBatch catchBatch; + + protected final Map<Sign, CaracteristicQualitativeValue> signsToCaracteristicValue; + + protected final Map<String, Species> speciesBySurveyCode; + + protected final Map<String, SpeciesProtocol> speciesProtocolBySurveyCode; + + // set of not found species already added in the errors + protected final Set<Species> speciesNotRecognized = new HashSet<>(); + + //set of species not in the protocol + protected final Set<Species> speciesNotInProtocol = new HashSet<>(); + + // set of species without lengthstep already added in the errors + protected final Set<Species> speciesInProtocolButWithoutLengthStepPmfmId = new HashSet<>(); + + private final List<BigfinDataRow> speciesRows = new ArrayList<>(); + + private final List<BigfinDataRow> speciesBatchRows = new ArrayList<>(); + + private final BigfinImportResult bigfinImportResult; + + private final BatchContainer<SpeciesBatch> rootSpeciesBatch; + + private final Map<Species, Caracteristic> lengthStepPmfmBySpecies; + + public BigfinImportContext(File importFile, + FishingOperation operation, + CatchBatch catchBatch, + Map<Sign, CaracteristicQualitativeValue> signsToCaracteristicValue, + Map<String, Species> speciesBySurveyCode, + Map<String, SpeciesProtocol> speciesProtocolBySurveyCode, + BatchContainer<SpeciesBatch> rootSpeciesBatch) { + this.rootSpeciesBatch = rootSpeciesBatch; + this.bigfinImportResult = new BigfinImportResult(importFile); + this.operation = operation; + this.catchBatch = catchBatch; + this.signsToCaracteristicValue = signsToCaracteristicValue; + this.speciesBySurveyCode = speciesBySurveyCode; + this.speciesProtocolBySurveyCode = speciesProtocolBySurveyCode; + this.lengthStepPmfmBySpecies = new HashMap<>(); + } + + public Caracteristic getLengthStepPmfm(Species species, PersistenceService persistenceService) { + Caracteristic caracteristic = lengthStepPmfmBySpecies.get(species); + if (caracteristic == null) { + + SpeciesProtocol speciesProtocol = getSpeciesProtocol(species); + caracteristic = persistenceService.getCaracteristic(Integer.parseInt(speciesProtocol.getLengthStepPmfmId())); + lengthStepPmfmBySpecies.put(species, caracteristic); + + } + return caracteristic; + } + + public boolean isNoError() { + return bigfinImportResult.getErrors().isEmpty(); + } + + public void addRowToProcess(BigfinDataRow bigfinDataRow) { + + if (bigfinDataRow.getSpeciesOrSpeciesBatch().isSpecies()) { + speciesRows.add(bigfinDataRow); + } else { + speciesBatchRows.add(bigfinDataRow); + } + + } + + public Map<Integer, SpeciesBatch> getSpeciesBatchesById() { + return SpeciesBatchs.getAllSpeciesBatchesById(this.rootSpeciesBatch); + } + + public Multimap<Species, SpeciesBatch> getRootSpeciesBatchBySpecies() { + Multimap<Species, SpeciesBatch> batchesBySpecies = Multimaps.index(rootSpeciesBatch.getChildren(), new Function<SpeciesBatch, Species>() { + @Override + public Species apply(SpeciesBatch input) { + return input.getSpecies(); + } + }); + return batchesBySpecies; + } + + public Multimap<Species, BigfinDataRow> getSpeciesRowsBySpecies() { + + // separate the imported rows by species + Multimap<Species, BigfinDataRow> rowsBySpecies = Multimaps.index(speciesRows, new Function<BigfinDataRow, Species>() { + @Override + public Species apply(BigfinDataRow bigfinDataRow) { + return bigfinDataRow.getSpeciesOrSpeciesBatch().getSpecies(); + } + }); + + return rowsBySpecies; + } + + public Multimap<SpeciesBatch, BigfinDataRow> getSpeciesBatchRowsBySpeciesBatch() { + + Multimap<SpeciesBatch, BigfinDataRow> rowsBySpeciesBatch = Multimaps.index(speciesBatchRows, new Function<BigfinDataRow, SpeciesBatch>() { + @Override + public SpeciesBatch apply(BigfinDataRow input) { + return input.getSpeciesOrSpeciesBatch().getBatch(); + } + }); + + return rowsBySpeciesBatch; + + } + + public boolean isStationFound(BigfinDataRow bean) { + + String station = bean.getStation(); + Date date = bean.getDate(); + boolean result = station != null + && station.equals(operation.getStationNumber()) + && date != null + && DateUtils.isSameDay(date, operation.getGearShootingStartDate()); + return result; + + } + + public SpeciesProtocol getSpeciesProtocol(Species species) { + + String code = species.getSurveyCode(); + if (StringUtils.isBlank(code)) { + code = species.getReferenceTaxonId().toString(); + } + SpeciesProtocol speciesProtocol = speciesProtocolBySurveyCode.get(code); + return speciesProtocol; + + } + + public boolean checkRow(BigfinDataRow bigfinDataRow) { + + // check if the station is the one of the operation + // and do not check again a species that has not been recognized before + boolean stationFound = isStationFound(bigfinDataRow); + + boolean canBeAdd = false; + + if (stationFound) { + + SpeciesOrSpeciesBatch speciesOrspeciesBatch = bigfinDataRow.getSpeciesOrSpeciesBatch(); + + if (speciesOrspeciesBatch.isSpecies()) { + + // do some checks on the given species + + checkSizeIsDefined(bigfinDataRow); + + Species species = speciesOrspeciesBatch.getSpecies(); + + boolean speciesIsKnown = checkSpeciesIsKnown(species); + + if (speciesIsKnown) { + + boolean speciesProtocoleIsSafe = checkSpeciesProtocol(species); + + if (speciesProtocoleIsSafe) { + + canBeAdd = true; + + } + } + + } else { + + // do some checks on the given species batch + + boolean speciesBatchIsLeaf = checkSpeciesBatchIsLeaf(bigfinDataRow); + + if (speciesBatchIsLeaf) { + + canBeAdd = true; + + } + } + + } else { + if (log.isInfoEnabled()) { + log.info("Station is not matching for record: " + bigfinDataRow.getRecordId()); + } + } + + return canBeAdd; + } + + public boolean checkSpeciesBatchIsLeaf(BigfinDataRow bigfinDataRow) { + + Preconditions.checkArgument(bigfinDataRow.getSpeciesOrSpeciesBatch().isBatch()); + + + SpeciesBatch batch = bigfinDataRow.getSpeciesOrSpeciesBatch().getBatch(); + boolean speciesBatchIsLeaf = batch.isChildBatchsEmpty(); + + if (!speciesBatchIsLeaf) { + + String warning = t("tutti.service.bigfinImport.warning.speciesBatch.tooCategorized", batch.getId()); + if (log.isWarnEnabled()) { + log.warn(warning); + } + bigfinImportResult.addWarning(warning); + } + + return speciesBatchIsLeaf; + + } + + public void checkSizeIsDefined(BigfinDataRow bigfinDataRow) { + + Preconditions.checkArgument(bigfinDataRow.getSpeciesOrSpeciesBatch().isSpecies()); + + if (bigfinDataRow.getSize() == null) { + String error = t("tutti.service.bigfinImport.error.szClass.unknwon", bigfinDataRow.getRecordId()); + if (log.isErrorEnabled()) { + log.error(error); + } + bigfinImportResult.addError(error); + } + + } + + public boolean checkSpeciesIsKnown(Species species) { + + boolean speciesIsKnown = true; + + if (species.getId() == null) { + + // bloquer tout si un "species" ne match pas le référentiel de Tutti : lister dans ce cas les codes non reconnus + if (speciesNotRecognized.add(species)) { + String error = t("tutti.service.bigfinImport.error.species.not.found", species.getExternalCode()); + if (log.isErrorEnabled()) { + log.error(error); + } + bigfinImportResult.addError(error); + } + + speciesIsKnown = false; + + } + + return speciesIsKnown; + + } + + public boolean checkSpeciesProtocol(Species species) { + + boolean speciesProtocoleIsSafe = true; + + String speciesLabel = getSpeciesLabel(species); + + SpeciesProtocol speciesProtocol = getSpeciesProtocol(species); + + // On n'importe pas les espèces non présentes dans le protocole et + // on liste les espèces/catégorisées non importées pour aider l'utilisateur + // à identifier le problème et on fait l'import des autres + if (speciesProtocol == null) { + + speciesProtocoleIsSafe = false; + + if (speciesNotInProtocol.add(species)) { + + String error = t("tutti.service.bigfinImport.warning.species.notInProtocol", speciesLabel); + if (log.isWarnEnabled()) { + log.warn(error); + } + bigfinImportResult.addWarning(error); + } + + } else { + + if (speciesProtocol.getLengthStepPmfmId() == null) { + + speciesProtocoleIsSafe = false; + + if (speciesInProtocolButWithoutLengthStepPmfmId.add(species)) { + // bloquer toute espèce reconnue du protocole mais qui n'a pas de méthode de mesure + String error = t("tutti.service.bigfinImport.error.species.without.lengthstep", speciesLabel); + if (log.isErrorEnabled()) { + log.error(error); + } + bigfinImportResult.addError(error); + } + } + } + + return speciesProtocoleIsSafe; + + } + + public BigfinImportResult getResult() { + return bigfinImportResult; + } + + protected String getSpeciesLabel(Species species) { + String speciesLabel = species.getSurveyCode(); + if (StringUtils.isBlank(speciesLabel)) { + speciesLabel = species.getRefTaxCode(); + } + return speciesLabel; + } + + public Species getSpeciesWithSurveyCode(Species species) { + String refTaxCode = species.getRefTaxCode(); + Species speciesWithSurveyCode = speciesBySurveyCode.get(refTaxCode); + return speciesWithSurveyCode; + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.