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.AuditLoggingRepository; import com.pollex.pam.security.SecurityUtils; import com.pollex.pam.service.dto.SatisfactionCustomerScoreDTO; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; import static com.pollex.pam.aop.logging.audit.AuditLoggingType.CUSTOMER_FILL_SATISFACTION; @Component @Transactional public class LoggingFillSatisfactionStrategy implements AuditLoggingStrategy{ @Autowired AuditLoggingRepository auditLoggingRepository; @Override public AuditLoggingType getType() { return CUSTOMER_FILL_SATISFACTION; } @Override public void auditLogging(ProceedingJoinPoint joinPoint) { Object arg = joinPoint.getArgs()[0]; if(arg instanceof List) { List dtos = (List) arg; List loggings = dtos.stream().map(dto -> { AuditLogging auditLogging = new AuditLogging(); auditLogging.setContent(new Gson().toJson(dto)); auditLogging.setFunctionalType(getType()); auditLogging.setCreatedBy(SecurityUtils.getCurrentUserLogin().orElse(null)); return auditLogging; }).collect(Collectors.toList()); auditLoggingRepository.saveAll(loggings); } else { SatisfactionCustomerScoreDTO dto = (SatisfactionCustomerScoreDTO) arg; AuditLogging auditLogging = new AuditLogging(); auditLogging.setContent(new Gson().toJson(dto)); auditLogging.setFunctionalType(getType()); auditLogging.setCreatedBy(SecurityUtils.getCurrentUserLogin().orElse(null)); auditLoggingRepository.save(auditLogging); } } }