diff --git a/src/main/java/org/hbp/mip/controllers/VariablesApi.java b/src/main/java/org/hbp/mip/controllers/VariablesApi.java index 7b31e33a2cc556c08b6a678a4b25eac4a2e951f2..49a5e7ded18d5be1b99f5ccf142b41c80b7bd885 100644 --- a/src/main/java/org/hbp/mip/controllers/VariablesApi.java +++ b/src/main/java/org/hbp/mip/controllers/VariablesApi.java @@ -6,6 +6,7 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; +import org.hbp.mip.model.Group; import org.hbp.mip.model.Value; import org.hbp.mip.model.Variable; import org.hibernate.Session; @@ -39,10 +40,37 @@ public class VariablesApi { @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isGrouping", required = false) String isGrouping, @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isCovariable", required = false) String isCovariable, @ApiParam(value = "Boolean value formatted like : (\"0\") or (\"1\") or (\"false\") or (\"true\")") @RequestParam(value = "isFilter", required = false) String isFilter) throws NotFoundException { + + // Get variales from DB Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<Variable> variables = session.createQuery("from Variable").list(); session.getTransaction().commit(); + + // Remove non-path-groups from sub-groups list + for(Variable v : variables) + { + Group g = v.getGroup(); + if(g != null) + { + Integer idxPathGrp = v.getIdxPathGrp(); + String code = g.getCode(); + + // Get group hierarchy matching code from DB + session = HibernateUtil.getSessionFactory().getCurrentSession(); + session.beginTransaction(); + org.hibernate.Query query = session.createQuery("from Group where code= :code"); + query.setString("code", code); + Group grpDB = (Group) query.uniqueResult(); + session.getTransaction().commit(); + + if(grpDB != null && grpDB.getGroups() != null && !grpDB.getGroups().isEmpty() && idxPathGrp != null) + { + grpDB.setGroups(grpDB.getGroups().subList(idxPathGrp, idxPathGrp+1)); + v.setGroup(grpDB); + } + } + } return new ResponseEntity<List<Variable>>(HttpStatus.OK).ok(variables); } diff --git a/src/main/java/org/hbp/mip/data/Database.java b/src/main/java/org/hbp/mip/data/Database.java index 7d2cbf10056cc5fff1117cb5582820d36c835430..474cda91b282656314bf382b7bc672e8103c9232 100644 --- a/src/main/java/org/hbp/mip/data/Database.java +++ b/src/main/java/org/hbp/mip/data/Database.java @@ -63,23 +63,46 @@ public class Database { Group g = v.getGroup(); if(g != null) { + String pathGrp = null; + List<Group> subGroups = g.getGroups(); // Should only contain one group + + // Get code of path sub-group and link current variable group to matching one from DB + if(subGroups != null && !subGroups.isEmpty()) + { + pathGrp = subGroups.get(0).getCode(); + } v.setGroup(readGroupFromDB(g.getCode())); + + // Search index of the path sub-group and set it for the current variable + int i = 0; + for(Group subGrp : v.getGroup().getGroups()) + { + if(pathGrp.equals(subGrp.getCode())) + { + v.setIdxPathGrp(i); + } + i++; + } } + + // Save values if do not exist List<Value> newValues = new LinkedList<>(); for(Value val : v.getValues()) { + // Check existence in the DB Value existingVal = readValueFromDB(val.getCode()); if(existingVal == null) { + // Save if does not exist Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(val); session.getTransaction().commit(); existingVal = val; } - newValues.add(existingVal); + newValues.add(existingVal); // Set reference } - v.setValues(newValues); + v.setValues(newValues); // Set reference } // Insert into DB diff --git a/src/main/java/org/hbp/mip/model/Group.java b/src/main/java/org/hbp/mip/model/Group.java index 25a783c48f3a24ac3d039b40432a68425972b038..d2b6348b098348de2db4c77260ac9a30d2484669 100644 --- a/src/main/java/org/hbp/mip/model/Group.java +++ b/src/main/java/org/hbp/mip/model/Group.java @@ -27,7 +27,7 @@ public class Group { @Column(unique = true) private String code = null; private String label = null; - @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<Group> groups = new LinkedList<Group>(); public Group() { diff --git a/src/main/java/org/hbp/mip/model/Variable.java b/src/main/java/org/hbp/mip/model/Variable.java index 77c5e8d3356f643bd0f774118b6cc5226b57be66..6e789acb0ae2a50b0c92ab804c649c4700cbad14 100644 --- a/src/main/java/org/hbp/mip/model/Variable.java +++ b/src/main/java/org/hbp/mip/model/Variable.java @@ -18,7 +18,7 @@ import java.util.List; @Table(name = "`variable`") @ApiModel(description = "") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-01-06T09:32:22.266Z") -@JsonIgnoreProperties(value = { "id" }) +@JsonIgnoreProperties(value = { "id", "idxPathGrp" }) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Variable { @Id @@ -40,6 +40,7 @@ public class Variable { private Group group = null; @ManyToMany(fetch = FetchType.EAGER) private List<Value> values = new LinkedList<Value>(); + private Integer idxPathGrp = null; // Trick attribut used to know which of group.getGroups() is the path group public Variable() { } @@ -225,6 +226,19 @@ public class Variable { this.units = units; } + /** + * Subgroup index for variable path + **/ + @ApiModelProperty(value = "Subgroup index for variable path") + @JsonProperty("idxPathGrp") + public Integer getIdxPathGrp() { + return idxPathGrp; + } + + public void setIdxPathGrp(Integer idxPathGrp) { + this.idxPathGrp = idxPathGrp; + } + @Override public String toString() { StringBuilder sb = new StringBuilder();