Symptom.java

/*
 * Copyright 2022 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.community;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.SelfCleaning;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.custom.validation.javax.SimpleString;
import org.gringlobal.model.CooperatorOwnedModel;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import static org.gringlobal.model.community.CommunityCodeValues.CODE_VALUE_LENGTH;


/**
 * {@code Symptom} captures information about observable symptoms of disease
 * affecting plant material.
 */
@Entity
@Table(name = "symptom", uniqueConstraints = {
	@UniqueConstraint(columnNames = { "name" }),
})
@JsonIdentityInfo(scope = Symptom.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class Symptom extends CooperatorOwnedModel implements SelfCleaning, Copyable<Symptom> {
	private static final long serialVersionUID = -4244733723344510341L;

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

	@CodeValueField(CommunityCodeValues.SYMPTOM_TYPE)
	@NotNull
	@Column(name = "symptom_type_code", nullable = false, length = CODE_VALUE_LENGTH)
	@ApiModelProperty(notes = "Type of symptom. Must be a value in the SYMPTOM_TYPE Code Group.", required = true) 
	private String symptomType;

	@SimpleString
	@NotNull
	@Column(name = "name", nullable = false, length = 100)
	@ApiModelProperty(notes = "Name of the symptom", required = true)
	private String name;

	@Lob
	private String note;

	@JsonIgnore
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "symptom")
	private List<SymptomAttach> attachments;


	@JsonIgnore
	@OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "symptom")
	private List<PathogenSymptomMap> pathogens;

	public Symptom(final Long id) {
		this.id = id;
	}

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		if (pathogens != null) {
			pathogens.size();
		}
	}

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