org.neo4j.rest.graphdb.batch.BatchRestAPI Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-neo4j-rest Show documentation
Show all versions of spring-data-neo4j-rest Show documentation
pring Data Neo4j Wrapper for the Neo4j REST API, provides a Graph Database proxy for the remote invocation.
/**
* Copyright (c) 2002-2013 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.rest.graphdb.batch;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.index.lucene.ValueContext;
import org.neo4j.rest.graphdb.RequestResult;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIImpl;
import org.neo4j.rest.graphdb.RestResultException;
import org.neo4j.rest.graphdb.converter.RestEntityExtractor;
import org.neo4j.rest.graphdb.converter.RestEntityPropertyRefresher;
import org.neo4j.rest.graphdb.entity.RestEntity;
import org.neo4j.rest.graphdb.entity.RestNode;
import org.neo4j.rest.graphdb.index.IndexInfo;
import org.neo4j.rest.graphdb.index.RestIndex;
import org.neo4j.rest.graphdb.util.DefaultConverter;
import javax.ws.rs.core.Response;
import java.util.*;
import static java.util.Arrays.asList;
public class BatchRestAPI {
private final RecordingRestRequest restRequest;
private final String baseUri;
private final RestAPI restApi;
public BatchRestAPI(RestAPI restApi) {
this.baseUri = restApi.getBaseUri();
this.restApi = restApi;
RestEntityExtractor converter = new RestEntityExtractor(restApi);
this.restRequest = new RecordingRestRequest(new RestOperations(converter), restApi.getBaseUri());
}
public void stop() {
restRequest.stop();
}
public RestNode getNodeById(long id) {
RestOperations.RestOperation nodeRequest = restRequest.get("node/" + id);
RestOperations.RestOperation labelsRequest = restRequest.get("node/" + id+"/labels");
try {
Map results = executeBatchRequest();
RestNode node = (RestNode) results.get(nodeRequest.getBatchId());
List labels = (List) results.get(labelsRequest.getBatchId());
node.setLabels(labels);
return node;
} catch(RestResultException rre) {
if (rre.getMessage().contains("NodeNotFoundException")) throw new NotFoundException("Node not found: "+id);
else throw rre;
}
}
// public void addToIndex( T entity, RestIndex index, String key, Object value ) {
// final RestEntity restEntity = (RestEntity) entity;
// String uri = restEntity.getUri();
// if (value instanceof ValueContext) {
// value = ((ValueContext)value).getCorrectValue();
// }
// final Map data = MapUtil.map("key", key, "value", value, "uri", uri);
// restRequest.post(index.indexPath(), data);
// }
public Map executeBatchRequest() {
stop();
RestOperations operations = restRequest.getOperations();
RequestResult response = restApi.batch(createBatchRequestData(operations));
return convertRequestResultToEntities(operations, response);
}
@SuppressWarnings("unchecked")
protected Map convertRequestResultToEntities(RestOperations operations, RequestResult response) {
Object result = response.toEntity();
if (RestResultException.isExceptionResult(result)) {
throw new RestResultException(result);
}
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy