View Javadoc
1   package fr.ifremer.tutti.service.genericformat.csv;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.persistence.entities.data.Cruise;
26  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
27  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModelEntry;
29  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.service.csv.AbstractTuttiImportExportModel;
32  import fr.ifremer.tutti.service.csv.CaracteristicValueParserFormatter;
33  import fr.ifremer.tutti.service.csv.TuttiCsvUtil;
34  import fr.ifremer.tutti.service.genericformat.GenericFormatImportEntityParserFactory;
35  import org.apache.commons.beanutils.PropertyUtils;
36  import org.nuiton.csv.ValueSetter;
37  
38  import java.io.Serializable;
39  
40  /**
41   * Model of a catch export.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 1.3
45   */
46  public class CatchModel extends AbstractTuttiImportExportModel<CatchRow> {
47  
48      public static final String WEIGHT_OR_VOL_TYPE = "Poids";
49  
50      public static CatchModel forExport(char separator, SampleCategoryModel sampleCategoryModel) {
51  
52          CatchModel exportModel = new CatchModel(separator);
53          exportModel.forExport(sampleCategoryModel);
54          return exportModel;
55  
56      }
57  
58      public static CatchModel forImport(char separator, SampleCategoryModel sampleCategoryModel, GenericFormatImportEntityParserFactory parserFactory) {
59  
60          CatchModel importModel = new CatchModel(separator);
61          importModel.forImport(sampleCategoryModel, parserFactory);
62          return importModel;
63  
64      }
65  
66      @Override
67      public CatchRow newEmptyInstance() {
68  
69          CatchRow row = CatchRow.newEmptyInstance();
70          row.forImport();
71          return row;
72  
73      }
74  
75      protected CatchModel(char separator) {
76          super(separator);
77      }
78  
79      protected void forExport(SampleCategoryModel sampleCategoryModel) {
80  
81          newColumnForExport("Annee", Cruise.PROPERTY_BEGIN_DATE, TuttiCsvUtil.YEAR);
82          newColumnForExport("Serie", Cruise.PROPERTY_PROGRAM, TuttiCsvUtil.PROGRAM_FORMATTER);
83          newColumnForExport("Serie_Partielle", Cruise.PROPERTY_SURVEY_PART);
84  
85          newColumnForExport("Code_Station", FishingOperation.PROPERTY_STATION_NUMBER);
86          newColumnForExport("Id_Operation", FishingOperation.PROPERTY_FISHING_OPERATION_NUMBER, TuttiCsvUtil.INTEGER);
87          newColumnForExport("Poche", FishingOperation.PROPERTY_MULTIRIG_AGGREGATION);
88  
89          newColumnForExport("Code_Taxon", SpeciesBatch.PROPERTY_SPECIES, TuttiCsvUtil.SPECIES_TECHNICAL_FORMATTER);
90          newColumnForExport("Code_Espece_Campagne", SpeciesBatch.PROPERTY_SPECIES, TuttiCsvUtil.SPECIES_SURVEY_CODE_FORMATTER);
91          newColumnForExport("Nom_Scientifique", SpeciesBatch.PROPERTY_SPECIES, TuttiCsvUtil.SPECIES_FORMATTER);
92          newColumnForExport("Benthos", CatchRow.PROPERTY_BENTHOS, TuttiCsvUtil.BOOLEAN);
93          newColumnForExport("Lot_A_Confirmer", CatchRow.PROPERTY_SPECIES_TO_CONFIRM, TuttiCsvUtil.BOOLEAN);
94  
95          for (SampleCategoryModelEntry entry : sampleCategoryModel.getCategory()) {
96  
97              String headerPrefix = entry.getCode();
98              int categoryOrder = entry.getOrder();
99              String collectionName = CatchRow.SAMPLE_CATEGORY;
100             newIndexNullableColumnForExport(headerPrefix, collectionName, categoryOrder, ExportSampleCategory.PROPERTY_CATEGORY_VALUE, TuttiCsvUtil.CARACTERISTIC_VALUE_FORMATTER);
101             newIndexNullableColumnForExport("Num_Ordre_" + headerPrefix + "_H2", collectionName, categoryOrder, ExportSampleCategory.PROPERTY_RANK_ORDER, TuttiCsvUtil.PRIMITIVE_INTEGER);
102             newIndexNullableColumnForExport("Tot_" + headerPrefix, collectionName, categoryOrder, ExportSampleCategory.PROPERTY_CATEGORY_WEIGHT, TuttiCsvUtil.FLOAT);
103             newIndexNullableColumnForExport("Ech_" + headerPrefix, collectionName, categoryOrder, ExportSampleCategory.PROPERTY_SAMPLE_WEIGHT, TuttiCsvUtil.FLOAT);
104             newIndexNullableColumnForExport("Type_Volume_Poids_" + headerPrefix, collectionName, categoryOrder, ExportSampleCategory.PROPERTY_WEIGHT_OR_VOL_TYPE);
105             newNullableColumnForExport("Unite_Volume_Poids_" + headerPrefix, CatchRow.BATCH_WEIGHT_UNIT);
106             newIndexNullableColumnForExport("Commentaire_" + headerPrefix, collectionName, categoryOrder, ExportSampleCategory.PROPERTY_COMMENT, TuttiCsvUtil.COMMENT_PARSER_FORMATTER);
107 
108         }
109 
110         // mensuration
111 
112         newColumnForExport("Code_Longueur", CatchRow.FREQUENCY_LENGTH_STEP_CARACTERISTIC, TuttiCsvUtil.CARACTERISTIC_TECHNICAL_FORMATTER);
113         newColumnForExport("Libelle_Longueur", CatchRow.FREQUENCY_LENGTH_STEP_CARACTERISTIC, TuttiCsvUtil.CARACTERISTIC_FORMATTER);
114         newColumnForExport("Taille", CatchRow.FREQUENCY_LENGTH_STEP, TuttiCsvUtil.FLOAT);
115         newColumnForExport("NumOrdre_Taille_H2", CatchRow.FREQUENCY_RANK_ORDER, TuttiCsvUtil.INTEGER);
116         newColumnForExport("Poids_Classe_Taille", CatchRow.FREQUENCY_WEIGHT, TuttiCsvUtil.FLOAT);
117         newNullableColumnForExport("Unite_Taille", CatchRow.FREQUENCY_LENGTH_STEP_CARACTERISTIC + "." + Caracteristic.PROPERTY_UNIT);
118         newNullableColumnForExport("Precision_Mesure", CatchRow.FREQUENCY_LENGTH_STEP_CARACTERISTIC + "." + Caracteristic.PROPERTY_PRECISION, TuttiCsvUtil.FLOAT);
119         newColumnForExport("Nbr", CatchRow.BATCH_NUMBER, TuttiCsvUtil.INTEGER);
120         newColumnForExport("Nbr_Calcule", CatchRow.BATCH_NUMBER_COMPUTED, TuttiCsvUtil.BOOLEAN);
121 
122         newColumnForExport("Poids_Reference", CatchRow.REFERENCE_WEIGHT, TuttiCsvUtil.PRIMITIVE_FLOAT);
123         newColumnForExport("Coef_Elev_Espece_Capture", CatchRow.RAISING_FACTOR, TuttiCsvUtil.PRIMITIVE_FLOAT);
124         newColumnForExport("Coef_Final_Elevation", CatchRow.FINAL_RAISING_FACTOR, TuttiCsvUtil.FLOAT);
125 
126         newColumnForExport("Serie_Id", Cruise.PROPERTY_PROGRAM, TuttiCsvUtil.PROGRAM_TECHNICAL_FORMATTER);
127         for (SampleCategoryModelEntry entry : sampleCategoryModel.getCategory()) {
128 
129             String headerPrefix = entry.getCode();
130             int categoryOrder = entry.getOrder();
131             String collectionName = CatchRow.SAMPLE_CATEGORY;
132 
133             newIndexNullableColumnForExport(headerPrefix + "_Id", collectionName, categoryOrder, ExportSampleCategory.PROPERTY_CATEGORY_VALUE, TuttiCsvUtil.CARACTERISTIC_VALUE_TECHNICAL_FORMATTER);
134             newIndexNullableColumnForExport(headerPrefix + "_Lot_Id", collectionName, categoryOrder, ExportSampleCategory.PROPERTY_BATCH_ID, TuttiCsvUtil.INTEGER);
135 
136         }
137 
138     }
139 
140     protected void forImport(SampleCategoryModel sampleCategoryModel, GenericFormatImportEntityParserFactory parserFactory) {
141 
142         newMandatoryColumn("Annee", Cruise.PROPERTY_BEGIN_DATE, TuttiCsvUtil.YEAR);
143         newIgnoredColumn("Serie");
144         newMandatoryColumn("Serie_Partielle", Cruise.PROPERTY_SURVEY_PART);
145 
146         newMandatoryColumn("Code_Station", FishingOperation.PROPERTY_STATION_NUMBER);
147         newMandatoryColumn("Id_Operation", FishingOperation.PROPERTY_FISHING_OPERATION_NUMBER, TuttiCsvUtil.INTEGER);
148         newMandatoryColumn("Poche", FishingOperation.PROPERTY_MULTIRIG_AGGREGATION);
149 
150         newMandatoryColumn("Code_Taxon", SpeciesBatch.PROPERTY_SPECIES, parserFactory.getSpeciesParser());
151         newIgnoredColumn("Code_Espece_Campagne");
152         newIgnoredColumn("Nom_Scientifique");
153         newMandatoryColumn("Benthos", CatchRow.PROPERTY_BENTHOS, TuttiCsvUtil.BOOLEAN);
154         newMandatoryColumn("Lot_A_Confirmer", CatchRow.PROPERTY_SPECIES_TO_CONFIRM, TuttiCsvUtil.BOOLEAN);
155 
156         for (SampleCategoryModelEntry entry : sampleCategoryModel.getCategory()) {
157 
158             String headerPrefix = entry.getCode();
159 
160             newIgnoredColumn(headerPrefix);
161             newMandatoryColumn("Num_Ordre_" + headerPrefix + "_H2", TuttiCsvUtil.PRIMITIVE_INTEGER, new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_RANK_ORDER));
162             newMandatoryColumn("Tot_" + headerPrefix, TuttiCsvUtil.FLOAT, new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_CATEGORY_WEIGHT));
163             newMandatoryColumn("Ech_" + headerPrefix, TuttiCsvUtil.FLOAT, new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_SAMPLE_WEIGHT));
164 
165             newIgnoredColumn("Type_Volume_Poids_" + headerPrefix);
166             newIgnoredColumn("Unite_Volume_Poids_" + headerPrefix);
167 
168             newMandatoryColumn("Commentaire_" + headerPrefix, TuttiCsvUtil.COMMENT_PARSER_FORMATTER, new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_COMMENT));
169 
170         }
171 
172         // mensuration
173 
174         newMandatoryColumn("Code_Longueur", CatchRow.FREQUENCY_LENGTH_STEP_CARACTERISTIC, parserFactory.getCaracteristicParser());
175         newIgnoredColumn("Libelle_Longueur");
176         newMandatoryColumn("Taille", CatchRow.FREQUENCY_LENGTH_STEP, TuttiCsvUtil.FLOAT);
177         newMandatoryColumn("NumOrdre_Taille_H2", CatchRow.FREQUENCY_RANK_ORDER, TuttiCsvUtil.INTEGER);
178         newMandatoryColumn("Poids_Classe_Taille", CatchRow.FREQUENCY_WEIGHT, TuttiCsvUtil.FLOAT);
179         newIgnoredColumn("Unite_Taille");
180         newIgnoredColumn("Precision_Mesure");
181         newMandatoryColumn("Nbr", CatchRow.BATCH_NUMBER, TuttiCsvUtil.INTEGER);
182         newMandatoryColumn("Nbr_Calcule", CatchRow.BATCH_NUMBER_COMPUTED, TuttiCsvUtil.BOOLEAN);
183 
184         newIgnoredColumn("Poids_Reference");
185         newIgnoredColumn("Coef_Elev_Espece_Capture");
186         newIgnoredColumn("Coef_Final_Elevation");
187 
188         newMandatoryColumn("Serie_Id", Cruise.PROPERTY_PROGRAM, parserFactory.getProgramParser());
189         for (SampleCategoryModelEntry entry : sampleCategoryModel.getCategory()) {
190 
191             String headerPrefix = entry.getCode();
192             newMandatoryColumn(headerPrefix + "_Id", CaracteristicValueParserFormatter.newParser(entry.getCaracteristic()), new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_CATEGORY_VALUE));
193             newMandatoryColumn(headerPrefix + "_Lot_Id", TuttiCsvUtil.INTEGER, new SampleCategoryValueSetter<>(entry, ExportSampleCategory.PROPERTY_BATCH_ID));
194         }
195 
196     }
197 
198     private static class SampleCategoryValueSetter<O> implements ValueSetter<CatchRow, O> {
199 
200         private final SampleCategoryModelEntry sampleCategoryModelEntry;
201 
202         private final String propertyName;
203 
204         public SampleCategoryValueSetter(SampleCategoryModelEntry sampleCategoryModelEntry, String propertyName) {
205             this.sampleCategoryModelEntry = sampleCategoryModelEntry;
206             this.propertyName = propertyName;
207         }
208 
209         @Override
210         public void set(CatchRow row, O value) throws Exception {
211 
212             ExportSampleCategory sampleCategory = row.getSampleCategory(sampleCategoryModelEntry);
213             PropertyUtils.setProperty(sampleCategory, propertyName, value);
214 
215         }
216     }
217 
218 }