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 {
private String title;
final public String getName() {
return title;
return name;
}
final public void setName(String name) {
this.title = name;
this.name = name;
}
public void setTitle(String title) {
......@@ -40,4 +40,9 @@ public abstract class Dimension<T> extends VersionedAuditedModel {
}
abstract public Set<T> getValues();
@Override
public String toString() {
return "id=" + id + " name=" + name + " title=" + title;
}
}
......@@ -8,12 +8,12 @@ import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel;
@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 {
@Column(length = 100, nullable = false)
@Column(length = 100, nullable = false, updatable = false)
private String name;
@Column(name="val", length = 100, nullable = false)
@Column(name = "val", length = 100, nullable = false, unique = false)
private String value;
public String getName() {
......@@ -34,6 +34,6 @@ public class DimensionKey extends BusinessModel {
@Override
public String toString() {
return "{key=" + name + ", value=" + value + "}";
return "{id=" + id + ", key=" + name + ", value=" + value + "}";
}
}
......@@ -20,7 +20,7 @@ public class Execution extends VersionedAuditedModel {
@JoinColumn(name = "parameterId")
private KPIParameter parameter;
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@OneToMany(orphanRemoval=true, fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "executionId")
private List<ExecutionDimension> dimensions = new ArrayList<ExecutionDimension>();
......@@ -44,7 +44,7 @@ public class Execution extends VersionedAuditedModel {
public String query() {
StringBuffer sb = new StringBuffer(), where = new StringBuffer();
String alias = "a";
String alias = "_base";
sb.append("select count(distinct ");
sb.append(alias);
sb.append(") from ");
......@@ -82,6 +82,10 @@ public class Execution extends VersionedAuditedModel {
return sb.toString();
}
public List<ExecutionDimension> getExecutionDimensions() {
return dimensions;
}
public Dimension<?> getDimension(int depth) {
if (depth >= dimensions.size())
......
......@@ -2,7 +2,6 @@ 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;
......@@ -11,9 +10,9 @@ import org.genesys2.server.model.BusinessModel;
@Entity
@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")
private Dimension<?> dimension;
......@@ -46,4 +45,9 @@ class ExecutionDimension extends BusinessModel {
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;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
......@@ -23,6 +24,9 @@ public class KPIParameter extends VersionedAuditedModel {
@Column(name = "`condition`", length = 100)
private String condition;
@Lob
private String description;
public String getName() {
return name;
}
......@@ -55,4 +59,12 @@ public class KPIParameter extends VersionedAuditedModel {
this.condition = condition;
}
public void setDescription(String description) {
this.description=description;
}
public String getDescription() {
return description;
}
}
package org.genesys2.server.model.kpi;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CollectionTable;
......@@ -7,22 +8,69 @@ import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Transient;
@Entity
public class NumericListDimension extends FixedListDimension<Number> {
@Column(name = "listvalue", nullable = false, columnDefinition="double")
@ElementCollection
@Column(length = 100)
private String javaType = "java.lang.Integer";
@Transient
private Class<?> clazz;
@Column(name = "listvalue", nullable = false)
@ElementCollection()
@CollectionTable(name = "kpidimensionnumeric", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<Number> values;
private Set<Double> values;
public void setValues(Set<Number> values) {
this.values = values;
public void setValues(Set<Number> list) {
Set<Double> doubles = new HashSet<Double>();
for (Number n : list) {
doubles.add(n.doubleValue());
}
this.values = doubles;
}
@Override
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 {
@JoinColumn(name = "parameterId")
private KPIParameter parameter;
@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "kpiobservationdimension", joinColumns = @JoinColumn(name = "observationId"), inverseJoinColumns = @JoinColumn(name = "dimensionId"))
private Set<DimensionKey> dimensions = new HashSet<DimensionKey>();
......@@ -57,8 +57,8 @@ public class Observation extends BusinessModel {
return parameter;
}
public void setParameter(KPIParameter kPIParameter) {
this.parameter = kPIParameter;
public void setParameter(KPIParameter parameter) {
this.parameter = parameter;
}
public Set<DimensionKey> getDimensions() {
......
......@@ -12,7 +12,7 @@ import javax.persistence.JoinColumn;
public class StringListDimension extends FixedListDimension<String> {
@Column(name = "listvalue", nullable = false)
@ElementCollection
@ElementCollection()
@CollectionTable(name = "kpidimensionstring", joinColumns = @JoinColumn(name = "dimensionId"))
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;
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 {
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);
List<?> getValues(JpaDimension loadedJpa);
Execution save(Execution execution);
Execution getExecution(long id);
void delete(Execution execution);
List<Observation> execute(Execution execution);
List<Observation> save(List<Observation> observations);
Object getSingleResult(String paQuery, Object... params);
void delete(List<Observation> observations);
}
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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.model.kpi.Dimension;
import org.genesys2.server.model.kpi.DimensionKey;
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;
import org.genesys2.server.persistence.domain.kpi.DimensionKeyRepository;
import org.genesys2.server.persistence.domain.kpi.DimensionRepository;
import org.genesys2.server.persistence.domain.kpi.ExecutionRepository;
import org.genesys2.server.persistence.domain.kpi.ObservationRepository;
import org.genesys2.server.persistence.domain.kpi.KPIParameterRepository;
import org.genesys2.server.service.KPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -18,23 +35,179 @@ public class KPIServiceImpl implements KPIService {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private DimensionRepository dimensionRepository;
@Autowired
private KPIParameterRepository parameterRepository;
@Autowired
private ExecutionRepository executionRepository;
@Autowired
private ObservationRepository observationRepository;
@Autowired
private DimensionKeyRepository dimensionKeyRepository;
@Override
@Transactional
public KPIParameter save(KPIParameter parameter) {
return parameterRepository.save(parameter);
}
@Override
public KPIParameter getParameter(long id) {
return parameterRepository.findOne(id);
}
@Override
public KPIParameter getParameter(String name) {
return parameterRepository.findByName(name);
}
@Override
@Transactional
public void delete(KPIParameter parameter) {
parameterRepository.delete(parameter);
}
@Override
@Transactional
public Dimension<?> save(Dimension<?> dimension) {
LOG.debug("Persising dimension " + dimension);
return dimensionRepository.save(dimension);
}
@Override