Cooperator.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 javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.component.elastic.ElasticLoader;
import org.gringlobal.custom.validation.javax.SimpleString;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

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

/**
 * Auto-generated by:
 * org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 */
@Entity
@Cacheable
@Document(indexName = "cooperator")
@Table(name = "cooperator", uniqueConstraints = { @UniqueConstraint(columnNames = { "last_name", "first_name", "organization", "geography_id", "address_line1" }) })
@JsonIdentityInfo(scope = Cooperator.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
@LookupDisplay(template = "concat(lastName, ', ', firstName, ', ', coalesce(organization, ''))")
public class Cooperator extends CooperatorOwnedModel implements Copyable<Cooperator>, ElasticLoader {

	private static final long serialVersionUID = -1510199507617079570L;

	@Id
	@JsonProperty
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "cooperator_id", columnDefinition = "int") // GG legacy database type
	private Long id;

	@Basic
	@Size(max = 100)
	@Column(name = "address_line1", length = 100)
	@SimpleString
	private String addressLine1;

	@Basic
	@Size(max = 100)
	@Column(name = "address_line2", length = 100)
	@SimpleString
	private String addressLine2;

	@Basic
	@Size(max = 100)
	@Column(name = "address_line3", length = 100)
	@SimpleString
	private String addressLine3;

	@Basic
	@Column(name = "category_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("COOPERATOR_CATEGORY")
	private String categoryCode;

	@Basic
	@Size(max = 100)
	@Column(length = 100)
	@SimpleString
	private String city;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "current_cooperator_id")
	@JsonIdentityInfo(scope = Cooperator.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private Cooperator currentCooperator;

	@Basic
	@Column(name = "discipline_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("COOPERATOR_DISCIPLINE")
	private String disciplineCode;

	@Basic
	@Size(max = 100)
	@Column(length = 100)
	@SimpleString
	private String email;

	@Basic
	@Size(max = 30)
	@Column(length = 30)
	@SimpleString
	private String fax;

	@Basic
	@Size(max = 100)
	@Column(name = "first_name", length = 100)
	@SimpleString
	private String firstName;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "geography_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties(value = { "createdBy", "modifiedBy", "ownedBy" })
	private Geography geography;

	@Basic
	@Size(max = 100)
	@Column(length = 100)
	@SimpleString
	private String job;

	@Basic
	@Size(max = 100)
	@Column(name = "last_name", length = 100)
	@SimpleString
	private String lastName;

	@Basic
	@Column
	@Lob
	private String note;

	@Basic
	@Size(max = 200)
	@Column(length = 200)
	@SimpleString
	private String organization;

	@Basic
	@Size(max = ABBR_LENGTH)
	@Column(name = "organization_abbrev", length = ABBR_LENGTH)
	@SimpleString
	private String organizationAbbrev;

	@Basic
	@Column(name = "organization_region_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("ORGANIZATION_REGION")
	private String organizationRegionCode;

	@Basic
	@Size(max = 100)
	@Column(name = "postal_index", length = 100)
	@SimpleString
	private String postalIndex;

	@Basic
	@Size(max = 30)
	@Column(name = "primary_phone", length = 30)
	@SimpleString
	private String primaryPhone;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_address_line1", length = 100)
	@SimpleString
	private String secondaryAddressLine1;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_address_line2", length = 100)
	@SimpleString
	private String secondaryAddressLine2;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_address_line3", length = 100)
	@SimpleString
	private String secondaryAddressLine3;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_city", length = 100)
	@SimpleString
	private String secondaryCity;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_email", length = 100)
	@SimpleString
	private String secondaryEmail;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "secondary_geography_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties(value = { "createdBy", "modifiedBy", "ownedBy" })
	private Geography secondaryGeography;

	@Basic
	@Size(max = 200)
	@Column(name = "secondary_organization", length = 200)
	@SimpleString
	private String secondaryOrganization;

	@Basic
	@Size(max = 10)
	@Column(name = "secondary_organization_abbrev", length = 10)
	@SimpleString
	private String secondaryOrganizationAbbrev;

	@Basic
	@Size(max = 30)
	@Column(name = "secondary_phone", length = 30)
	@SimpleString
	private String secondaryPhone;

	@Basic
	@Size(max = 100)
	@Column(name = "secondary_postal_index", length = 100)
	@SimpleString
	private String secondaryPostalIndex;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "site_id")
	@JsonIgnoreProperties(value = { "createdBy", "modifiedBy", "ownedBy" })
	private Site site;

	@Basic
	@NotNull
	@Column(name = "status_code", nullable = false, length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.COOPERATOR_STATUS)
	private String statusCode;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "sys_lang_id", nullable = false)
//	@JsonIdentityInfo(scope = SysLang.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
//	@JsonIdentityReference(alwaysAsId = true)
	private SysLang sysLang;

	@Basic
	@Column(length = CODE_VALUE_LENGTH)
	@CodeValueField("COOPERATOR_TITLE")
	private String title;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "web_cooperator_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties(value = { "createdBy", "modifiedBy", "ownedBy" })
	private WebCooperator webCooperator;

	@Basic
	@Pattern(regexp = "^[A-Z]{3}\\d{3,4}$")
	@Column(name = "fao_institute_number", length = 10)
	private String faoInstituteNumber;

	@Basic
	@Size(min=1, max = 10)
	@Column(name = "itpgrfa_pid", length = 10)
	@SimpleString
	private String itpgrfaPid;

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

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(this.currentCooperator);
		lazyLoad(this.geography);
		lazyLoad(this.secondaryGeography);
		lazyLoad(this.site);
		lazyLoad(this.sysLang);
		lazyLoad(this.webCooperator);
	}
	
	@Override
	public void prepareForIndexing() {
		this.lazyLoad();
	}

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