View Javadoc
1   package fr.ifremer.tutti.service.export.cps;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 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.base.Preconditions;
28  import fr.ifremer.tutti.persistence.ProgressionModel;
29  import fr.ifremer.tutti.persistence.entities.referential.Species;
30  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
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.TuttiDataContext;
35  import fr.ifremer.tutti.service.TuttiServiceContext;
36  import fr.ifremer.tutti.service.sampling.CacheExtractedKey;
37  import fr.ifremer.tutti.service.cruise.CruiseCache;
38  import fr.ifremer.tutti.service.sampling.CruiseSamplingCache;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.nuiton.csv.Export;
42  import org.nuiton.decorator.Decorator;
43  import org.nuiton.jaxx.application.ApplicationTechnicalException;
44  
45  import java.io.BufferedWriter;
46  import java.io.File;
47  import java.nio.charset.StandardCharsets;
48  import java.nio.file.Files;
49  import java.util.List;
50  import java.util.Map;
51  import java.util.Optional;
52  
53  import static org.nuiton.i18n.I18n.t;
54  
55  /**
56   * @author Kevin Morin (Code Lutin)
57   * @since 4.5
58   */
59  public class CalcifiedPiecesSamplingExportService extends AbstractTuttiService {
60  
61      private static final Log log = LogFactory.getLog(CalcifiedPiecesSamplingExportService.class);
62  
63      protected PersistenceService persistenceService;
64      protected DecoratorService decoratorService;
65  
66      @Override
67      public void setServiceContext(TuttiServiceContext context) {
68          super.setServiceContext(context);
69          persistenceService = getService(PersistenceService.class);
70          decoratorService = getService(DecoratorService.class);
71      }
72  
73      /**
74       * Export selected cruise with the csv sumatra format.
75       *
76       * @param file where to generate report
77       * @since 2.0
78       */
79      public void exportCruiseCalcifiedPiecesSamplingsReport(File file, ProgressionModel progressionModel) {
80  
81          Preconditions.checkNotNull(file, "Cannot export to a null file");
82  
83          TuttiDataContext dataContext = context.getDataContext();
84          Preconditions.checkState(dataContext.isCanUseCruiseSamplingCache() && dataContext.isCruiseCacheLoaded() && dataContext.isCruiseCacheUpToDate());
85  
86          Decorator<Species> decorator = decoratorService.getDecoratorByType(Species.class, DecoratorService.WITH_SURVEY_CODE_NO_NAME);
87          SamplingNumberRowModel csvModel = new SamplingNumberRowModel(context.getConfig().getCsvSeparator(), decorator);
88  
89          Map<String, Species> referenceSpeciesByReferenceTaxonId = Speciess.splitReferenceSpeciesByReferenceTaxonId(dataContext.getReferentSpecies());
90  
91          Optional<CruiseCache> optionalCruiseCache = context.getDataContext().getOptionalCruiseCache();
92          if (!optionalCruiseCache.isPresent()) {
93              throw new IllegalStateException("No cruise cache found");
94          }
95          CruiseCache cruiseCache = optionalCruiseCache.get();
96  
97          Optional<CruiseSamplingCache> optionalSamplingCruiseCache = cruiseCache.getSamplingCruiseCache();
98          if (!optionalSamplingCruiseCache.isPresent()) {
99              throw new IllegalStateException("No sampling cruise cache found");
100         }
101 
102         List<CacheExtractedKey> rows = optionalSamplingCruiseCache.get().getSamplingNumbers(referenceSpeciesByReferenceTaxonId);
103 
104         if (log.isInfoEnabled()) {
105             log.info("Loaded " + rows.size() + " keys to export to " + file);
106         }
107         try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
108 
109             progressionModel.increments(t("tutti.service.cpsExport.step.toFile", file.getName()));
110 
111             Export export = Export.newExport(csvModel, rows);
112             export.write(writer);
113 
114         } catch (Exception e) {
115             throw new ApplicationTechnicalException(t("tutti.service.cpsExport.error", file), e);
116         }
117 
118     }
119 
120 }