Dimension.java

/**
 * Copyright 2014 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.genesys.server.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.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 lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
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 com.fasterxml.jackson.annotation.JsonView;

/**
 * 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 = "kpidimension")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_fullClass")
@Getter
@Setter
public abstract class Dimension<T> extends AuditedVersionedModel implements SelfCleaning, Copyable<Dimension> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1672379271657218936L;
	
	@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;
	}
}