Biblioteca Java - Rev 18

Subversion Repositories:
Rev:
package com.linkscreens.graphsin.repository;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicLong;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;

import scala.NotImplementedError;

public class AlbumRepository {
        public static AtomicLong ID_GENERATOR = new AtomicLong(0);
        private final GraphDatabaseService graphDb;
    private final Index<Node> index;
   
    public AlbumRepository( GraphDatabaseService graphDb, Index<Node> index )
    {
        this.graphDb = graphDb;
        this.index = index;
    }
   
    //temporary fast way to generate a unique id for album
    private synchronized long generateID(){
        return  ID_GENERATOR.getAndAdd(1);
    }
   
    public AlbumNode createAlbum( String albumName ) throws Exception
    {
        Node newAlbum = graphDb.createNode();
        String id = "_aid_"+generateID();
        Node alreadyExist = index.get( AlbumNode.ID, id ).getSingle();
        if ( alreadyExist != null )
        {
            throw new Exception( "Album with this title already exists " );
        }
        newAlbum.setProperty( AlbumNode.TITLE, albumName );
        newAlbum.setProperty( AlbumNode.ID, id );
        index.add( newAlbum, AlbumNode.ID, id );  
       
        return new AlbumNode( newAlbum );
    }
   
    public AlbumNode getAlbumById( String id )
    {
        Node albumNode = index.get( AlbumNode.ID, id ).getSingle();
        if ( albumNode == null )
        {
            throw new IllegalArgumentException( "Album[" + id
                    + "] not found" );
        }
        return new AlbumNode( albumNode );
    }
   
    /**
     * TBD need to be implemented. delete album and all contained media items
     * @param person
     */

    public void deleteAlbum( AlbumNode album )
    {
        throw new NotImplementedError("To be implemented");
    }
   
    public Iterable<PersonNode> getAllPersonAlbums(PersonNode p)
    {
       
        throw new NotImplementedError("To be implemented");
    }

       
}