Adding example project

This commit is contained in:
Sameera Nelson 2016-05-09 00:29:00 +05:30
parent b0b3a20749
commit 7d38405380
5 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.baeldung.spring.data.neo4j;
import com.baeldung.spring.data.neo4j.config.LibraryNeo4jConfiguration;
import com.baeldung.spring.data.neo4j.model.Book;
import com.baeldung.spring.data.neo4j.model.Person;
import com.baeldung.spring.data.neo4j.service.BookService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LibraryNeo4jConfiguration.class)
public class BookServiceTest {
private static final Log LOGGER = LogFactory.getLog(BookServiceTest.class);
@Autowired
private BookService bookService;
@Test
public void testSaveBook() {
final Person author1 = new Person();
author1.setName("Mark Twain");
author1.setBorn(1835);
final Book book = new Book();
book.setTitle("The Adventures of Tom Sawyer");
book.setReleased(1876);
book.setPerson(author1);
bookService.save(book);
final Book savedBook = bookService.findBookById(book.getId());
assertEquals(book.getTitle(), savedBook.getTitle());
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.data.neo4j.config;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.data.neo4j.server.RemoteServer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@ComponentScan("com.baeldung.spring.data.neo4j")
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
public class LibraryNeo4jConfiguration extends Neo4jConfiguration {
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";
@Override
public Neo4jServer neo4jServer() {
return new RemoteServer(URL,"neo4j","password");
}
@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("com.baeldung.spring.data.neo4j.model");
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.spring.data.neo4j.model;
import org.neo4j.graphdb.Direction;
import org.neo4j.ogm.annotation.*;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@NodeEntity
public class Book {
private static final AtomicLong TS = new AtomicLong();
@GraphId
private Long id;
private String title;
private int released;
@Relationship(type="AUTHORED_BY", direction = Relationship.INCOMING)
private Person person;
public Book() {
this.id = TS.getAndIncrement();
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getReleased() {
return released;
}
public void setReleased(int released) {
this.released = released;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.data.neo4j.repostory;
import com.baeldung.spring.data.neo4j.model.Book;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Repository
public interface BookRepository extends GraphRepository<Book> {
Book findByTitle(@Param("title") String title);
@Query("MATCH (m:Book) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
Collection<Book> findByTitleContaining(@Param("title") String title);
@Query("MATCH (m:Book)<-[:ACTED_IN]-(a:Person) RETURN m.title as Book, collect(a.name) as cast LIMIT {limit}")
List<Map<String,Object>> graph(@Param("limit") int limit);
}

View File

@ -0,0 +1,62 @@
package com.baeldung.spring.data.neo4j.service;
import com.baeldung.spring.data.neo4j.model.Book;
import com.baeldung.spring.data.neo4j.repostory.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
private Map<String, Object> toD3Format(final Iterator<Map<String, Object>> result) {
List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> rels= new ArrayList<Map<String,Object>>();
int i=0;
while (result.hasNext()) {
Map<String, Object> row = result.next();
nodes.add(map("title",row.get("book"),"label","book"));
int target=i;
i++;
for (Object name : (Collection) row.get("cast")) {
Map<String, Object> actor = map("title", name,"label","actor");
int source = nodes.indexOf(actor);
if (source == -1) {
nodes.add(actor);
source = i++;
}
rels.add(map("source",source,"target",target));
}
}
return map("nodes", nodes, "links", rels);
}
private Map<String, Object> map(final String key1, final Object value1, final String key2, final Object value2) {
Map<String, Object> result = new HashMap<String,Object>(2);
result.put(key1,value1);
result.put(key2,value2);
return result;
}
public Map<String, Object> graph(final int limit) {
Iterator<Map<String, Object>> result = bookRepository.graph(limit).iterator();
return toD3Format(result);
}
public Book save(final Book book){
return bookRepository.save(book);
}
public Book findBookById(final Long id){
return bookRepository.findOne(id);
}
public void deleteAllInGraph(){
bookRepository.deleteAll();
}
}