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.registry;
017
018import org.gbif.api.vocabulary.MetadataType;
019
020import java.io.Serializable;
021import java.util.Date;
022import java.util.Objects;
023import java.util.StringJoiner;
024import java.util.UUID;
025
026import javax.validation.constraints.Min;
027import javax.validation.constraints.NotNull;
028import javax.validation.constraints.Size;
029
030public class Metadata implements Serializable, LenientEquals<Metadata> {
031
032  private Integer key;
033  private UUID datasetKey;
034  private MetadataType type;
035  private String content;
036
037  private String createdBy;
038  private String modifiedBy;
039  private Date created;
040  private Date modified;
041
042  @Min(1)
043  public Integer getKey() {
044    return key;
045  }
046
047  public void setKey(Integer key) {
048    this.key = key;
049  }
050
051  @NotNull
052  public UUID getDatasetKey() {
053    return datasetKey;
054  }
055
056  public void setDatasetKey(UUID datasetKey) {
057    this.datasetKey = datasetKey;
058  }
059
060  @NotNull
061  public MetadataType getType() {
062    return type;
063  }
064
065  public void setType(MetadataType type) {
066    this.type = type;
067  }
068
069  @NotNull
070  @Size(min = 1)
071  public String getContent() {
072    return content;
073  }
074
075  public void setContent(String content) {
076    this.content = content;
077  }
078
079  @NotNull
080  @Size(min = 3)
081  public String getCreatedBy() {
082    return createdBy;
083  }
084
085  public void setCreatedBy(String createdBy) {
086    this.createdBy = createdBy;
087  }
088
089  @NotNull
090  @Size(min = 3)
091  public String getModifiedBy() {
092    return modifiedBy;
093  }
094
095  public void setModifiedBy(String modifiedBy) {
096    this.modifiedBy = modifiedBy;
097  }
098
099  @NotNull
100  public Date getCreated() {
101    return created;
102  }
103
104  public void setCreated(Date created) {
105    this.created = created;
106  }
107
108  @NotNull
109  public Date getModified() {
110    return modified;
111  }
112
113  public void setModified(Date modified) {
114    this.modified = modified;
115  }
116
117  @Override
118  public boolean equals(Object o) {
119    if (this == o) {
120      return true;
121    }
122    if (o == null || getClass() != o.getClass()) {
123      return false;
124    }
125    Metadata metadata = (Metadata) o;
126    return Objects.equals(key, metadata.key)
127        && Objects.equals(datasetKey, metadata.datasetKey)
128        && type == metadata.type
129        && Objects.equals(content, metadata.content)
130        && Objects.equals(createdBy, metadata.createdBy)
131        && Objects.equals(modifiedBy, metadata.modifiedBy)
132        && Objects.equals(created, metadata.created)
133        && Objects.equals(modified, metadata.modified);
134  }
135
136  @Override
137  public int hashCode() {
138    return Objects.hash(key, datasetKey, type, content, createdBy, modifiedBy, created, modified);
139  }
140
141  @Override
142  public String toString() {
143    return new StringJoiner(", ", Metadata.class.getSimpleName() + "[", "]")
144        .add("key=" + key)
145        .add("datasetKey=" + datasetKey)
146        .add("type=" + type)
147        .add("content='" + content + "'")
148        .add("createdBy='" + createdBy + "'")
149        .add("modifiedBy='" + modifiedBy + "'")
150        .add("created=" + created)
151        .add("modified=" + modified)
152        .toString();
153  }
154
155  /**
156   * Does not include the key or server controlled values (created, createdBy etc).
157   */
158  @Override
159  public boolean lenientEquals(Metadata other) {
160    if (this == other) {
161      return true;
162    }
163    return Objects.equals(this.datasetKey, other.datasetKey)
164        && Objects.equals(this.type, other.type)
165        && Objects.equals(this.content, other.content);
166  }
167}