fixed RestGraphDatabase bugs wrt creation of Relationshops with properties and index query lookup
This commit is contained in:
@@ -73,8 +73,7 @@ public class RestGraphDatabase implements GraphDatabaseService, GraphDatabase {
|
||||
|
||||
@Override
|
||||
public Relationship createRelationship(Node startNode, Node endNode, RelationshipType type, Property... props) {
|
||||
Relationship relationship = startNode.createRelationshipTo(endNode, type);
|
||||
return relationship;
|
||||
return RestRelationship.create((RestNode)startNode,(RestNode)endNode,type,props);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,9 +21,7 @@ import org.neo4j.graphdb.*;
|
||||
import org.neo4j.graphdb.Traverser.Order;
|
||||
import org.neo4j.helpers.collection.IterableWrapper;
|
||||
import org.neo4j.helpers.collection.IteratorUtil;
|
||||
import org.neo4j.helpers.collection.MapUtil;
|
||||
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@@ -42,14 +40,7 @@ public class RestNode extends RestEntity implements Node {
|
||||
}
|
||||
|
||||
public Relationship createRelationshipTo( Node toNode, RelationshipType type ) {
|
||||
Map<String, Object> data = MapUtil.map( "to", ( (RestNode) toNode ).getUri(),
|
||||
"type", type.name() );
|
||||
|
||||
ClientResponse response = restRequest.post( "relationships", JsonHelper.createJsonFrom( data ) );
|
||||
if ( restRequest.statusOtherThan( response, Status.CREATED ) ) {
|
||||
throw new RuntimeException( "" + response.getStatus() );
|
||||
}
|
||||
return new RestRelationship( response.getLocation(), getRestGraphDatabase() );
|
||||
return RestRelationship.create(this,(RestNode)toNode,type);
|
||||
}
|
||||
|
||||
public Iterable<Relationship> getRelationships() {
|
||||
|
||||
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.data.graph.neo4j.rest.support;
|
||||
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import org.neo4j.graphdb.*;
|
||||
import org.neo4j.helpers.collection.MapUtil;
|
||||
import org.springframework.data.graph.core.Property;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -74,4 +78,18 @@ public class RestRelationship extends RestEntity implements Relationship {
|
||||
public boolean isType( RelationshipType type ) {
|
||||
return type.name().equals( getStructuralData().get( "type" ) );
|
||||
}
|
||||
|
||||
public static Relationship create(RestNode startNode, RestNode endNode, RelationshipType type, Property... props) {
|
||||
final RestRequest restRequest = startNode.getRestRequest();
|
||||
Map<String, Object> data = MapUtil.map("to", endNode.getUri(), "type", type.name());
|
||||
if (props!=null && props.length>0) {
|
||||
data.put("data",Property.toMap(props));
|
||||
}
|
||||
|
||||
ClientResponse response = restRequest.post( "relationships", JsonHelper.createJsonFrom( data ) );
|
||||
if ( restRequest.statusOtherThan( response, Response.Status.CREATED ) ) {
|
||||
throw new RuntimeException( "" + response.getStatus() );
|
||||
}
|
||||
return new RestRelationship( response.getLocation(), startNode.getRestGraphDatabase() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,8 @@ public abstract class RestIndex<T extends PropertyContainer> implements Index<T>
|
||||
}
|
||||
|
||||
public IndexHits<T> query( String key, Object value ) {
|
||||
ClientResponse response = restRequest.get( indexPath( key ) + "?query=" + RestRequest.encode( value ) );
|
||||
String indexPath=key!=null ? indexPath( key ): indexPath("null");
|
||||
ClientResponse response = restRequest.get( indexPath + "?query=" + RestRequest.encode( value ) );
|
||||
if ( restRequest.statusIs( response, Response.Status.OK ) ) {
|
||||
Collection hits = (Collection) restRequest.toEntity( response );
|
||||
return new SimpleIndexHits<T>( hits, hits.size() );
|
||||
@@ -107,7 +108,7 @@ public abstract class RestIndex<T extends PropertyContainer> implements Index<T>
|
||||
protected abstract T createEntity( Map<?, ?> item );
|
||||
|
||||
public IndexHits<T> query( Object value ) {
|
||||
throw new UnsupportedOperationException();
|
||||
return query(null,value);
|
||||
}
|
||||
|
||||
private class SimpleIndexHits<T extends PropertyContainer> implements IndexHits<T> {
|
||||
|
||||
@@ -64,6 +64,7 @@ public class Property {
|
||||
}
|
||||
|
||||
public static Map<String, Object> toMap(Property... props) {
|
||||
if (props==null) return null;
|
||||
final HashMap<String, Object> result = new HashMap<String, Object>();
|
||||
for (Property prop : props) {
|
||||
result.put(prop.name,prop.value);
|
||||
|
||||
@@ -297,7 +297,7 @@ public class Neo4jTemplateApiTest {
|
||||
assertNotNull(relationship);
|
||||
assertEquals(referenceNode, relationship.getStartNode());
|
||||
assertEquals(node1,relationship.getEndNode());
|
||||
assertEquals(HAS, relationship.getType());
|
||||
assertEquals(HAS.name(), relationship.getType().name());
|
||||
assertEquals("rel2",relationship.getProperty("name","not set"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user