Geography.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 java.time.Instant;

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

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.util.EntityIdSerializer;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
import org.springframework.data.elasticsearch.annotations.Document;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

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 = "geography")
@Table(name = "geography")
@JsonIdentityInfo(scope = Geography.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
@LookupDisplay(template = "trim(concat(countryCode, ' ', coalesce(adm1, ''), ' ', coalesce(adm2, '')))")
public class Geography extends CooperatorOwnedModel implements Copyable<Geography> {

	private static final long serialVersionUID = 7938841830458125715L;

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

	@Basic
	@Size(max = ABBR_LENGTH)
	@Column(name = "adm1_abbrev", length = ABBR_LENGTH)
	private String adm1Abbrev;

	@Basic
	@Size(max = CODE_VALUE_LENGTH)
	@Column(name = "adm1_type_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("GEOGRAPHY_ADMIN1_TYPE")
	private String adm1TypeCode;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	private String adm2;

	@Basic
	@Size(max = ABBR_LENGTH)
	@Column(name = "adm2_abbrev", length = ABBR_LENGTH)
	private String adm2Abbrev;

	@Basic
	@Size(max = CODE_VALUE_LENGTH)
	@Column(name = "adm2_type_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("GEOGRAPHY_ADMIN2_TYPE")
	private String adm2TypeCode;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	private String adm3;

	@Basic
	@Size(max = ABBR_LENGTH)
	@Column(name = "adm3_abbrev", length = ABBR_LENGTH)
	private String adm3Abbrev;

	@Basic
	@Size(max = 20)
	@Column(name = "adm3_type_code", length = 20)
	private String adm3TypeCode;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	private String adm4;

	@Basic
	@Size(max = ABBR_LENGTH)
	@Column(name = "adm4_abbrev", length = ABBR_LENGTH)
	private String adm4Abbrev;

	@Basic
	@Size(max = 20)
	@Column(name = "adm4_type_code", length = 20)
	private String adm4TypeCode;

	@Basic
	@Column(name = "changed_date")
	private Instant changedDate;

	@Basic
	@Size(min = 1, max = CODE_VALUE_LENGTH)
	@NotNull
	@Column(name = "country_code", nullable = false, length = CODE_VALUE_LENGTH)
	@CodeValueField(value = CommunityCodeValues.GEOGRAPHY_COUNTRY_CODE, indexed = true)
	private String countryCode;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "current_geography_id")
	@JsonSerialize(using = EntityIdSerializer.class)
	@Field(type = FieldType.Long)
	private Geography currentGeography;

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

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_valid", nullable = false, length = 1)
	private String isValid;

	@Basic
	@Column
	@Lob
	private String note;

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


	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(this.currentGeography);
	}

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