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.util;
017
018import org.gbif.api.vocabulary.Country;
019import org.gbif.api.vocabulary.IdentifierType;
020
021import javax.annotation.Nullable;
022
023import java.util.regex.Pattern;
024
025/**
026 * This class contains utility methods for identifiers. Currently there are 3 separate Identifier
027 * classes: </br> 1) org.gbif.api.model.checklistbank.Identifier 2)
028 * org.gbif.api.model.common.Identifier 3) org.gbif.api.model.registry.Identifier </br> Methods
029 * common to 2 or more classes should be listed here.
030 */
031public class IdentifierUtils {
032
033  public static final Pattern WIKIDATA_PATTERN =
034      Pattern.compile("http(s)?://www.wikidata.org/entity/([A-Za-z][0-9]+)$");
035
036  /**
037   * Creates a http link for an identifier if possible by passing it to some known resolvers for the
038   * specific id type. If no link can be constructed, null is returned.
039   *
040   * @param identifier Identifier's identifier
041   * @param type Identifier's type
042   * @return the url or null if it cannot be created
043   */
044  @Nullable
045  public static String getIdentifierLink(String identifier, IdentifierType type) {
046    if (identifier == null || type == null) {
047      return null;
048    }
049    switch (type) {
050      case HANDLER:
051      case URI:
052      case URL:
053      case FTP:
054        return identifier;
055      case DOI:
056        return "https://doi.org/" + identifier;
057      case LSID:
058        return "http://www.lsid.info/" + identifier;
059      case GBIF_PORTAL:
060        return "https://www.gbif.org/dataset/" + identifier;
061    }
062    return null;
063  }
064
065  /** CITES identifier validation according to https://cites.org/eng/common/reg/e_si.html. */
066  public static boolean isValidCitesIdentifier(String identifier) {
067    if (identifier == null || identifier.isEmpty()) {
068      return false;
069    }
070
071    String[] parts = identifier.split("\\s+");
072    if (parts.length < 2) {
073      return false;
074    }
075
076    if (parts[0].length() != 2) {
077      return false;
078    }
079
080    Country country = Country.fromIsoCode(parts[0]);
081    return country != null;
082  }
083
084  public static boolean isValidWikidataIdentifier(String identifier) {
085    if (identifier == null || identifier.isEmpty()) {
086      return false;
087    }
088
089    return WIKIDATA_PATTERN.matcher(identifier).matches();
090  }
091}