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 io.swagger.v3.oas.annotations.media.Schema;
019
020import java.io.Serializable;
021import java.util.Date;
022import java.util.Objects;
023import java.util.StringJoiner;
024
025import javax.validation.constraints.NotNull;
026import javax.validation.constraints.Null;
027import javax.validation.constraints.Size;
028
029public class Comment implements Serializable, LenientEquals<Comment> {
030
031  @Schema(
032    description = "Identifier for the comment",
033    accessMode = Schema.AccessMode.READ_ONLY
034  )
035  private Integer key;
036
037  @Schema(
038    description = "The text of the comment"
039  )
040  private String content;
041
042  @Schema(
043    description = "The GBIF username of the creator of the comment",
044    accessMode = Schema.AccessMode.READ_ONLY
045  )
046  private String createdBy;
047
048  @Schema(
049    description = "The GBIF username of the last user to modify the comment",
050    accessMode = Schema.AccessMode.READ_ONLY
051  )
052  private String modifiedBy;
053
054  @Schema(
055    description = "Timestamp of when the comment was created",
056    accessMode = Schema.AccessMode.READ_ONLY
057  )
058  private Date created;
059
060  @Schema(
061    description = "Timestamp of when the comment was last modified",
062    accessMode = Schema.AccessMode.READ_ONLY
063  )
064  private Date modified;
065
066  @Null(groups = {PrePersist.class})
067  @NotNull(groups = {PostPersist.class})
068  public Integer getKey() {
069    return key;
070  }
071
072  public void setKey(Integer key) {
073    this.key = key;
074  }
075
076  @NotNull
077  @Size(min = 1)
078  public String getContent() {
079    return content;
080  }
081
082  public void setContent(String content) {
083    this.content = content;
084  }
085
086  @Size(min = 3)
087  public String getCreatedBy() {
088    return createdBy;
089  }
090
091  public void setCreatedBy(String createdBy) {
092    this.createdBy = createdBy;
093  }
094
095  @Size(min = 3)
096  public String getModifiedBy() {
097    return modifiedBy;
098  }
099
100  public void setModifiedBy(String modifiedBy) {
101    this.modifiedBy = modifiedBy;
102  }
103
104  @Null(groups = {PrePersist.class})
105  @NotNull(groups = {PostPersist.class})
106  public Date getCreated() {
107    return created;
108  }
109
110  public void setCreated(Date created) {
111    this.created = created;
112  }
113
114  @Null(groups = {PrePersist.class})
115  @NotNull(groups = {PostPersist.class})
116  public Date getModified() {
117    return modified;
118  }
119
120  public void setModified(Date modified) {
121    this.modified = modified;
122  }
123
124  @Override
125  public boolean equals(Object o) {
126    if (this == o) {
127      return true;
128    }
129    if (o == null || getClass() != o.getClass()) {
130      return false;
131    }
132    Comment comment = (Comment) o;
133    return Objects.equals(key, comment.key)
134        && Objects.equals(content, comment.content)
135        && Objects.equals(createdBy, comment.createdBy)
136        && Objects.equals(modifiedBy, comment.modifiedBy)
137        && Objects.equals(created, comment.created)
138        && Objects.equals(modified, comment.modified);
139  }
140
141  @Override
142  public int hashCode() {
143    return Objects.hash(key, content, createdBy, modifiedBy, created, modified);
144  }
145
146  @Override
147  public String toString() {
148    return new StringJoiner(", ", Comment.class.getSimpleName() + "[", "]")
149        .add("key=" + key)
150        .add("content='" + content + "'")
151        .add("createdBy='" + createdBy + "'")
152        .add("modifiedBy='" + modifiedBy + "'")
153        .add("created=" + created)
154        .add("modified=" + modified)
155        .toString();
156  }
157
158  /**
159   * A lenient equality check ignoring server side fields. If the objects are equal or have the same content, they are
160   * considered the same.
161   */
162  @Override
163  public boolean lenientEquals(Comment other) {
164    if (this == other) {
165      return true;
166    }
167    return Objects.equals(this.content, other.content);
168  }
169}