5. Contact Service Code Example

Example 9. ContactService

The ContactService could just extend the PersistenceService, but instead it overrides each method so every service can set it's own security rules. Without making any assumptions that they would all be consistent. If the application is known to have consistent security rules, they could be applied to the parent persistent interfaces and it wouldn't be necessary to apply them to each level.

                
public interface ContactService extends PersistenceService<Person, PersonResponse, PersonFindResponse> {

    @Override
    @Secured ({ USER })
    public PersonResponse findById(Integer id);

    @Override
    @Secured ({ USER })
    public PersonFindResponse find();
    
    @Override
    @Secured ({ USER })
    public PersonFindResponse find(int page, int pageSize);

    @Override
    @Secured ({ USER })
    public PersonResponse create(Person person);

    @Override
    @Secured ({ USER })
    public PersonResponse update(Person person);

    @Override
    @Secured ({ ADMIN })
    public ResponseResult delete(Integer id);

}                
                
            

Example 10. ContactServiceImpl

The ContactServiceImpl is the implementation for the ContactService. It shows that if you needed to override the default behavior for save, the doSave method can be overridden. If the service wasn't also trying to handle saves/conversions for subclasses this wouldn't be necessary. Otherwise just the abstract methods for creating responses is needed.

                
@Service
public class ContactServiceImpl extends AbstractPersistenceService<org.springbyexample.contact.orm.entity.person.Person, Person,
                                                                   PersonResponse, PersonFindResponse>
        implements ContactService {

    private static final String SAVE_MSG = "contact.save.msg";
    
    private final StudentConverter studentConverter;
    private final ProfessionalConverter professionalConverter;
    
    @Autowired
    public ContactServiceImpl(PersonRepository repository, 
                              ContactConverter converter, 
                              StudentConverter studentConverter, ProfessionalConverter professionalConverter,
                              MessageHelper messageHelper) {
        super(repository, converter, messageHelper);

        this.studentConverter = studentConverter;
        this.professionalConverter = professionalConverter;
    }

    @Override
    protected PersonResponse doSave(Person request) {
        org.springbyexample.contact.orm.entity.person.Person bean = null;
        
        if (request instanceof Student) {
            bean = studentConverter.convertFrom((Student) request);
        } else if (request instanceof Professional) {
            bean = professionalConverter.convertFrom((Professional) request);
        } else {
            bean = converter.convertFrom(request);
        }
        
        Person result = converter.convertTo(repository.saveAndFlush(bean)); 
        
        return createSaveResponse(result);
    }

    @Override
    protected PersonResponse createSaveResponse(Person result) {
        return new PersonResponse().withMessageList(new Message().withMessageType(MessageType.INFO)
                    .withMessage(getMessage(SAVE_MSG, new Object[] { result.getFirstName(), result.getLastName()})))
                    .withResults(result);
    }

    @Override
    protected PersonResponse createResponse(Person result) {
        return new PersonResponse().withResults(result);
    }

    @Override
    protected PersonFindResponse createFindResponse(List<Person> results) {
        return new PersonFindResponse().withResults(results).withCount(results.size());
    }

    @Override
    protected PersonFindResponse createFindResponse(List<Person> results, long count) {
        return new PersonFindResponse().withResults(results).withCount(count);
    }
    
}