package com.pollex.pam.aop.logging.audit.strategy; import com.google.gson.Gson; import com.pollex.pam.aop.logging.audit.AuditLoggingType; import com.pollex.pam.domain.AuditLogging; import com.pollex.pam.repository.AppointmentClosedInfoRepository; import com.pollex.pam.repository.AuditLoggingRepository; import com.pollex.pam.security.SecurityUtils; import com.pollex.pam.service.dto.AppointmentCloseDTO; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import static com.pollex.pam.aop.logging.audit.AuditLoggingType.APPOINTMENT_CLOSE; import static com.pollex.pam.aop.logging.audit.AuditLoggingType.EDIT_CLOSED_APPOINTMENT; @Component @Transactional public class LoggingAppointmentCloseStrategy implements AuditLoggingStrategy{ @Autowired AuditLoggingRepository auditLoggingRepository; @Autowired AppointmentClosedInfoRepository appointmentClosedInfoRepository; @Override public AuditLoggingType getType() { return APPOINTMENT_CLOSE; } @Override public void auditLogging(ProceedingJoinPoint joinPoint) { AppointmentCloseDTO dto = (AppointmentCloseDTO) joinPoint.getArgs()[0]; Long appointmentId = dto.getAppointmentId(); AuditLogging auditLogging = new AuditLogging(); auditLogging.setContent(new Gson().toJson(dto)); auditLogging.setCreatedBy(SecurityUtils.getCurrentUserLogin().orElse(null)); boolean isAppointmentClosedInfoExist = appointmentClosedInfoRepository.findByAppointmentId(appointmentId).isPresent(); if(isAppointmentClosedInfoExist) { auditLogging.setFunctionalType(EDIT_CLOSED_APPOINTMENT); } else { auditLogging.setFunctionalType(APPOINTMENT_CLOSE); } auditLoggingRepository.save(auditLogging); } }