...

/

Develop the Business Service Tier

Develop the Business Service Tier

Let's see how the business service can be developed for a many-to-many unidirectional relationship scenario

We'll cover the following...

We will proceed with the business service tier.

“Find all records” operation

First, we will scan the test for the “find all records” operation.

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class UserGroupServiceImplTest {
@Autowired
private UserService userService;
@Autowired
private GroupService groupService;
@Autowired
private UserGroupService userGroupService;
@Test
public void testFindAll() {
Assert.assertEquals(0L, userGroupService.findAll().size());
}
}

Let’s see how to code the equivalent method in the business service.

@Service
@Transactional
public class UserGroupServiceImpl implements UserGroupService {
@Autowired
private GroupDao groupDao;
@Autowired
private UserDao userDao;
@Autowired
private UserGroupDao userGroupDao;
@Autowired
private UserMapper userMapper;
@Autowired
private GroupMapper groupMapper;
@Override
public List<UserGroupDto> findAll() {
List<UserGroupDto> userGroupDtos = new ArrayList<UserGroupDto>();
List<User> userList = userGroupDao.getAll();
for(User user : userList) {
UserDto userDto = userMapper.mapEntityToDto(user);
Set<Group> groups = user.getGroups();
for(Group group : groups) {
UserGroupDto userGroupDto = new UserGroupDto();
userGroupDto.setUserDto(userDto);
GroupDto groupDto = groupMapper.mapEntityToDto(group);
userGroupDto.setGroupDto(groupDto);
userGroupDtos.add(userGroupDto);
}
}
return userGroupDtos;
}
}

Create operation

Let’s move on to the create test ...