InventoryExtra.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;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.compatibility.SysTableFieldInfo;
import org.gringlobal.component.elastic.ElasticTrigger;
import org.gringlobal.custom.json.CustomTypeIdResolver;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "inventory_extra", uniqueConstraints = {
	@UniqueConstraint(columnNames = { "inventory_id" }),
})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "extra_type", discriminatorType = DiscriminatorType.STRING, length = 30)
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "_class")
@JsonTypeIdResolver(InventoryExtra.InventoryExtraTypeIdResolver.class)
@Getter
@Setter
public abstract class InventoryExtra extends CooperatorOwnedModel implements ElasticTrigger, Copyable<InventoryExtra> {

	private static final long serialVersionUID = 984030822617251221L;

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

	@OneToOne(fetch = FetchType.LAZY, cascade = {}, optional = false)
	@JoinColumn(name = "inventory_id", updatable = false)
	@LookupDisplay
	private Inventory inventory;
 
	@Column(name = "extra_type", insertable = false, updatable = false, nullable = false)
	@CodeValueField(value = CommunityCodeValues.INVENTORY_EXTRA_TYPE)
	@SysTableFieldInfo(readonly = true)
	private String extraType;

	@Override
	public Object[] reindexedEntities() {
		return ArrayUtils.toArray(inventory);
	}

	public static class InventoryExtraTypeIdResolver extends CustomTypeIdResolver implements TypeIdResolver {
		@Override
		protected Map<String, Class<?>> initClassesMap() {
			Map<String, Class<?>> classes = new HashMap<>();
			classes.put(SeedInventoryExtra.class.getSimpleName(), SeedInventoryExtra.class);
			classes.put(TissueCultureExtra.class.getSimpleName(), TissueCultureExtra.class);
			return classes;
		}
	}
}