View Javadoc
1   package fr.ifremer.tutti.persistence.entities.data;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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.base.Function;
28  import com.google.common.base.Predicate;
29  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
30  import fr.ifremer.tutti.persistence.entities.referential.Species;
31  
32  import java.io.Serializable;
33  import java.util.Collection;
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.TreeMap;
37  
38  /**
39   * @author Kevin Morin (Code Lutin)
40   */
41  public class SpeciesBatchs extends AbstractSpeciesBatchs {
42  
43      public static final Function<SpeciesBatch, Serializable> GET_SAMPLE_CATEGORY_VALUE = SpeciesBatch::getSampleCategoryValue;
44  
45      public static SpeciesBatch newInstance(SpeciesBatch parent) {
46          SpeciesBatch speciesBatch = newSpeciesBatch();
47          speciesBatch.setBenthosBatch(parent.isBenthosBatch());
48          return speciesBatch;
49      }
50  
51      public static Map<Integer, SpeciesBatch> getAllSpeciesBatchesById(BatchContainer<SpeciesBatch> rootSpeciesBatch) {
52          Map<Integer, SpeciesBatch> result = new TreeMap<>();
53  
54          for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) {
55              getAllspeciesBatchesById(speciesBatch, result);
56          }
57          return result;
58      }
59  
60      private static void getAllspeciesBatchesById(SpeciesBatch speciesBatch, Map<Integer, SpeciesBatch> result) {
61          result.put(speciesBatch.getIdAsInt(), speciesBatch);
62          if (!speciesBatch.isChildBatchsEmpty()) {
63              for (SpeciesBatch batch : speciesBatch.getChildBatchs()) {
64                  getAllspeciesBatchesById(batch, result);
65              }
66          }
67      }
68  
69      public static Predicate<SpeciesBatch> newSpeciesAbleBatchCategoryPredicate(Integer cateogryId, Integer value) {
70          return new SpeciesAbleBatchCategoryPredicate(cateogryId, value);
71      }
72  
73      public static SpeciesBatch createNewChild(SpeciesBatch parent) {
74  
75          SpeciesBatch child = newInstance(parent);
76          child.setFishingOperation(parent.getFishingOperation());
77          child.setSpecies(parent.getSpecies());
78          child.setParentBatch(parent);
79          parent.addChildBatchs(child);
80          return child;
81  
82      }
83  
84      public static SpeciesBatch newBenthosBatch() {
85          SpeciesBatch speciesBatch = newSpeciesBatch();
86          speciesBatch.setBenthosBatch(true);
87          return speciesBatch;
88      }
89  
90      public static void grabSpeciesChildBatchs(Collection<SpeciesBatch> childs, Set<Species> speciesSet) {
91  
92          for (SpeciesBatch child : childs) {
93              speciesSet.add(child.getSpecies());
94          }
95  
96      }
97  
98      public static void grabSampleCategorieValuesChildBatchs(SpeciesBatch batch, Set<Integer> categoryIds) {
99  
100         CaracteristicQualitativeValue sampleCategoryValue = (CaracteristicQualitativeValue) batch.getSampleCategoryValue();
101 
102         categoryIds.add(sampleCategoryValue.getIdAsInt());
103         if (!batch.isChildBatchsEmpty()) {
104             for (SpeciesBatch child : batch.getChildBatchs()) {
105                 grabSampleCategorieValuesChildBatchs(child, categoryIds);
106             }
107         }
108 
109 
110     }
111 
112     public static class SpeciesAbleBatchCategoryPredicate implements Predicate<SpeciesBatch> {
113 
114         private final Integer id;
115 
116         private final Integer qualitativeValue;
117 
118         public SpeciesAbleBatchCategoryPredicate(Integer id, Integer qualitativeValue) {
119             this.id = id;
120             this.qualitativeValue = qualitativeValue;
121         }
122 
123         @Override
124         public boolean apply(SpeciesBatch input) {
125             return id.equals(input.getSampleCategoryId()) &&
126                     input.getSampleCategoryValue() instanceof CaracteristicQualitativeValue &&
127                     qualitativeValue.equals(((CaracteristicQualitativeValue) input.getSampleCategoryValue()).getIdAsInt());
128         }
129 
130     }
131 }