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.referential.Caracteristic;
28  import fr.ifremer.tutti.persistence.entities.referential.Gear;
29  import fr.ifremer.tutti.service.csv.CaracteristicValueParseException;
30  import fr.ifremer.tutti.service.csv.CsvComsumer;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
32  import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
33  import fr.ifremer.tutti.service.genericformat.GenericFormatImportEntityParserFactory;
34  import fr.ifremer.tutti.service.genericformat.GenericFormatImportValidationHelper;
35  import fr.ifremer.tutti.service.genericformat.csv.GearCaracteristicModel;
36  import fr.ifremer.tutti.service.genericformat.csv.GearCaracteristicRow;
37  import org.nuiton.csv.ImportRow;
38  
39  import java.io.Serializable;
40  import java.nio.file.Path;
41  
42  /**
43   * Created on 2/11/15.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 3.14
47   */
48  public class CsvConsumerForGearCaracteristic extends CsvComsumer<GearCaracteristicRow, GearCaracteristicModel> {
49  
50      public CsvConsumerForGearCaracteristic(Path file, char separator, GenericFormatImportEntityParserFactory parserFactory, boolean reportError) {
51          super(file, GearCaracteristicModel.forImport(separator, parserFactory), reportError);
52      }
53  
54      public GenericFormatImportCruiseContext validateRow(ImportRow<GearCaracteristicRow> row, GenericFormatContextSupport importContext) {
55  
56  
57          GenericFormatImportValidationHelper validationHelper = importContext.getValidationHelper();
58  
59          GenericFormatImportCruiseContext cruiseContext = validationHelper.getCruise(this, row, importContext);
60  
61          if (cruiseContext != null) {
62  
63              GearCaracteristicRow bean = row.getBean();
64  
65              Gear gear = bean.getGear();
66              short rankOrder = bean.getRankOrder();
67  
68              Gear cruiseGear = validationHelper.getGear(this, importContext, row, gear, rankOrder);
69              if (cruiseGear != null) {
70                  bean.setGear(cruiseGear);
71              }
72  
73              //TODO Use a validator ?
74              Caracteristic caracteristic = bean.getCaracteristic();
75              if (caracteristic == null) {
76                  //TODO Should done by parser ?
77              }
78  
79              //TODO Use a validator ?
80              if (bean.getValue() == null) {
81  
82              }
83  
84              // parse caracteristic value
85              String value = (String) bean.getValue();
86              try {
87                  Serializable serializable = importContext.parseCaracteristicValue(caracteristic, value);
88                  bean.setValue(serializable);
89              } catch (CaracteristicValueParseException e) {
90                  addCheckError(row, e);
91              }
92  
93          }
94  
95          reportError(row);
96  
97          return cruiseContext;
98  
99      }
100 
101     public void prepareRowForPersist(GenericFormatImportCruiseContext cruiseContext, ImportRow<GearCaracteristicRow> row) {
102 
103         GearCaracteristicRow bean = row.getBean();
104 
105         cruiseContext.addGearCaracteristic(bean.getGear(), bean.getCaracteristic(), bean.getValue());
106 
107     }
108 
109 }