View Javadoc
1   package fr.ifremer.tutti.persistence.entities.referential;
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.Preconditions;
29  import com.google.common.base.Predicate;
30  import com.google.common.collect.Maps;
31  import com.google.common.collect.Multimap;
32  import com.google.common.collect.Multimaps;
33  import org.apache.commons.lang3.StringUtils;
34  import org.nuiton.jaxx.application.ApplicationBusinessException;
35  
36  import java.util.Comparator;
37  import java.util.Map;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  public class Speciess extends AbstractSpeciess {
42  
43      public static final Predicate<Species> IS_TEMPORARY = Speciess::isTemporary;
44  
45      /**
46       * Is the given {@code species} a temporary data ?
47       *
48       * @param species species to test
49       * @return {@code true} if the given {@code species} is temporary
50       * @since 3.8
51       */
52      public static boolean isTemporary(Species species) {
53  
54          Preconditions.checkNotNull(species);
55          Preconditions.checkNotNull(species.getId());
56  
57          return TuttiReferentialEntities.isStatusTemporary(species) && isTemporaryId(species.getIdAsInt());
58  
59      }
60  
61      /**
62       * Is the given {@code id} is a temporary species id ?
63       *
64       * @param id id to test
65       * @return {@code true} if the id is a species temporary id
66       * @since 3.14
67       */
68      public static boolean isTemporaryId(Integer id) {
69  
70          Preconditions.checkNotNull(id);
71          return id < 0;
72  
73      }
74  
75      public static final Comparator<Species> SPECIES_BY_NAME_COMPARATOR = (o1, o2) -> {
76          if (o1 == null) {
77              return -1;
78          }
79          if (o2 == null) {
80              return 1;
81          }
82          return o1.getName().compareTo(o2.getName());
83      };
84  
85      public static final Function<Species, String> GET_REFERECE_TAXON_ID = input -> String.valueOf(input.getReferenceTaxonId());
86  
87      public static final Function<Species, Integer> GET_REFERECE_TAXON_ID_AS_INT = Species::getReferenceTaxonId;
88  
89      public static final Function<Species, String> GET_REF_TAX_CODE = input -> String.valueOf(input.getRefTaxCode());
90  
91      public static final Function<Species, String> GET_SURVEY_CODE = input -> String.valueOf(input.getSurveyCode());
92  
93      public static final Function<Species, String> GET_NAME = Species::getName;
94  
95      /**
96       * Indexe une liste d'espèces référentes par la propriété {@link Species#PROPERTY_REFERENCE_TAXON_ID}.
97       *
98       * Attention de ne pas utiliser cette méthode sur une liste d'espèces non référentes, car le {@link Species#PROPERTY_REFERENCE_TAXON_ID}
99       * peut servir pour différentes espèces (le référent et ses synonymes).
100      *
101      * Cette méthode vérifie donc avant d'effectuer le split que toutes les espèces données sont référentes.
102      *
103      * @param list la liste des espèces référentes.
104      * @return la dictionnaire des espèces référentes indexées par leur code {@link Species#PROPERTY_REFERENCE_TAXON_ID}.
105      */
106     public static Map<String, Species> splitReferenceSpeciesByReferenceTaxonId(Iterable<Species> list) {
107 
108         for (Species species : list) {
109             Preconditions.checkArgument(species.isReferenceTaxon(), "L'espèce " + species.getId() + " n'est pas référente.");
110         }
111 
112         return Maps.uniqueIndex(list, GET_REFERECE_TAXON_ID);
113     }
114 
115     public static Multimap<String, Species> splitByReferenceTaxonId(Iterable<Species> list) {
116         return Multimaps.index(list, GET_REFERECE_TAXON_ID);
117     }
118 
119     public static Multimap<String, Species> splitByRefTaxCode(Iterable<Species> list) {
120         return Multimaps.index(list, GET_REF_TAX_CODE);
121     }
122 
123     public static Multimap<String, Species> splitBySurveyCode(Iterable<Species> list) {
124         return Multimaps.index(list, GET_SURVEY_CODE);
125     }
126 
127 //    public static Set<String> toReferenceTaxonIds(List<Species> list) {
128 //        return list == null ?
129 //                             Collections.<String>emptySet() :
130 //                             Sets.newHashSet(Lists.transform(list, GET_REFERECE_TAXON_ID));
131 //    }
132 
133     public static String getSurveyCodeOrRefTaxCode(Species species) {
134         String code = species.getSurveyCode();
135 
136         if (code == null) {
137 
138             // use refTaxCode
139             code = species.getRefTaxCode();
140         }
141 
142         if (StringUtils.isEmpty(code)) {
143 
144 
145             throw new ApplicationBusinessException(t("tutti.persistence.error.species.withNoSurveyCodeOrRefTaxCode", species.getReferenceTaxonId(), species.getName()));
146 
147         }
148 
149         return code;
150     }
151 } //Speciess