logger.debug("receiving file: '" + file.getOriginalFilename() + "'");
logger.debug("file format: '" + format + "'");
logger.debug("search keys: '" + searchKeys + "'");
DendriteGraph graph = metaGraphService.getDendriteGraph(graphId);
if (graph == null) {
response.put("status", "error");
response.put("msg", "cannot find graph '" + graphId + "'");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
try {
// extract search indices from client JSON
JSONObject jsonKeys = new JSONObject(searchKeys);
JSONArray jsonVertices = jsonKeys.getJSONArray("vertices");
JSONArray jsonEdges = jsonKeys.getJSONArray("edges");
// build search indices for vertices
for (int i = 0; i < jsonVertices.length(); ++i){
JSONObject jsonVertex = jsonVertices.getJSONObject(i);
String key = jsonVertex.getString("name");
String type = jsonVertex.getString("type");
// create the search index (if it doesn't already exist and isn't a reserved key)
if (graph.getType(key) == null && !RESERVED_KEYS.contains(key)) {
Class cls;
List<Parameter> parameters = new ArrayList<>();
if (type.equals("string")) {
cls = String.class;
parameters.add(Parameter.of(Mapping.MAPPING_PREFIX, Mapping.STRING));
} else if (type.equals("text")) {
cls = String.class;
parameters.add(Parameter.of(Mapping.MAPPING_PREFIX, Mapping.TEXT));
} else if (type.equals("integer")) {
cls = Integer.class;
} else if (type.equals("float")) {
cls = FullFloat.class;
} else if (type.equals("double")) {
cls = FullDouble.class;
} else if (type.equals("geocoordinate")) {
cls = Geoshape.class;
} else {
graph.rollback();
response.put("status", "error");
response.put("msg", "unknown type '" + type + "'");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
graph.makeKey(key)
.dataType(cls)
.indexed(Vertex.class)
.indexed(DendriteGraph.INDEX_NAME, Vertex.class, parameters.toArray(new Parameter[parameters.size()]))
.make();
}
}
// build search indices for edges
for (int i = 0; i < jsonEdges.length(); ++i){
JSONObject jsonEdge = jsonEdges.getJSONObject(i);
String key = jsonEdge.getString("name");
String type = jsonEdge.getString("type");
// create the search index (if it doesn't already exist and isn't a reserved key)
if (graph.getType(key) == null && !RESERVED_KEYS.contains(key)) {
Class cls;
List<Parameter> parameters = new ArrayList<>();
if (type.equals("string")) {
cls = String.class;
parameters.add(Parameter.of(Mapping.MAPPING_PREFIX, Mapping.STRING));
} else if (type.equals("text")) {
cls = String.class;
parameters.add(Parameter.of(Mapping.MAPPING_PREFIX, Mapping.TEXT));
} else if (type.equals("integer")) {
cls = Integer.class;
} else if (type.equals("float")) {
cls = FullFloat.class;
} else if (type.equals("double")) {
cls = FullDouble.class;
} else if (type.equals("geocoordinate")) {
cls = Geoshape.class;
} else {
graph.rollback();
response.put("status", "error");
response.put("msg", "unknown type '" + type + "'");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
graph.makeKey(key)
.dataType(cls)
.indexed(Edge.class)
.indexed(DendriteGraph.INDEX_NAME, Edge.class, parameters.toArray(new Parameter[parameters.size()]))
.make();
}
}
// commit the indices
graph.commit();
InputStream inputStream = file.getInputStream();
if (format.equalsIgnoreCase("GraphSON")) {
GraphSONReader.inputGraph(graph, inputStream);
} else if (format.equalsIgnoreCase("GraphML")) {
GraphMLReader.inputGraph(graph, inputStream);
} else if (format.equalsIgnoreCase("GML")) {
GMLReader.inputGraph(graph, inputStream);
} else if (format.equalsIgnoreCase("FaunusGraphSON")) {
FaunusGraphSONReader.inputGraph(graph, inputStream);
} else {
graph.rollback();
response.put("status", "error");
response.put("msg", "unknown format '" + format + "'");
inputStream.close();
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
inputStream.close();
} catch (Throwable t) {
t.printStackTrace();
graph.rollback();
response.put("status", "error");
response.put("msg", "exception: " + t.toString());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);