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.data.CatchBatch;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.persistence.entities.referential.Gear;
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.OperationModel;
36  import fr.ifremer.tutti.service.genericformat.csv.OperationRow;
37  import org.apache.commons.lang3.BooleanUtils;
38  import org.nuiton.csv.ImportRow;
39  import org.nuiton.validator.NuitonValidatorResult;
40  import org.nuiton.validator.NuitonValidatorScope;
41  
42  import java.nio.file.Path;
43  import java.util.Set;
44  
45  /**
46   * Created on 2/11/15.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.14
50   */
51  public class CsvConsumerForOperation extends CsvComsumer<OperationRow, OperationModel> {
52  
53      public CsvConsumerForOperation(Path file, char separator, GenericFormatImportEntityParserFactory parserFactory, boolean reportError) {
54          super(file, OperationModel.forImport(separator, parserFactory), reportError);
55      }
56  
57      public GenericFormatImportCruiseContext validateRow(ImportRow<OperationRow> row, GenericFormatContextSupport importContext) {
58  
59          GenericFormatImportValidationHelper validationHelper = importContext.getValidationHelper();
60  
61          GenericFormatImportCruiseContext cruiseContext = validationHelper.getCruise(this, row, importContext);
62  
63          if (cruiseContext != null) {
64  
65              OperationRow bean = row.getBean();
66  
67              FishingOperation fishingOperation = bean.getFishingOperation();
68  
69              if (cruiseContext.isFishingOperationAlreadyImported(bean)) {
70  
71                  addCheckError(row, new FishingOperationAlreadyImportedException(fishingOperation));
72  
73              } else {
74  
75                  Gear gear = bean.getGear();
76                  Short rankOrder = bean.getGearRankOrder();
77  
78                  if (gear != null && rankOrder != null) {
79  
80                      Gear cruiseGear = validationHelper.getGear(this, importContext, row, gear, rankOrder);
81                      if (cruiseGear != null) {
82                          bean.setGear(cruiseGear);
83                      }
84                  }
85                  NuitonValidatorResult validatorResult = validationHelper.validateFishingOperation(fishingOperation);
86  
87                  if (validatorResult.hasFatalMessages()) {
88  
89                      Set<String> errorMessages = validationHelper.getMessages(validatorResult, NuitonValidatorScope.FATAL);
90                      addCheckError(row, new FishingOperationNotValidException(fishingOperation, errorMessages));
91  
92                  }
93  
94                  //TODO other checks ?
95  
96              }
97  
98          }
99  
100         reportError(row);
101 
102         return cruiseContext;
103 
104     }
105 
106     public void prepareRowForPersist(ImportRow<OperationRow> row, boolean importSpecies, boolean importBenthos, boolean importMarineLitter) {
107 
108         OperationRow bean = row.getBean();
109 
110         CatchBatch catchBatch = bean.getCatchBatch();
111 
112         if (BooleanUtils.toBooleanDefaultIfNull(bean.getCatchTotalWeightComputed(), false)) {
113             catchBatch.setCatchTotalWeight(null);
114         }
115         if (BooleanUtils.toBooleanDefaultIfNull(bean.getCatchTotalRejectedWeightComputed(), false)) {
116             catchBatch.setCatchTotalRejectedWeight(null);
117         }
118 
119         prepareSpecies(importSpecies, bean, catchBatch);
120 
121         prepareBenthos(importBenthos, bean, catchBatch);
122 
123         prepareMarineLitter(importMarineLitter, bean, catchBatch);
124 
125     }
126 
127     protected void prepareSpecies(boolean importSpecies, OperationRow bean, CatchBatch catchBatch) {
128 
129         if (!importSpecies || BooleanUtils.toBooleanDefaultIfNull(bean.getSpeciesTotalSortedWeightComputed(), false)) {
130             catchBatch.setSpeciesTotalSortedWeight(null);
131         }
132         if (!importSpecies || BooleanUtils.toBooleanDefaultIfNull(bean.getSpeciesTotalInertWeightComputed(), false)) {
133             catchBatch.setSpeciesTotalInertWeight(null);
134         }
135         if (!importSpecies || BooleanUtils.toBooleanDefaultIfNull(bean.getSpeciesTotalLivingNotItemizedWeightComputed(), false)) {
136             catchBatch.setSpeciesTotalLivingNotItemizedWeight(null);
137         }
138 
139     }
140 
141     protected void prepareBenthos(boolean importBenthos, OperationRow bean, CatchBatch catchBatch) {
142         if (!importBenthos || BooleanUtils.toBooleanDefaultIfNull(bean.getBenthosTotalSortedWeightComputed(), false)) {
143             catchBatch.setBenthosTotalSortedWeight(null);
144         }
145         if (!importBenthos || BooleanUtils.toBooleanDefaultIfNull(bean.getBenthosTotalInertWeightComputed(), false)) {
146             catchBatch.setBenthosTotalInertWeight(null);
147         }
148         if (!importBenthos || BooleanUtils.toBooleanDefaultIfNull(bean.getBenthosTotalLivingNotItemizedWeightComputed(), false)) {
149             catchBatch.setBenthosTotalLivingNotItemizedWeight(null);
150         }
151     }
152 
153     protected void prepareMarineLitter(boolean importMarineLitter, OperationRow bean, CatchBatch catchBatch) {
154         if (!importMarineLitter || BooleanUtils.toBooleanDefaultIfNull(bean.getMarineLitterTotalWeightComputed(), false)) {
155             catchBatch.setMarineLitterTotalWeight(null);
156         }
157     }
158 
159 }