com.qwazr.graph.GraphServiceImpl Maven / Gradle / Ivy
/**
* Copyright 2015-2016 Emmanuel Keller / QWAZR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qwazr.graph;
import com.fasterxml.jackson.core.type.TypeReference;
import com.qwazr.graph.model.GraphDefinition;
import com.qwazr.graph.model.GraphNode;
import com.qwazr.graph.model.GraphNodeResult;
import com.qwazr.graph.model.GraphRequest;
import com.qwazr.graph.model.GraphResult;
import com.qwazr.server.AbstractServiceImpl;
import com.qwazr.server.ServerException;
import com.qwazr.utils.json.JsonMapper;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ws.rs.core.Response.Status;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
class GraphServiceImpl extends AbstractServiceImpl implements GraphServiceInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(GraphServiceImpl.class);
private volatile GraphManager graphManager;
GraphServiceImpl(GraphManager graphManager) {
this.graphManager = graphManager;
}
public GraphServiceImpl() {
}
@PostConstruct
public void init() {
graphManager = getContextAttribute(GraphManager.class);
}
@Override
public Set list() {
return graphManager.nameSet();
}
@Override
public GraphDefinition createUpdateGraph(final String graphName, final GraphDefinition graphDef) {
try {
graphManager.createUpdateGraph(graphName, graphDef);
return graphDef;
} catch (IOException | ServerException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
private GraphDefinition getGraphOrNotFound(final String graphName) throws ServerException, IOException {
GraphDefinition graphDef = graphManager.get(graphName);
if (graphDef == null)
throw new ServerException(Status.NOT_FOUND, "Graph not found: " + graphName);
return graphDef;
}
@Override
public GraphResult getGraph(String graphName) {
try {
GraphDefinition graphDef = getGraphOrNotFound(graphName);
GraphInstance graphInstance = graphManager.getGraphInstance(graphName);
return new GraphResult(graphDef, graphInstance.getSize());
} catch (ServerException | IOException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
private GraphDefinition deleteGraphLocal(String graphName) throws IOException, URISyntaxException, ServerException {
GraphDefinition base = getGraphOrNotFound(graphName);
graphManager.delete(graphName);
return base;
}
@Override
public GraphDefinition deleteGraph(String graphName) {
try {
return deleteGraphLocal(graphName);
} catch (IOException | URISyntaxException | ServerException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
@Override
public GraphNode createUpdateNode(String graphName, String node_id, GraphNode node, Boolean upsert) {
try {
graphManager.getGraphInstance(graphName).createUpdateNode(node_id, node, upsert);
return node;
} catch (IOException | ServerException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
@Override
public Set createUpdateNodes(String graphName, LinkedHashMap nodes, Boolean upsert) {
try {
graphManager.getGraphInstance(graphName).createUpdateNodes(nodes, upsert);
return nodes.keySet();
} catch (URISyntaxException | IOException | ServerException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
public final static TypeReference