LocationData.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 static org.gringlobal.model.community.CommunityCodeValues.CODE_VALUE_LENGTH;

import javax.persistence.Basic;
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.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
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.model.CooperatorOwnedModel;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * LocationData allows for attaching user-defined attributes to each {#link Location}.
 */
@Entity
@Table(name = "location_data")
@Getter
@Setter
@NoArgsConstructor
public class LocationData extends CooperatorOwnedModel implements SelfCleaning, Copyable<LocationData> {
	private static final long serialVersionUID = 5798374432657688903L;

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

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "location_id", nullable = false)
	@NotNull
	private Location location;

	@CodeValueField(CommunityCodeValues.LOCATION_PROPERTY)
	@NotNull
	@Column(name = "location_property_code", nullable = false, length = CODE_VALUE_LENGTH)
	private String locationPropertyCode;

	@NotNull
	@Column(name = "value", nullable = false)
	private String value;

	@CodeValueField(CommunityCodeValues.LOCATION_UNIT_CODE)
	@Column(name = "unit_code", length = CODE_VALUE_LENGTH)
	private String unitCode;

	@Basic
	@Column
	@Lob
	private String note;

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

	@Override
	public void trimStringsToNull() {
		SelfCleaning.super.trimStringsToNull();
	}

	@Override
	public void lazyLoad() {
		super.lazyLoad();

		lazyLoad(location);
	}

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