View Javadoc
1   package fr.ifremer.tutti.service.genericformat.importactions;
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.adagio.core.dao.referential.ObjectTypeCode;
28  import fr.ifremer.tutti.persistence.ProgressionModel;
29  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
30  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
32  import fr.ifremer.tutti.service.genericformat.GenericFormatImportContext;
33  import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
34  import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
35  import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
36  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForAccidentalCatch;
37  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
38  import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.nuiton.csv.ImportRow;
42  import org.nuiton.csv.ImportRuntimeException;
43  import org.nuiton.jaxx.application.ApplicationTechnicalException;
44  
45  import java.io.IOException;
46  import java.util.Collection;
47  
48  import static org.nuiton.i18n.I18n.t;
49  
50  /**
51   * Created on 3/3/15.
52   *
53   * @author Tony Chemit - chemit@codelutin.com
54   * @since 3.14
55   */
56  public class ImportAccidentalCatchAction extends ImportActionSupport {
57  
58      /** Logger. */
59      private static final Log log = LogFactory.getLog(ImportAccidentalCatchAction.class);
60  
61      private final GenericformatImportPersistenceHelper persistenceHelper;
62  
63      public ImportAccidentalCatchAction(GenericFormatImportContext importContext, GenericformatImportPersistenceHelper persistenceHelper) {
64          super(importContext);
65          this.persistenceHelper = persistenceHelper;
66      }
67  
68      @Override
69      protected boolean canExecute() {
70          return importContext.isTechnicalFilesValid()
71                 && importContext.getSurveyFileResult().isValid()
72                 && importContext.getOperationFileResult().isValid()
73                 && importContext.getImportRequest().isImportAccidentalCatch();
74      }
75  
76      @Override
77      protected void skipExecute() {
78  
79          importContext.increments(t("tutti.service.genericFormat.skip.import.accidentalCatches"));
80  
81          if (!importContext.getImportRequest().isImportAccidentalCatch()) {
82              GenericFormatCsvFileResult importFileResult = importContext.getAccidentalCatchFileResult();
83              importFileResult.setSkipped(true);
84          }
85  
86      }
87  
88      @Override
89      protected void doExecute() {
90  
91          if (log.isInfoEnabled()) {
92              log.info("Import accidentalCatch.csv file.");
93          }
94          importContext.increments(t("tutti.service.genericFormat.import.accidentalCatches"));
95          GenericFormatCsvFileResult importFileResult = importContext.getAccidentalCatchFileResult();
96          try (CsvConsumerForAccidentalCatch consumer = importContext.loadAccidentalCatches(true)) {
97              for (ImportRow<AccidentalCatchRow> row : consumer) {
98  
99                  GenericFormatImportOperationContext operationContext = consumer.validateRow(row, importContext);
100                 if (operationContext != null) {
101                     consumer.prepareRowForPersist(operationContext, row);
102                 }
103 
104             }
105         } catch (IOException e) {
106             throw new ApplicationTechnicalException("Could not close accidentalCatch.csv file", e);
107         } catch (ImportRuntimeException e) {
108 
109             importFileResult.addGlobalError(e.getMessage());
110 
111         }
112 
113         persistAccidentalBatches();
114 
115     }
116 
117     private void persistAccidentalBatches() {
118 
119         importContext.doActionOnCruiseContexts(new GenericFormatImportContext.CruiseContextAction() {
120 
121             @Override
122             public void onCruise(GenericFormatImportCruiseContext cruiseContext, ProgressionModel progressionModel) {
123 
124                 for (GenericFormatImportOperationContext fishingOperationContext : cruiseContext) {
125 
126                     boolean override = fishingOperationContext.isOverride();
127 
128                     if (override) {
129 
130                         deleteAccidentalBatches(fishingOperationContext);
131 
132                     }
133 
134                     String cruiseStr = cruiseContext.getCruiseLabel();
135                     String operationStr = fishingOperationContext.getFishingOperationLabel();
136 
137                     importContext.increments(t("tutti.service.genericFormat.persist.operation.accidentalBatches", cruiseStr, operationStr));
138 
139                     if (fishingOperationContext.withAccidentalBatches()) {
140 
141                         Collection<AccidentalBatch> accidentalBatches = fishingOperationContext.getAccidentalBatches();
142                         if (log.isInfoEnabled()) {
143                             log.info("Persist " + accidentalBatches.size() + " accidental batch(es) of " + operationStr + " for cruise: " + cruiseStr);
144                         }
145                         persistAccidentalBatches(accidentalBatches);
146 
147                     }
148 
149                 }
150 
151             }
152 
153             private void deleteAccidentalBatches(GenericFormatImportOperationContext fishingOperationContext) {
154 
155                 FishingOperation fishingOperation = fishingOperationContext.getFishingOperation();
156                 persistenceHelper.deleteAccidentalBatchForFishingOperation(fishingOperation.getIdAsInt());
157 
158             }
159 
160             private void persistAccidentalBatches(Collection<AccidentalBatch> batches) {
161 
162                 for (AccidentalBatch accidentalBatch : batches) {
163 
164                     Integer objectId = accidentalBatch.getIdAsInt();
165                     Collection<AttachmentRow> attachmentRows = importContext.popAttachmentRows(ObjectTypeCode.SAMPLE, objectId);
166 
167                     // Id was only here to get attachments
168                     accidentalBatch.setId((String) null);
169 
170                     AccidentalBatch createdAccidentalBatch = persistenceHelper.createAccidentalBatch(accidentalBatch);
171                     persistenceHelper.persistAttachments(createdAccidentalBatch.getIdAsInt(), attachmentRows);
172 
173                 }
174 
175             }
176 
177         });
178 
179     }
180 
181 }