CooperatorOwnedLang.java

/*
 * Copyright 2020 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 lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.EmptyModel;
import org.gringlobal.custom.elasticsearch.SearchField;

import java.util.Optional;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * The Class CooperatorLang.
 *
 * @author Maxym Borodenko
 */
@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
public abstract class CooperatorOwnedLang<L extends CooperatorOwnedLang<L, E>, E extends EmptyModel> extends CooperatorOwnedModel {

	private static final long serialVersionUID = 4811007750372889988L;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(updatable = false, nullable = false)
	protected E entity;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "sys_lang_id", nullable = false, updatable = false)
	protected SysLang sysLang;

	@NotNull
	@Size(min = 1, max = 500)
	@Basic
	@Column(nullable = false, length = 500)
	@SearchField
	protected String title;

	@Basic
	@Column
	@Lob
	@SearchField
	protected String description;

	public CooperatorOwnedLang(String title, String description, SysLang sysLang) {
		this.title = title;
		this.description = description;
		this.sysLang = sysLang;
	}

	public CooperatorOwnedLang(Optional<L> defaultTranslation, SysLang sysLang) {
		this.sysLang = sysLang;
		if (defaultTranslation.isPresent()) {
			this.title = defaultTranslation.get().title;
			this.description = defaultTranslation.get().description;
		}
	}

	@Override
	public void lazyLoad() {
		lazyLoad(sysLang);
		lazyLoad(entity);
		super.lazyLoad();
	}
}