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 MediaItemRepository {
        public static AtomicLong ID_GENERATOR = new AtomicLong(0);
        private final GraphDatabaseService graphDb;
    private final Index<Node> index;
   
    public MediaItemRepository( 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 MediaItemNode createMediaItem( String itemdescritpion ) throws Exception
    {
        Node mediaitem = graphDb.createNode();
        String id = "_aid_"+generateID();
        Node alreadyExist = index.get( MediaItemNode.ID, id ).getSingle();
        if ( alreadyExist != null )
        {
            throw new Exception( "Album with this title already exists " );
        }
        mediaitem.setProperty( MediaItemNode.DESCRIPTION, itemdescritpion );
        mediaitem.setProperty( MediaItemNode.ID, id );
        index.add( mediaitem, MediaItemNode.ID, id );  
       
        return new MediaItemNode( mediaitem );
    }
   
    public MediaItemNode getMediaItemById( String id )
    {
        Node item = index.get( MediaItemNode.ID, id ).getSingle();
        if ( item == null )
        {
            throw new IllegalArgumentException( "MediaItem[" + id
                    + "] not found" );
        }
        return new MediaItemNode( item );
    }
   
    /**
     * 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");
    }
}