Commit 032dc226 authored by Matija Obreza's avatar Matija Obreza

Delete KPI entities, persist DimensionKeys in new transaction

parent 0a7f58a4
......@@ -6,12 +6,15 @@ import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class BooleanDimension extends Dimension<Boolean> {
@Column
private int mode = 3;
@JsonIgnore
@Override
public Set<Boolean> getValues() {
Set<Boolean> b = new HashSet<Boolean>();
......
package org.genesys2.server.model.kpi;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CollectionTable;
......@@ -14,7 +15,7 @@ public class StringListDimension extends FixedListDimension<String> {
@Column(name = "listvalue", nullable = false)
@ElementCollection()
@CollectionTable(name = "kpidimensionstring", joinColumns = @JoinColumn(name = "dimensionId"))
private Set<String> values;
private Set<String> values = new HashSet<String>();
public void setValues(Set<String> values) {
this.values = values;
......
......@@ -5,7 +5,6 @@ import java.util.Set;
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;
......@@ -19,21 +18,24 @@ public interface KPIService {
KPIParameter getParameter(long id);
void delete(KPIParameter loaded);
KPIParameter delete(KPIParameter parameter);
Dimension<?> save(Dimension<?> dimension);
Dimension<?> getDimension(long id);
Set<?> getValues(JpaDimension loadedJpa);
Dimension<?> delete(Dimension<?> dimension);
Set<?> getValues(Dimension<?> loadedJpa);
Execution save(Execution execution);
Execution getExecution(long id);
void delete(Execution execution);
Execution delete(Execution execution);
List<Observation> execute(Execution execution);
List<Observation> save(List<Observation> observations);
void delete(List<Observation> observations);
......
......@@ -27,6 +27,7 @@ import org.genesys2.server.service.KPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
......@@ -75,8 +76,10 @@ public class KPIServiceImpl implements KPIService {
@Override
@Transactional
public void delete(KPIParameter parameter) {
public KPIParameter delete(KPIParameter parameter) {
parameterRepository.delete(parameter);
parameter.setId(null);
return parameter;
}
@Override
......@@ -86,6 +89,14 @@ public class KPIServiceImpl implements KPIService {
return dimensionRepository.save(dimension);
}
@Override
@Transactional
public Dimension<?> delete(Dimension<?> dimension) {
dimensionRepository.delete(dimension);
dimension.setId(null);
return dimension;
}
@Override
public Dimension<?> getDimension(long id) {
Dimension<?> dim = dimensionRepository.findOne(id);
......@@ -117,29 +128,32 @@ public class KPIServiceImpl implements KPIService {
@Override
@Transactional
public void delete(Execution execution) {
public Execution delete(Execution execution) {
executionRepository.delete(execution);
execution.setId(null);
return execution;
}
@Override
public Set<?> getValues(JpaDimension jpaDimension) {
StringBuilder paQuery = new StringBuilder();
paQuery.append("select distinct a.").append(jpaDimension.getField()).append(" from ");
paQuery.append(jpaDimension.getEntity()).append(" a");
if (StringUtils.isNotBlank(jpaDimension.getCondition())) {
paQuery.append(" where ").append(jpaDimension.getCondition());
}
LOG.debug(paQuery);
Query q = entityManager.createQuery(paQuery.toString());
public Set<?> getValues(Dimension<?> dim) {
if (dim instanceof JpaDimension) {
JpaDimension jpaDim = (JpaDimension) dim;
StringBuilder paQuery = new StringBuilder();
paQuery.append("select distinct a.").append(jpaDim.getField()).append(" from ");
paQuery.append(jpaDim.getEntity()).append(" a");
if (StringUtils.isNotBlank(jpaDim.getCondition())) {
paQuery.append(" where ").append(jpaDim.getCondition());
}
LOG.debug(paQuery);
// for (int i = 0; i < params.length; i++) {
// LOG.debug("\t?" + (i + 1) + " = " + params[i]);
// q.setParameter(i + 1, params[i]);
// }
Query q = entityManager.createQuery(paQuery.toString());
return new HashSet<Object>(q.getResultList());
return new HashSet<Object>(q.getResultList());
} else {
// Dimension<?> dim2 = dimensionRepository.findOne(dim.getId());
// dim2.getValues().size();
return dim.getValues();
}
}
@Override
......@@ -159,6 +173,7 @@ public class KPIServiceImpl implements KPIService {
@Override
public List<Observation> execute(Execution execution) {
List<Observation> results = new ArrayList<Observation>();
execution = executionRepository.findOne(execution.getId());
internalExecute(execution.query(), execution, results, 0, new ArrayList<Object>());
return results;
}
......@@ -166,7 +181,6 @@ public class KPIServiceImpl implements KPIService {
@Override
@Transactional
public List<Observation> save(List<Observation> observations) {
Set<DimensionKey> dks = new HashSet<DimensionKey>();
for (Observation obs : observations) {
Set<DimensionKey> dims = new HashSet<DimensionKey>();
for (DimensionKey dk : obs.getDimensions()) {
......@@ -174,16 +188,19 @@ public class KPIServiceImpl implements KPIService {
if (existing != null) {
dims.add(existing);
} else {
dims.add(dk);
dks.add(dk);
dims.add(makeDimensionKey(dk));
}
}
obs.setDimensions(dims);
}
dimensionKeyRepository.save(dks);
return observationRepository.save(observations);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
private DimensionKey makeDimensionKey(DimensionKey dk) {
return dimensionKeyRepository.save(dk);
}
@Override
@Transactional
public void delete(List<Observation> observations) {
......@@ -200,10 +217,7 @@ public class KPIServiceImpl implements KPIService {
} else {
// Recurse
Set<?> values = null;
if (dim instanceof JpaDimension) {
values = getValues((JpaDimension) dim);
} else
values = dim.getValues();
values = getValues(dim);
for (Object val : values) {
params.add(val);
internalExecute(paQuery, paramExec, results, depth + 1, params);
......
......@@ -83,6 +83,18 @@ public class KPIController extends RestController {
return kpiService.getParameter(name);
}
/**
* Delete parameter
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/parameter/{name:.+}", method = { RequestMethod.DELETE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
KPIParameter deleteParameter(@PathVariable("name") String name) {
return kpiService.delete(kpiService.getParameter(name));
}
/**
* Update parameter
*
......@@ -132,6 +144,18 @@ public class KPIController extends RestController {
return kpiService.getDimension(id);
}
/**
* Get parameter
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/dimension/{id}", method = { RequestMethod.DELETE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Dimension<?> deleteDimension(@PathVariable("id") long id) {
return kpiService.delete(kpiService.getDimension(id));
}
/**
* Update {@link BooleanDimension}
*
......@@ -170,7 +194,7 @@ public class KPIController extends RestController {
}
/**
* Get parameter
* Get execution
*
* @return
* @throws ValidationException
......@@ -182,6 +206,18 @@ public class KPIController extends RestController {
return ExecutionJson.from(execution);
}
/**
* Delete execution
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/execution/{id}", method = { RequestMethod.DELETE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
ExecutionJson deleteExecution(@PathVariable("id") long id) {
return ExecutionJson.from(kpiService.delete(kpiService.getExecution(id)));
}
/**
* Update {@link BooleanDimension}
*
......
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