SourceDescObservation.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.gringlobal.model;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.api.exception.InvalidApiUsageException;

/**
 * Auto-generated by:
 * org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 */
@Entity
@Table(name = "source_desc_observation")
@JsonIdentityInfo(scope = SourceDescObservation.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class SourceDescObservation extends CooperatorOwnedModel implements Copyable<SourceDescObservation> {

	private static final long serialVersionUID = -5553561071290008204L;

	@Id
	@JsonProperty
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "source_desc_observation_id", columnDefinition = "int")
	private Long id;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "accession_source_id", nullable = false)
	private AccessionSource accessionSource;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "source_descriptor_id", nullable = false)
	private SourceDescriptor sourceDescriptor;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "source_descriptor_code_id")
	private SourceDescriptorCode sourceDescriptorCode;

	@Basic
	@Column(name = "numeric_value")
	private Double numericValue;

	@Basic
	@Column(name = "original_value", length = 30)
	private String originalValue;

	@Basic
	@Column(name = "string_value")
	private String stringValue;

	@Basic
	@Column
	@Lob
	private String note;

	@PrePersist
	@PreUpdate
	private void preupdate() {
		assert sourceDescriptor != null;

		if ("Y".equals(sourceDescriptor.getIsCoded())) {
			if (sourceDescriptorCode == null) {
				throw new InvalidApiUsageException("sourceDescriptorCode not specified");
			}
			if (!sourceDescriptor.getId().equals(sourceDescriptorCode.getSourceDescriptor().getId())) {
				throw new InvalidApiUsageException("sourceDescriptor should be equal to SourceDescriptorCode.sourceDescriptor");
			}
		} else {
			if (numericValue == null && stringValue == null) {
				throw new InvalidApiUsageException("Both of numericValue and stringValue not specified");
			}
			if (numericValue != null) {
				if (sourceDescriptor.getNumericMinimum() != null && numericValue < sourceDescriptor.getNumericMinimum())
					throw new InvalidApiUsageException("Out of numeric range");
				if (sourceDescriptor.getNumericMaximum() != null && numericValue > sourceDescriptor.getNumericMaximum())
					throw new InvalidApiUsageException("Out of numeric range");
			}
			if (stringValue != null) {
				if (sourceDescriptor.getMaxLength() != null && stringValue.length() > sourceDescriptor.getMaxLength())
					throw new InvalidApiUsageException("stringValue longer than crop trait maxLength");
			}
		}
	}

	public SourceDescObservation(final Long id) {
		this.id = id;
	}
	
	@Override
	public void lazyLoad() {
		super.lazyLoad();
		if (accessionSource != null) {
			lazyLoad(accessionSource, true);
//			lazyLoad(accessionSource.getAccession()); // included in lazyLoad(accessionSource)
		}
		if (sourceDescriptor != null) {
			lazyLoad(sourceDescriptor);
		}
		if (sourceDescriptorCode != null) {
			lazyLoad(sourceDescriptorCode);
		}
	}

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