diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java index 52323f689b..f8983dfd16 100644 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java @@ -1,62 +1,18 @@ 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.*; +import java.util.Map; -@Service -public class BookService { +/** + * Created by SDN on 5/13/2016. + */ +public interface BookService { + Map graph(int limit); - @Autowired - private BookRepository bookRepository; + Book save(Book book); - private Map toD3Format(final Iterator> result) { - List> nodes = new ArrayList>(); - List> rels= new ArrayList>(); - int i=0; - while (result.hasNext()) { - Map row = result.next(); - nodes.add(map("title",row.get("book"),"label","book")); - int target=i; - i++; - for (Object name : (Collection) row.get("cast")) { - Map 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); - } + Book findBookById(Long id); - private Map map(final String key1, final Object value1, final String key2, final Object value2) { - Map result = new HashMap(2); - result.put(key1,value1); - result.put(key2,value2); - return result; - } - - public Map graph(final int limit) { - Iterator> 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(); - } -} \ No newline at end of file + void deleteAllInGraph(); +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java new file mode 100644 index 0000000000..cd50f8b740 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java @@ -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 BookServiceImpl implements BookService { + + @Autowired + private BookRepository bookRepository; + + private Map toD3Format(final Iterator> result) { + List> nodes = new ArrayList>(); + List> rels= new ArrayList>(); + int i=0; + while (result.hasNext()) { + Map row = result.next(); + nodes.add(map("title",row.get("book"),"label","book")); + int target=i; + i++; + for (Object name : (Collection) row.get("cast")) { + Map 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 map(final String key1, final Object value1, final String key2, final Object value2) { + Map result = new HashMap(2); + result.put(key1,value1); + result.put(key2,value2); + return result; + } + + public Map graph(final int limit) { + Iterator> 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(); + } +} \ No newline at end of file diff --git a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java index 100465838e..9a791f87da 100644 --- a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java +++ b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java @@ -21,7 +21,7 @@ public class BookServiceTest { private static final Log LOGGER = LogFactory.getLog(BookServiceTest.class); @Autowired - private BookService bookService; + private BookService bookServiceImpl; @Test public void testSaveBook() { @@ -33,8 +33,7 @@ public class BookServiceTest { book.setReleased(1876); book.setPerson(author1); - bookService.save(book); - final Book savedBook = bookService.findBookById(book.getId()); + final Book savedBook = bookServiceImpl.save(book); assertEquals(book.getTitle(), savedBook.getTitle()); } }