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

CSV file loaded from within the JAR + Bugfixes

parent 40cde152
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,9 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
import java.util.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
......@@ -158,6 +160,9 @@ public class ModelsApi {
model.getDataset().getData().put("MidTemp", values);
model.getDataset().getHeader().add("MidTemp");
System.out.println("queryID" + model.getQuery().getId());
System.out.println("dataset id" + model.getDataset().getCode());
return new ResponseEntity<Model>(HttpStatus.OK).ok(model);
}
......
......@@ -21,8 +21,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@Api(value = "/queries/requests", description = "the requests API")
public class RequestsApi {
private final String VALUES_FILE = "/home/mirco/Workspace/GitLab/portal-backend/src/main/resources/data/values.csv";
@ApiOperation(value = "Send a request", response = Dataset.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success") })
@RequestMapping(method = RequestMethod.POST)
......@@ -30,7 +28,7 @@ public class RequestsApi {
@RequestBody @ApiParam(value = "Query to process", required = true) Query query
) throws NotFoundException {
Dataset dataset = CSVUtil.parseValues(VALUES_FILE, query);
Dataset dataset = CSVUtil.parseValues("data/values.csv", query);
return new ResponseEntity<Dataset>(HttpStatus.OK).ok(dataset);
}
......
......@@ -21,7 +21,7 @@ public class Group {
@Id
private String code = null;
private String label = null;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OneToMany(fetch = FetchType.EAGER)
private List<Group> groups = new LinkedList<Group>();
public Group() {
......
......@@ -8,8 +8,12 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
@Entity
......@@ -24,11 +28,14 @@ public class Model {
private Boolean valid = null;
private Date createdAt = null;
private Date updatedAt = null;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private Query query = null;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private Dataset dataset = null;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private Chart chart = null;
@ManyToOne
private User createdBy = null;
......
......@@ -24,26 +24,26 @@ public class Query {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = null;
private String request = null;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "query_variable", joinColumns = {
@JoinColumn(name = "id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "code",
nullable = false, updatable = false) })
private List<Variable> variables = new LinkedList<Variable>();
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Variable> variables = new LinkedList<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "query_covariable", joinColumns = {
@JoinColumn(name = "id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "code",
nullable = false, updatable = false) })
private List<Variable> covariables = new LinkedList<Variable>();
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Variable> covariables = new LinkedList<>();
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "query_grouping", joinColumns = {
@JoinColumn(name = "id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "code",
nullable = false, updatable = false) })
private List<Variable> grouping = new LinkedList<Variable>();
private List<Variable> grouping = new LinkedList<>();
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Filter> filters = new LinkedList<Filter>();
private List<Filter> filters = new LinkedList<>();
public Query() {
}
......
......@@ -24,19 +24,6 @@ public class Tag {
public Tag() {
}
/**
* Unique identifier
**/
/*@ApiModelProperty(value = "Unique identifier")
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}*/
/**
* Name
**/
......
......@@ -38,7 +38,7 @@ public class Variable {
@ManyToOne(fetch = FetchType.EAGER)
private Group group = null;
@ManyToMany(fetch = FetchType.EAGER)
private List<Value> values = new LinkedList<Value>();
private List<Value> values = new LinkedList<>();
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "variables")
private List<Query> queries = new LinkedList<>();
......
......@@ -21,10 +21,9 @@ public class CSVUtil {
public static Dataset parseValues(String filename, Query query)
{
List<String[]> rows = getFilteredRows(filename, query.getFilters());
Dataset result = new Dataset();
File file = new File(filename);
List<String[]> rows = getFilteredRows(file, query.getFilters());
System.out.println("nb of filtered rows : "+rows.size());
String code = GenerateDSCode(query);
Date date = new Date();
List<String> header = new LinkedList<>();
......@@ -38,11 +37,13 @@ public class CSVUtil {
header.addAll(variables.stream().map(Variable::getCode).collect(Collectors.toList()));
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
InputStream is = Dataset.class.getClassLoader().getResourceAsStream(filename);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String[] firstRow = br.readLine().split(SEPARATOR, -1);
br.close();
fr.close();
isr.close();
is.close();
for (Variable v : variables) {
String type = getTypeFromDB(v);
......@@ -119,9 +120,9 @@ public class CSVUtil {
}
}
// TODO : Remove this limit -> only to avoid bug with Virtua's front-end
if(l.size() > 100)
if(l.size() > 50)
{
l = l.subList(0, 99);
l = l.subList(0, 49);
}
data.put(c, l);
System.out.println("Adding "+l.size()+" values to "+c);
......@@ -137,11 +138,12 @@ public class CSVUtil {
return result;
}
private static List<String[]> getFilteredRows(File file, List<Filter> filters) {
private static List<String[]> getFilteredRows(String filename, List<Filter> filters) {
List<String[]> filteredRows = new LinkedList<>();
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
InputStream is = Dataset.class.getClassLoader().getResourceAsStream(filename);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String[] firstRow = br.readLine().split(SEPARATOR, -1); // 1st row -> headers
for (String line = br.readLine(); line != null; line = br.readLine()) {
String[] row = line.split(SEPARATOR, -1);
......@@ -150,7 +152,8 @@ public class CSVUtil {
}
}
br.close();
fr.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
......@@ -304,6 +307,10 @@ public class CSVUtil {
String type = (String) q.uniqueResult();
session.getTransaction().commit();
if(type == null)
{
type = "unknown";
}
return type;
}
......
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