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.Gear;
30  import fr.ifremer.tutti.persistence.entities.referential.Gears;
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.CsvConsumerForTemporaryGear;
36  import fr.ifremer.tutti.service.referential.csv.GearRow;
37  import fr.ifremer.tutti.service.referential.producer.CsvProducerForTemporaryGear;
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 ReferentialTemporaryGearService extends AbstractTuttiService {
56  
57      /** Logger. */
58      private static final Log log =
59              LogFactory.getLog(ReferentialTemporaryGearService.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<Gear, Integer> createReferentialImportRequest() {
73  
74          List<Gear> allGears = Lists.newArrayList(persistenceService.getAllGear());
75          return new ReferentialImportRequest<>(allGears, TuttiEntities.<Gear>newIdAstIntFunction(), Gears.GET_NAME);
76  
77      }
78  
79      public ReferentialImportResult<Gear> importTemporaryGear(File file) {
80  
81          if (log.isInfoEnabled()) {
82              log.info("Will import gears from file: " + file);
83          }
84  
85          ReferentialImportRequest<Gear, Integer> requestResult = createReferentialImportRequest();
86  
87          try (CsvConsumerForTemporaryGear consumer = new CsvConsumerForTemporaryGear(file.toPath(), getCsvSeparator(), true)) {
88  
89              for (ImportRow<GearRow> 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.gears.error", file), e);
97          }
98  
99          return executeImportRequest(requestResult);
100 
101     }
102 
103 
104     public ReferentialImportResult<Gear> executeImportRequest(ReferentialImportRequest<Gear, Integer> requestResult) {
105 
106         ReferentialImportResult<Gear> result = new ReferentialImportResult<>();
107 
108         if (requestResult.withEntitiesToDelete()) {
109 
110             List<Integer> idsToDelete = requestResult.getIdsToDelete();
111             persistenceService.deleteTemporaryGears(idsToDelete);
112             result.setNbRefDeleted(idsToDelete.size());
113 
114         }
115 
116         if (requestResult.withEntitiesToAdd()) {
117 
118             List<Gear> entitiesToAdd = requestResult.getEntitiesToAdd();
119             List<Gear> entitiesAdded = persistenceService.addTemporaryGears(entitiesToAdd);
120             result.addAllRefsAdded(entitiesAdded);
121 
122         }
123 
124         if (requestResult.withEntitiesToUpdate()) {
125 
126             List<Gear> entitiesToUpdate = requestResult.getEntitiesToUpdate();
127             List<Gear> entitiesUpdated = persistenceService.updateTemporaryGears(entitiesToUpdate);
128             result.addAllRefsUpdated(entitiesUpdated);
129 
130         }
131 
132         if (requestResult.withEntitiesToLink()) {
133 
134             List<Gear> entitiesToLink = requestResult.getEntitiesToLink();
135             List<Gear> entitiesLinked = persistenceService.linkTemporaryGears(entitiesToLink);
136             result.addAllRefsLinked(entitiesLinked);
137 
138         }
139 
140         return result;
141 
142     }
143 
144     public void exportExistingTemporaryGear(File file) throws IOException {
145 
146         List<Gear> toExport = getTemporaryGears();
147         exportTemporaryGear(file, toExport);
148 
149     }
150 
151     public List<Gear> getTemporaryGears() {
152         if (log.isInfoEnabled()) {
153             log.info("Getting all gears from database");
154         }
155         List<Gear> targetList = Lists.newArrayList(persistenceService.getAllGear());
156         if (log.isInfoEnabled()) {
157             log.info("Got " + targetList.size() + " gears");
158         }
159         List<Gear> toExport = persistenceService.retainTemporaryGearList(targetList);
160         if (log.isInfoEnabled()) {
161             log.info("Got " + toExport.size() + " temporary gears");
162         }
163         return toExport;
164     }
165 
166     public void exportTemporaryGearExample(File file) throws IOException {
167 
168         List<Gear> toExport = Lists.newArrayList();
169 
170         {
171             Gear g = Gears.newGear();
172             g.setName("Gear fishing name 1");
173             g.setLabel("Gear fishing label 1");
174             toExport.add(g);
175         }
176         {
177             Gear g = Gears.newGear();
178             g.setName("Gear fishing name 2");
179             g.setLabel("Gear fishing label 2");
180             toExport.add(g);
181         }
182         {
183             Gear g = Gears.newGear();
184             g.setName("Gear scientific name 3");
185             g.setLabel("Gear scientific label 3");
186             g.setScientificGear(true);
187             toExport.add(g);
188         }
189         {
190             Gear g = Gears.newGear();
191             g.setName("Gear scientific name 4");
192             g.setLabel("Gear scientific label 4");
193             g.setScientificGear(true);
194             toExport.add(g);
195         }
196         exportTemporaryGear(file, toExport);
197 
198     }
199 
200     public void exportTemporaryGear(File file, List<Gear> toExport) throws IOException {
201 
202         try (CsvProducerForTemporaryGear producerForTemporarySpecies = new CsvProducerForTemporaryGear(file.toPath(), getCsvSeparator())) {
203 
204             List<GearRow> dataToExport = producerForTemporarySpecies.getDataToExport(toExport);
205             producerForTemporarySpecies.write(dataToExport);
206 
207         } catch (Exception e) {
208             throw new ApplicationTechnicalException(t("tutti.service.referential.export.gear.error", file), e);
209         }
210 
211     }
212 
213     protected char getCsvSeparator() {
214         return ';';
215     }
216 }