Adding Book service interface

This commit is contained in:
Sameera Nelson 2016-05-13 18:57:52 +05:30
parent a317e91d37
commit 6f95540287
3 changed files with 74 additions and 57 deletions

View File

@ -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<String, Object> graph(int limit);
@Autowired
private BookRepository bookRepository;
Book save(Book book);
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);
}
Book findBookById(Long id);
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();
}
}
void deleteAllInGraph();
}

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 BookServiceImpl implements 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();
}
}

View File

@ -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());
}
}