Commit 127228ae authored by Matija Obreza's avatar Matija Obreza
Browse files

KPI Model

parent 72f51ffc
package org.genesys2.server.model.kpi;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
@Entity
public class BooleanListDimension extends FixedListDimension<Boolean> {
@Column(name = "listvalue", nullable = false)
@ElementCollection
@CollectionTable(name = "kpidimensionbool", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<Boolean> values;
public void setValues(Set<Boolean> values) {
this.values = values;
}
@Override
public Set<Boolean> getValues() {
return this.values;
}
}
package org.genesys2.server.model.kpi;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.genesys2.server.model.VersionedAuditedModel;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "className", discriminatorType = DiscriminatorType.STRING, length = 100)
@Table(name = "kpidimension")
public abstract class Dimension<T> extends VersionedAuditedModel {
@Column(length = 100, nullable = false)
private String name;
@Column(length = 100, nullable = false)
private String title;
final public String getName() {
return title;
}
final public void setName(String name) {
this.title = name;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
abstract public Set<T> getValues();
}
package org.genesys2.server.model.kpi;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel;
@Entity
@Table(name="kpidimensionkey", uniqueConstraints = { @UniqueConstraint(name = "UQ_dimensionkey", columnNames = { "name", "val" }) })
public class DimensionKey extends BusinessModel {
@Column(length = 100, nullable = false)
private String name;
@Column(name="val", length = 100, nullable = false)
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "{key=" + name + ", value=" + value + "}";
}
}
package org.genesys2.server.model.kpi;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.genesys2.server.model.VersionedAuditedModel;
@Entity
@Table(name = "kpiexecution")
public class Execution extends VersionedAuditedModel {
@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "parameterId")
private KPIParameter parameter;
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "executionId")
private List<ExecutionDimension> dimensions = new ArrayList<ExecutionDimension>();
public void setParameter(KPIParameter parameter) {
this.parameter = parameter;
}
public KPIParameter getParameter() {
return parameter;
}
public void addDimension(Dimension<?> dimension, String link, String field) {
// what do we do?
ExecutionDimension ped = new ExecutionDimension();
ped.setDimension(dimension);
ped.setLink(link);
ped.setField(field);
dimensions.add(ped);
}
public String query() {
StringBuffer sb = new StringBuffer(), where = new StringBuffer();
String alias = "a";
sb.append("select count(distinct ");
sb.append(alias);
sb.append(") from ");
sb.append(parameter.getEntity());
sb.append(" ").append(alias);
int pedC = 0;
for (ExecutionDimension ped : dimensions) {
pedC++;
if (ped.getLink() != null) {
sb.append(" inner join ");
sb.append(alias).append(".");
sb.append(ped.getLink());
sb.append(" _ped").append(pedC).append(" ");
}
if (pedC > 1)
where.append(" and ");
if (ped.getLink() == null) {
where.append("( ").append(alias).append(".").append(ped.getField()).append(" = ?").append(pedC).append(" )");
} else {
where.append("( _ped").append(pedC).append(".").append(ped.getField()).append(" = ?").append(pedC).append(" )");
}
}
if (where.length() > 0 || parameter.getCondition() != null) {
sb.append(" where ");
if (parameter.getCondition() != null) {
sb.append(parameter.getCondition());
sb.append(" and ");
}
sb.append(where);
}
return sb.toString();
}
public Dimension<?> getDimension(int depth) {
if (depth >= dimensions.size())
return null;
return dimensions.get(depth).getDimension();
}
}
package org.genesys2.server.model.kpi;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
@Entity
@Table(name = "kpiexecutiondimension")
class ExecutionDimension extends BusinessModel {
@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "dimensionId")
private Dimension<?> dimension;
@Column(length = 100, nullable = true)
private String link;
@Column(length = 100, nullable = false)
private String field;
public void setDimension(Dimension<?> dimension) {
this.dimension = dimension;
}
public void setField(String field) {
this.field = field;
}
public String getField() {
return field;
}
public Dimension<?> getDimension() {
return dimension;
}
public String getLink() {
return this.link;
}
public void setLink(String link) {
this.link = link;
}
}
\ No newline at end of file
package org.genesys2.server.model.kpi;
import java.util.Set;
public abstract class FixedListDimension<T> extends Dimension<T> {
public abstract void setValues(Set<T> list);
}
package org.genesys2.server.model.kpi;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class JpaDimension extends Dimension<Object> {
@Column(length = 100)
private String entity;
@Column(length = 50)
private String field;
@Override
public Set<Object> getValues() {
throw new RuntimeException("JpaDimensions must be evaluated in KPIService");
}
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
package org.genesys2.server.model.kpi;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.VersionedAuditedModel;
@Entity
@Table(name = "kpiparameter", uniqueConstraints = { @UniqueConstraint(name = "UQ_kpiparameter_name", columnNames = { "name" }) })
public class KPIParameter extends VersionedAuditedModel {
@Column(length = 100, nullable = false)
private String name;
@Column(length = 100, nullable = false)
private String title;
@Column(length = 100, nullable = false)
private String entity;
@Column(name = "`condition`", length = 100)
private String condition;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
}
package org.genesys2.server.model.kpi;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
@Entity
public class NumericListDimension extends FixedListDimension<Number> {
@Column(name = "listvalue", nullable = false, columnDefinition="double")
@ElementCollection
@CollectionTable(name = "kpidimensionnumeric", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<Number> values;
public void setValues(Set<Number> values) {
this.values = values;
}
@Override
public Set<Number> getValues() {
return this.values;
}
}
package org.genesys2.server.model.kpi;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
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 javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.genesys2.server.model.BusinessModel;
@Entity
@Table(name = "kpiobservation")
public class Observation extends BusinessModel {
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp = new Date();
@Column(name = "`value`")
private long value;
@ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "parameterId")
private KPIParameter parameter;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionId"))
private Set<DimensionKey> dimensions = new HashSet<DimensionKey>();
@Column
private int dimensionCount;
@PrePersist
void prePersist() {
this.dimensionCount = this.dimensions == null ? 0 : this.dimensions.size();
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
public KPIParameter getParameter() {
return parameter;
}
public void setParameter(KPIParameter kPIParameter) {
this.parameter = kPIParameter;
}
public Set<DimensionKey> getDimensions() {
return dimensions;
}
public void setDimensions(Set<DimensionKey> dimensions) {
this.dimensions = dimensions;
}
public int getDimensionCount() {
return this.dimensionCount;
}
public void setDimensionCount(int dimensionCount) {
this.dimensionCount = dimensionCount;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
return parameter.getName() + "=" + value + " D=" + dimensions + " T=" + timestamp.getTime();
}
}
package org.genesys2.server.model.kpi;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
@Entity
public class StringListDimension extends FixedListDimension<String> {
@Column(name = "listvalue", nullable = false)
@ElementCollection
@CollectionTable(name = "kpidimensionstring", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<String> values;
public void setValues(Set<String> values) {
this.values = values;
}
@Override
public Set<String> getValues() {
return this.values;
}
}
package org.genesys2.server.service;
public interface KPIService {
void test1();
Object getSingleResult(String paQuery, Object... params);
}
package org.genesys2.server.service.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.service.KPIService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class KPIServiceImpl implements KPIService {
public static final Log LOG = LogFactory.getLog(KPIServiceImpl.class);
@PersistenceContext
private EntityManager entityManager;
@Override
public void test1() {
Query q = entityManager.createQuery("select count(a) from Accession a where a.mlsStatus=true");
Object res = q.getSingleResult();
LOG.info("Result res=" + res);
}
@Override
public Object getSingleResult(String paQuery, Object... params) {
LOG.debug(paQuery);
Query q = entityManager.createQuery(paQuery);
for (int i = 0; i < params.length; i++) {
LOG.debug("\t?" + (i + 1) + " = " + params[i]);
q.setParameter(i + 1, params[i]);
}
Object res = q.getSingleResult();
return res;
}
}
......@@ -82,7 +82,7 @@ public class SpringDataBaseConfig {
entityManager.setJpaVendorAdapter(hibernateJpaVendorAdapter());
entityManager.setJpaProperties(jpaProperties());
entityManager.setPackagesToScan("org.genesys2.server.model.acl", "org.genesys2.server.model.impl", "org.genesys2.server.model.genesys",
"org.genesys2.server.model.oauth");
"org.genesys2.server.model.oauth", "org.genesys2.server.model.kpi");
return entityManager;
}
......
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.genesys2.server.model.kpi.BooleanListDimension;
import org.genesys2.server.model.kpi.Dimension;
import org.genesys2.server.model.kpi.DimensionKey;
import org.genesys2.server.model.kpi.Execution;