View Javadoc
1   package fr.ifremer.tutti.service.genericformat.producer;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Maps;
29  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
30  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
31  import fr.ifremer.tutti.persistence.entities.referential.Species;
32  import fr.ifremer.tutti.service.csv.CsvProducer;
33  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
34  import fr.ifremer.tutti.service.genericformat.csv.IndividualObservationRow;
35  import fr.ifremer.tutti.service.genericformat.csv.SpeciesExportModel;
36  import fr.ifremer.tutti.service.genericformat.csv.SpeciesExportRow;
37  
38  import java.nio.file.Path;
39  import java.util.Collections;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * Created on 2/6/15.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 3.13
48   */
49  public class CsvProducerForSpecies extends CsvProducer<SpeciesExportRow, SpeciesExportModel> {
50  
51      final Map<String, Species> speciesByReferenceTaxonId;
52  
53      final Map<String, SpeciesExportRow> speciesToExport = Maps.newTreeMap();
54  
55      public CsvProducerForSpecies(Path file, SpeciesExportModel model, Map<String, Species> speciesByReferenceTaxonId) {
56          super(file, model);
57          this.speciesByReferenceTaxonId = speciesByReferenceTaxonId;
58      }
59  
60      public void prepareSpeciesBatchRows(BatchContainer<SpeciesBatch> rootSpeciesBatch) {
61  
62          for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) {
63              addSpecies(speciesBatch.getSpecies());
64          }
65  
66      }
67  
68      public void prepareBenthosBatchRows(BatchContainer<SpeciesBatch> rootBenthosBatch) {
69  
70          for (SpeciesBatch benthosBatch : rootBenthosBatch.getChildren()) {
71              addSpecies(benthosBatch.getSpecies());
72          }
73  
74      }
75  
76      public void prepareIndividualRows(List<IndividualObservationRow> rows) {
77  
78          for (IndividualObservationRow row : rows) {
79              addSpecies(row.getSpecies());
80          }
81  
82      }
83  
84      public void prepareAccidentalRows(List<AccidentalCatchRow> rows) {
85  
86          for (AccidentalCatchRow row : rows) {
87              addSpecies(row.getSpecies());
88          }
89  
90      }
91  
92      public List<SpeciesExportRow> getDataToExport() {
93  
94          List<SpeciesExportRow> result = Lists.newArrayList(speciesToExport.values());
95  
96          Collections.sort(result, (o1, o2) -> o1.getSpecies().getReferenceTaxonId().compareTo(o2.getSpecies().getReferenceTaxonId()));
97          return result;
98  
99      }
100 
101     public void addSpecies(Species species) {
102 
103         String speciesId = String.valueOf(species.getReferenceTaxonId());
104 
105         if (!speciesToExport.containsKey(speciesId)) {
106 
107             // not treated species, add a new row
108             Species fullSpecies = speciesByReferenceTaxonId.get(speciesId);
109             SpeciesExportRow row = new SpeciesExportRow();
110             row.setSpecies(fullSpecies);
111             speciesToExport.put(speciesId, row);
112             // add the survey code (see http://forge.codelutin.com/issues/4799)
113             species.setSurveyCode(fullSpecies.getSurveyCode());
114         }
115     }
116 
117 }