Biblioteca Java - Rev 18

Subversion Repositories:
Rev:
/**
 * Licensed to Neo Technology under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo Technology licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.linkscreens.graphsin.repository;

import java.util.ArrayList;

import org.neo4j.graphdb.Direction;
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 org.neo4j.helpers.collection.IterableWrapper;

public class PersonRepository
{
    private final GraphDatabaseService graphDb;
    private final Index<Node> index;

    public PersonRepository( GraphDatabaseService graphDb, Index<Node> index )
    {
        this.graphDb = graphDb;
        this.index = index;
    }

    public PersonNode createPerson( String name ) throws Exception
    {
        Node newPersonNode = graphDb.createNode();
        Node alreadyExist = index.get( PersonNode.EMAIL, name ).getSingle();
        if ( alreadyExist != null )
        {
            throw new Exception( "Person with this name already exists " );
        }
        newPersonNode.setProperty( PersonNode.EMAIL, name );
        index.add( newPersonNode, PersonNode.EMAIL, name );
        return new PersonNode( newPersonNode );
    }
   

    public PersonNode getPersonByEmail( String name )
    {
        Node personNode = index.get( PersonNode.EMAIL, name ).getSingle();
        if ( personNode == null )
        {
            throw new IllegalArgumentException( "Person[" + name
                    + "] not found" );
        }
        return new PersonNode( personNode );
    }

    public void deletePerson( PersonNode person )
    {
        Node personNode = person.getUnderlyingNode();
        index.remove( personNode, PersonNode.EMAIL, person.getName() );
        for ( PersonNode friend : person.getFriends() )
        {
            person.removeFriend( friend );
        }
        personNode.getSingleRelationship( RelTypes.A_PERSON, Direction.INCOMING ).delete();

        for ( StatusUpdateNode status : person.getStatus() )
        {
            Node statusNode = status.getUnderlyingNode();
            for ( Relationship r : statusNode.getRelationships() )
            {
                r.delete();
            }
            statusNode.delete();
        }

        personNode.delete();
    }

    /**
     * Updated to retrevive all nodes based on index and not based on reference node which is deprecated because of performance
     * issues. See: http://stackoverflow.com/questions/15088616/is-concept-of-reference-node-in-neo4j-still-used-or-deprecated
     * @return
     */

   
    public Iterable<PersonNode> getAllPersons()
    {
       
        IndexHits<Node> all = index.query("*:*");
        ArrayList<PersonNode> list = new ArrayList();
        while(all.hasNext())
                list.add(new PersonNode(all.next()));
        return list;
    }
}