Person Copy Intercept

What is copyInterceptor?

Let me explain, when we use automatic mapping, we may need to do some custom work, like the email address is already exist or not, we may need to find the id to object. Those work we can do from here.

How to create a CopyInterceptor?

Please follow the bellow example, make sure the class must be a @Component.

Is it automatically start work if I create @Component?

No after create it you have to assign this component to a DTO, with the annotation @DataMappingInfo, Example @DataMappingInfo(customProcessor = PersonCopyIntercept.class). Please see example in the PersonDetailDTO.

import com.problemfighter.pfspring.restapi.common.ApiRestException;
import com.problemfighter.pfspring.restapi.inter.CopyInterceptor;
import com.problemfighter.pfspring.webtestmodule.example.model.dto.person.PersonDetailDTO;
import com.problemfighter.pfspring.webtestmodule.example.model.dto.person.PersonUpdateDTO;
import com.problemfighter.pfspring.webtestmodule.example.model.entity.Person;
import com.problemfighter.pfspring.webtestmodule.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PersonCopyIntercept implements CopyInterceptor<Person, PersonDetailDTO, PersonUpdateDTO> {

    @Autowired
    private PersonService personService;


    private void checkEmailAlreadyExist(String email) throws ApiRestException {
        if (email != null && personService.isEmailAlreadyExist(email)) {
            ApiRestException.otherError("Email already exist");
        }
    }

    @Override
    public void meAsSrc(PersonUpdateDTO source, Person destination) {
        if (personService.findByEmailAndId(source.email, source.id) == null) {
            checkEmailAlreadyExist(source.email);
        }
    }

    @Override
    public void meAsDst(Person source, PersonUpdateDTO destination) {

    }

    @Override
    public void meAsSrc(PersonDetailDTO source, Person destination) {
        checkEmailAlreadyExist(source.email);
    }

    @Override
    public void meAsDst(Person source, PersonDetailDTO destination) {

    }
}