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 fr.ifremer.tutti.persistence.ProgressionModel;
28  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
29  import fr.ifremer.tutti.persistence.entities.data.Cruise;
30  import fr.ifremer.tutti.persistence.entities.data.Cruises;
31  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
32  import fr.ifremer.tutti.persistence.entities.data.FishingOperations;
33  import fr.ifremer.tutti.persistence.model.CruiseDataModel;
34  import fr.ifremer.tutti.persistence.model.OperationDataModel;
35  import fr.ifremer.tutti.persistence.model.ProgramDataModel;
36  import fr.ifremer.tutti.service.PersistenceService;
37  import fr.ifremer.tutti.service.genericformat.importactions.RestoreAfterValidateAction;
38  import org.nuiton.decorator.Decorator;
39  
40  import java.util.LinkedHashSet;
41  import java.util.Set;
42  
43  /**
44   * Created on 2/24/15.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 3.14
48   */
49  public class GenericFormatValidateFileContext extends GenericFormatContextSupport {
50  
51      private final RestoreAfterValidateAction closeAction;
52  
53      public GenericFormatValidateFileContext(GenericFormatImportRequest importRequest,
54                                              ProgressionModel progressionModel,
55                                              PersistenceService persistenceService,
56                                              Decorator<Cruise> cruiseDecorator,
57                                              Decorator<FishingOperation> fishingOperationDecorator) {
58  
59          super(importRequest, progressionModel, persistenceService, cruiseDecorator, fishingOperationDecorator);
60          this.closeAction = new RestoreAfterValidateAction(this, persistenceService);
61  
62      }
63  
64      @Override
65      protected void onClose() {
66  
67          closeAction.execute();
68  
69      }
70  
71      public ProgramDataModel toDataModel() {
72  
73          final Set<CruiseDataModel> importedCruises = new LinkedHashSet<>();
74  
75          doActionOnSortedCruiseContexts((cruiseContext, progressionModel) -> {
76  
77              Cruise cruise = cruiseContext.getCruise();
78  
79              CruiseDataModel existingCruiseData = cruiseContext.getExistingCruiseData();
80  
81              Set<OperationDataModel> operations = new LinkedHashSet<>();
82  
83              for (GenericFormatImportOperationContext fishingOperationContext : cruiseContext.orderedFishingOperationContexts()) {
84                  FishingOperation fishingOperation = fishingOperationContext.getFishingOperation();
85  
86                  // Add a natural Id as id (used in import to find which cruise to import)
87                  String naturalId = FishingOperations.getNaturalId(fishingOperation);
88                  fishingOperation.setId(naturalId);
89                  OperationDataModel operation = new OperationDataModel(fishingOperation);
90                  fishingOperation.setId((String) null);
91  
92                  OperationDataModel existingFishingOperationData = fishingOperationContext.getExistingFishingOperationData();
93                  if (existingFishingOperationData != null) {
94                      operation.setOptionalId(existingFishingOperationData.getId());
95                  }
96  
97                  operations.add(operation);
98              }
99  
100             // Add a natural Id as id (used in import to find which cruise to import)
101             String naturalId = Cruises.getNaturalId(cruise);
102             cruise.setId(naturalId);
103             CruiseDataModel cruiseModel = new CruiseDataModel(cruise, operations);
104             cruise.setId((String) null);
105 
106             if (existingCruiseData != null) {
107                 cruiseModel.setOptionalId(existingCruiseData.getId());
108             }
109 
110             importedCruises.add(cruiseModel);
111 
112         });
113         return new ProgramDataModel(getImportRequest().getProgram(), importedCruises);
114 
115     }
116 
117     public void addImportedFishingOperation(FishingOperation fishingOperation, CatchBatch catchBatch) {
118 
119         // add a temporary id to simulate persist behaviour
120         fishingOperation.setId(getNextFishingOperationId());
121 
122         // add a temporary id to simulate persist behaviour
123         catchBatch.setId(getNextCatchBatchId());
124 
125         super.addImportedFishingOperation(fishingOperation, catchBatch);
126 
127     }
128 
129     private int fishinOperationId = -1;
130 
131     private int catchBatchId = -1;
132 
133     private int getNextFishingOperationId() {
134         return fishinOperationId--;
135     }
136 
137     private int getNextCatchBatchId() {
138         return catchBatchId--;
139     }
140 
141 }