Biblioteca Java - Rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/repository/AlbumNode.java @ 22
package com.linkscreens.graphsin.repository;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.graphdb.traversal.Uniqueness;
import org.neo4j.helpers.collection.IterableWrapper;
public class AlbumNode {
public static final String TITLE = "title";
public static final String ID = "id";
private final Node underlyingNode;
public AlbumNode( 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 getTitle()
{
return (String)underlyingNode.getProperty( TITLE );
}
@Override
public int hashCode()
{
return underlyingNode.hashCode();
}
@Override
public boolean equals( Object o )
{
return o instanceof AlbumNode &&
underlyingNode.equals( ( (AlbumNode)o ).getUnderlyingNode() );
}
@Override
public String toString()
{
return "Album[" + getTitle() + "]";
}
private IterableWrapper<MediaItemNode, Path> createCreateMediaItemsFromPath(
Traverser iterableToWrap )
{
return new IterableWrapper<MediaItemNode, Path>( iterableToWrap )
{
@Override
protected MediaItemNode underlyingObjectToObject( Path path )
{
return new MediaItemNode( path.endNode() );
}
};
}
public void addMediaItemInAlbum(MediaItemNode item){
underlyingNode.createRelationshipTo( item.getUnderlyingNode(), RelTypes.HAS_MEDIAITEM );
}
public Iterable<MediaItemNode> getMediaItems(){
TraversalDescription travDesc = graphDb().traversalDescription()
.breadthFirst()
.relationships( RelTypes.HAS_MEDIAITEM )
.uniqueness( Uniqueness.NODE_GLOBAL )
.evaluator( Evaluators.toDepth( 1 ) )
.evaluator( Evaluators.excludeStartPosition() );
return createCreateMediaItemsFromPath( travDesc.traverse( underlyingNode ) );
}
}
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.graphdb.traversal.Uniqueness;
import org.neo4j.helpers.collection.IterableWrapper;
public class AlbumNode {
public static final String TITLE = "title";
public static final String ID = "id";
private final Node underlyingNode;
public AlbumNode( 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 getTitle()
{
return (String)underlyingNode.getProperty( TITLE );
}
@Override
public int hashCode()
{
return underlyingNode.hashCode();
}
@Override
public boolean equals( Object o )
{
return o instanceof AlbumNode &&
underlyingNode.equals( ( (AlbumNode)o ).getUnderlyingNode() );
}
@Override
public String toString()
{
return "Album[" + getTitle() + "]";
}
private IterableWrapper<MediaItemNode, Path> createCreateMediaItemsFromPath(
Traverser iterableToWrap )
{
return new IterableWrapper<MediaItemNode, Path>( iterableToWrap )
{
@Override
protected MediaItemNode underlyingObjectToObject( Path path )
{
return new MediaItemNode( path.endNode() );
}
};
}
public void addMediaItemInAlbum(MediaItemNode item){
underlyingNode.createRelationshipTo( item.getUnderlyingNode(), RelTypes.HAS_MEDIAITEM );
}
public Iterable<MediaItemNode> getMediaItems(){
TraversalDescription travDesc = graphDb().traversalDescription()
.breadthFirst()
.relationships( RelTypes.HAS_MEDIAITEM )
.uniqueness( Uniqueness.NODE_GLOBAL )
.evaluator( Evaluators.toDepth( 1 ) )
.evaluator( Evaluators.excludeStartPosition() );
return createCreateMediaItemsFromPath( travDesc.traverse( underlyingNode ) );
}
}