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.Iterables;
28  import com.google.common.collect.Lists;
29  import fr.ifremer.tutti.persistence.entities.data.Cruise;
30  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
31  import fr.ifremer.tutti.persistence.entities.data.Program;
32  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
33  import fr.ifremer.tutti.service.TuttiValidationDataContextSupport;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * Created on 2/19/15.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.14
43   */
44  public class GenericFormatImportValidationDataContext extends TuttiValidationDataContextSupport {
45  
46      private Cruise cruise;
47  
48      private TuttiProtocol protocol;
49  
50      private FishingOperation fishingOperation;
51  
52      private final GenericFormatContextSupport importContext;
53  
54      public GenericFormatImportValidationDataContext(GenericFormatContextSupport importContext) {
55          this.importContext = importContext;
56      }
57  
58      // Not used
59      @Override
60      protected List<Program> loadExistingPrograms() {
61          return Lists.newArrayList();
62      }
63  
64      // Not used
65      @Override
66      protected List<TuttiProtocol> loadExistingProtocols() {
67          return Lists.newArrayList();
68      }
69  
70      @Override
71      protected List<FishingOperation> loadExistingFishingOperations() {
72  
73          List<FishingOperation> result = new ArrayList<>();
74  
75          if (cruise != null) {
76  
77              GenericFormatImportCruiseContext cruiseContext = importContext.getCruiseContext(cruise);
78              Iterable<FishingOperation> importedFishingOperations = cruiseContext.getFishingOperations();
79              Iterables.addAll(result, importedFishingOperations);
80              if (fishingOperation != null) {
81                  result.remove(fishingOperation);
82              }
83  
84          }
85          return result;
86      }
87  
88      @Override
89      protected Program getProgram() {
90          return importContext.getImportRequest().getProgram();
91      }
92  
93      @Override
94      protected Cruise getCruise() {
95          return cruise;
96      }
97  
98      @Override
99      protected TuttiProtocol getProtocol() {
100         return protocol;
101     }
102 
103     @Override
104     protected FishingOperation getFishingOperation() {
105         return fishingOperation;
106     }
107 
108     public void setCruise(Cruise cruise) {
109         this.cruise = cruise;
110         resetExistingFishingOperations();
111     }
112 
113     public void setProtocol(TuttiProtocol protocol) {
114         this.protocol = protocol;
115     }
116 
117     public void setFishingOperation(FishingOperation fishingOperation) {
118         this.fishingOperation = fishingOperation;
119         resetExistingFishingOperations();
120     }
121 }