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);

    /**
     * Find by last name.
     */
    @Secured ({ USER })
    public PersonFindResponse findByLastName(String lastName);

    @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 PersonResponse delete(Person person);

}              
                
            

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 PersonStudentConverter personStudentConverter;
    private final StudentConverter studentConverter;
    private final ProfessionalConverter professionalConverter;

    @Autowired
    public ContactServiceImpl(PersonRepository repository,
                              ContactConverter converter, PersonStudentConverter personStudentConverter,
                              StudentConverter studentConverter, ProfessionalConverter professionalConverter,
                              MessageHelper messageHelper) {
        super(repository, converter, messageHelper);

        this.personStudentConverter = personStudentConverter;
        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 = studentConverter.convertFrom(personStudentConverter.convertTo(request));
        }

        Person result = converter.convertTo(repository.saveAndFlush(bean));

        return createSaveResponse(result);
    }

    @Override
    public PersonFindResponse findByLastName(String lastName) {
        List<Person> results = converter.convertListTo(((PersonRepository)repository).findByLastName(lastName));

        return createFindResponse(results);
    }

    @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 createDeleteResponse() {
        return new PersonResponse().withMessageList(new Message().withMessageType(MessageType.INFO)
                .withMessageKey(DELETE_MSG).withMessage(getMessage(DELETE_MSG)));
    }

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

}