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.collections.duplicates;
017
018import org.gbif.api.util.VocabularyUtils;
019import org.gbif.api.vocabulary.Country;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.List;
024import java.util.StringJoiner;
025import java.util.UUID;
026import java.util.stream.Collectors;
027
028/** Request to be used for the GRSciColl API duplicates services. */
029public class DuplicatesRequest implements Serializable {
030
031  private Boolean sameName;
032  private Boolean sameFuzzyName;
033  private Boolean sameCode;
034  private Boolean sameCountry;
035  private Boolean sameCity;
036  private Boolean sameInstitution;
037  private List<String> inCountries;
038  private List<String> notInCountries;
039  private List<UUID> excludeKeys;
040  private List<UUID> inInstitutions;
041  private List<UUID> notInInstitutions;
042
043  public Boolean getSameName() {
044    return sameName;
045  }
046
047  public void setSameName(Boolean sameName) {
048    this.sameName = sameName;
049  }
050
051  public Boolean getSameFuzzyName() {
052    return sameFuzzyName;
053  }
054
055  public void setSameFuzzyName(Boolean sameFuzzyName) {
056    this.sameFuzzyName = sameFuzzyName;
057  }
058
059  public Boolean getSameCode() {
060    return sameCode;
061  }
062
063  public void setSameCode(Boolean sameCode) {
064    this.sameCode = sameCode;
065  }
066
067  public Boolean getSameCountry() {
068    return sameCountry;
069  }
070
071  public void setSameCountry(Boolean sameCountry) {
072    this.sameCountry = sameCountry;
073  }
074
075  public Boolean getSameCity() {
076    return sameCity;
077  }
078
079  public void setSameCity(Boolean sameCity) {
080    this.sameCity = sameCity;
081  }
082
083  public List<Country> getInCountries() {
084    if (inCountries == null) {
085      return Collections.emptyList();
086    }
087    return inCountries.stream().map(this::toCountry).collect(Collectors.toList());
088  }
089
090  public void setInCountries(List<String> inCountries) {
091    this.inCountries = inCountries;
092  }
093
094  public List<Country> getNotInCountries() {
095    if (notInCountries == null) {
096      return Collections.emptyList();
097    }
098    return notInCountries.stream().map(this::toCountry).collect(Collectors.toList());
099  }
100
101  public void setNotInCountries(List<String> notInCountries) {
102    this.notInCountries = notInCountries;
103  }
104
105  public List<UUID> getExcludeKeys() {
106    return excludeKeys;
107  }
108
109  public void setExcludeKeys(List<UUID> excludeKeys) {
110    this.excludeKeys = excludeKeys;
111  }
112
113  public Boolean getSameInstitution() {
114    return sameInstitution;
115  }
116
117  public void setSameInstitution(Boolean sameInstitution) {
118    this.sameInstitution = sameInstitution;
119  }
120
121  public List<UUID> getInInstitutions() {
122    return inInstitutions;
123  }
124
125  public void setInInstitutions(List<UUID> inInstitutions) {
126    this.inInstitutions = inInstitutions;
127  }
128
129  public List<UUID> getNotInInstitutions() {
130    return notInInstitutions;
131  }
132
133  public void setNotInInstitutions(List<UUID> notInInstitutions) {
134    this.notInInstitutions = notInInstitutions;
135  }
136
137  public boolean isEmpty() {
138    return sameName == null
139        && sameFuzzyName == null
140        && sameCode == null
141        && sameCity == null
142        && sameCountry == null;
143  }
144
145  private Country toCountry(String countryParam) {
146    Country country = null;
147    if (countryParam != null && !countryParam.isEmpty()) {
148      country = Country.fromIsoCode(countryParam);
149
150      if (country == null) {
151        // if nothing found also try by enum name
152        country = VocabularyUtils.lookupEnum(countryParam, Country.class);
153      }
154    }
155    return country;
156  }
157
158  @Override
159  public String toString() {
160    return new StringJoiner(", ", DuplicatesRequest.class.getSimpleName() + "[", "]")
161        .add("sameName=" + sameName)
162        .add("sameFuzzyName=" + sameFuzzyName)
163        .add("sameCode=" + sameCode)
164        .add("sameCountry=" + sameCountry)
165        .add("sameCity=" + sameCity)
166        .add("sameInstitution=" + sameInstitution)
167        .add("inCountries=" + inCountries)
168        .add("notInCountries=" + notInCountries)
169        .add("excludeKeys=" + excludeKeys)
170        .add("inInstitutions=" + inInstitutions)
171        .add("notInInstitutions=" + notInInstitutions)
172        .toString();
173  }
174}