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 d321712f02bce8788e06806c501f439de9e5b2a9 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Feb 3 19:12:13 2015 +0100 deplacement du code csv dabs un paquetage dédié + nettoyage code --- .../tutti/service/bigfin/csv/BigfinDataRow.java | 146 +++++++++++++ .../service/bigfin/csv/BigfinDataRowModel.java | 231 +++++++++++++++++++++ .../service/bigfin/csv/SpeciesOrSpeciesBatch.java | 42 ++++ 3 files changed, 419 insertions(+) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRow.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRow.java new file mode 100644 index 0000000..ab7c15b --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRow.java @@ -0,0 +1,146 @@ +package fr.ifremer.tutti.service.bigfin.csv; + +/* + * #%L + * Tutti :: Service + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import fr.ifremer.tutti.service.bigfin.signs.Sex; +import fr.ifremer.tutti.service.bigfin.signs.Size; +import fr.ifremer.tutti.service.bigfin.signs.VracHorsVrac; + +import java.util.Date; + +/** + * @author Kevin Morin (Code Lutin) + * @since 3.8 + */ +public class BigfinDataRow { + + public static final String PROPERTY_RECORD_ID = "recordId"; + + public static final String PROPERTY_DATE = "date"; + + public static final String PROPERTY_STATION = "station"; + + public static final String PROPERTY_SPECIES_OR_SPECIES_BATCH = "speciesOrSpeciesBatch"; + + public static final String PROPERTY_LENGTH = "length"; + + public static final String PROPERTY_WEIGHT = "weight"; + + public static final String PROPERTY_SIZE = "size"; + + public static final String PROPERTY_SEX = "sex"; + + public static final String PROPERTY_VRAC_HORS_VRAC = "vracHorsVrac"; + + protected String recordId; + + protected Date date; + + protected String station; + + protected SpeciesOrSpeciesBatch speciesOrSpeciesBatch; + + protected float length; + + protected Float weight; + + protected Size size; + + protected Sex sex; + + protected VracHorsVrac vracHorsVrac; + + public String getRecordId() { + return recordId; + } + + public void setRecordId(String recordId) { + this.recordId = recordId; + } + + public float getLength() { + return length; + } + + public void setLength(float length) { + this.length = length; + } + + public Float getWeight() { + return weight; + } + + public void setWeight(Float weight) { + this.weight = weight; + } + + public SpeciesOrSpeciesBatch getSpeciesOrSpeciesBatch() { + return speciesOrSpeciesBatch; + } + + public void setSpeciesOrSpeciesBatch(SpeciesOrSpeciesBatch speciesOrSpeciesBatch) { + this.speciesOrSpeciesBatch = speciesOrSpeciesBatch; + } + + public String getStation() { + return station; + } + + public void setStation(String station) { + this.station = station; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public Sex getSex() { + return sex; + } + + public void setSex(Sex sex) { + this.sex = sex; + } + + public VracHorsVrac getVracHorsVrac() { + return vracHorsVrac; + } + + public void setVracHorsVrac(VracHorsVrac vracHorsVrac) { + this.vracHorsVrac = vracHorsVrac; + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRowModel.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRowModel.java new file mode 100644 index 0000000..6c378ef --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/BigfinDataRowModel.java @@ -0,0 +1,231 @@ +package fr.ifremer.tutti.service.bigfin.csv; + +/* + * #%L + * Tutti :: Service + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2014 Ifremer + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.referential.Species; +import fr.ifremer.tutti.persistence.entities.referential.Speciess; +import fr.ifremer.tutti.service.TuttiCsvUtil; +import fr.ifremer.tutti.service.bigfin.signs.Sex; +import fr.ifremer.tutti.service.bigfin.signs.Size; +import fr.ifremer.tutti.service.bigfin.signs.VracHorsVrac; +import org.apache.commons.lang3.StringUtils; +import org.nuiton.csv.Common; +import org.nuiton.csv.ValueParser; + +import java.text.ParseException; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Kevin Morin (Code Lutin) + * @since 3.8 + */ +public class BigfinDataRowModel extends TuttiCsvUtil.AbstractTuttiImportModel<BigfinDataRow> { + + public BigfinDataRowModel(Map<String, Species> speciesBySurveyCode, Map<Integer, SpeciesBatch> speciesBatchesById) { + + super(','); + + newIgnoredColumn("Study name"); + + newMandatoryColumn("ID", BigfinDataRow.PROPERTY_RECORD_ID); + + // date et heure de l'enregistrement (non importé mais utile pour contrôle à l'import) + newMandatoryColumn("Date", BigfinDataRow.PROPERTY_DATE, new Common.DateValue("MM/dd/yy HH:mm:ss")); + + newIgnoredColumn("LOC"); + + // n° de la station (non importé mais utile pour contrôle à l'import) + newMandatoryColumn("STA", BigfinDataRow.PROPERTY_STATION); + + newMandatoryColumn("COMMENT", BigfinDataRow.PROPERTY_VRAC_HORS_VRAC, new VracHorsVracValueParser()); + + newIgnoredColumn("GPS X"); + + newIgnoredColumn("GPS Y"); + + // code espèce = code campagne (saisie libre donc risque fort de mauvaise saisie) + newMandatoryColumn("SPEC", BigfinDataRow.PROPERTY_SPECIES_OR_SPECIES_BATCH, new SpeciesOrSpeciesBatchValueParser(speciesBySurveyCode, speciesBatchesById)); + + newMandatoryColumn("LENGTH (mm)", BigfinDataRow.PROPERTY_LENGTH, Common.PRIMITIVE_FLOAT); + + newMandatoryColumn("WEIGHT (g)", BigfinDataRow.PROPERTY_WEIGHT, Common.FLOAT); + + newMandatoryColumn("SEX", BigfinDataRow.PROPERTY_SEX, new SexValueParser()); + + // sz class : si code différents de 0 1 ou 2 alors tout bloquer et donner l'id des lignes en anomalies + newMandatoryColumn("SIZE", BigfinDataRow.PROPERTY_SIZE, new SizeValueParser()); + + newIgnoredColumn("MT"); + newIgnoredColumn("MS"); + + } + + @Override + public BigfinDataRow newEmptyInstance() { + return new BigfinDataRow(); + } + + private static class SpeciesOrSpeciesBatchValueParser implements ValueParser<SpeciesOrSpeciesBatch> { + + final Map<String, Species> foundSpecies = new HashMap<>(); + + private final Map<String, Species> speciesBySurveyCode; + + private final Map<Integer, SpeciesBatch> speciesBatches; + + public SpeciesOrSpeciesBatchValueParser(Map<String, Species> speciesBySurveyCode, Map<Integer, SpeciesBatch> speciesBatchesById) { + this.speciesBySurveyCode = speciesBySurveyCode; + this.speciesBatches = speciesBatchesById; + } + + @Override + public SpeciesOrSpeciesBatch parse(String value) throws ParseException { + + SpeciesBatch speciesBatch = null; + Species species = null; + + if (StringUtils.isNotBlank(value)) { + + speciesBatch = getSpeciesBatch(value); + + if (speciesBatch == null) { + + // try a species + species = getSpecies(value); + } + + } + + SpeciesOrSpeciesBatch result; + + if (speciesBatch == null) { + + // Just a species + result = new SpeciesOrSpeciesBatch(species); + } else { + + // SpeciesBatch + result = new SpeciesOrSpeciesBatch(speciesBatch); + } + + return result; + + } + + protected SpeciesBatch getSpeciesBatch(String value) { + + SpeciesBatch speciesBatch = null; + + if (StringUtils.isNotBlank(value)) { + try { + Integer batchId = Integer.valueOf(value); + speciesBatch = speciesBatches.get(batchId); + } catch (NumberFormatException e) { + // Not a number + // Not a species batch + } + + } + + return speciesBatch; + + } + + protected Species getSpecies(String value) { + + Species species; + + if (StringUtils.isBlank(value)) { + + species = Speciess.newSpecies(); + + } else { + + // use upper case + value = value.toUpperCase(); + + // if code already found + species = foundSpecies.get(value); + + //if not found, look for it in the survey codes + if (species == null) { + species = speciesBySurveyCode.get(value); + } + + // Si on ne trouve pas une espèce de code campagne XXXXXXX, on essaye alors avec le code XXXX-XXX + if (species == null) { + String alternativeSpeciesCode = value.substring(0, 4) + '-' + value.substring(4); + species = speciesBySurveyCode.get(alternativeSpeciesCode); + } + + if (species == null) { + species = Speciess.newSpecies(); + species.setExternalCode(value); + } + + // record the code in the found codes + foundSpecies.put(value, species); + + } + + return species; + + } + + } + + private static class SizeValueParser implements ValueParser<Size> { + @Override + public Size parse(String value) throws ParseException { + Size result = Size.getValue(value.toUpperCase()); + return result; + } + } + + private static class SexValueParser implements ValueParser<Sex> { + @Override + public Sex parse(String value) throws ParseException { + Sex result = Sex.getValue(value.toUpperCase()); + if (result == null) { + throw new ParseException("Could not parse Sex value: " + value, 0); + } + return result; + } + } + + private static class VracHorsVracValueParser implements ValueParser<VracHorsVrac> { + @Override + public VracHorsVrac parse(String value) throws ParseException { + // On importe dans le Vrac par défaut, sauf si il y a le texte HV ou hv dans le champ text + VracHorsVrac result = VracHorsVrac.getValue(value.toUpperCase()); + if (result == null) { + result = VracHorsVrac.VRAC; + } + return result; + } + } +} diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/SpeciesOrSpeciesBatch.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/SpeciesOrSpeciesBatch.java new file mode 100644 index 0000000..c7681b1 --- /dev/null +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/bigfin/csv/SpeciesOrSpeciesBatch.java @@ -0,0 +1,42 @@ +package fr.ifremer.tutti.service.bigfin.csv; + +import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch; +import fr.ifremer.tutti.persistence.entities.referential.Species; + +/** + * Created on 2/3/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.13 + */ +public class SpeciesOrSpeciesBatch { + + protected Species species; + + protected SpeciesBatch batch; + + public SpeciesOrSpeciesBatch(Species species) { + this.species = species; + } + + public SpeciesOrSpeciesBatch(SpeciesBatch batch) { + this.species = batch.getSpecies(); + this.batch = batch; + } + + public boolean isSpecies() { + return batch == null; + } + + public boolean isBatch() { + return batch != null; + } + + public Species getSpecies() { + return species; + } + + public SpeciesBatch getBatch() { + return batch; + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.