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 fr.ifremer.tutti.persistence.entities.CaracteristicMap;
28  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
29  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
30  import fr.ifremer.tutti.service.csv.CsvProducer;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatExportOperationContext;
32  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchModel;
33  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.apache.commons.collections4.MapUtils;
36  
37  import java.io.Serializable;
38  import java.nio.file.Path;
39  import java.util.ArrayList;
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 CsvProducerForAccidentalCatch extends CsvProducer<AccidentalCatchRow, AccidentalCatchModel> {
50  
51      public CsvProducerForAccidentalCatch(Path file, AccidentalCatchModel model) {
52          super(file, model);
53      }
54  
55      public List<AccidentalCatchRow> getDataToExport(GenericFormatExportOperationContext operationExportContext) {
56  
57          List<AccidentalCatchRow> rows = new ArrayList<>();
58  
59          List<AccidentalBatch> accidentalBatches = operationExportContext.getAccidentalBatches();
60  
61          if (CollectionUtils.isNotEmpty(accidentalBatches)) {
62              for (AccidentalBatch accidentalBatch : accidentalBatches) {
63  
64                  addAccidentalBatch(operationExportContext, rows, accidentalBatch);
65  
66              }
67  
68          }
69  
70          return rows;
71  
72      }
73  
74      protected void addAccidentalBatch(GenericFormatExportOperationContext operationExportContext, List<AccidentalCatchRow> rows, AccidentalBatch accidentalBatch) {
75  
76          addCaracteristicRow(operationExportContext,
77                              rows,
78                              accidentalBatch,
79                              operationExportContext.getDeadOrAliveCaracteristic(),
80                              accidentalBatch.getDeadOrAlive());
81  
82          addCaracteristicRow(operationExportContext,
83                              rows,
84                              accidentalBatch,
85                              operationExportContext.getGenderCaracteristic(),
86                              accidentalBatch.getGender());
87  
88          addCaracteristicRow(operationExportContext,
89                              rows,
90                              accidentalBatch,
91                              operationExportContext.getWeightMeasuredCaracteristic(),
92                              accidentalBatch.getWeight());
93  
94          if (accidentalBatch.getLengthStepCaracteristic() != null) {
95              addCaracteristicRow(operationExportContext,
96                                  rows,
97                                  accidentalBatch,
98                                  operationExportContext.getPmfmIdCaracteristic(),
99                                  accidentalBatch.getLengthStepCaracteristic().getIdAsInt());
100 
101             addCaracteristicRow(operationExportContext,
102                                 rows,
103                                 accidentalBatch,
104                                 accidentalBatch.getLengthStepCaracteristic(),
105                                 accidentalBatch.getSize());
106         }
107 
108         CaracteristicMap caracteristics = accidentalBatch.getCaracteristics();
109         if (MapUtils.isNotEmpty(caracteristics)) {
110             for (Map.Entry<Caracteristic, Serializable> entry : caracteristics.entrySet()) {
111                 addCaracteristicRow(operationExportContext,
112                                     rows,
113                                     accidentalBatch,
114                                     entry.getKey(),
115                                     entry.getValue());
116             }
117         }
118 
119     }
120 
121     protected void addCaracteristicRow(GenericFormatExportOperationContext operationExportContext,
122                                        List<AccidentalCatchRow> rows,
123                                        AccidentalBatch accidentalBatch,
124                                        Caracteristic caracteristic,
125                                        Serializable caracteristicValue) {
126         if (caracteristicValue != null) {
127 
128             AccidentalCatchRow row = new AccidentalCatchRow();
129             row.setCruise(operationExportContext.getCruise());
130             row.setFishingOperation(operationExportContext.getOperation());
131 
132             row.setComment(accidentalBatch.getComment());
133             row.setSpecies(accidentalBatch.getSpecies());
134             row.setBatchId(accidentalBatch.getIdAsInt());
135 
136             row.setCaracteristic(caracteristic);
137             row.setCaracteristicValue(caracteristicValue);
138             rows.add(row);
139 
140         }
141     }
142 
143 }