View Javadoc
1   package fr.ifremer.tutti.service.referential;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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 com.google.common.collect.Lists;
28  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
29  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
30  import fr.ifremer.tutti.persistence.entities.referential.Vessels;
31  import fr.ifremer.tutti.service.AbstractTuttiService;
32  import fr.ifremer.tutti.service.DecoratorService;
33  import fr.ifremer.tutti.service.PersistenceService;
34  import fr.ifremer.tutti.service.TuttiServiceContext;
35  import fr.ifremer.tutti.service.referential.consumer.CsvConsumerForTemporaryVessel;
36  import fr.ifremer.tutti.service.referential.csv.VesselRow;
37  import fr.ifremer.tutti.service.referential.producer.CsvProducerForTemporaryVessel;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.nuiton.csv.ImportRow;
41  import org.nuiton.jaxx.application.ApplicationTechnicalException;
42  
43  import java.io.File;
44  import java.io.IOException;
45  import java.util.List;
46  
47  import static org.nuiton.i18n.I18n.t;
48  
49  /**
50   * Created on 11/16/14.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   * @since 3.10
54   */
55  public class ReferentialTemporaryVesselService extends AbstractTuttiService {
56  
57      /** Logger. */
58      private static final Log log =
59              LogFactory.getLog(ReferentialTemporaryVesselService.class);
60  
61      protected PersistenceService persistenceService;
62  
63      protected DecoratorService decoratorService;
64  
65      @Override
66      public void setServiceContext(TuttiServiceContext context) {
67          super.setServiceContext(context);
68          persistenceService = getService(PersistenceService.class);
69          decoratorService = getService(DecoratorService.class);
70      }
71  
72      public ReferentialImportRequest<Vessel, String> createReferentialImportRequest() {
73  
74          List<Vessel> existingVessels = Lists.newArrayList(persistenceService.getAllVessel());
75          return new ReferentialImportRequest<>(existingVessels, TuttiEntities.<Vessel>newIdFunction(), Vessels.GET_INTERNAL_REGISTRATION_CODE);
76  
77      }
78  
79      public ReferentialImportResult<Vessel> importTemporaryVessel(File file) {
80  
81          if (log.isInfoEnabled()) {
82              log.info("Will import vessels from file: " + file);
83          }
84  
85          ReferentialImportRequest<Vessel, String> requestResult = createReferentialImportRequest();
86  
87          try (CsvConsumerForTemporaryVessel consumer = new CsvConsumerForTemporaryVessel(file.toPath(), getCsvSeparator(), true)) {
88  
89              for (ImportRow<VesselRow> bean : consumer) {
90  
91                  consumer.checkRow(bean, persistenceService, decoratorService, requestResult);
92  
93              }
94  
95          } catch (IOException e) {
96              throw new ApplicationTechnicalException(t("tutti.service.referential.import.vessels.error", file), e);
97          }
98  
99          return executeImportRequest(requestResult);
100 
101     }
102 
103     public ReferentialImportResult<Vessel> executeImportRequest(ReferentialImportRequest<Vessel, String> requestResult) {
104 
105         ReferentialImportResult<Vessel> result = new ReferentialImportResult<>();
106 
107         if (requestResult.withEntitiesToDelete()) {
108 
109             List<String> idsToDelete = requestResult.getIdsToDelete();
110             persistenceService.deleteTemporaryVessels(idsToDelete);
111             result.setNbRefDeleted(idsToDelete.size());
112 
113         }
114 
115         if (requestResult.withEntitiesToAdd()) {
116 
117             List<Vessel> entitiesToAdd = requestResult.getEntitiesToAdd();
118             List<Vessel> entitiesAdded = persistenceService.addTemporaryVessels(entitiesToAdd);
119             result.addAllRefsAdded(entitiesAdded);
120         }
121 
122         if (requestResult.withEntitiesToUpdate()) {
123 
124             List<Vessel> entitiesToUpdate = requestResult.getEntitiesToUpdate();
125             List<Vessel> entitiesUpdated = persistenceService.updateTemporaryVessels(entitiesToUpdate);
126             result.addAllRefsUpdated(entitiesUpdated);
127 
128         }
129 
130         if (requestResult.withEntitiesToLink()) {
131 
132             List<Vessel> entitiesToLink = requestResult.getEntitiesToLink();
133             List<Vessel> entitiesLinked = persistenceService.linkTemporaryVessels(entitiesToLink);
134             result.addAllRefsLinked(entitiesLinked);
135 
136         }
137 
138         return result;
139 
140     }
141 
142     public List<Vessel> getTemporaryVessels() {
143 
144         if (log.isInfoEnabled()) {
145             log.info("Getting all vessels from database");
146         }
147         List<Vessel> targetList = Lists.newArrayList(persistenceService.getAllVessel());
148         if (log.isInfoEnabled()) {
149             log.info("Got " + targetList.size() + " vessels");
150         }
151         List<Vessel> toExport = persistenceService.retainTemporaryVesselList(targetList);
152 
153         if (log.isInfoEnabled()) {
154             log.info("Got " + toExport.size() + " temporary vessels");
155         }
156         return toExport;
157 
158     }
159 
160     public void exportExistingTemporaryVessel(File file) throws IOException {
161 
162         List<Vessel> toExport = getTemporaryVessels();
163         exportTemporaryVessel(file, toExport);
164 
165     }
166 
167     public void exportTemporaryVesselExample(File file) throws IOException {
168 
169         List<Vessel> toExport = Lists.newArrayList();
170 
171         {
172             Vessel v = Vessels.newVessel();
173             v.setRegistrationCode("RegCode1");
174             v.setName("Temporary fishing vessel name 1");
175             v.setInternationalRegistrationCode("International registration code F1");
176             v.setScientificVessel(false);
177             toExport.add(v);
178         }
179         {
180             Vessel v = Vessels.newVessel();
181             v.setRegistrationCode("RegCode2");
182             v.setName("Temporary fishing vessel name 2");
183             v.setInternationalRegistrationCode("International registration code F2");
184             v.setScientificVessel(false);
185             toExport.add(v);
186         }
187         {
188             Vessel v = Vessels.newVessel();
189             v.setRegistrationCode("RegCode3");
190             v.setName("Temporary scientific vessel name 3");
191             v.setInternationalRegistrationCode("International registration code S3");
192             v.setScientificVessel(true);
193             toExport.add(v);
194         }
195         {
196             Vessel v = Vessels.newVessel();
197             v.setRegistrationCode("RegCode4");
198             v.setName("Temporary scientific vessel name 4");
199             v.setInternationalRegistrationCode("International registration code S4");
200             v.setScientificVessel(true);
201             toExport.add(v);
202         }
203         exportTemporaryVessel(file, toExport);
204 
205     }
206 
207     public void exportTemporaryVessel(File file, List<Vessel> toExport) throws IOException {
208 
209         try (CsvProducerForTemporaryVessel producerForTemporaryVessel = new CsvProducerForTemporaryVessel(file.toPath(), getCsvSeparator())) {
210 
211             List<VesselRow> dataToExport = producerForTemporaryVessel.getDataToExport(toExport);
212             producerForTemporaryVessel.write(dataToExport);
213 
214         } catch (Exception e) {
215             throw new ApplicationTechnicalException(t("tutti.service.referential.export.vessel.error", file), e);
216         }
217 
218     }
219 
220     protected char getCsvSeparator() {
221         return ';';
222     }
223 }