ExecutionDimension.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 javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
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.BasicModel;
import org.genesys.blocks.model.SelfCleaning;
import org.gringlobal.custom.json.IgnoreEntityRefDeserializer;

@Entity
@Table(name = "kpi_execution_dimension")
@Getter
@Setter
public class ExecutionDimension extends BasicModel implements SelfCleaning {

	private static final long serialVersionUID = -8928972295709221085L;

	@NotNull
	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "dimensionId")
	@IgnoreEntityRefDeserializer
	private Dimension<?> dimension;

	/** Linked entity from the Parameter to inner join directly (e.g. accession.institute) **/
	@Size(max=100)
	@Pattern(regexp = "[a-z][a-zA-Z0-9_\\.\\(\\)]*")
	@Column(length = 100, nullable = true)
	private String link;

	/** Field in the Parameter (e.g. accession.instCode) or Linked entity ({@link #link} (e.g. accession.institute.code) **/
	@Size(max=100)
	@Pattern(regexp = "[_a-z][a-zA-Z0-9_\\.\\(\\)]*")
	@Column(length = 100, nullable = false)
	private String field;

	/** Alias: how we store the key name in the observation **/
	@Size(max=100)
	@Pattern(regexp = "[_a-z][a-zA-Z0-9_\\.]*")
	@Column(length = 100)
	private String alias;

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

	@Override
	public String toString() {
		return "id=" + getId() + " link=" + link + " field=" + field + " alias=" + alias + " dim=" + dimension.getName();
	}

	public String toName() {
		return alias == null ? ((link == null ? "" : link + ".") + field) : alias;
	}

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