View Javadoc
1   package fr.ifremer.tutti.service.genericformat;
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 com.google.common.collect.ImmutableList;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.adagio.core.dao.referential.ObjectTypeCode;
30  import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
31  import fr.ifremer.tutti.persistence.entities.data.Attachment;
32  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
33  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
34  import fr.ifremer.tutti.persistence.entities.data.Cruise;
35  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
36  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
37  import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch;
38  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
39  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
40  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
41  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
42  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
43  import fr.ifremer.tutti.persistence.entities.referential.Gear;
44  import fr.ifremer.tutti.persistence.entities.referential.Person;
45  import fr.ifremer.tutti.persistence.entities.referential.Species;
46  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
47  import fr.ifremer.tutti.service.PersistenceService;
48  import fr.ifremer.tutti.service.TuttiServiceContext;
49  import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
50  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
51  import fr.ifremer.tutti.service.referential.ReferentialImportRequest;
52  import fr.ifremer.tutti.service.referential.ReferentialImportResult;
53  import fr.ifremer.tutti.service.referential.ReferentialTemporaryGearService;
54  import fr.ifremer.tutti.service.referential.ReferentialTemporaryPersonService;
55  import fr.ifremer.tutti.service.referential.ReferentialTemporarySpeciesService;
56  import fr.ifremer.tutti.service.referential.ReferentialTemporaryVesselService;
57  import org.apache.commons.collections4.CollectionUtils;
58  
59  import java.io.File;
60  import java.util.Collection;
61  import java.util.List;
62  import java.util.Map;
63  import java.util.Set;
64  
65  /**
66   * Created on 2/19/15.
67   *
68   * @author Tony Chemit - chemit@codelutin.com
69   * @since 3.14
70   */
71  public class GenericformatImportPersistenceHelper {
72  
73      private final GenericFormatContextSupport importContext;
74  
75      private final PersistenceService persistenceService;
76  
77      private final ReferentialTemporaryGearService referentialTemporaryGearService;
78  
79      private final ReferentialTemporaryPersonService referentialTemporaryPersonService;
80  
81      private final ReferentialTemporarySpeciesService referentialTemporarySpeciesService;
82  
83      private final ReferentialTemporaryVesselService referentialTemporaryVesselService;
84  
85      private final ProtocolImportExportService protocolImportExportService;
86  
87      private final Caracteristic copyIndividualObservationModeCaracteristic;
88      private final Caracteristic weightMeasuredCaracteristic;
89      private final Caracteristic pmfmIdCaracteristic;
90      private final Caracteristic sampleCodeCaracteristic;
91  
92      public GenericformatImportPersistenceHelper(TuttiServiceContext context, GenericFormatContextSupport importContext) {
93          this.importContext = importContext;
94          this.persistenceService = context.getService(PersistenceService.class);
95          this.referentialTemporaryGearService = context.getService(ReferentialTemporaryGearService.class);
96          this.referentialTemporaryPersonService = context.getService(ReferentialTemporaryPersonService.class);
97          this.referentialTemporarySpeciesService = context.getService(ReferentialTemporarySpeciesService.class);
98          this.referentialTemporaryVesselService = context.getService(ReferentialTemporaryVesselService.class);
99          this.protocolImportExportService = context.getService(ProtocolImportExportService.class);
100         this.weightMeasuredCaracteristic = persistenceService.getWeightMeasuredCaracteristic();
101         this.copyIndividualObservationModeCaracteristic = persistenceService.getCopyIndividualObservationModeCaracteristic();
102         this.pmfmIdCaracteristic = persistenceService.getPmfmIdCaracteristic();
103         this.sampleCodeCaracteristic = persistenceService.getSampleCodeCaracteristic();
104     }
105 
106     public Set<FishingOperation> getFishingOperations(Integer cruiseId) {
107 
108         List<FishingOperation> allFishingOperation = persistenceService.getAllFishingOperation(cruiseId);
109         return Sets.newLinkedHashSet(allFishingOperation);
110 
111     }
112 
113     public void deleteAllAttachments(ObjectTypeCode objectTypeCode, Integer objectId) {
114 
115         persistenceService.deleteAllAttachment(objectTypeCode, objectId);
116 
117     }
118 
119     public void persistAttachments(Integer objectId, Collection<AttachmentRow> attachmentRows) {
120 
121         if (importContext.getImportRequest().isImportAttachments() && CollectionUtils.isNotEmpty(attachmentRows)) {
122 
123             for (AttachmentRow attachmentRow : attachmentRows) {
124 
125                 Attachment attachment = attachmentRow.toAttachment(objectId);
126                 File attachmentFile = importContext.getImportRequest().getArchive().extractAttachment(attachment.getPath());
127                 persistenceService.createAttachment(attachment, attachmentFile);
128 
129             }
130 
131         }
132 
133     }
134 
135     public Cruise createCruise(Cruise cruise) {
136         return persistenceService.createCruise(cruise);
137     }
138 
139     public Cruise saveCruise(Cruise cruise) {
140         return persistenceService.saveCruise(cruise, false, false);
141     }
142 
143     public void saveGearCaracteristics(Gear gear, Cruise cruise) {
144         persistenceService.saveGearCaracteristics(gear, cruise);
145     }
146 
147     public FishingOperation createFishingOperation(FishingOperation fishingOperation) {
148         return persistenceService.createFishingOperation(fishingOperation);
149     }
150 
151 
152     public FishingOperation saveFishingOperation(FishingOperation fishingOperation) {
153         return persistenceService.saveFishingOperation(fishingOperation);
154     }
155 
156     public CatchBatch getExistingCatchBatch(Integer operationId) {
157         boolean withCatchBatch = isWithCatchBatch(operationId);
158         CatchBatch catchBatch;
159         if (withCatchBatch) {
160             catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId);
161         } else {
162             catchBatch = null;
163         }
164         return catchBatch;
165     }
166 
167     public boolean isWithCatchBatch(Integer operationId) {
168         return persistenceService.isFishingOperationWithCatchBatch(operationId);
169     }
170 
171     public CatchBatch createCatchBatch(CatchBatch catchBatch) {
172         return persistenceService.createCatchBatch(catchBatch);
173     }
174 
175     public CatchBatch saveCatchBatch(CatchBatch catchBatch) {
176         return persistenceService.saveCatchBatch(catchBatch);
177     }
178 
179     public SpeciesBatch createSpeciesBatch(SpeciesBatch batch, Integer parentId) {
180         return persistenceService.createSpeciesBatch(batch, parentId, false);
181     }
182 
183     public void saveSpeciesBatchFrequency(Integer batchId, List<SpeciesBatchFrequency> frequencies) {
184         persistenceService.saveSpeciesBatchFrequency(batchId, frequencies);
185     }
186 
187     public void deleteSpeciesBatchForFishingOperation(Integer fishingOperationId) {
188 
189         BatchContainer<SpeciesBatch> rootSpeciesBatch = persistenceService.getRootSpeciesBatch(fishingOperationId, false);
190         for (SpeciesBatch batch : rootSpeciesBatch.getChildren()) {
191             persistenceService.deleteSpeciesBatch(batch.getIdAsInt());
192         }
193 
194     }
195 
196     public SpeciesBatch createBenthosBatch(SpeciesBatch batch, Integer parentId) {
197         return persistenceService.createBenthosBatch(batch, parentId, false);
198     }
199 
200     public void saveBenthosBatchFrequency(Integer batchId, List<SpeciesBatchFrequency> frequencies) {
201         persistenceService.saveBenthosBatchFrequency(batchId, frequencies);
202     }
203 
204     public void deleteBenthosBatchForFishingOperation(Integer fishingOperationId) {
205 
206         BatchContainer<SpeciesBatch> rootBenthosBatch = persistenceService.getRootBenthosBatch(fishingOperationId, false);
207         for (SpeciesBatch batch : rootBenthosBatch.getChildren()) {
208             persistenceService.deleteBenthosBatch(batch.getIdAsInt());
209         }
210 
211     }
212 
213     public MarineLitterBatch createMarineLitterBatch(MarineLitterBatch marineLitterBatch) {
214         return persistenceService.createMarineLitterBatch(marineLitterBatch);
215     }
216 
217     public void deleteMarineLitterForFishingOperation(Integer fishingOperationId) {
218 
219         BatchContainer<MarineLitterBatch> rootMarineLitters = persistenceService.getRootMarineLitterBatch(fishingOperationId);
220         for (MarineLitterBatch batch : rootMarineLitters.getChildren()) {
221             persistenceService.deleteMarineLitterBatch(batch.getIdAsInt());
222         }
223     }
224 
225     public AccidentalBatch createAccidentalBatch(AccidentalBatch accidentalBatch) {
226         return persistenceService.createAccidentalBatch(accidentalBatch);
227     }
228 
229     public void deleteAccidentalBatchForFishingOperation(Integer fishingOperationId) {
230         persistenceService.deleteAccidentalBatchForFishingOperation(fishingOperationId);
231     }
232 
233     public void setSampleCategoryModel(SampleCategoryModel sampleCategoryModel) {
234         persistenceService.setSampleCategoryModel(sampleCategoryModel);
235     }
236 
237     public ReferentialImportRequest<Gear, Integer> createGearImportRequest() {
238         return referentialTemporaryGearService.createReferentialImportRequest();
239     }
240 
241     public ReferentialImportResult<Gear> importGears(ReferentialImportRequest<Gear, Integer> referentialImportRequest) {
242         return referentialTemporaryGearService.executeImportRequest(referentialImportRequest);
243     }
244 
245     public ReferentialImportRequest<Person, Integer> createPersonImportRequest() {
246         return referentialTemporaryPersonService.createReferentialImportRequest();
247     }
248 
249     public ReferentialImportResult<Person> importPersons(ReferentialImportRequest<Person, Integer> referentialImportRequest) {
250         return referentialTemporaryPersonService.executeImportRequest(referentialImportRequest);
251     }
252 
253     public ReferentialImportRequest<Vessel, String> createVesselsImportRequest() {
254         return referentialTemporaryVesselService.createReferentialImportRequest();
255     }
256 
257     public ReferentialImportResult<Vessel> importVessels(ReferentialImportRequest<Vessel, String> referentialImportRequest) {
258         return referentialTemporaryVesselService.executeImportRequest(referentialImportRequest);
259     }
260 
261     public ReferentialImportRequest<Species, Integer> createSpeciesImportRequest() {
262         return referentialTemporarySpeciesService.createReferentialImportRequest();
263     }
264 
265     public ReferentialImportResult<Species> importSpecies(ReferentialImportRequest<Species, Integer> referentialImportRequest) {
266         return referentialTemporarySpeciesService.executeImportRequest(referentialImportRequest);
267     }
268 
269     public TuttiProtocol importProtocol(File file) {
270         return protocolImportExportService.importProtocol(file);
271     }
272 
273     public List<Species> getAllReferentSpecies() {
274         return persistenceService.getAllReferentSpecies();
275     }
276 
277     public TuttiProtocol createProtocol(TuttiProtocol tuttiProtocol) {
278         TuttiProtocol createdProtocol = persistenceService.createProtocol(tuttiProtocol);
279         persistenceService.setProtocol(createdProtocol);
280         return createdProtocol;
281     }
282 
283     public String getProtocolFirstAvailableName(String protocolOriginalName) {
284         return persistenceService.getFirstAvailableName(protocolOriginalName);
285     }
286 
287     public Map<Integer, Integer> getAllObsoleteReferentTaxons() {
288         return persistenceService.getAllObsoleteReferentTaxons();
289     }
290 
291     public void deleteIndividualObservationBatchForFishingOperation(Integer fishingOperationId) {
292         persistenceService.deleteAllIndividualObservationsForFishingOperation(fishingOperationId);
293     }
294 
295     public List<IndividualObservationBatch> createIndividualObservationBatch(FishingOperation fishingOperation, ImmutableList<IndividualObservationBatch> individualObservationBatch) {
296         return persistenceService.createIndividualObservationBatches(fishingOperation, individualObservationBatch);
297     }
298 
299     public Caracteristic getCaracteristic(Integer caracteristicId) {
300         return persistenceService.getCaracteristic(caracteristicId);
301     }
302 
303     public Caracteristic getCopyIndividualObservationModeCaracteristic() {
304         return copyIndividualObservationModeCaracteristic;
305     }
306 
307     public Caracteristic getPmfmIdCaracteristic() {
308         return pmfmIdCaracteristic;
309     }
310 
311     public Caracteristic getWeightMeasuredCaracteristic() {
312         return weightMeasuredCaracteristic;
313     }
314 
315 
316     public Caracteristic getSampleCodeCaracteristic() {
317         return sampleCodeCaracteristic;
318     }
319 
320 }