Dimension.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.kpi;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.gringlobal.model.CooperatorOwnedModel;

/**
 * Dimension is a filter applied to the query to select matching records for
 * calculation of a {@link KPIParameter}.
 *
 * <p>
 * <b>Example:</b> See {@link BooleanDimension}.
 * </p>
 *
 * @author mobreza
 *
 * @param <T>
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "className", discriminatorType = DiscriminatorType.STRING, length = 100)
@Table(name = "kpi_dimension")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "_class", visible = true)
@JsonSubTypes({
	@Type(value = BooleanDimension.class, name = "BooleanDimension"),
	@Type(value = JpaDimension.class, name = "JpaDimension"),
	@Type(value = StringListDimension.class, name = "StringListDimension"),
	@Type(value = NumericListDimension.class, name = "NumericListDimension")
})
@Getter
@Setter
public abstract class Dimension<T> extends CooperatorOwnedModel implements SelfCleaning, Copyable<Dimension> {

	private static final long serialVersionUID = -7008711507113928128L;

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

	@NotNull
	@Size(max = 100)
	@Column(length = 100, unique = true, nullable = false)
	private String name;

	@NotNull
	@Size(max = 100)
	@Column(length = 100, unique = true, nullable = false)
	private String title;

	@PrePersist
	@PreUpdate
	private void preupdate() {
		trimStringsToNull();
	}

	@JsonView(JsonViews.Public.class)
	abstract public Set<T> getValues();

	@Override
	public String toString() {
		return "id=" + getId() + " name=" + name + " title=" + title;
	}

	public abstract Class<T> getTargetType();


	@Override
	public Dimension<T> apply(Dimension source) {
		this.setName(source.getName());
		this.setTitle(source.getTitle());
		return this;
	}
}