Biblioteca Java - Rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/network/SocialNetworkServiceImpl.java @ 31
package com.linkscreens.graphsin.network;
import java.util.ArrayList;
import java.util.Iterator;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.helpers.collection.IteratorUtil;
import com.linkscreens.graphsin.model.*;
import com.linkscreens.graphsin.repository.AlbumNode;
import com.linkscreens.graphsin.repository.AlbumRepository;
import com.linkscreens.graphsin.repository.CommentNode;
import com.linkscreens.graphsin.repository.MediaItemNode;
import com.linkscreens.graphsin.repository.MediaItemRepository;
import com.linkscreens.graphsin.repository.PersonNode;
import com.linkscreens.graphsin.repository.PersonRepository;
import com.linkscreens.graphsin.repository.StatusUpdateNode;
public class SocialNetworkServiceImpl implements SocialNetworkService {
GraphDatabaseService graphDb;
PersonRepository personRepository;
AlbumRepository albumRepository;
MediaItemRepository mediaRepository;
public SocialNetworkServiceImpl(String src_dir) throws Exception {
setup(src_dir);
}
private void setup(String storeDir) throws Exception
{
this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir );
try ( Transaction tx = graphDb.beginTx() )
{
Index<Node> index = graphDb.index().forNodes( "nodes" );
personRepository = new PersonRepository( graphDb, index );
Index<Node> indexA = graphDb.index().forNodes( "albums" );
albumRepository = new AlbumRepository(graphDb, indexA);
Index<Node> indexM = graphDb.index().forNodes( "mediaitems" );
mediaRepository = new MediaItemRepository(graphDb, indexM);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#addStatus(java.lang.String, java.lang.String)
*/
@Override
public void addStatus(String name, String text){
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = personRepository.getPersonByEmail(name);
p.addStatus(text);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#createFriend(java.lang.String, java.lang.String)
*/
@Override
public void createFriend(String name1, String name2){
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p1 = this.personRepository.getPersonByEmail(name1);
PersonNode p2 = this.personRepository.getPersonByEmail(name2);
p1.addFriend(p2);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getFriendRecommandation(java.lang.String)
*/
@Override
public Person getFriendRecommandation(String name){
Person reco = null;
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = this.personRepository.getPersonByEmail(name);
PersonNode recommendation = IteratorUtil.single( p.getFriendRecommendation( 1 ).iterator() );
reco = new Person(recommendation.getName());
}
return reco;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getFriendsOf(java.lang.String)
*/
@Override
public ArrayList<Person> getFriendsOf(String name){
ArrayList<Person> friends = new ArrayList<>();
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = personRepository.getPersonByEmail(name);
for (Iterator iterator = p.getFriends().iterator(); iterator.hasNext();) {
PersonNode person = (PersonNode) iterator.next();
friends.add(new Person(person.getName()));
}
}
return friends;
}
@Override
public Person getPersonByEMail(String email) {
Person p = null;
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode node = personRepository.getPersonByEmail(email);
p = new Person(node.getName());
}
return p;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getAllPersons()
*/
@Override
public ArrayList<Person> getAllPersons(){
ArrayList<Person> persons = new ArrayList<>();
try ( Transaction tx = graphDb.beginTx() )
{
for (Iterator iterator =personRepository.getAllPersons().iterator(); iterator.hasNext();) {
PersonNode person = (PersonNode) iterator.next();
persons.add(new Person(person.getName()));
}
}
return persons;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#retrieveStatusUpdates(java.lang.String)
*/
@Override
public ArrayList<StatusUpdate> retrieveFriendsStatusUpdates(String name) throws Exception
{
PersonNode person;
ArrayList<StatusUpdate> updates = new ArrayList<StatusUpdate>();
try ( Transaction tx = graphDb.beginTx() )
{
person = this.personRepository.getPersonByEmail(name);
Iterator<StatusUpdateNode> i = person.friendStatuses();
while(i.hasNext()){
StatusUpdateNode su = i.next();
//updates.add(su.getPerson().getName()+":+"+su.getDate().toString()+":"+su.getStatusText());
updates.add(
new StatusUpdate(su.getStatusText(), su.getDate())
);
}
}
return updates;
}
public ArrayList<StatusUpdate> retreivePersonStatusUpdates(String email){
ArrayList<StatusUpdate> list = new ArrayList<StatusUpdate>();
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode person = personRepository.getPersonByEmail(email);
Iterator<StatusUpdateNode> i = person.getStatus().iterator();
while(i.hasNext()){
StatusUpdateNode su = i.next();
StatusUpdate u = new StatusUpdate(su.getStatusText(), su.getDate());
list.add(u);
}
tx.success();
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#retreivePersonMediaAlbums(java.lang.String)
*/
@Override
public ArrayList<Album> retreivePersonMediaAlbums(String email){
PersonNode person;
ArrayList<Album> list = new ArrayList<Album>();
try ( Transaction tx = graphDb.beginTx() )
{
person = this.personRepository.getPersonByEmail(email);
Iterator<AlbumNode> i = person.getAlbums().iterator();
while(i.hasNext()){
AlbumNode su = i.next();
list.add(new Album(su.getId(),su.getTitle()));
}
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItems(java.lang.String)
*/
@Override
public ArrayList<MediaItem> getMediaItems(String albumId){
ArrayList<MediaItem> list = new ArrayList();
try ( Transaction tx = graphDb.beginTx() )
{
AlbumNode album = albumRepository.getAlbumById(albumId);
Iterator<MediaItemNode> i = album.getMediaItems().iterator();
while(i.hasNext()){
MediaItemNode item = i.next();
list.add(new MediaItem(item.getId(),item.getDescription()));
}
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItemComments(java.lang.String)
*/
@Override
public ArrayList<Comment> getMediaItemComments(String itemId){
ArrayList<Comment> list = new ArrayList();
try ( Transaction tx = graphDb.beginTx() )
{
MediaItemNode item = this.mediaRepository.getMediaItemById(itemId);
Iterator<CommentNode> i = item.getComments().iterator();
while(i.hasNext()){
CommentNode c = i.next();
list.add(new Comment(c.getCommentText(),c.getDate()));
}
}
return list;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.helpers.collection.IteratorUtil;
import com.linkscreens.graphsin.model.*;
import com.linkscreens.graphsin.repository.AlbumNode;
import com.linkscreens.graphsin.repository.AlbumRepository;
import com.linkscreens.graphsin.repository.CommentNode;
import com.linkscreens.graphsin.repository.MediaItemNode;
import com.linkscreens.graphsin.repository.MediaItemRepository;
import com.linkscreens.graphsin.repository.PersonNode;
import com.linkscreens.graphsin.repository.PersonRepository;
import com.linkscreens.graphsin.repository.StatusUpdateNode;
public class SocialNetworkServiceImpl implements SocialNetworkService {
GraphDatabaseService graphDb;
PersonRepository personRepository;
AlbumRepository albumRepository;
MediaItemRepository mediaRepository;
public SocialNetworkServiceImpl(String src_dir) throws Exception {
setup(src_dir);
}
private void setup(String storeDir) throws Exception
{
this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir );
try ( Transaction tx = graphDb.beginTx() )
{
Index<Node> index = graphDb.index().forNodes( "nodes" );
personRepository = new PersonRepository( graphDb, index );
Index<Node> indexA = graphDb.index().forNodes( "albums" );
albumRepository = new AlbumRepository(graphDb, indexA);
Index<Node> indexM = graphDb.index().forNodes( "mediaitems" );
mediaRepository = new MediaItemRepository(graphDb, indexM);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#addStatus(java.lang.String, java.lang.String)
*/
@Override
public void addStatus(String name, String text){
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = personRepository.getPersonByEmail(name);
p.addStatus(text);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#createFriend(java.lang.String, java.lang.String)
*/
@Override
public void createFriend(String name1, String name2){
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p1 = this.personRepository.getPersonByEmail(name1);
PersonNode p2 = this.personRepository.getPersonByEmail(name2);
p1.addFriend(p2);
}
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getFriendRecommandation(java.lang.String)
*/
@Override
public Person getFriendRecommandation(String name){
Person reco = null;
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = this.personRepository.getPersonByEmail(name);
PersonNode recommendation = IteratorUtil.single( p.getFriendRecommendation( 1 ).iterator() );
reco = new Person(recommendation.getName());
}
return reco;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getFriendsOf(java.lang.String)
*/
@Override
public ArrayList<Person> getFriendsOf(String name){
ArrayList<Person> friends = new ArrayList<>();
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode p = personRepository.getPersonByEmail(name);
for (Iterator iterator = p.getFriends().iterator(); iterator.hasNext();) {
PersonNode person = (PersonNode) iterator.next();
friends.add(new Person(person.getName()));
}
}
return friends;
}
@Override
public Person getPersonByEMail(String email) {
Person p = null;
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode node = personRepository.getPersonByEmail(email);
p = new Person(node.getName());
}
return p;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getAllPersons()
*/
@Override
public ArrayList<Person> getAllPersons(){
ArrayList<Person> persons = new ArrayList<>();
try ( Transaction tx = graphDb.beginTx() )
{
for (Iterator iterator =personRepository.getAllPersons().iterator(); iterator.hasNext();) {
PersonNode person = (PersonNode) iterator.next();
persons.add(new Person(person.getName()));
}
}
return persons;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#retrieveStatusUpdates(java.lang.String)
*/
@Override
public ArrayList<StatusUpdate> retrieveFriendsStatusUpdates(String name) throws Exception
{
PersonNode person;
ArrayList<StatusUpdate> updates = new ArrayList<StatusUpdate>();
try ( Transaction tx = graphDb.beginTx() )
{
person = this.personRepository.getPersonByEmail(name);
Iterator<StatusUpdateNode> i = person.friendStatuses();
while(i.hasNext()){
StatusUpdateNode su = i.next();
//updates.add(su.getPerson().getName()+":+"+su.getDate().toString()+":"+su.getStatusText());
updates.add(
new StatusUpdate(su.getStatusText(), su.getDate())
);
}
}
return updates;
}
public ArrayList<StatusUpdate> retreivePersonStatusUpdates(String email){
ArrayList<StatusUpdate> list = new ArrayList<StatusUpdate>();
try ( Transaction tx = graphDb.beginTx() )
{
PersonNode person = personRepository.getPersonByEmail(email);
Iterator<StatusUpdateNode> i = person.getStatus().iterator();
while(i.hasNext()){
StatusUpdateNode su = i.next();
StatusUpdate u = new StatusUpdate(su.getStatusText(), su.getDate());
list.add(u);
}
tx.success();
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#retreivePersonMediaAlbums(java.lang.String)
*/
@Override
public ArrayList<Album> retreivePersonMediaAlbums(String email){
PersonNode person;
ArrayList<Album> list = new ArrayList<Album>();
try ( Transaction tx = graphDb.beginTx() )
{
person = this.personRepository.getPersonByEmail(email);
Iterator<AlbumNode> i = person.getAlbums().iterator();
while(i.hasNext()){
AlbumNode su = i.next();
list.add(new Album(su.getId(),su.getTitle()));
}
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItems(java.lang.String)
*/
@Override
public ArrayList<MediaItem> getMediaItems(String albumId){
ArrayList<MediaItem> list = new ArrayList();
try ( Transaction tx = graphDb.beginTx() )
{
AlbumNode album = albumRepository.getAlbumById(albumId);
Iterator<MediaItemNode> i = album.getMediaItems().iterator();
while(i.hasNext()){
MediaItemNode item = i.next();
list.add(new MediaItem(item.getId(),item.getDescription()));
}
}
return list;
}
/* (non-Javadoc)
* @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItemComments(java.lang.String)
*/
@Override
public ArrayList<Comment> getMediaItemComments(String itemId){
ArrayList<Comment> list = new ArrayList();
try ( Transaction tx = graphDb.beginTx() )
{
MediaItemNode item = this.mediaRepository.getMediaItemById(itemId);
Iterator<CommentNode> i = item.getComments().iterator();
while(i.hasNext()){
CommentNode c = i.next();
list.add(new Comment(c.getCommentText(),c.getDate()));
}
}
return list;
}
}