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