Skip to content
Snippets Groups Projects
Commit d2af6e64 authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

updated VariableApi to use new model

parent ac799405
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
...@@ -47,34 +48,30 @@ public class VariablesApi { ...@@ -47,34 +48,30 @@ public class VariablesApi {
List<Variable> variables = session.createQuery("from Variable").list(); List<Variable> variables = session.createQuery("from Variable").list();
session.getTransaction().commit(); session.getTransaction().commit();
// Remove non-path-groups from sub-groups list
for(Variable v : variables) for(Variable v : variables)
{ {
Group g = v.getGroup(); Group g = null;
if(g != null) Group fils = null;
{ for(int i=v.getGrpPath().size()-1; i >= 0; i--) {
Integer idxPathGrp = v.getIdxPathGrp();
String code = g.getCode();
// Get group hierarchy matching code from DB
session = HibernateUtil.getSessionFactory().getCurrentSession(); session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); session.beginTransaction();
org.hibernate.Query query = session.createQuery("from Group where code= :code"); org.hibernate.Query query = session.createQuery("from Group where code= :code");
query.setString("code", code); query.setString("code", v.getGrpPath().get(i));
Group grpDB = (Group) query.uniqueResult(); g = (Group) query.uniqueResult();
session.getTransaction().commit(); session.getTransaction().commit();
g.setGroups(new LinkedList<>());
if(grpDB != null && grpDB.getGroups() != null && !grpDB.getGroups().isEmpty() && idxPathGrp != null) if(fils != null)
{ {
grpDB.setGroups(grpDB.getGroups().subList(idxPathGrp, idxPathGrp+1)); g.addGroup(fils);
v.setGroup(grpDB);
} }
fils = g.clone();
} }
v.setGroup(fils);
} }
return new ResponseEntity<List<Variable>>(HttpStatus.OK).ok(variables); return new ResponseEntity<List<Variable>>(HttpStatus.OK).ok(variables);
} }
@ApiOperation(value = "Get a variable", notes = "", response = Variable.class) @ApiOperation(value = "Get a variable", notes = "", response = Variable.class)
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "Found"), @ApiResponse(code = 200, message = "Found"),
......
...@@ -60,29 +60,10 @@ public class Database { ...@@ -60,29 +60,10 @@ public class Database {
// Sync groups and values with DB // Sync groups and values with DB
for(Variable v : variables) for(Variable v : variables)
{ {
Group g = v.getGroup(); if(v.getGroup() != null) {
if(g != null) List<String> path = new LinkedList<>();
{ v.setGroup(readGroupFromDB(getVarGrpRec(v.getGroup(), path)));
String pathGrp = null; v.setGrpPath(path);
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 // Save values if do not exist
...@@ -112,6 +93,19 @@ public class Database { ...@@ -112,6 +93,19 @@ public class Database {
session.getTransaction().commit(); session.getTransaction().commit();
} }
private static String getVarGrpRec(Group group, List<String> path) {
if(group.getGroups() != null)
{
path.add(group.getCode());
if(!group.getGroups().isEmpty())
{
getVarGrpRec(group.getGroups().get(0), path);
}
return group.getCode();
}
return null;
}
private static Group readGroupFromDB(String code) private static Group readGroupFromDB(String code)
{ {
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Session session = HibernateUtil.getSessionFactory().getCurrentSession();
......
...@@ -83,4 +83,13 @@ public class Group { ...@@ -83,4 +83,13 @@ public class Group {
this.groups.add(group); this.groups.add(group);
} }
public Group clone()
{
Group g = new Group();
g.setCode(this.getCode());
g.setLabel(this.getLabel());
g.setGroups(this.getGroups());
return g;
}
} }
...@@ -18,7 +18,7 @@ import java.util.List; ...@@ -18,7 +18,7 @@ import java.util.List;
@Table(name = "`variable`") @Table(name = "`variable`")
@ApiModel(description = "") @ApiModel(description = "")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-01-06T09:32:22.266Z") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-01-06T09:32:22.266Z")
@JsonIgnoreProperties(value = { "idxPathGrp" }) @JsonIgnoreProperties(value = { "grpPath" })
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Variable { public class Variable {
...@@ -34,11 +34,12 @@ public class Variable { ...@@ -34,11 +34,12 @@ public class Variable {
private Boolean isGrouping = null; private Boolean isGrouping = null;
private Boolean isCovariable = null; private Boolean isCovariable = null;
private Boolean isFilter = null; private Boolean isFilter = null;
@ElementCollection(fetch = FetchType.EAGER)
private List<String> grpPath = new LinkedList<>();
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
private Group group = null; private Group group = null;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private List<Value> values = new LinkedList<Value>(); private List<Value> values = new LinkedList<Value>();
private Integer idxPathGrp = null; // Tricky attribut used to know which of group.getGroups() is the path group
public Variable() { public Variable() {
} }
...@@ -212,16 +213,16 @@ public class Variable { ...@@ -212,16 +213,16 @@ public class Variable {
} }
/** /**
* Subgroup index for variable path * Group path
**/ **/
@ApiModelProperty(value = "Subgroup index for variable path") @ApiModelProperty(value = "Group path")
@JsonProperty("idxPathGrp") @JsonProperty("grpPath")
public Integer getIdxPathGrp() { public List<String> getGrpPath() {
return idxPathGrp; return grpPath;
} }
public void setIdxPathGrp(Integer idxPathGrp) { public void setGrpPath(List<String> grpPath) {
this.idxPathGrp = idxPathGrp; this.grpPath = grpPath;
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment