View Javadoc
1   package fr.ifremer.tutti.service.genericformat.consumer;
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.data.AccidentalBatchs;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.service.csv.CaracteristicValueParseException;
32  import fr.ifremer.tutti.service.csv.CsvComsumer;
33  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
34  import fr.ifremer.tutti.service.genericformat.GenericFormatImportEntityParserFactory;
35  import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
36  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchModel;
37  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.nuiton.csv.ImportRow;
41  
42  import java.io.Serializable;
43  import java.nio.file.Path;
44  
45  /**
46   * Created on 2/11/15.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.14
50   */
51  public class CsvConsumerForAccidentalCatch extends CsvComsumer<AccidentalCatchRow, AccidentalCatchModel> {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(CsvConsumerForAccidentalCatch.class);
55  
56      public CsvConsumerForAccidentalCatch(Path file, char separator, GenericFormatImportEntityParserFactory parserFactory, boolean reportError) {
57          super(file, AccidentalCatchModel.forImport(separator, parserFactory), reportError);
58      }
59  
60      public GenericFormatImportOperationContext validateRow(ImportRow<AccidentalCatchRow> row, GenericFormatContextSupport importContext) {
61  
62          GenericFormatImportOperationContext operationContext = importContext.getValidationHelper().getFishingOperationContext(this, row, importContext);
63  
64          if (operationContext != null) {
65  
66              //TODO checks!
67              AccidentalCatchRow bean = row.getBean();
68  
69              //TODO Use a validator ?
70              Caracteristic caracteristic = bean.getCaracteristic();
71              if (caracteristic == null) {
72                  //TODO Should done by parser ?
73              }
74  
75              //TODO Use a validator ?
76              if (bean.getCaracteristicValue() == null) {
77  
78              }
79  
80              // parse caracteristic value
81              String value = (String) bean.getCaracteristicValue();
82              try {
83                  Serializable serializable = importContext.parseCaracteristicValue(caracteristic, value);
84                  bean.setCaracteristicValue(serializable);
85              } catch (CaracteristicValueParseException e) {
86                  addCheckError(row, e);
87              }
88  
89          }
90  
91          reportError(row);
92  
93          return operationContext;
94  
95      }
96  
97      public void prepareRowForPersist(GenericFormatImportOperationContext operationContext, ImportRow<AccidentalCatchRow> row) {
98  
99          AccidentalCatchRow bean = row.getBean();
100         Integer batchId = bean.getBatchId();
101         AccidentalBatch batch = operationContext.getAccidentalBatchById(batchId);
102         if (batch == null) {
103 
104             if (log.isInfoEnabled()) {
105                 log.info("Detects accidentalBatch: " + batchId);
106             }
107 
108             batch = AccidentalBatchs.newAccidentalBatch();
109             batch.setFishingOperation(operationContext.getFishingOperation());
110             batch.setCaracteristics(new CaracteristicMap());
111             batch.setComment(bean.getComment());
112             batch.setSpecies(bean.getSpecies());
113             batch.setId(bean.getBatchId());
114             operationContext.addAccidentalBatch(batchId, batch);
115 
116         }
117 
118         batch.getCaracteristics().put(bean.getCaracteristic(), bean.getCaracteristicValue());
119 
120     }
121 }