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.Vessel;
28  import fr.ifremer.tutti.persistence.entities.referential.Vessels;
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.VesselModel;
34  import fr.ifremer.tutti.service.referential.csv.VesselRow;
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 CsvConsumerForTemporaryVessel extends CsvComsumer<VesselRow, VesselModel> {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(CsvConsumerForTemporaryVessel.class);
56  
57      public CsvConsumerForTemporaryVessel(Path file, char separator, boolean reportError) {
58          super(file, VesselModel.forImport(separator), reportError);
59      }
60  
61      public void checkRow(ImportRow<VesselRow> row,
62                           PersistenceService persistenceService,
63                           DecoratorService decoratorService,
64                           ReferentialImportRequest<Vessel, String> requestResult) {
65  
66          if (row.isValid()) {
67  
68              VesselRow bean = row.getBean();
69  
70              String id = bean.getId();
71              boolean delete = BooleanUtils.isTrue(bean.getToDelete());
72  
73              if (StringUtils.isBlank(id)) {
74  
75                  // Ajout
76                  checkAdd(bean, requestResult);
77  
78              } else {
79  
80                  // Mise à jour ou Suppression
81  
82                  Vessel vessel = requestResult.getExistingEntityById(id);
83  
84                  if (vessel == null) {
85  
86                      throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.notExistingId", id));
87                  }
88  
89                  if (delete) {
90  
91                      // Suppression
92                      checkDelete(bean, vessel, persistenceService, decoratorService, requestResult);
93  
94                  } else {
95  
96                      // Mise à jour
97                      checkUpdate(bean, vessel, requestResult);
98  
99                  }
100             }
101 
102         }
103 
104         reportError(row);
105 
106     }
107 
108     public void checkRowForGenericFormatImport(ImportRow<VesselRow> row, ReferentialImportRequest<Vessel, String> requestResult) {
109 
110         VesselRow bean = row.getBean();
111         String internationalRegistrationCode = bean.getInternationalRegistrationCode();
112 
113         if (row.isValid()) {
114 
115             String id = bean.getId();
116 
117             if (id == null) {
118 
119                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.noId")));
120 
121             } else if (!Vessels.isTemporaryId(id)) {
122 
123                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.idNotTemporary", id)));
124 
125             } else if (requestResult.isIdAlreadyAdded(id)) {
126 
127                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.id.alreaydAdded", id)));
128 
129             }
130 
131             if (StringUtils.isBlank(internationalRegistrationCode)) {
132 
133                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.noRegistrationCode")));
134 
135             } else if (requestResult.isNaturalIdAlreadyAdded(internationalRegistrationCode)) {
136 
137                 addCheckError(row, new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.name.alreaydAdded", internationalRegistrationCode)));
138 
139             }
140 
141         }
142 
143         reportError(row);
144 
145         if (row.isValid()) {
146 
147             Vessel entity = bean.toEntity();
148             boolean toAdd = requestResult.addExistingNaturalId(internationalRegistrationCode);
149             if (toAdd) {
150 
151                 if (log.isInfoEnabled()) {
152                     log.info("Will add vessel with internationalRegistrationCode: " + internationalRegistrationCode);
153                 }
154                 requestResult.addEntityToAdd(entity);
155 
156             } else {
157 
158                 if (log.isInfoEnabled()) {
159                     log.info("Will link vessel with internationalRegistrationCode: " + internationalRegistrationCode);
160                 }
161                 requestResult.addEntityToLink(entity);
162 
163             }
164 
165         }
166     }
167 
168     protected void checkAdd(VesselRow bean, ReferentialImportRequest<Vessel, String> requestResult) {
169 
170         String internationalRegistrationCode = bean.getInternationalRegistrationCode();
171         boolean delete = BooleanUtils.isTrue(bean.getToDelete());
172 
173         if (delete) {
174             throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.cannotDeleteWithoutId"));
175         }
176 
177         if (StringUtils.isBlank(internationalRegistrationCode)) {
178             throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.noRegistrationCode"));
179         }
180 
181         if (!requestResult.addExistingNaturalId(internationalRegistrationCode)) {
182             throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.existingRegistrationCode", internationalRegistrationCode));
183         }
184 
185         if (log.isInfoEnabled()) {
186             log.info("Will add vessel with internationalRegistrationCode: " + internationalRegistrationCode);
187         }
188 
189         requestResult.addEntityToAdd(bean.toEntity());
190 
191 
192     }
193 
194     protected void checkDelete(VesselRow bean, Vessel vessel,
195                                PersistenceService persistenceService,
196                                DecoratorService decoratorService,
197                                ReferentialImportRequest<Vessel, String> requestResult) {
198 
199         String id = StringUtils.trimToNull(bean.getId());
200         String internationalRegistrationCode = bean.getInternationalRegistrationCode();
201 
202         if (persistenceService.isTemporaryVesselUsed(id)) {
203 
204             String vesselRef = id + " :" + decoratorService.getDecoratorByType(Vessel.class).toString(vessel);
205             throw new ApplicationBusinessException(t("tutti.service.referential.import.error.vessel.used", vesselRef));
206         }
207 
208         if (log.isInfoEnabled()) {
209             log.info("Will delete vessel with internationalRegistrationCode: " + internationalRegistrationCode);
210         }
211 
212         requestResult.addIdToDelete(id);
213         requestResult.removeExistingNaturalId(internationalRegistrationCode);
214 
215     }
216 
217     protected void checkUpdate(VesselRow bean, Vessel vessel, ReferentialImportRequest<Vessel, String> requestResult) {
218 
219         String id = StringUtils.trimToNull(bean.getId());
220         String internationalRegistrationCode = bean.getInternationalRegistrationCode();
221 
222         if (StringUtils.isBlank(internationalRegistrationCode)) {
223             throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.noRegistrationCode", id));
224         }
225 
226         if (!internationalRegistrationCode.equals(vessel.getInternationalRegistrationCode()) && !requestResult.addExistingNaturalId(internationalRegistrationCode)) {
227             throw new ApplicationBusinessException(t("tutti.service.referential.import.vessel.error.existingRegistrationCode", internationalRegistrationCode));
228         }
229 
230         if (log.isInfoEnabled()) {
231             log.info("Will update vessel with internationalRegistrationCode: " + internationalRegistrationCode);
232         }
233 
234         requestResult.addEntityToUpdate(bean.toEntity());
235 
236     }
237 
238 }