ExecutionGroup.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.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
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.SelfCleaning;
/**
* GroupBy for Executions.
*/
@Embeddable
@Getter
@Setter
public class ExecutionGroup implements SelfCleaning, Serializable {
private static final long serialVersionUID = 5558114961129207503L;
/**
* Supported operations.
*/
public enum Operation {
/** The year. */
YEAR,
/** The month. */
MONTH,
/** The day. */
DAY,
/** The length. */
LENGTH
}
@Size(max = 100)
@Pattern(regexp = "^$|(PA|PC|ED\\d+)(\\.[a-z]([a-zA-Z0-9_]+)){1,}") // blank or PA.xxx or PC.zxx or ED1.fff or PA.xxx.yyyZzz
@Column(length = 100)
private String link;
@NotNull
@Size(max = 100)
@Pattern(regexp = "[a-zA-Z0-9_\\.]*(?:\\(\\))?")
@Column(length = 100, nullable = false)
private String field;
@Column(length = 20)
@Enumerated(EnumType.STRING)
private Operation op;
/** The alias. */
@Size(max = 100)
@Pattern(regexp = "[_a-z][a-zA-Z0-9_\\.]+")
@Column(length = 100)
private String alias;
@PrePersist
@PreUpdate
private void preupdate() {
trimStringsToNull();
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((alias == null) ? 0 : alias.hashCode());
result = prime * result + ((field == null) ? 0 : field.hashCode());
result = prime * result + ((op == null) ? 0 : op.hashCode());
return result;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ExecutionGroup other = (ExecutionGroup) obj;
if (alias == null) {
if (other.alias != null)
return false;
} else if (!alias.equals(other.alias))
return false;
if (field == null) {
if (other.field != null)
return false;
} else if (!field.equals(other.field))
return false;
if (op != other.op)
return false;
return true;
}
public String toName() {
return alias == null ? field : alias;
}
/**
* Generate JPA statement for this group
*
* @return JPA fragment
*/
public String toJpa(String alias) {
var sb = new StringBuilder();
if (op != null) {
sb.append(op).append("(");
}
if (alias != null) {
sb.append(alias);
}
if (field != null && ! field.equals(".")) {
if (alias != null) {
sb.append(".");
}
sb.append(field);
}
if (op != null) {
sb.append(")");
}
return sb.toString();
}
@Override
public String toString() {
return toJpa(null);
}
}