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.HashMap;
import java.util.Map;
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.JsonView;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
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.custom.json.CustomTypeIdResolver;
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.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "_class")
@JsonTypeIdResolver(Dimension.DimensionTypeIdResolver.class)
@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();

	public static class DimensionTypeIdResolver extends CustomTypeIdResolver implements TypeIdResolver {
		@Override
		protected Map<String, Class<?>> initClassesMap() {
			Map<String, Class<?>> classes = new HashMap<>();
			classes.put(BooleanDimension.class.getSimpleName(), BooleanDimension.class);
			classes.put(NumericListDimension.class.getSimpleName(), NumericListDimension.class);
			classes.put(StringListDimension.class.getSimpleName(), StringListDimension.class);
			classes.put(JpaDimension.class.getSimpleName(), JpaDimension.class);
			return classes;
		}
	}

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