001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.checklistbank.search;
017
018import org.gbif.api.model.common.LinneanClassification;
019import org.gbif.api.model.common.LinneanClassificationKeys;
020import org.gbif.api.util.ClassificationUtils;
021import org.gbif.api.vocabulary.Rank;
022import org.gbif.api.vocabulary.TaxonomicStatus;
023
024import java.util.LinkedHashMap;
025import java.util.Objects;
026import java.util.StringJoiner;
027
028import javax.validation.constraints.NotNull;
029
030import com.fasterxml.jackson.annotation.JsonProperty;
031
032/**
033 * Class used for returning results of a suggest operation.
034 * This class contains additional attributes that are required for displaying/providing textual information.
035 */
036@SuppressWarnings("unused")
037public class NameUsageSuggestResult implements LinneanClassification, LinneanClassificationKeys {
038
039  private Integer key;
040  private Integer nameKey;
041
042  // for LinneanClassification
043  private String kingdom;
044  private String phylum;
045  @JsonProperty("class")
046  private String clazz;
047  private String order;
048  private String family;
049  private String genus;
050  private String subgenus;
051  private String species;
052  // for LinneanClassificationKeys
053  private Integer kingdomKey;
054  private Integer phylumKey;
055  private Integer classKey;
056  private Integer orderKey;
057  private Integer familyKey;
058  private Integer genusKey;
059  private Integer subgenusKey;
060  private Integer speciesKey;
061
062  private String parent;
063  private Integer parentKey;
064  private Integer nubKey;
065  private String scientificName;
066  private String canonicalName;
067  private Rank rank;
068  private TaxonomicStatus status;
069
070  /**
071   * @return the name key for retrieving a parsed name object
072   */
073  public Integer getNameKey() {
074    return nameKey;
075  }
076
077  public void setNameKey(Integer nameKey) {
078    this.nameKey = nameKey;
079  }
080
081  /**
082   * @return true if its a synonym
083   */
084  public boolean isSynonym() {
085    return status != null && status.isSynonym();
086  }
087
088  public String getScientificName() {
089    return scientificName;
090  }
091
092  public void setScientificName(String scientificName) {
093    this.scientificName = scientificName;
094  }
095
096  public TaxonomicStatus getStatus() {
097    return status;
098  }
099
100  public void setStatus(TaxonomicStatus status) {
101    this.status = status;
102  }
103
104  public Integer getNubKey() {
105    return nubKey;
106  }
107
108  public void setNubKey(Integer nubKey) {
109    this.nubKey = nubKey;
110  }
111
112  /**
113   * Taxon key.
114   */
115  public Integer getKey() {
116    return key;
117  }
118
119  @Override
120  public String getKingdom() {
121    return kingdom;
122  }
123
124  @Override
125  public String getPhylum() {
126    return phylum;
127  }
128
129  @Override
130  public String getClazz() {
131    return clazz;
132  }
133
134  @Override
135  public String getOrder() {
136    return order;
137  }
138
139  @Override
140  public String getFamily() {
141    return family;
142  }
143
144  @Override
145  public String getGenus() {
146    return genus;
147  }
148
149  @Override
150  public String getSubgenus() {
151    return subgenus;
152  }
153
154  @Override
155  public String getSpecies() {
156    return species;
157  }
158
159  @Override
160  public Integer getKingdomKey() {
161    return kingdomKey;
162  }
163
164  @Override
165  public Integer getPhylumKey() {
166    return phylumKey;
167  }
168
169  @Override
170  public Integer getClassKey() {
171    return classKey;
172  }
173
174  @Override
175  public Integer getOrderKey() {
176    return orderKey;
177  }
178
179  @Override
180  public Integer getFamilyKey() {
181    return familyKey;
182  }
183
184  @Override
185  public Integer getGenusKey() {
186    return genusKey;
187  }
188
189  @Override
190  public Integer getSubgenusKey() {
191    return subgenusKey;
192  }
193
194  @Override
195  public Integer getSpeciesKey() {
196    return speciesKey;
197  }
198
199  public String getCanonicalName() {
200    return canonicalName;
201  }
202
203  public void setCanonicalName(String canonicalName) {
204    this.canonicalName = canonicalName;
205  }
206
207  @Override
208  public void setClassKey(Integer classKey) {
209    this.classKey = classKey;
210  }
211
212  @Override
213  public void setClazz(String clazz) {
214    this.clazz = clazz;
215  }
216
217  @Override
218  public void setFamily(String family) {
219    this.family = family;
220  }
221
222  @Override
223  public void setFamilyKey(Integer familyKey) {
224    this.familyKey = familyKey;
225  }
226
227  @Override
228  public void setGenus(String genus) {
229    this.genus = genus;
230  }
231
232  @Override
233  public void setGenusKey(Integer genusKey) {
234    this.genusKey = genusKey;
235  }
236
237  public void setKey(Integer key) {
238    this.key = key;
239  }
240
241  @Override
242  public void setKingdom(String kingdom) {
243    this.kingdom = kingdom;
244  }
245
246  @Override
247  public void setKingdomKey(Integer kingdomKey) {
248    this.kingdomKey = kingdomKey;
249  }
250
251  @Override
252  public void setOrder(String order) {
253    this.order = order;
254  }
255
256  @Override
257  public void setOrderKey(Integer orderKey) {
258    this.orderKey = orderKey;
259  }
260
261  @Override
262  public void setPhylum(String phylum) {
263    this.phylum = phylum;
264  }
265
266  @Override
267  public void setPhylumKey(Integer phylumKey) {
268    this.phylumKey = phylumKey;
269  }
270
271  @Override
272  public void setSpecies(String species) {
273    this.species = species;
274  }
275
276  @Override
277  public void setSpeciesKey(Integer speciesKey) {
278    this.speciesKey = speciesKey;
279  }
280
281  @Override
282  public void setSubgenus(String subgenus) {
283    this.subgenus = subgenus;
284  }
285
286  @Override
287  public void setSubgenusKey(Integer subgenusKey) {
288    this.subgenusKey = subgenusKey;
289  }
290
291  public String getParent() {
292    return parent;
293  }
294
295  public Integer getParentKey() {
296    return parentKey;
297  }
298
299  public void setParent(String parent) {
300    this.parent = parent;
301  }
302
303  public void setParentKey(Integer parentKey) {
304    this.parentKey = parentKey;
305  }
306
307  public Rank getRank() {
308    return rank;
309  }
310
311  public void setRank(Rank rank) {
312    this.rank = rank;
313  }
314
315  @Override
316  public String getHigherRank(Rank rank) {
317    return ClassificationUtils.getHigherRank(this, rank);
318  }
319
320  @Override
321  public Integer getHigherRankKey(Rank rank) {
322    return ClassificationUtils.getHigherRankKey(this, rank);
323  }
324
325  /**
326   * An ordered map with entries for all higher Linnean ranks down to the actual direct parent of this usage.
327   * The map starts with the highest rank, e.g. the kingdom and maps the name usage key to its canonical name.
328   * The name usage itself is never included, even though a higher rank might point to the usage itself.
329   *
330   * @return map of higher ranks
331   */
332  @NotNull
333  public LinkedHashMap<Integer, String> getHigherClassificationMap() {
334    return ClassificationUtils.getHigherClassificationMap(this, key, parentKey, parent);
335  }
336
337  @Override
338  public boolean equals(Object o) {
339    if (this == o) {
340      return true;
341    }
342    if (o == null || getClass() != o.getClass()) {
343      return false;
344    }
345    NameUsageSuggestResult that = (NameUsageSuggestResult) o;
346    return Objects.equals(key, that.key) &&
347      Objects.equals(nameKey, that.nameKey) &&
348      Objects.equals(kingdom, that.kingdom) &&
349      Objects.equals(phylum, that.phylum) &&
350      Objects.equals(clazz, that.clazz) &&
351      Objects.equals(order, that.order) &&
352      Objects.equals(family, that.family) &&
353      Objects.equals(genus, that.genus) &&
354      Objects.equals(subgenus, that.subgenus) &&
355      Objects.equals(species, that.species) &&
356      Objects.equals(kingdomKey, that.kingdomKey) &&
357      Objects.equals(phylumKey, that.phylumKey) &&
358      Objects.equals(classKey, that.classKey) &&
359      Objects.equals(orderKey, that.orderKey) &&
360      Objects.equals(familyKey, that.familyKey) &&
361      Objects.equals(genusKey, that.genusKey) &&
362      Objects.equals(subgenusKey, that.subgenusKey) &&
363      Objects.equals(speciesKey, that.speciesKey) &&
364      Objects.equals(parent, that.parent) &&
365      Objects.equals(parentKey, that.parentKey) &&
366      Objects.equals(nubKey, that.nubKey) &&
367      Objects.equals(scientificName, that.scientificName) &&
368      Objects.equals(canonicalName, that.canonicalName) &&
369      rank == that.rank &&
370      status == that.status;
371  }
372
373  @Override
374  public int hashCode() {
375    return Objects
376      .hash(key, nameKey, kingdom, phylum, clazz, order, family, genus, subgenus, species,
377        kingdomKey, phylumKey, classKey, orderKey, familyKey, genusKey, subgenusKey, speciesKey,
378        parent, parentKey, nubKey, scientificName, canonicalName, rank, status);
379  }
380
381  @Override
382  public String toString() {
383    return new StringJoiner(", ", NameUsageSuggestResult.class.getSimpleName() + "[", "]")
384      .add("key=" + key)
385      .add("nameKey=" + nameKey)
386      .add("kingdom='" + kingdom + "'")
387      .add("phylum='" + phylum + "'")
388      .add("clazz='" + clazz + "'")
389      .add("order='" + order + "'")
390      .add("family='" + family + "'")
391      .add("genus='" + genus + "'")
392      .add("subgenus='" + subgenus + "'")
393      .add("species='" + species + "'")
394      .add("kingdomKey=" + kingdomKey)
395      .add("phylumKey=" + phylumKey)
396      .add("classKey=" + classKey)
397      .add("orderKey=" + orderKey)
398      .add("familyKey=" + familyKey)
399      .add("genusKey=" + genusKey)
400      .add("subgenusKey=" + subgenusKey)
401      .add("speciesKey=" + speciesKey)
402      .add("parent='" + parent + "'")
403      .add("parentKey=" + parentKey)
404      .add("nubKey=" + nubKey)
405      .add("scientificName='" + scientificName + "'")
406      .add("canonicalName='" + canonicalName + "'")
407      .add("rank=" + rank)
408      .add("status=" + status)
409      .toString();
410  }
411}