View Javadoc
1   package fr.ifremer.tutti.persistence.entities.data;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Joiner;
26  import com.google.common.collect.Lists;
27  import com.google.common.collect.Maps;
28  import fr.ifremer.tutti.persistence.TuttiPersistence;
29  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
30  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
31  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicType;
32  
33  import java.io.Serializable;
34  import java.util.Collections;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.TreeMap;
38  
39  /**
40   * Define the complete sample category model.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 2.4
44   */
45  public class SampleCategoryModel implements Serializable {
46  
47      private static final long serialVersionUID = 1L;
48  
49      protected final List<SampleCategoryModelEntry> category;
50  
51      protected final List<Integer> samplingOrder;
52  
53      protected final List<SampleCategoryModelEntry> reverseCategory;
54  
55      protected final Map<Integer, SampleCategoryModelEntry> categoryMap;
56  
57      public SampleCategoryModel(List<SampleCategoryModelEntry> category) {
58          List<Integer> samplingOrder = Lists.newArrayList();
59          this.category = Lists.newArrayList(category);
60          this.categoryMap = Maps.newLinkedHashMap();
61          for (SampleCategoryModelEntry def : category) {
62              categoryMap.put(def.getCategoryId(), def);
63              samplingOrder.add(def.getCategoryId());
64          }
65  
66          this.samplingOrder = Collections.unmodifiableList(samplingOrder);
67  
68          List<SampleCategoryModelEntry> reverseCategory = Lists.newArrayList(category);
69          Collections.reverse(reverseCategory);
70          this.reverseCategory = Collections.unmodifiableList(reverseCategory);
71      }
72  
73      public void load(TuttiPersistence service) {
74  
75          for (SampleCategoryModelEntry def : category) {
76              def.load(service);
77          }
78      }
79  
80      public List<Integer> getSamplingOrder() {
81          return samplingOrder;
82      }
83  
84      public List<SampleCategoryModelEntry> getCategory() {
85          return category;
86      }
87  
88      public List<SampleCategoryModelEntry> getReverseCategory() {
89          return reverseCategory;
90      }
91  
92      public Map<Integer, SampleCategoryModelEntry> getCategoryMap() {
93          return categoryMap;
94      }
95  
96      public SampleCategoryModelEntry getCategoryById(Integer categoryId) {
97          return categoryMap.get(categoryId);
98      }
99  
100     public SampleCategoryModelEntry getCategoryByIndex(int index) {
101         return category.get(index);
102     }
103 
104     @Override
105     public String toString() {
106         List<String> entries = Lists.newArrayList();
107         for (SampleCategoryModelEntry entry : category) {
108             entries.add(entry.toString());
109         }
110         return Joiner.on('|').join(entries);
111     }
112 
113     public Integer getLastCategoryId() {
114         return samplingOrder.isEmpty() ? null : samplingOrder.get(samplingOrder.size() - 1);
115     }
116 
117     public int getNbSampling() {
118         return samplingOrder.size();
119     }
120 
121     public Integer getFirstCategoryId() {
122         return samplingOrder.isEmpty() ? null : samplingOrder.get(0);
123     }
124 
125     public boolean containsCategoryId(Integer id) {
126         return samplingOrder.contains(id);
127     }
128 
129     public int indexOf(SampleCategoryModelEntry sampleCategoryDef) {
130         return category.indexOf(sampleCategoryDef);
131     }
132 
133     public Map<String, CaracteristicQualitativeValue> toMap() {
134 
135         Map<String, CaracteristicQualitativeValue> sampleCategoryValueMap = new TreeMap<>();
136 
137         for (SampleCategoryModelEntry sampleCategoryModelEntry : getCategory()) {
138             Caracteristic caracteristic = sampleCategoryModelEntry.getCaracteristic();
139             if (caracteristic.getCaracteristicType() == CaracteristicType.QUALITATIVE) {
140                 List<CaracteristicQualitativeValue> qualitativeValue = caracteristic.getQualitativeValue();
141                 for (CaracteristicQualitativeValue value : qualitativeValue) {
142                     sampleCategoryValueMap.put(value.getId(), value);
143                 }
144             }
145         }
146         return sampleCategoryValueMap;
147 
148     }
149 }