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.entities.data.Cruise;
28  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
29  
30  import java.io.Serializable;
31  import java.util.Iterator;
32  import java.util.LinkedHashMap;
33  import java.util.Map;
34  
35  /**
36   * Created on 2/22/15.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 3.14
40   */
41  public class GenericFormatImportCruiseResult implements Serializable, Iterable<GenericFormatImportOperationResult> {
42  
43      private static final long serialVersionUID = 1L;
44  
45      private final Map<String, GenericFormatImportOperationResult> fishingOperationResults;
46  
47      private final Cruise cruise;
48  
49      private final boolean override;
50  
51      private final String label;
52  
53      private int nbOperationsCreated;
54  
55      private int nbOperationsUpdated;
56  
57      private boolean withInvalidWeights;
58  
59      public GenericFormatImportCruiseResult(GenericFormatImportCruiseContext cruiseContext) {
60          label = cruiseContext.getCruiseLabel();
61          this.cruise = cruiseContext.getCruise();
62          this.override = cruiseContext.getExistingCruiseData() != null;
63          this.fishingOperationResults = new LinkedHashMap<>();
64  
65          this.withInvalidWeights = false;
66  
67          for (GenericFormatImportOperationContext operationContext : cruiseContext.orderedFishingOperationContexts()) {
68  
69              GenericFormatImportOperationResult operationResult = new GenericFormatImportOperationResult(operationContext);
70              fishingOperationResults.put(operationContext.getFishingOperation().getId(), operationResult);
71  
72              if (operationContext.isOverride()) {
73                  nbOperationsUpdated++;
74              } else {
75                  nbOperationsCreated++;
76              }
77              if (operationResult.isWithInvalidWeights()) {
78                  withInvalidWeights = true;
79              }
80  
81          }
82  
83      }
84  
85      public String getLabel() {
86          return label;
87      }
88  
89      public String getId() {
90          return cruise.getId();
91      }
92  
93      public int getNbOperationsCreated() {
94          return nbOperationsCreated;
95      }
96  
97      public int getNbOperationsUpdated() {
98          return nbOperationsUpdated;
99      }
100 
101     public int getNbOperations() {
102         return nbOperationsCreated + nbOperationsUpdated;
103     }
104 
105     public boolean isWithInvalidWeights() {
106         return withInvalidWeights;
107     }
108 
109     public Cruise getCruise() {
110         return cruise;
111     }
112 
113     public boolean isOverride() {
114         return override;
115     }
116 
117     public String getVesselName() {
118         Vessel vessel = cruise.getVessel();
119         String result = vessel.getInternationalRegistrationCode();
120         if (result == null) {
121             result = vessel.getRegistrationCode();
122         }
123         return result;
124     }
125 
126     @Override
127     public Iterator<GenericFormatImportOperationResult> iterator() {
128         return fishingOperationResults.values().iterator();
129     }
130 
131 }