AccessionAlias.java
/*
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.model.genesys;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.custom.elasticsearch.SearchField;
import com.fasterxml.jackson.annotation.JsonBackReference;
/**
* Accession "alias"
*/
@Entity
//@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "accessionId", "aliasType", "name" }) })
@Table(name = "accession_alias")
@Audited
@Getter
@Setter
@NoArgsConstructor
public class AccessionAlias extends BasicModel implements AccessionRelated, SelfCleaning {
private static final long serialVersionUID = 4990299133164025782L;
private static final Pattern HAS_WORD_CHARACTER = Pattern.compile("\\w", Pattern.MULTILINE);
private static Pattern USED_BY_PATTERN = Pattern.compile("^((\\p{Alnum}+):(.+))?$");
public static enum AliasType {
ACCENAME(0), DONORNUMB(1), BREDNUMB(2), DATABASEID(3), LOCALNAME(4), OTHERNUMB(5), COLLNUMB(6);
private int id;
private AliasType(final int id) {
this.id = id;
}
public static AliasType getType(final Integer id) {
if (id == null) {
return null;
}
for (final AliasType aliasType : AliasType.values()) {
if (id.equals(aliasType.getId())) {
return aliasType;
}
}
throw new IllegalArgumentException("No matching type for id " + id);
}
public int getId() {
return id;
}
}
@Version
private Long version;
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "accessionId", nullable = false, updatable = false)
@JsonBackReference
private AccessionId accession;
@Size(max = 150)
@Column(name = "name", length = 150)
@SearchField
private String name;
@Column
private AliasType aliasType;
@Size(max = 2)
@Column(length = 2)
private String lang;
@javax.validation.constraints.Pattern(regexp = "\\p{Alnum}+")
@Size(max = 64)
@Column(length = 64)
private String usedBy;
public AccessionAlias(AliasType aliasType, String name) {
this.aliasType = aliasType;
this.usedBy = null;
if (name != null) {
if (aliasType == AliasType.OTHERNUMB) {
Matcher m = USED_BY_PATTERN.matcher(name);
if (m.matches()) {
this.usedBy = StringUtils.trimToNull(m.group(2));
this.name = StringUtils.trimToNull(m.group(3));
} else {
if (name.startsWith(":")) {
name = StringUtils.trimToNull(name.substring(1));
}
this.name = name;
}
} else {
this.name = name;
}
} else {
this.name = null;
}
}
@PrePersist
@PreUpdate
private void prePersist() {
trimStringsToNull();
}
@Transient
public boolean isEmpty() {
return StringUtils.isBlank(name) || ! HAS_WORD_CHARACTER.matcher(name).find();
}
@Override
public String toString() {
return "name=" + name + " usedBy=" + usedBy + " type=" + aliasType;
}
public boolean equalTo(AccessionAlias other) {
if (aliasType != null && other.aliasType != null) {
if (aliasType != other.aliasType)
return false;
} else {
return false;
}
if (StringUtils.compare(this.name, other.name) != 0) {
return false;
}
if (StringUtils.compare(this.usedBy, other.usedBy) != 0) {
return false;
}
return true;
}
@Override
public boolean canEqual(Object other) {
return other instanceof AccessionAlias;
}
}