AccessionRemark.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.Lob;
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 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.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonBackReference;

/**
 * Accession "alias"
 */
@Entity
@Table(name = "accession_remark")
@Audited
@Getter
@Setter
@NoArgsConstructor
public class AccessionRemark extends BasicModel implements AccessionRelated, SelfCleaning {

	/**
	 *
	 */
	private static final long serialVersionUID = 815335916194920054L;
	private static Pattern REMARKFIELD_PATTERN = Pattern.compile("^((\\p{Alnum}+):(.+))?$");

	@Version
	private long version = 0;

	@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "accessionId", nullable = false, updatable = false)
	@JsonBackReference
	private AccessionId accession;

	@Lob
	@Column(name = "remark", nullable = false)
	@Type(type = "org.hibernate.type.TextType")
	private String remark;

	@Column(length = 30)
	private String fieldName;

	public AccessionRemark(String str) {
		Matcher m = REMARKFIELD_PATTERN.matcher(str);
		if (m.matches()) {
			this.fieldName = StringUtils.trimToNull(m.group(2));
			this.remark = StringUtils.trimToNull(m.group(3));
		} else {
			if (str.startsWith(":")) {
				str = str.substring(1);
			}
			this.remark = StringUtils.trimToNull(str);
		}
	}

	@PrePersist
	@PreUpdate
	private void prePersist() {
		trimStringsToNull();
	}

	public boolean equalTo(AccessionRemark other) {
		if (other == null) {
			return false;
		}
		if (StringUtils.compare(fieldName, other.fieldName) != 0) {
			return false;
		}
		if (StringUtils.compare(remark, other.remark) != 0) {
			return false;
		}

		return true;
	}

	@Override
	public String toString() {
		return "" + this.fieldName + ":" + this.remark;
	}

	@Transient
	public boolean isEmpty() {
		return StringUtils.isBlank(this.remark);
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof AccessionRemark;
	}
}