001/*
002 * Copyright 2013 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.vocabulary;
017
018import org.gbif.api.util.VocabularyUtils;
019
020import java.util.List;
021import javax.annotation.Nullable;
022
023import com.google.common.base.Strings;
024import com.google.common.collect.ImmutableList;
025
026/**
027 * Enumeration for all possible identifier types.
028 */
029public enum IdentifierType {
030
031  URL,
032  /**
033   * Reference controlled by a separate system, used for example by DOI.
034   * {http://en.wikipedia.org/wiki/Handle_(computing)}
035   */
036  LSID,
037  HANDLER,
038  DOI,
039  UUID,
040  FTP,
041  URI,
042  UNKNOWN,
043  /**
044   * Indicates the identifier originated from an auto_increment column in the portal.data_provider or
045   * portal.data_resource table respectively.
046   */
047  GBIF_PORTAL,
048  /**
049   * Identifies the node (e.g: 'DK' for Denmark, 'sp2000' for Species 2000).
050   */
051  GBIF_NODE,
052
053  /**
054   * Participant identifier from the GBIF IMS Filemaker system.
055   */
056  GBIF_PARTICIPANT;
057
058  // TODO: Check if this is used, it didn't exist in the new Registry2 API, but I preserved it from the old vocabulary
059  public static final List<IdentifierType> TYPES;
060
061  static {
062    TYPES = ImmutableList.copyOf(IdentifierType.values());
063  }
064
065  /**
066   * @return the matching IdentifierType or null
067   */
068  public static IdentifierType fromString(String identifierType) {
069    return (IdentifierType) VocabularyUtils.lookupEnum(identifierType, IdentifierType.class);
070  }
071
072
073  /**
074   * Tries to infer the identifier type from a given identifier.
075   * Most identifiers have a URI protocol prefix or a specific structure that
076   * allows the guess.
077   *
078   * @return the inferred identifier type or Unknown if identifier is null or cant be inferred.
079   */
080  public static IdentifierType inferFrom(@Nullable String identifier) {
081    String lcIdentifier = Strings.nullToEmpty(identifier).trim().toLowerCase();
082
083    if (lcIdentifier.isEmpty()) {
084      return UNKNOWN;
085    }
086
087    if (lcIdentifier.startsWith("doi:10")
088        || lcIdentifier.startsWith("urn:doi:")
089        || lcIdentifier.startsWith("http://dx.doi.org/10.")) {
090      return DOI;
091    }
092    if (lcIdentifier.startsWith("http:")
093        || lcIdentifier.startsWith("https:")
094        || lcIdentifier.startsWith("www.")) {
095      return URL;
096    }
097    if (lcIdentifier.startsWith("ftp:")) {
098      return FTP;
099    }
100    if (lcIdentifier.startsWith("urn:lsid:") || lcIdentifier.startsWith("lsid:")) {
101      return LSID;
102    }
103
104    if (lcIdentifier.startsWith("urn:uuid:") || lcIdentifier.startsWith("uuid:")) {
105      return UUID;
106    }
107    try {
108      java.util.UUID.fromString(lcIdentifier);
109      return UUID;
110    } catch (IllegalArgumentException ignored) {
111      // We're just trying to convert a String to anything readable. Apparently the UUID approach failed.
112    }
113
114    return UNKNOWN;
115  }
116}