Search⌘ K
AI Features

Develop the Business Service Tier

Explore how to develop the business service tier by implementing core operations such as find, create, edit, and remove in a one-to-one unidirectional relationship. Understand the role of mappers converting between entities and data access objects, and learn how the service manages identifiers internally without exposing them to the user interface.

We will begin working with the business service tier, seeing as the data access tier has been developed and is ready for integration.

“Find all records” operation

First, let’s look at the test for the find all records operation. Since this is the first function being tested, the other operations are still not developed and only the find all records operation can be tested with zero records returned when the table is empty.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class BookServiceImplTest {
@Autowired
private BookService service ;
@Test
public void testFindAll() {
Assert.assertEquals(0L, service.findAll().size());
}
}

Look at how to code the matching method in the business service. The data access tier uses entities, and the business service tier uses data access objects. So there exists a mapper that transforms a data transfer object to an entity and vice-versa.

Java
@Service
@Transactional
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Autowired
private BookMapper mapper;
@Override
public List<BookDto> findAll() {
List<Book> books = dao.getAll();
List<BookDto> bookDtos = new ArrayList<BookDto>();
for(Book book : books){
bookDtos.add(mapper.mapEntityToDto(book));
}
return bookDtos;
}
}

Mappers

The mapper typically has two significant operations, that transform a data access object to its equivalent entity and vice-versa. One thing to observe here is that the Shipping identifier is created every time the Book is updated or added. As we have mentioned before, the management of the Shipping entity identifier will be done internally by the service. The user interface team will only pass the data values, like city field, in the Shipping entity, but is not bothered about the identifier of the Shipping entity.

Java
@Component
public class BookMapper {
public Book mapDtoToEntity(BookDto bookDto){
Book book = new Book();
Shipping shipping = new Shipping();
if(null!=bookDto.getId() book.setId(bookDto.getId());
if(null!=bookDto.getName()) book.setName(bookDto.getName());
if(null!=bookDto.getCity()){ shipping.setCity(bookDto.getCity()); book.setShipping(shipping);}
return book;
}
public BookDto mapEntityToDto(Book book){
BookDto bookDto = new BookDto();
if(null!=book.getId()) bookDto.setId(book.getId());
if(null!=book.getName()) bookDto.setName(book.getName());
if(null!=book.getShipping()){
if(null !=book.getShipping().getCity()){
bookDto.setCity(book.getShipping().getCity());
}
}
return bookDto;
}
}

Create operation

We will discuss the create test operation next. This creates an instance of the entity and checks if the count is one with the fetch all records operation.

Java
public class BookServiceImplTest {
@Autowired
private BookService service ;
@Test
public void testCreate() {
BookDto bookDto = new BookDto();
bookDto.setName("Java SE");
bookDto.setCity("Delhi");
service.create(bookDto);
Assert.assertEquals(1L, service.findAll().size());
}
}

The create method in the business service tier accepts a data access object, converts it to the equivalent entity, and then delegates to the data access tier to save it to the database.

Java
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Autowired
private BookMapper mapper;
@Override
public void create(BookDto bookDto) {
dao.insert(mapper.mapDtoToEntity(bookDto));
}
}

Row identifier operation

We will define the following test operation as finding a row with a given identifier.

Java
public class BookServiceImplTest {
@Autowired
private BookService service ;
@Test
public void testFindById() {
BookDto bookDto = new BookDto();
bookDto.setName("Java SE");
bookDto.setCity("Delhi");
service.create(bookDto);
List<BookDto> bookDtos = service.findAll();
BookDto bDto = bookDtos.get(0);
BookDto bDto2 = service.findById(bDto.getId());
Assert.assertEquals("Java SE", bDto2.getName());
Assert.assertEquals("Delhi", bDto2.getCity());
}
}

The similar business service operation to find a row with a given identifier will now be discussed. Yet again the same order is followed, contact the data access tier and converse with the mapper.

Java
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Autowired
private BookMapper mapper;
@Override
public BookDto findById(int id) {
Book book = dao.getById(id);
if(null !=book){
return mapper.mapEntityToDto(book);
}
return null;
}
}

Remove operation

The remove test operation is next which accepts a given identifier and removes the equivalent row from the database. The test method first creates one instance, removes the same instance, and checks to see if there are zero results in the database.

Java
public class BookServiceImplTest {
@Autowired
private BookService service ;
@Test
public void testRemove() {
BookDto bookDto = new BookDto();
bookDto.setName("Java SE");
bookDto.setCity("Delhi");
service.create(bookDto);
Assert.assertEquals(1L, service.findAll().size());
List<BookDto> bookDtos = service.findAll();
BookDto bDto = bookDtos.get(0);
service.remove(bDto.getId());
Assert.assertEquals(0L, service.findAll().size());
}
}

The equivalent business service method is defined afterwards, which accepts an identifier and removes the matching instance from the database through the data access tier operation.

Java
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Autowired
private BookMapper mapper;
@Override
public void remove(int id) {
Book book = dao.getById(id);
dao.delete(book);
}
}

Edit operation

The last test operation is the edit which creates an instance, edits the city, and then checks if the edit was effective.

Java
public class BookServiceImplTest {
@Autowired
private BookService service ;
@Test
public void testEdit() {
BookDto bookDto = new BookDto();
bookDto.setName("Java SE");
bookDto.setCity("Delhi");
service.create(bookDto);
Assert.assertEquals(1L, service.findAll().size());
List<BookDto> bookDtos2 = service.findAll();
BookDto bDto2 = bookDtos2.get(0);
bDto2.setCity("Toronto");
service.edit(bDto2);
List<BookDto> bookDtos = service.findAll();
BookDto bDto = bookDtos.get(0);
Assert.assertEquals("Toronto", bDto.getCity());
}
}

The equivalent operation of the business service is the edit operation which makes the call to the data access update method after talking with the mapper method. Please note that the edit operation does not have the identifier of the Shipping entity and generates a new instance.

Java
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
@Autowired
private BookMapper mapper;
@Override
public void edit(BookDto bookDto) {
dao.update(mapper.mapDtoToEntity(bookDto));
}
}

In the next lesson, we’ll develop the presentation tier.