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.collections; 017 018import org.gbif.api.model.registry.LenientEquals; 019import org.gbif.api.model.registry.PostPersist; 020import org.gbif.api.model.registry.PrePersist; 021 022import java.io.Serializable; 023import java.util.Date; 024import java.util.Objects; 025import java.util.StringJoiner; 026import java.util.UUID; 027 028import javax.annotation.Nullable; 029import javax.validation.constraints.NotNull; 030import javax.validation.constraints.Null; 031import javax.validation.constraints.Size; 032 033/** Models the mapping of a GRSciColl institution or collection to an occurrence record. */ 034public class OccurrenceMapping implements Serializable, LenientEquals<OccurrenceMapping> { 035 036 private Integer key; 037 private String code; 038 /* 039 Only used for collections when they need to specify the institution code. 040 */ 041 private String parentCode; 042 private String identifier; 043 private UUID datasetKey; 044 private String createdBy; 045 private Date created; 046 047 public OccurrenceMapping() {} 048 049 public OccurrenceMapping(String code, String identifier, UUID datasetKey) { 050 this(code, identifier, datasetKey, null); 051 } 052 053 public OccurrenceMapping(String code, String identifier, UUID datasetKey, String parentCode) { 054 this.code = code; 055 this.identifier = identifier; 056 this.datasetKey = datasetKey; 057 this.parentCode = parentCode; 058 } 059 060 /** Unique identifier, assigned by the persistence store. */ 061 @Null(groups = {PrePersist.class}) 062 @NotNull(groups = {PostPersist.class}) 063 public Integer getKey() { 064 return key; 065 } 066 067 public void setKey(Integer key) { 068 this.key = key; 069 } 070 071 @Nullable 072 @Size(min = 1) 073 public String getCode() { 074 return code; 075 } 076 077 public void setCode(String code) { 078 this.code = code; 079 } 080 081 @Nullable 082 @Size(min = 1) 083 public String getParentCode() { 084 return parentCode; 085 } 086 087 public void setParentCode(String parentCode) { 088 this.parentCode = parentCode; 089 } 090 091 @Nullable 092 @Size(min = 1) 093 public String getIdentifier() { 094 return identifier; 095 } 096 097 public void setIdentifier(String identifier) { 098 this.identifier = identifier; 099 } 100 101 @NotNull 102 public UUID getDatasetKey() { 103 return datasetKey; 104 } 105 106 public void setDatasetKey(UUID datasetKey) { 107 this.datasetKey = datasetKey; 108 } 109 110 public String getCreatedBy() { 111 return createdBy; 112 } 113 114 public void setCreatedBy(String createdBy) { 115 this.createdBy = createdBy; 116 } 117 118 public Date getCreated() { 119 return created; 120 } 121 122 public void setCreated(Date created) { 123 this.created = created; 124 } 125 126 @Override 127 public boolean equals(Object o) { 128 if (this == o) { 129 return true; 130 } 131 if (o == null || getClass() != o.getClass()) { 132 return false; 133 } 134 OccurrenceMapping that = (OccurrenceMapping) o; 135 return Objects.equals(key, that.key) 136 && Objects.equals(code, that.code) 137 && Objects.equals(parentCode, that.parentCode) 138 && Objects.equals(identifier, that.identifier) 139 && Objects.equals(datasetKey, that.datasetKey) 140 && Objects.equals(createdBy, that.createdBy) 141 && Objects.equals(created, that.created); 142 } 143 144 @Override 145 public int hashCode() { 146 return Objects.hash(key, code, parentCode, identifier, datasetKey, createdBy, created); 147 } 148 149 @Override 150 public String toString() { 151 return new StringJoiner(", ", OccurrenceMapping.class.getSimpleName() + "[", "]") 152 .add("key=" + key) 153 .add("code='" + code + "'") 154 .add("parentCode='" + parentCode + "'") 155 .add("identifier='" + identifier + "'") 156 .add("datasetKey=" + datasetKey) 157 .add("createdBy='" + createdBy + "'") 158 .add("created=" + created) 159 .toString(); 160 } 161 162 @Override 163 public boolean lenientEquals(OccurrenceMapping other) { 164 if (this == other) { 165 return true; 166 } 167 return Objects.equals(code, other.code) 168 && Objects.equals(parentCode, other.parentCode) 169 && Objects.equals(identifier, other.identifier) 170 && Objects.equals(datasetKey, other.datasetKey); 171 } 172}