Commit bba5dc48 authored by Matija Obreza's avatar Matija Obreza
Browse files

KPI repositories and tests

parent 127228ae
package org.genesys2.server.model.kpi;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class BooleanDimension extends Dimension<Boolean> {
@Column
private int mode = 3;
@Override
public Set<Boolean> getValues() {
Set<Boolean> b = new HashSet<Boolean>();
if (hasTrue()) {
b.add(Boolean.TRUE);
}
if ((mode ^ 2) > 0) {
b.add(Boolean.FALSE);
}
return b;
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public boolean hasTrue() {
return (mode & 1) > 0;
}
public void useTrue(boolean use) {
if (use)
mode |= 1;
else
mode &= ~1;
}
public boolean hasFalse() {
return (mode & 2) > 0;
}
public void useFalse(boolean use) {
if (use)
mode |= 2;
else
mode &= ~2;
}
}
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;
}
}
...@@ -24,11 +24,11 @@ public abstract class Dimension<T> extends VersionedAuditedModel { ...@@ -24,11 +24,11 @@ public abstract class Dimension<T> extends VersionedAuditedModel {
private String title; private String title;
final public String getName() { final public String getName() {
return title; return name;
} }
final public void setName(String name) { final public void setName(String name) {
this.title = name; this.name = name;
} }
public void setTitle(String title) { public void setTitle(String title) {
...@@ -40,4 +40,9 @@ public abstract class Dimension<T> extends VersionedAuditedModel { ...@@ -40,4 +40,9 @@ public abstract class Dimension<T> extends VersionedAuditedModel {
} }
abstract public Set<T> getValues(); abstract public Set<T> getValues();
@Override
public String toString() {
return "id=" + id + " name=" + name + " title=" + title;
}
} }
...@@ -8,12 +8,12 @@ import javax.persistence.UniqueConstraint; ...@@ -8,12 +8,12 @@ import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.BusinessModel;
@Entity @Entity
@Table(name="kpidimensionkey", uniqueConstraints = { @UniqueConstraint(name = "UQ_dimensionkey", columnNames = { "name", "val" }) }) @Table(name = "kpidimensionkey", uniqueConstraints = { @UniqueConstraint(name = "UQ_dimensionkey", columnNames = { "name", "val" }) })
public class DimensionKey extends BusinessModel { public class DimensionKey extends BusinessModel {
@Column(length = 100, nullable = false) @Column(length = 100, nullable = false, updatable = false)
private String name; private String name;
@Column(name="val", length = 100, nullable = false) @Column(name = "val", length = 100, nullable = false, unique = false)
private String value; private String value;
public String getName() { public String getName() {
...@@ -34,6 +34,6 @@ public class DimensionKey extends BusinessModel { ...@@ -34,6 +34,6 @@ public class DimensionKey extends BusinessModel {
@Override @Override
public String toString() { public String toString() {
return "{key=" + name + ", value=" + value + "}"; return "{id=" + id + ", key=" + name + ", value=" + value + "}";
} }
} }
...@@ -20,7 +20,7 @@ public class Execution extends VersionedAuditedModel { ...@@ -20,7 +20,7 @@ public class Execution extends VersionedAuditedModel {
@JoinColumn(name = "parameterId") @JoinColumn(name = "parameterId")
private KPIParameter parameter; private KPIParameter parameter;
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }) @OneToMany(orphanRemoval=true, fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "executionId") @JoinColumn(name = "executionId")
private List<ExecutionDimension> dimensions = new ArrayList<ExecutionDimension>(); private List<ExecutionDimension> dimensions = new ArrayList<ExecutionDimension>();
...@@ -44,7 +44,7 @@ public class Execution extends VersionedAuditedModel { ...@@ -44,7 +44,7 @@ public class Execution extends VersionedAuditedModel {
public String query() { public String query() {
StringBuffer sb = new StringBuffer(), where = new StringBuffer(); StringBuffer sb = new StringBuffer(), where = new StringBuffer();
String alias = "a"; String alias = "_base";
sb.append("select count(distinct "); sb.append("select count(distinct ");
sb.append(alias); sb.append(alias);
sb.append(") from "); sb.append(") from ");
...@@ -82,6 +82,10 @@ public class Execution extends VersionedAuditedModel { ...@@ -82,6 +82,10 @@ public class Execution extends VersionedAuditedModel {
return sb.toString(); return sb.toString();
} }
public List<ExecutionDimension> getExecutionDimensions() {
return dimensions;
}
public Dimension<?> getDimension(int depth) { public Dimension<?> getDimension(int depth) {
if (depth >= dimensions.size()) if (depth >= dimensions.size())
......
...@@ -2,7 +2,6 @@ package org.genesys2.server.model.kpi; ...@@ -2,7 +2,6 @@ package org.genesys2.server.model.kpi;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
...@@ -11,9 +10,9 @@ import org.genesys2.server.model.BusinessModel; ...@@ -11,9 +10,9 @@ import org.genesys2.server.model.BusinessModel;
@Entity @Entity
@Table(name = "kpiexecutiondimension") @Table(name = "kpiexecutiondimension")
class ExecutionDimension extends BusinessModel { public class ExecutionDimension extends BusinessModel {
@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "dimensionId") @JoinColumn(name = "dimensionId")
private Dimension<?> dimension; private Dimension<?> dimension;
...@@ -46,4 +45,9 @@ class ExecutionDimension extends BusinessModel { ...@@ -46,4 +45,9 @@ class ExecutionDimension extends BusinessModel {
this.link = link; this.link = link;
} }
@Override
public String toString() {
return "id=" + id + " link=" + link + " field=" + field + " dim=" + dimension.getName();
}
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ package org.genesys2.server.model.kpi; ...@@ -2,6 +2,7 @@ package org.genesys2.server.model.kpi;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
...@@ -23,6 +24,9 @@ public class KPIParameter extends VersionedAuditedModel { ...@@ -23,6 +24,9 @@ public class KPIParameter extends VersionedAuditedModel {
@Column(name = "`condition`", length = 100) @Column(name = "`condition`", length = 100)
private String condition; private String condition;
@Lob
private String description;
public String getName() { public String getName() {
return name; return name;
} }
...@@ -55,4 +59,12 @@ public class KPIParameter extends VersionedAuditedModel { ...@@ -55,4 +59,12 @@ public class KPIParameter extends VersionedAuditedModel {
this.condition = condition; this.condition = condition;
} }
public void setDescription(String description) {
this.description=description;
}
public String getDescription() {
return description;
}
} }
package org.genesys2.server.model.kpi; package org.genesys2.server.model.kpi;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.CollectionTable; import javax.persistence.CollectionTable;
...@@ -7,22 +8,69 @@ import javax.persistence.Column; ...@@ -7,22 +8,69 @@ import javax.persistence.Column;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.Transient;
@Entity @Entity
public class NumericListDimension extends FixedListDimension<Number> { public class NumericListDimension extends FixedListDimension<Number> {
@Column(name = "listvalue", nullable = false, columnDefinition="double") @Column(length = 100)
@ElementCollection private String javaType = "java.lang.Integer";
@Transient
private Class<?> clazz;
@Column(name = "listvalue", nullable = false)
@ElementCollection()
@CollectionTable(name = "kpidimensionnumeric", joinColumns = @JoinColumn(name = "dimensionId")) @CollectionTable(name = "kpidimensionnumeric", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<Number> values; private Set<Double> values;
public void setValues(Set<Number> values) { public void setValues(Set<Number> list) {
this.values = values; Set<Double> doubles = new HashSet<Double>();
for (Number n : list) {
doubles.add(n.doubleValue());
}
this.values = doubles;
} }
@Override @Override
public Set<Number> getValues() { public Set<Number> getValues() {
return this.values; Set<Number> numbers = new HashSet<Number>();
for (double d : values) {
numbers.add(toType(d));
}
return numbers;
}
private Number toType(Double d) {
if (this.clazz == Integer.class) {
return d.intValue();
} else if (this.clazz == Long.class) {
return d.longValue();
} else if (this.clazz == Float.class) {
return d.floatValue();
} else if (this.clazz == Double.class) {
return d;
} else if (this.clazz == Short.class) {
return d.shortValue();
} else if (this.clazz == Byte.class) {
return d.byteValue();
}
return null;
} }
public Class<?> getClazz() {
if (clazz == null) {
try {
this.clazz = Class.forName(javaType);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
return clazz;
}
public void setClazz(Class<?> clazz) {
this.clazz = clazz;
this.javaType = this.clazz.getName();
}
} }
...@@ -33,7 +33,7 @@ public class Observation extends BusinessModel { ...@@ -33,7 +33,7 @@ public class Observation extends BusinessModel {
@JoinColumn(name = "parameterId") @JoinColumn(name = "parameterId")
private KPIParameter parameter; private KPIParameter parameter;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionId")) @JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionId"))
private Set<DimensionKey> dimensions = new HashSet<DimensionKey>(); private Set<DimensionKey> dimensions = new HashSet<DimensionKey>();
...@@ -57,8 +57,8 @@ public class Observation extends BusinessModel { ...@@ -57,8 +57,8 @@ public class Observation extends BusinessModel {
return parameter; return parameter;
} }
public void setParameter(KPIParameter kPIParameter) { public void setParameter(KPIParameter parameter) {
this.parameter = kPIParameter; this.parameter = parameter;
} }
public Set<DimensionKey> getDimensions() { public Set<DimensionKey> getDimensions() {
......
...@@ -12,7 +12,7 @@ import javax.persistence.JoinColumn; ...@@ -12,7 +12,7 @@ import javax.persistence.JoinColumn;
public class StringListDimension extends FixedListDimension<String> { public class StringListDimension extends FixedListDimension<String> {
@Column(name = "listvalue", nullable = false) @Column(name = "listvalue", nullable = false)
@ElementCollection @ElementCollection()
@CollectionTable(name = "kpidimensionstring", joinColumns = @JoinColumn(name = "dimensionId")) @CollectionTable(name = "kpidimensionstring", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<String> values; private Set<String> values;
......
/**
* Copyright 2014 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.genesys2.server.persistence.domain.kpi;
import org.genesys2.server.model.kpi.DimensionKey;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DimensionKeyRepository extends JpaRepository<DimensionKey, Long> {
DimensionKey findByNameAndValue(String name, String value);
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain.kpi;
import org.genesys2.server.model.kpi.Dimension;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DimensionRepository extends JpaRepository<Dimension<?>, Long> {
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain.kpi;
import org.genesys2.server.model.kpi.Execution;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ExecutionRepository extends JpaRepository<Execution, Long> {
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain.kpi;
import org.genesys2.server.model.kpi.KPIParameter;
import org.springframework.data.jpa.repository.JpaRepository;
public interface KPIParameterRepository extends JpaRepository<KPIParameter, Long> {
KPIParameter findByName(String name);
}
/**
* Copyright 2014 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.genesys2.server.persistence.domain.kpi;
import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ObservationRepository extends JpaRepository<Observation, Long> {
}
package org.genesys2.server.service; package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.kpi.Dimension;
import org.genesys2.server.model.kpi.Execution;
import org.genesys2.server.model.kpi.JpaDimension;
import org.genesys2.server.model.kpi.KPIParameter;
import org.genesys2.server.model.kpi.Observation;
public interface KPIService { public interface KPIService {
void test1(); long getSingleResult(String paQuery, Object... params);
KPIParameter save(KPIParameter parameter);
KPIParameter getParameter(String name);
KPIParameter getParameter(long id);
void delete(KPIParameter loaded);
Dimension<?> save(Dimension<?> dimension);
Dimension<?> getDimension(long id);