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 java.util.Date;
016import javax.validation.constraints.NotNull;
017import javax.validation.constraints.Null;
018import javax.validation.constraints.Size;
019
020import com.google.common.base.Objects;
021
022/**
023 * A tag that has a namespace, name and a value. {@code created} and {@code createdBy} are automatically set upon
024 * persisting.
025 */
026// TODO: Document the rules regarding duplicate names and values
027public class MachineTag implements LenientEquals<MachineTag> {
028
029  private Integer key;
030  private String namespace;
031  private String name;
032  private String value;
033  private String createdBy;
034  private Date created;
035
036  public static MachineTag newInstance(String namespace, String name, String value) {
037    return new MachineTag(namespace, name, value);
038  }
039
040  public MachineTag() {
041    // Needed
042  }
043
044  /**
045   * This is the default constructor to create new Machine Tags which takes all user settable properties.
046   */
047  public MachineTag(String namespace, String name, String value) {
048    this.namespace = namespace;
049    this.name = name;
050    this.value = value;
051  }
052
053  @Null(groups = {PrePersist.class})
054  @NotNull(groups = {PostPersist.class})
055  public Integer getKey() {
056    return key;
057  }
058
059  public void setKey(Integer key) {
060    this.key = key;
061  }
062
063  @NotNull
064  @Size(min = 1, max = 255)
065  public String getNamespace() {
066    return namespace;
067  }
068
069  public void setNamespace(String namespace) {
070    this.namespace = namespace;
071  }
072
073  @NotNull
074  @Size(min = 1, max = 255)
075  public String getName() {
076    return name;
077  }
078
079  public void setName(String name) {
080    this.name = name;
081  }
082
083  @NotNull
084  @Size(min = 1, max = 255)
085  public String getValue() {
086    return value;
087  }
088
089  public void setValue(String value) {
090    this.value = value;
091  }
092
093  @Size(min = 3)
094  public String getCreatedBy() {
095    return createdBy;
096  }
097
098  public void setCreatedBy(String createdBy) {
099    this.createdBy = createdBy;
100  }
101
102  @Null(groups = {PrePersist.class})
103  @NotNull(groups = {PostPersist.class})
104  public Date getCreated() {
105    return created;
106  }
107
108  public void setCreated(Date created) {
109    this.created = created;
110  }
111
112  @Override
113  public int hashCode() {
114    return Objects.hashCode(key, namespace, name, value, createdBy, created);
115  }
116
117  @Override
118  public boolean equals(Object object) {
119    if (object instanceof MachineTag) {
120      MachineTag that = (MachineTag) object;
121      return Objects.equal(this.key, that.key)
122        && Objects.equal(this.namespace, that.namespace)
123        && Objects.equal(this.name, that.name)
124        && Objects.equal(this.value, that.value)
125        && Objects.equal(this.createdBy, that.createdBy)
126        && Objects.equal(this.created, that.created);
127    }
128    return false;
129  }
130
131  @Override
132  public String toString() {
133    return Objects.toStringHelper(this)
134      .add("key", key)
135      .add("namespace", namespace)
136      .add("name", name)
137      .add("value", value)
138      .add("createdBy", createdBy)
139      .add("created", created)
140      .toString();
141  }
142
143  /**
144   * This implementation of the {@link #equals(Object)} method does only check <em>business equality</em> and disregards
145   * automatically set and maintained fields like {@code createdBy, key} and possibly others in the future.
146   */
147  @Override
148  public boolean lenientEquals(MachineTag other) {
149    if (this == other) {
150      return true;
151    }
152
153    return Objects.equal(this.namespace, other.namespace)
154      && Objects.equal(this.name, other.name)
155      && Objects.equal(this.value, other.value);
156    // TODO: http://dev.gbif.org/issues/browse/REG-401
157    // && Objects.equal(this.createdBy, other.createdBy);
158  }
159}