Execution.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.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.security.model.AclAwareModel;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Evaluates {@link KPIParameter} by {@link Dimension}s.
 * 
 * @author matijaobreza
 * 
 */
@Entity
@Table(name = "kpiexecution")
@Getter
@Setter
public class Execution extends AuditedVersionedModel implements SelfCleaning, AclAwareModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1102563708369373562L;

	public static enum ExecutionType {
		COUNT, SUM, AVERAGE
	}

	/**
	 * This specifies the "key" under which observations are filed
	 */
	@NotNull
	@Size(min = 1, max = 100)
	@Column(length = 100, unique = true, nullable = false)
	private String name;

	@NotNull
	@Column(nullable = false)
	private ExecutionType type = ExecutionType.COUNT;

	@Size(max = 100)
	@Column(length = 100)
	private String title;

	/**
	 * Markdown description of the execution
	 */
	@Lob
	private String description;

	@NotNull
	@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false)
	@JoinColumn(name = "parameterId")
	private KPIParameter parameter;

	/**
	 * This joins the parameter PA.link in the query and allows us to count properties of
	 * collections
	 */
	@Size(min = 1, max = 50)
	@Pattern(regexp = "[a-z]([a-zA-Z0-9_\\.]+)")
	@Column(length = 50)
	private String link;

	/** 
	 * This is the property we're observing. Usually it is the 
	 * `id` (of `PA`), but can be another property.
	 * 
	 * If {@link #link} is declared, this property must use the full path (e.g. `PC.id`).
	 */
	@NotNull
	@Pattern(regexp = "[a-zA-Z0-9_\\.]+")
	@Size(max = 30)
	@Column(nullable = false, length = 30)
	private String property = "id";

	@Valid
	@OneToMany(orphanRemoval = true, fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
	@JoinColumn(name = "executionId")
	private List<ExecutionDimension> executionDimensions = new ArrayList<ExecutionDimension>();

	@Valid
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "kpiexecutiongroup", joinColumns = @JoinColumn(name = "executionId"))
	private List<ExecutionGroup> groups = new ArrayList<ExecutionGroup>();

	@JsonIgnore
	@OneToMany(mappedBy = "execution", orphanRemoval = true, fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<ExecutionRun> runs = new ArrayList<ExecutionRun>();

	@PrePersist
	@PreUpdate
	private void preupdate() {
		trimStringsToNull();
		if (groups != null) {
			groups.forEach(group -> group.trimStringsToNull());
		}
	}

	/**
	 * Order of dimensions matters!
	 * 
	 * @param dimension
	 * @param link
	 * @param field
	 */
	public void addDimension(Dimension<?> dimension, String link, String field) {
		ExecutionDimension ped = new ExecutionDimension();
		ped.setDimension(dimension);
		ped.setLink(link);
		ped.setField(field);

		executionDimensions.add(ped);
	}

	public String query() {
		StringBuffer sb = new StringBuffer(), where = new StringBuffer();

		final String aliasDereferenced = "PC";
		final String aliasParameter = "PA";

		sb.append("select ");

		{
			int execGroupCounter = 0;
			for (ExecutionGroup group : groups) {
				execGroupCounter++;
				sb.append(group.toJpa(group.getLink() == null ? null : "EG" + execGroupCounter));
				if (group.getAlias() != null) {
					sb.append(" as ").append("EGn").append(execGroupCounter);
				}
				sb.append(", ");
			}
		}


		// If link is provided, then the full path to property must also be provided!
		var propertyToEvaluate = aliasParameter + "." + property;

		if (link != null) {
			propertyToEvaluate = property;
		}

		switch (type) {
		case SUM:
			sb.append("sum(").append(propertyToEvaluate).append(")");
			sb.append(", ");
			sb.append("count(").append(propertyToEvaluate).append(")");
			break;

		case AVERAGE:
			sb.append("avg(").append(propertyToEvaluate).append(")");
			sb.append(", ");
			sb.append("count(").append(propertyToEvaluate).append(")");
			sb.append(", ");
			sb.append("stddev(").append(propertyToEvaluate).append(")");
			break;

		case COUNT:
		default:
			sb.append("count(distinct ").append(propertyToEvaluate).append(")");
		}
		sb.append(" from ");
		sb.append(parameter.getEntity());
		sb.append(" ").append(aliasParameter);

		int execDimCounter = 0;
		for (ExecutionDimension execDim : executionDimensions) {
			execDimCounter++;
			// System.err.println("DIM" + execDimCounter + " " + execDim);
			if (execDim.getLink() != null) {
				sb.append(" inner join ");
				sb.append(aliasParameter).append(".");
				sb.append(execDim.getLink());
				sb.append(" ED").append(execDimCounter).append(" ");
			}

			if (execDimCounter > 1)
				where.append(" and ");

			if (execDim.getLink() == null) {
				where.append("( ").append(aliasParameter).append(".").append(execDim.getField()).append(" = ?").append(execDimCounter).append(
					" )");
			} else {
				where.append("( ED").append(execDimCounter).append(".").append(execDim.getField()).append(" = ?").append(execDimCounter).append(" )");
			}
		}

		if (link != null) {
			// We're joining a collection to count it's property
			sb.append(" inner join ");
			sb.append(aliasParameter).append(".");
			sb.append(link);
			sb.append(" ").append(aliasDereferenced);
		}
		
		{
			// Handle left joined group-bys
			int execGroupCounter = 0;
			for (ExecutionGroup execGroup : groups) {
				execGroupCounter++;
				if (execGroup.getLink() != null) {
					sb.append(" left join ");
					// sb.append(aliasParameter).append("."); // User must provide the base in the link!
					sb.append(execGroup.getLink());
					sb.append(" EG").append(execGroupCounter).append(" ");
				}
			}
		}

		if (where.length() > 0 || parameter.getCondition() != null) {
			sb.append(" where ");
			if (parameter.getCondition() != null) {
				sb.append(aliasParameter).append(".").append(parameter.getCondition());
			}
			if (executionDimensions.size() > 0) {
				if (parameter.getCondition() != null) {
					sb.append(" and ");
				}
				sb.append(where);
			}
		}

		if (!groups.isEmpty()) {
			sb.append(" group by ");
			int execGroupCounter = 0;
			for (ExecutionGroup group : groups) {
				execGroupCounter++;
				if (execGroupCounter > 1) {
					sb.append(", ");
				}
				sb.append(group.toJpa(group.getLink() == null ? null : "EG" + execGroupCounter));
			}
		}

		return sb.toString();
	}
	
	public Dimension<?> getDimension(int depth) {
		if (depth >= executionDimensions.size())
			return null;
		return executionDimensions.get(depth).getDimension();
	}

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