Skip to content

Commit

Permalink
remove dependency on PropertyContainer
Browse files Browse the repository at this point in the history
 * Created a new interface called DetachedPropertyContainer that is similar to PropertyContainer but decouples from the neo4j embedded database (e.g. no GraphDatabaseService). #1
 * Fixed a test case that depended on the order of a Set.
  • Loading branch information
Daniel Tyreus committed Feb 25, 2014
1 parent e5251eb commit 103daa9
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 40 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@
<comments>GPLv3 was chosen to match the licensing of Neo4J Community Edition</comments>
</license>
</licenses>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/synclab/neo4j/client/DetachedNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
package com.synclab.neo4j.client;

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.PropertyContainer;

/**
* A implementation of {@link org.neo4j.graphdb.PropertyContainer} similar to {@link org.neo4j.graphdb.Node} except
* A implementation of {@link DetachedPropertyContainer} similar to {@link org.neo4j.graphdb.Node} except
* that this version is completely detached from the database. All properties are
* set after the initial query response and nothing is lazily loaded. Writing to
* a DetachedNode will not update the database.
*
* @author pdtyreus
*/
public interface DetachedNode extends PropertyContainer {
public interface DetachedNode extends DetachedPropertyContainer {
public long getId();
public Iterable<Label> getLabels();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2014 P. Daniel Tyreus
*
* This program 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 <http://www.gnu.org/licenses/>.
*/
package com.synclab.neo4j.client;

/**
*
* @author P. Daniel Tyreus
*/
public interface DetachedPropertyContainer {
public boolean hasProperty(String string);
public Object getProperty(String string);
public Object getProperty(String string, Object o);
public Iterable<String> getPropertyKeys();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
*/
package com.synclab.neo4j.client;

import org.neo4j.graphdb.PropertyContainer;

/**
* A implementation of {@link org.neo4j.graphdb.PropertyContainer} similar to {@link org.neo4j.graphdb.Relationship} except
* A implementation of {@link DetachedPropertyContainer} similar to {@link org.neo4j.graphdb.Relationship} except
* that this version is completely detached from the database. All properties are
* set after the initial query response and nothing is lazily loaded. Writing to
* a DetachedRelationship will not update the database.
*
* @author pdtyreus
*/
public interface DetachedRelationship extends PropertyContainer {
public interface DetachedRelationship extends DetachedPropertyContainer {

public long getEndNodeId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.util.Set;

/**
*
* A POJO representation of JSON response using the graph format
* from the neo4j transactional REST endpoint.
*
* @author pdtyreus
*/
public class GraphFormatResponse implements DetachedEntityResponse, BatchDetachedEntityResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.List;

/**
*
* A POJO representation of an element from <code>results</code> JSON array
* in the graph format response from the neo4j transactional REST endpoint.
*
* @author pdtyreus
*/
public class GraphFormatResult {
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/com/synclab/neo4j/client/response/GraphNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.util.HashMap;
import java.util.List;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;

/**
* A read-only implementation {@link org.neo4j.graphdb.PropertyContainer} for deserializing "graph" format responses.
* A POJO representation of an element from the <code>nodes</code> JSON array
* in the graph format response from the neo4j transactional REST endpoint.
*
* @author pdtyreus
*/
Expand All @@ -45,39 +45,33 @@ public GraphNode(@JsonProperty("id") String id, @JsonProperty("labels") List<Str
this.labels = labels;
}

public GraphDatabaseService getGraphDatabase() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasProperty(String string) {
return properties.containsKey(string);
}

@Override
public Object getProperty(String string) {
return properties.get(string);
}

@Override
public Object getProperty(String string, Object o) {
Object ret = properties.get(string);
return (ret == null ? o : ret);
}

public void setProperty(String string, Object o) {
throw new UnsupportedOperationException("These GraphNodes are ready only. Use a Cypher statement to update property values");
}

public Object removeProperty(String string) {
throw new UnsupportedOperationException("These GraphNodes are ready only. Use a Cypher statement to update property values");
}

@Override
public Iterable<String> getPropertyKeys() {
return properties.keySet();
}

@Override
public long getId() {
return Long.parseLong(id);
}

@Override
public Iterable<Label> getLabels() {

List<Label> labelList= new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.synclab.neo4j.client.DetachedRelationship;
import java.util.HashMap;
import org.neo4j.graphdb.GraphDatabaseService;

/**
* A read-only implementation {@link org.neo4j.graphdb.PropertyContainer} for deserializing "graph" format responses.
* A POJO representation of an element from the <code>relationships</code> JSON array
* in the graph format response from the neo4j transactional REST endpoint.
*
* @author pdtyreus
*/
Expand Down Expand Up @@ -51,6 +51,7 @@ public GraphRelationship(@JsonProperty("id") String id,
this.endNode = endNode;
}

@Override
public long getId() {
return Long.parseLong(id);
}
Expand All @@ -59,39 +60,33 @@ public String getType() {
return type;
}

@Override
public long getStartNodeId() {
return Long.parseLong(startNode);
}

@Override
public long getEndNodeId() {
return Long.parseLong(endNode);
}

public GraphDatabaseService getGraphDatabase() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasProperty(String string) {
return properties.containsKey(string);
}

@Override
public Object getProperty(String string) {
return properties.get(string);
}

@Override
public Object getProperty(String string, Object o) {
Object ret = properties.get(string);
return (ret == null ? o : ret);
}

public void setProperty(String string, Object o) {
throw new UnsupportedOperationException("These GraphRelationships are ready only. Use a Cypher statement to update property values");
}

public Object removeProperty(String string) {
throw new UnsupportedOperationException("These GraphRelationships are ready only. Use a Cypher statement to update property values");
}

@Override
public Iterable<String> getPropertyKeys() {
return properties.keySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

import com.synclab.neo4j.client.response.GraphFormatResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.synclab.neo4j.client.DetachedNode;
import java.awt.Label;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import static org.junit.Assert.*;

Expand All @@ -41,8 +45,11 @@ public void testDeserialize() {
assertEquals(3, response.getNodes().get(0).size());
assertEquals(2, response.getRelationships().get(0).size());

Object spokes = response.getNodes().get(0).iterator().next().getProperty("spokes");
assertEquals(3, ((Integer) spokes).intValue());
DetachedNode firstNode = response.getNodes().get(0).iterator().next();
assertNotNull(firstNode);
assertNotNull(firstNode.getLabels());
assertNotNull(firstNode.getPropertyKeys());


} catch (IOException e) {
fail("unable to parse graph format: " + e.getMessage());
Expand Down

0 comments on commit 103daa9

Please sign in to comment.