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.vocabulary.EndpointType;
016
017import java.net.URI;
018import java.util.Date;
019import java.util.List;
020import javax.annotation.Nullable;
021import javax.validation.constraints.Min;
022import javax.validation.constraints.NotNull;
023import javax.validation.constraints.Null;
024import javax.validation.constraints.Size;
025
026import com.google.common.base.Objects;
027import com.google.common.collect.Lists;
028
029public class Endpoint implements MachineTaggable, LenientEquals<Endpoint> {
030
031  private Integer key;
032  private EndpointType type;
033  private URI url;
034  private String description;
035  private String createdBy;
036  private String modifiedBy;
037  private Date created;
038  private Date modified;
039  private List<MachineTag> machineTags = Lists.newArrayList();
040
041  @Null(groups = PrePersist.class)
042  @NotNull(groups = PostPersist.class)
043  @Min(1)
044  public Integer getKey() {
045    return key;
046  }
047
048  public void setKey(Integer key) {
049    this.key = key;
050  }
051
052  @NotNull
053  public EndpointType getType() {
054    return type;
055  }
056
057  public void setType(EndpointType type) {
058    this.type = type;
059  }
060
061  @Nullable
062  public URI getUrl() {
063    return url;
064  }
065
066  public void setUrl(URI url) {
067    this.url = url;
068  }
069
070  @Nullable
071  public String getDescription() {
072    return description;
073  }
074
075  public void setDescription(String description) {
076    this.description = description;
077  }
078
079  @Size(min = 3)
080  public String getCreatedBy() {
081    return createdBy;
082  }
083
084  public void setCreatedBy(String createdBy) {
085    this.createdBy = createdBy;
086  }
087
088  @Size(min = 3)
089  public String getModifiedBy() {
090    return modifiedBy;
091  }
092
093  public void setModifiedBy(String modifiedBy) {
094    this.modifiedBy = modifiedBy;
095  }
096
097  @Null(groups = PrePersist.class)
098  @NotNull(groups = PostPersist.class)
099  public Date getCreated() {
100    return created;
101  }
102
103  public void setCreated(Date created) {
104    this.created = created;
105  }
106
107  @Null(groups = PrePersist.class)
108  @NotNull(groups = PostPersist.class)
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 List<MachineTag> getMachineTags() {
119    return machineTags;
120  }
121
122  @Override
123  public void setMachineTags(List<MachineTag> machineTags) {
124    this.machineTags = machineTags;
125  }
126
127  @Override
128  public void addMachineTag(MachineTag machineTag) {
129    machineTags.add(machineTag);
130  }
131
132  @Override
133  public int hashCode() {
134    return Objects.hashCode(key, type, url, description, createdBy, modifiedBy, created, modified, machineTags);
135  }
136
137  @Override
138  public boolean equals(Object object) {
139    if (object instanceof Endpoint) {
140      Endpoint that = (Endpoint) object;
141      return Objects.equal(this.key, that.key)
142        && Objects.equal(this.type, that.type)
143        && Objects.equal(this.url, that.url)
144        && Objects.equal(this.description, that.description)
145        && Objects.equal(this.createdBy, that.createdBy)
146        && Objects.equal(this.modifiedBy, that.modifiedBy)
147        && Objects.equal(this.created, that.created)
148        && Objects.equal(this.modified, that.modified)
149        && Objects.equal(this.machineTags, that.machineTags);
150    }
151    return false;
152  }
153
154  @Override
155  public String toString() {
156    return Objects.toStringHelper(this)
157      .add("key", key)
158      .add("type", type)
159      .add("url", url)
160      .add("description", description)
161      .add("createdBy", createdBy)
162      .add("modifiedBy", modifiedBy)
163      .add("created", created)
164      .add("modified", modified)
165      .add("machineTags", machineTags)
166      .toString();
167  }
168
169  /**
170   * Does not include server controlled values, or nested properties.
171   */
172  @Override
173  public boolean lenientEquals(Endpoint other) {
174    if (this == other) {
175      return true;
176    } else {
177      return Objects.equal(this.type, other.type)
178        && Objects.equal(this.url, other.url)
179        && Objects.equal(this.description, other.description);
180    }
181  }
182
183}