001/*
002 * Copyright 2013 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.registry;
014
015import org.gbif.api.util.IdentifierUtils;
016import org.gbif.api.vocabulary.IdentifierType;
017
018import java.util.Date;
019import javax.annotation.Nullable;
020import javax.validation.constraints.Min;
021import javax.validation.constraints.NotNull;
022import javax.validation.constraints.Null;
023import javax.validation.constraints.Size;
024
025import com.google.common.base.Objects;
026import org.codehaus.jackson.annotate.JsonIgnore;
027
028public class Identifier implements LenientEquals<Identifier> {
029
030  private Integer key;
031  private IdentifierType type;
032  private String identifier;
033  private String createdBy;
034  private Date created;
035
036  public Identifier() {
037  }
038
039  public Identifier(IdentifierType type, String identifier) {
040    this.type = type;
041    this.identifier = identifier;
042  }
043
044  @Null(groups = {PrePersist.class})
045  @NotNull(groups = {PostPersist.class})
046  @Min(1)
047  public Integer getKey() {
048    return key;
049  }
050
051  public void setKey(Integer key) {
052    this.key = key;
053  }
054
055  @NotNull
056  public IdentifierType getType() {
057    return type;
058  }
059
060  public void setType(IdentifierType type) {
061    this.type = type;
062  }
063
064  @NotNull
065  @Size(min = 1)
066  public String getIdentifier() {
067    return identifier;
068  }
069
070  public void setIdentifier(String identifier) {
071    this.identifier = identifier;
072  }
073
074  @Size(min = 3)
075  public String getCreatedBy() {
076    return createdBy;
077  }
078
079  public void setCreatedBy(String createdBy) {
080    this.createdBy = createdBy;
081  }
082
083  @Null(groups = {PrePersist.class})
084  @NotNull(groups = {PostPersist.class})
085  public Date getCreated() {
086    return created;
087  }
088
089  public void setCreated(Date created) {
090    this.created = created;
091  }
092
093  /**
094   * Creates a http link for an identifier if possible by passing it to some known resolvers for the specific id type.
095   * If no link can be constructed, null is returned.
096   *
097   * @return the url or null if it cannot be created
098   *
099   * @see org.gbif.api.util.IdentifierUtils#getIdentifierLink(String, org.gbif.api.vocabulary.IdentifierType)
100   */
101  @Nullable
102  @JsonIgnore
103  public String getIdentifierLink() {
104    return IdentifierUtils.getIdentifierLink(identifier, type);
105  }
106
107  @Override
108  public int hashCode() {
109    return Objects.hashCode(key, type, identifier, createdBy, created);
110  }
111
112  @Override
113  public boolean equals(Object object) {
114    if (object instanceof Identifier) {
115      Identifier that = (Identifier) object;
116      return Objects.equal(this.key, that.key)
117        && Objects.equal(this.type, that.type)
118        && Objects.equal(this.identifier, that.identifier)
119        && Objects.equal(this.createdBy, that.createdBy)
120        && Objects.equal(this.created, that.created);
121    }
122    return false;
123  }
124
125  @Override
126  public String toString() {
127    return Objects.toStringHelper(this)
128      .add("key", key)
129      .add("type", type)
130      .add("identifier", identifier)
131      .add("createdBy", createdBy)
132      .add("created", created)
133      .toString();
134  }
135
136  /**
137   * A lenient equality check ignoring server controlled values (createdBy, key etc).
138   */
139  @Override
140  public boolean lenientEquals(Identifier other) {
141    if (this == other) {
142      return true;
143    }
144    return Objects.equal(this.type, other.type)
145      && Objects.equal(this.identifier, other.identifier);
146  }
147
148}