Biblioteca Java - Rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/network/SocialNetworkTestUtils.java @ 29
package com.linkscreens.graphsin.network;
import java.util.ArrayList;
import java.util.Random;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.IteratorUtil;
import com.linkscreens.graphsin.repository.AlbumNode;
import com.linkscreens.graphsin.repository.AlbumRepository;
import com.linkscreens.graphsin.repository.MediaItemNode;
import com.linkscreens.graphsin.repository.MediaItemRepository;
import com.linkscreens.graphsin.repository.PersonNode;
import com.linkscreens.graphsin.repository.PersonRepository;
public class SocialNetworkTestUtils {
private static final Random r = new Random( System.currentTimeMillis() );
private static final int nrOfPersons = 10;
private static final int nrOffFriends = 3;
private static final int nrOfPosts = 5;
private static final int nrOfAlbums = 5;
private static final int nrOfMediaItems = 4;
private static final int nrOfComments = 3;
GraphDatabaseService graphDb;
PersonRepository personRepository;
AlbumRepository albumRepository;
MediaItemRepository mediaRepository;
void populate(SocialNetworkServiceImpl net) throws Exception{
this.graphDb = net.graphDb;
this.personRepository = net.personRepository;
this.albumRepository = net.albumRepository;
this.mediaRepository = net.mediaRepository;
try ( Transaction tx = net.graphDb.beginTx() )
{
createPersons();
setupFriendsBetweenPeople( nrOffFriends );
multipleStatusesGenerator();
createAlbums();
tx.success();
}
}
//GRAPH GENERATOR
void createAlbums() throws Exception{
System.out.println("Create albums");
//iterate all persons
for ( PersonNode person : personRepository.getAllPersons() )
{
int albumsCount = r.nextInt( nrOfAlbums ) + 1;
//add a random number of albums to user
for ( int j = 0; j < albumsCount; j++ )
{
AlbumNode a = albumRepository.createAlbum(person.getName()+" album "+j);
person.addAlbum(a); //add album to peson
//for each album add a number of media items and for each item a number of comments
for ( int k = 0; k < nrOfMediaItems; k++ ){
MediaItemNode item = mediaRepository.createMediaItem(person.getName()+a.getTitle()+" item "+k);
a.addMediaItemInAlbum(item);
ArrayList<String> comments = new ArrayList<>();
//create some comments
for(int i=0;i<nrOfComments;i++){
comments.add( "Comment msg "+i );
}
for(String text:comments){
item.addStatus(text);
}
}
System.out.println("Add item for"+person.getName()+" in album "+a.getTitle());
}
}
}
void setupFriendsBetweenPeople( int maxNrOfFriendsEach )
{
for ( PersonNode person : personRepository.getAllPersons() )
{
int nrOfFriends = r.nextInt( maxNrOfFriendsEach ) + 1;
for ( int j = 0; j < nrOfFriends; j++ )
{
person.addFriend( getRandomPerson() );
}
}
}
PersonNode getRandomPerson()
{
return personRepository.getPersonByEmail( "person#"
+ r.nextInt( nrOfPersons ) );
}
void deleteSocialGraph()
{
try ( Transaction tx = graphDb.beginTx() )
{
for ( PersonNode person : personRepository.getAllPersons() )
{
personRepository.deletePerson( person );
}
}
}
PersonNode getRandomFriendOf( PersonNode p )
{
ArrayList<PersonNode> friends = new ArrayList<>();
IteratorUtil.addToCollection( p.getFriends().iterator(), friends );
return friends.get( r.nextInt( friends.size() ) );
}
String getRandomPersonWithFriends()
{
PersonNode p;
try ( Transaction tx = graphDb.beginTx() ){
do
{
p = getRandomPerson();
}
while ( p.getNrOfFriends() == 0 );
return p.getName();
}
}
void createPersons() throws Exception
{
for ( int i = 0; i < nrOfPersons; i++ )
{
personRepository.createPerson( "person#" + i );
}
}
void multipleStatusesGenerator() throws Exception
{
ArrayList<String> statuses = new ArrayList<>();
for(int i=0;i<r.nextInt( nrOfPosts );i++){
statuses.add( "Test msg "+i );
}
for(int i=0;i<r.nextInt( nrOfPersons );i++){
PersonNode person = getRandomPerson();
for ( String status : statuses )
{
person.addStatus( status );
}
}
}
}
import java.util.ArrayList;
import java.util.Random;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.IteratorUtil;
import com.linkscreens.graphsin.repository.AlbumNode;
import com.linkscreens.graphsin.repository.AlbumRepository;
import com.linkscreens.graphsin.repository.MediaItemNode;
import com.linkscreens.graphsin.repository.MediaItemRepository;
import com.linkscreens.graphsin.repository.PersonNode;
import com.linkscreens.graphsin.repository.PersonRepository;
public class SocialNetworkTestUtils {
private static final Random r = new Random( System.currentTimeMillis() );
private static final int nrOfPersons = 10;
private static final int nrOffFriends = 3;
private static final int nrOfPosts = 5;
private static final int nrOfAlbums = 5;
private static final int nrOfMediaItems = 4;
private static final int nrOfComments = 3;
GraphDatabaseService graphDb;
PersonRepository personRepository;
AlbumRepository albumRepository;
MediaItemRepository mediaRepository;
void populate(SocialNetworkServiceImpl net) throws Exception{
this.graphDb = net.graphDb;
this.personRepository = net.personRepository;
this.albumRepository = net.albumRepository;
this.mediaRepository = net.mediaRepository;
try ( Transaction tx = net.graphDb.beginTx() )
{
createPersons();
setupFriendsBetweenPeople( nrOffFriends );
multipleStatusesGenerator();
createAlbums();
tx.success();
}
}
//GRAPH GENERATOR
void createAlbums() throws Exception{
System.out.println("Create albums");
//iterate all persons
for ( PersonNode person : personRepository.getAllPersons() )
{
int albumsCount = r.nextInt( nrOfAlbums ) + 1;
//add a random number of albums to user
for ( int j = 0; j < albumsCount; j++ )
{
AlbumNode a = albumRepository.createAlbum(person.getName()+" album "+j);
person.addAlbum(a); //add album to peson
//for each album add a number of media items and for each item a number of comments
for ( int k = 0; k < nrOfMediaItems; k++ ){
MediaItemNode item = mediaRepository.createMediaItem(person.getName()+a.getTitle()+" item "+k);
a.addMediaItemInAlbum(item);
ArrayList<String> comments = new ArrayList<>();
//create some comments
for(int i=0;i<nrOfComments;i++){
comments.add( "Comment msg "+i );
}
for(String text:comments){
item.addStatus(text);
}
}
System.out.println("Add item for"+person.getName()+" in album "+a.getTitle());
}
}
}
void setupFriendsBetweenPeople( int maxNrOfFriendsEach )
{
for ( PersonNode person : personRepository.getAllPersons() )
{
int nrOfFriends = r.nextInt( maxNrOfFriendsEach ) + 1;
for ( int j = 0; j < nrOfFriends; j++ )
{
person.addFriend( getRandomPerson() );
}
}
}
PersonNode getRandomPerson()
{
return personRepository.getPersonByEmail( "person#"
+ r.nextInt( nrOfPersons ) );
}
void deleteSocialGraph()
{
try ( Transaction tx = graphDb.beginTx() )
{
for ( PersonNode person : personRepository.getAllPersons() )
{
personRepository.deletePerson( person );
}
}
}
PersonNode getRandomFriendOf( PersonNode p )
{
ArrayList<PersonNode> friends = new ArrayList<>();
IteratorUtil.addToCollection( p.getFriends().iterator(), friends );
return friends.get( r.nextInt( friends.size() ) );
}
String getRandomPersonWithFriends()
{
PersonNode p;
try ( Transaction tx = graphDb.beginTx() ){
do
{
p = getRandomPerson();
}
while ( p.getNrOfFriends() == 0 );
return p.getName();
}
}
void createPersons() throws Exception
{
for ( int i = 0; i < nrOfPersons; i++ )
{
personRepository.createPerson( "person#" + i );
}
}
void multipleStatusesGenerator() throws Exception
{
ArrayList<String> statuses = new ArrayList<>();
for(int i=0;i<r.nextInt( nrOfPosts );i++){
statuses.add( "Test msg "+i );
}
for(int i=0;i<r.nextInt( nrOfPersons );i++){
PersonNode person = getRandomPerson();
for ( String status : statuses )
{
person.addStatus( status );
}
}
}
}