AccessionCollect.java

/*
 * Copyright 2019 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.Set;
import java.util.stream.Collectors;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
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 org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonBackReference;

/**
 * Collecting data
 */
@Entity
@Table(name = "accession_collect")
@Audited
@Getter
@Setter
public class AccessionCollect extends BasicModel implements AccessionRelated, SelfCleaning {

	private static final long serialVersionUID = 6848317825287346724L;

	@Version
	private long version = 0;

	@OneToOne(mappedBy = "coll", optional = false, fetch = FetchType.LAZY, cascade = {})
	@JsonBackReference
	private AccessionId accession;

	@Size(max = 8)
	@Column(name = "collDate", length = 8)
	private String collDate;

	@Size(max = 64)
	@Column(name = "collNumb", length = 64)
	private String collNumb;

	@Size(max = 128)
	@Column(name = "collMissId", length = 128)
	private String collMissId;

	@Column(name = "collCode", nullable = false, length = 128)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_collect_code", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
	@Field(type = FieldType.Keyword)
	private Set<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") @Size(max = 128) String> collCode;

	@Column(name = "collName", nullable = false, length = 250)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_collect_name", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
	private Set<@Size(max = 250) String> collName;

	@Column(name = "collInstAddress", nullable = false, length = 128)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_collect_instaddr", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
	private Set<@Size(max = 128) String> collInstAddress;

	@Column(name = "collSite")
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String collSite;

	@Column
	private Integer collSrc;

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

		// remove blank collCode
		if (collCode != null && !collCode.isEmpty()) {
			Set<String> notBlank = collCode.stream().map(str -> StringUtils.trimToNull(str)).filter(str -> str != null).collect(Collectors.toSet());
			collCode.clear();
			collCode.addAll(notBlank);
		}
		// remove blank collName
		if (collName != null && !collName.isEmpty()) {
			Set<String> notBlank = collName.stream().map(str -> StringUtils.trimToNull(str)).filter(str -> str != null).map(str -> StringUtils.abbreviate(str, "...", 250)).collect(Collectors.toSet());
			collName.clear();
			collName.addAll(notBlank);
		}
	}

	@Transient
	public boolean isEmpty() {
		if (StringUtils.isNotBlank(collDate)) {
			return false;
		}

		if (StringUtils.isNotBlank(collNumb)) {
			return false;
		}

		if (StringUtils.isNotBlank(collMissId)) {
			return false;
		}

		if (collName != null && collName.stream().filter(StringUtils::isNotBlank).count() > 0) {
			return false;
		}

		if (collCode != null && collCode.stream().filter(StringUtils::isNotBlank).count() > 0) {
			return false;
		}

		if (collInstAddress != null && collInstAddress.stream().filter(StringUtils::isNotBlank).count() > 0) {
			return false;
		}

		if (StringUtils.isNotBlank(collSite)) {
			return false;
		}

		if (collSrc != null) {
			return false;
		}

		return true;
	}

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