Commit 55b1a1c4 authored by Matija Obreza's avatar Matija Obreza

KPI grouping for `null` values

- JPA requires explicit left join
- executionGroup.link introduced
parent 1130bbf5
......@@ -31,6 +31,7 @@ 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;
......@@ -93,20 +94,23 @@ public class Execution extends AuditedVersionedModel implements SelfCleaning {
* collections
*/
@Size(min = 1, max = 50)
@Pattern(regexp = "[a-z]([a-zA-Z0-9_]+)")
@Pattern(regexp = "[a-z]([a-zA-Z0-9_\\.]+)")
@Column(length = 50)
private String link;
/** This is the property of {@link #parameter} we're observing */
@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 = "execution_group", joinColumns = @JoinColumn(name = "executionId"))
private List<ExecutionGroup> groups = new ArrayList<ExecutionGroup>();
......@@ -205,14 +209,18 @@ public class Execution extends AuditedVersionedModel implements SelfCleaning {
sb.append("select ");
for (ExecutionGroup group : groups) {
sb.append(group.toJpa());
if (group.getAlias() != null) {
sb.append(" as ").append(group.toName());
{
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(group.toName());
}
sb.append(", ");
}
sb.append(", ");
}
switch (type) {
case SUM:
sb.append("sum(").append(link == null ? aliasParameter : aliasDereferenced).append(".").append(property).append(")");
......@@ -261,6 +269,20 @@ public class Execution extends AuditedVersionedModel implements SelfCleaning {
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 ");
......@@ -277,13 +299,13 @@ public class Execution extends AuditedVersionedModel implements SelfCleaning {
if (!groups.isEmpty()) {
sb.append(" group by ");
int groupCounter = 0;
int execGroupCounter = 0;
for (ExecutionGroup group : groups) {
groupCounter++;
if (groupCounter > 1) {
execGroupCounter++;
if (execGroupCounter > 1) {
sb.append(", ");
}
sb.append(group.toJpa());
sb.append(group.toJpa(group.getLink() == null ? null : "EG" + execGroupCounter));
}
}
......
......@@ -53,9 +53,14 @@ public class ExecutionGroup implements SelfCleaning, Serializable {
LENGTH
}
@Size(max = 100)
@Pattern(regexp = "(PA|PC|ED\\d+)\\.[a-z]([a-zA-Z0-9_\\.]+)") // PA.xxx or PC.zxx or ED1.fff
@Column(length = 100)
private String link;
@NotNull
@Size(max = 100)
@Pattern(regexp = "[_a-z][a-zA-Z0-9_\\.\\(\\)]*")
@Pattern(regexp = "[a-zA-Z0-9_\\.]*")
@Column(length = 100, nullable = false)
private String field;
......@@ -76,6 +81,24 @@ public class ExecutionGroup implements SelfCleaning, Serializable {
trimStringsToNull();
}
/**
* Gets the link.
*
* @return the link
*/
public String getLink() {
return link;
}
/**
* Sets the link.
*
* @param link the new link
*/
public void setLink(String link) {
this.link = link;
}
/**
* Gets the field.
*
......@@ -179,11 +202,14 @@ public class ExecutionGroup implements SelfCleaning, Serializable {
*
* @return JPA fragment
*/
public String toJpa() {
public String toJpa(String alias) {
StringBuffer sb = new StringBuffer();
if (op != null) {
sb.append(op.toString()).append("(");
}
if (alias != null) {
sb.append(alias).append(".");
}
sb.append(field);
if (op != null) {
sb.append(")");
......
......@@ -4602,3 +4602,16 @@ databaseChangeLog:
- column:
name: alias
type: varchar(100)
- changeSet:
id: 1545160286000-2
author: matijaobreza
comment: Add `link` to KPI ExecutionGroup
changes:
- addColumn:
tableName: execution_group
columns:
- column:
name: link
type: varchar(100)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment