View Javadoc
1   package fr.ifremer.tutti.service.referential.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.Species;
28  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
29  import fr.ifremer.tutti.service.DecoratorService;
30  import fr.ifremer.tutti.service.PersistenceService;
31  import fr.ifremer.tutti.service.csv.CsvComsumer;
32  import fr.ifremer.tutti.service.referential.ReferentialImportRequest;
33  import fr.ifremer.tutti.service.referential.csv.SpeciesModel;
34  import fr.ifremer.tutti.service.referential.csv.SpeciesRow;
35  import org.apache.commons.lang3.BooleanUtils;
36  import org.apache.commons.lang3.StringUtils;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.nuiton.csv.ImportRow;
40  import org.nuiton.jaxx.application.ApplicationBusinessException;
41  
42  import java.nio.file.Path;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * Created on 2/11/15.
48   *
49   * @author Tony Chemit - chemit@codelutin.com
50   * @since 3.14
51   */
52  public class CsvConsumerForTemporarySpecies extends CsvComsumer<SpeciesRow, SpeciesModel> {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(CsvConsumerForTemporarySpecies.class);
56  
57      public CsvConsumerForTemporarySpecies(Path file, char separator, boolean addReferenceTaxonId, boolean reportError) {
58          super(file, SpeciesModel.forImport(separator, addReferenceTaxonId), reportError);
59      }
60  
61      public void checkRow(ImportRow<SpeciesRow> row,
62                           PersistenceService persistenceService,
63                           DecoratorService decoratorService,
64                           ReferentialImportRequest<Species, Integer> requestResult) {
65  
66          if (row.isValid()) {
67  
68              SpeciesRow bean = row.getBean();
69  
70              Integer id = bean.getIdAsInt();
71              boolean delete = BooleanUtils.isTrue(bean.getToDelete());
72  
73              if (id == null) {
74  
75                  // Ajout
76                  checkAdd(bean, requestResult);
77  
78              } else {
79  
80                  // Mise à jour ou Suppression
81  
82                  Species species = requestResult.getExistingEntityById(id);
83  
84                  if (species == null) {
85                      throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.notExistingId", id));
86                  }
87  
88                  if (delete) {
89  
90                      // Suppression
91                      checkDelete(bean, species, persistenceService, decoratorService, requestResult);
92  
93                  } else {
94  
95                      // Mise à jour
96                      checkUpdate(bean, species, requestResult);
97  
98                  }
99              }
100 
101         }
102 
103         reportError(row);
104 
105     }
106 
107     public void checkRowForGenericFormatImport(ImportRow<SpeciesRow> row, ReferentialImportRequest<Species, Integer> requestResult) {
108 
109         SpeciesRow bean = row.getBean();
110         String name = bean.getName();
111 
112         if (row.isValid()) {
113 
114             Integer id = bean.getIdAsInt();
115 
116             if (id == null) {
117 
118                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.noId")));
119 
120             } else if (!Speciess.isTemporaryId(id)) {
121 
122                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.idNotTemporary", id)));
123 
124             } else if (requestResult.isIdAlreadyAdded(id)) {
125 
126                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.id.alreaydAdded", id)));
127 
128             }
129 
130             if (StringUtils.isBlank(name)) {
131 
132                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.noName")));
133 
134             } else if (requestResult.isNaturalIdAlreadyAdded(name)) {
135 
136                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.name.alreaydAdded", name)));
137 
138             }
139 
140             if (bean.getReferenceTaxonId() == null) {
141 
142                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.species.error.noReferencetTaxonId")));
143 
144             }
145 
146         }
147 
148         reportError(row);
149 
150         if (row.isValid()) {
151 
152             Species entity = bean.toEntity(null);
153             boolean toAdd = requestResult.addExistingNaturalId(name);
154             if (toAdd) {
155 
156                 if (log.isInfoEnabled()) {
157                     log.info("Will add species with name: " + name);
158                 }
159                 requestResult.addEntityToAdd(entity);
160 
161             } else {
162 
163                 if (log.isInfoEnabled()) {
164                     log.info("Will link species with name: " + name);
165                 }
166                 requestResult.addEntityToLink(entity);
167 
168             }
169 
170         }
171 
172     }
173 
174     protected void checkAdd(SpeciesRow bean, ReferentialImportRequest<Species, Integer> requestResult) {
175 
176         String name = bean.getName();
177         boolean delete = BooleanUtils.isTrue(bean.getToDelete());
178 
179         if (delete) {
180             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.cannotDeleteWithoutId"));
181         }
182 
183         if (StringUtils.isBlank(name)) {
184             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.noName"));
185         }
186 
187         if (!requestResult.addExistingNaturalId(name)) {
188             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.existingName", name));
189         }
190 
191         if (log.isInfoEnabled()) {
192             log.info("Will add species with name: " + name);
193         }
194 
195         requestResult.addEntityToAdd(bean.toEntity(null));
196 
197 
198     }
199 
200     protected void checkDelete(SpeciesRow bean, Species species, PersistenceService persistenceService,
201                                DecoratorService decoratorService,
202                                ReferentialImportRequest<Species, Integer> requestResult) {
203 
204         Integer id = bean.getIdAsInt();
205         String name = bean.getName();
206         Integer referenceTaxonId = species.getReferenceTaxonId();
207 
208         if (persistenceService.isTemporarySpeciesUsed(referenceTaxonId)) {
209 
210             String speciesRef = id + " : " + decoratorService.getDecoratorByType(Species.class, DecoratorService.WITH_SURVEY_CODE).toString(species);
211             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.used", speciesRef));
212         }
213 
214         if (log.isInfoEnabled()) {
215             log.info("Will delete species with referenceTaxonId: " + referenceTaxonId);
216         }
217 
218         requestResult.addIdToDelete(referenceTaxonId);
219         requestResult.removeExistingNaturalId(name);
220 
221     }
222 
223     protected void checkUpdate(SpeciesRow bean, Species species, ReferentialImportRequest<Species, Integer> requestResult) {
224 
225         Integer id = bean.getIdAsInt();
226         String name = bean.getName();
227         Integer referenceTaxonId = species.getReferenceTaxonId();
228 
229         if (StringUtils.isBlank(name)) {
230             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.noName", id));
231         }
232 
233         if (!species.getName().equals(name) && !requestResult.addExistingNaturalId(name)) {
234             throw new ApplicationBusinessException(t("tutti.service.referential.import.species.error.existingName", name));
235         }
236 
237         if (log.isInfoEnabled()) {
238             log.info("Will update species with referenceTaxonId: " + referenceTaxonId);
239         }
240 
241         requestResult.addEntityToUpdate(bean.toEntity(referenceTaxonId));
242 
243     }
244 
245 
246 }