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