Observation.java

/*
 * Copyright 2018 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.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.BasicModel;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * Holds results of {@link Execution} run.
 */
@Entity
@Table(name = "kpiobservation")
@Getter
@Setter
@NoArgsConstructor
public class Observation extends BasicModel {

	private static final long serialVersionUID = -7621192124417368090L;

	@JsonIgnore
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "executionRunId")
	private ExecutionRun executionRun;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionKeyId"))
	@JsonIgnoreProperties({ "id" })
	private List<DimensionKey> dimensions = new ArrayList<DimensionKey>();

	@JsonIgnore
	@Column
	private int dimensionCount;

	@Column(name = "`value`")
	private double value;

	@Column(nullable = true, name="stddev1")
	private Double stdDev;

	/** Contains {@code COUNT(..)} if value was calculated with AVERAGE  */
	@Column(nullable = true, name="count1")
	private Long count;

	public Observation(List<DimensionKey> dimensions, double value) {
		this.dimensions.addAll(dimensions);
		this.dimensionCount = dimensions.size();
		this.value = value;
	}

	@PrePersist
	void prePersist() {
		this.dimensionCount = this.dimensions == null ? 0 : this.dimensions.size();
	}
	
	public void setDimensions(List<DimensionKey> dimensions) {
		this.dimensions = dimensions;
		this.dimensionCount = dimensions == null ? 0 : dimensions.size();
	}

	@Override
	public String toString() {
		return "value=" + value + (stdDev == null ? "" : " stdDev=" + stdDev) + " D=" + dimensions;
	}

	public boolean hasDimensionKeys(Collection<DimensionKey> otherDks) {
		if (getDimensions().containsAll(otherDks) && otherDks.containsAll(getDimensions())) {
			return true;
		}
		return false;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof Observation;
	}
}