Biblioteca Java - Rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/repository/MediaItemNode.java @ 36
package com.linkscreens.graphsin.repository;
import java.util.Collections;
import java.util.Date;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.helpers.collection.IterableWrapper;
public class MediaItemNode {
public static final String DESCRIPTION = "description";
public static final String ID = "id";
private final Node underlyingNode;
public MediaItemNode( Node album) {
this.underlyingNode = album;
}
private GraphDatabaseService graphDb()
{
return underlyingNode.getGraphDatabase();
}
protected Node getUnderlyingNode()
{
return underlyingNode;
}
public String getId(){
return (String)underlyingNode.getProperty( ID );
}
public String getDescription()
{
return (String)underlyingNode.getProperty( DESCRIPTION );
}
@Override
public int hashCode()
{
return underlyingNode.hashCode();
}
@Override
public boolean equals( Object o )
{
return o instanceof MediaItemNode &&
underlyingNode.equals( ( (MediaItemNode)o ).getUnderlyingNode() );
}
@Override
public String toString()
{
return "MediaItem[" + getDescription() + "]";
}
//managing statuses
private Node createNewCommentNode( String text )
{
Node com = graphDb().createNode();
com.setProperty( StatusUpdateNode.TEXT, text );
com.setProperty( StatusUpdateNode.DATE, new Date().getTime() );
return com;
}
public Iterable<CommentNode> getComments()
{
Relationship firstStatus = underlyingNode.getSingleRelationship(
RelTypes.COMMENT, Direction.OUTGOING );
if ( firstStatus == null )
{
return Collections.emptyList(); //don't have any comment
}
// START SNIPPET: getStatusTraversal
TraversalDescription traversal = graphDb().traversalDescription()
.depthFirst()
.relationships( RelTypes.NEXT_COMMENT );
// END SNIPPET: getStatusTraversal
return new IterableWrapper<CommentNode, Path>(
traversal.traverse( firstStatus.getEndNode() ) )
{
@Override
protected CommentNode underlyingObjectToObject( Path path )
{
return new CommentNode( path.endNode() );
}
};
}
public void addStatus( String text )
{
CommentNode oldStatus;
if ( getComments().iterator().hasNext() )
{
oldStatus = getComments().iterator().next();
} else
{
oldStatus = null;
}
Node newStatus = createNewCommentNode( text );
if ( oldStatus != null )
{
underlyingNode.getSingleRelationship( RelTypes.COMMENT, Direction.OUTGOING ).delete();
newStatus.createRelationshipTo( oldStatus.getUnderlyingNode(), RelTypes.NEXT_COMMENT );
}
underlyingNode.createRelationshipTo( newStatus, RelTypes.COMMENT );
}
}
import java.util.Collections;
import java.util.Date;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.helpers.collection.IterableWrapper;
public class MediaItemNode {
public static final String DESCRIPTION = "description";
public static final String ID = "id";
private final Node underlyingNode;
public MediaItemNode( Node album) {
this.underlyingNode = album;
}
private GraphDatabaseService graphDb()
{
return underlyingNode.getGraphDatabase();
}
protected Node getUnderlyingNode()
{
return underlyingNode;
}
public String getId(){
return (String)underlyingNode.getProperty( ID );
}
public String getDescription()
{
return (String)underlyingNode.getProperty( DESCRIPTION );
}
@Override
public int hashCode()
{
return underlyingNode.hashCode();
}
@Override
public boolean equals( Object o )
{
return o instanceof MediaItemNode &&
underlyingNode.equals( ( (MediaItemNode)o ).getUnderlyingNode() );
}
@Override
public String toString()
{
return "MediaItem[" + getDescription() + "]";
}
//managing statuses
private Node createNewCommentNode( String text )
{
Node com = graphDb().createNode();
com.setProperty( StatusUpdateNode.TEXT, text );
com.setProperty( StatusUpdateNode.DATE, new Date().getTime() );
return com;
}
public Iterable<CommentNode> getComments()
{
Relationship firstStatus = underlyingNode.getSingleRelationship(
RelTypes.COMMENT, Direction.OUTGOING );
if ( firstStatus == null )
{
return Collections.emptyList(); //don't have any comment
}
// START SNIPPET: getStatusTraversal
TraversalDescription traversal = graphDb().traversalDescription()
.depthFirst()
.relationships( RelTypes.NEXT_COMMENT );
// END SNIPPET: getStatusTraversal
return new IterableWrapper<CommentNode, Path>(
traversal.traverse( firstStatus.getEndNode() ) )
{
@Override
protected CommentNode underlyingObjectToObject( Path path )
{
return new CommentNode( path.endNode() );
}
};
}
public void addStatus( String text )
{
CommentNode oldStatus;
if ( getComments().iterator().hasNext() )
{
oldStatus = getComments().iterator().next();
} else
{
oldStatus = null;
}
Node newStatus = createNewCommentNode( text );
if ( oldStatus != null )
{
underlyingNode.getSingleRelationship( RelTypes.COMMENT, Direction.OUTGOING ).delete();
newStatus.createRelationshipTo( oldStatus.getUnderlyingNode(), RelTypes.NEXT_COMMENT );
}
underlyingNode.createRelationshipTo( newStatus, RelTypes.COMMENT );
}
}