package no.dusken.barweb.service;
import no.dusken.barweb.model.BarPerson;
import no.dusken.common.model.Person;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class BarPersonCasterAspect {
@Autowired
private BarPersonService barPersonService;
@Autowired
private GjengService gjengService;
@Around("execution(* no.dusken.common.service.PersonService.save(*)) && " +
"!execution(* no.dusken.barweb.service.BarPersonService.save(*))")
public Object saveAsBarPerson(ProceedingJoinPoint pjp) throws Throwable {
Person person = (Person) pjp.getArgs()[0];
BarPerson barPerson = getPerson(person);
barPerson.setEmailAddress(person.getEmailAddress());
barPerson.setExternalID(person.getExternalID());
barPerson.setExternalSource(person.getExternalSource());
barPerson.setSurname(person.getSurname());
barPerson.setActive(person.getActive());
barPerson.setBirthdate(person.getBirthdate());
barPerson.setDepartment(person.getDepartment());
barPerson.setFirstname(person.getFirstname());
barPerson.setTimeCreated(person.getTimeCreated());
barPerson.setUsername(person.getUsername());
return barPersonService.save(barPerson);
}
private BarPerson getPerson(Person person) {
BarPerson barPerson;
if(person.isNew()){
barPerson = new BarPerson();
if(person.getDepartment() != null){
barPerson.setGjeng(gjengService.getByDefaultGjengTrue());
}
}else {
barPerson = barPersonService.findOne(person.getId());
}
return barPerson;
}
}