AppSettingTriggers.java
/*
* Copyright 2026 Global Crop Diversity Trust
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gringlobal.service.triggers;
import java.util.Collection;
import java.util.Objects;
import org.gringlobal.application.config.IntegrationConfig;
import org.gringlobal.model.AppSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
@Component("appSettingTriggers")
@Slf4j
public class AppSettingTriggers {
@Autowired(required = false)
private IntegrationConfig.GenesysClientFactory genesysClientFactory;
@Pointcut("target(org.gringlobal.persistence.AppSettingRepository)")
public void targetAppSettingRepository() {};
@Pointcut("execution(* *.save(..)) || execution(* *.saveAndFlush(..)) || execution(* *.delete(..))")
public void affectsSingleRecord() {};
@Pointcut("execution(* *.saveAll(..)) || execution(* *.saveAllAndFlush(..)) || execution(* *.deleteAll(..))")
public void affectsManyRecords() {};
@Around(value = "targetAppSettingRepository() && affectsSingleRecord() && args(appSetting)")
public Object aroundAppSettingSave(final ProceedingJoinPoint joinPoint, final AppSetting appSetting) throws Throwable {
if (genesysClientFactory != null && Objects.equals(appSetting.getCategoryTag(), IntegrationConfig.GenesysClientFactory.SETTINGS_GENESYS)) {
genesysClientFactory.clearToken();
}
return joinPoint.proceed();
}
@Around(value = "targetAppSettingRepository() && affectsManyRecords() && args(appSettings)")
public Object aroundAppSettingsSave(final ProceedingJoinPoint joinPoint, final Collection<AppSetting> appSettings) throws Throwable {
if (genesysClientFactory != null && appSettings.stream().anyMatch(s -> Objects.equals(s.getCategoryTag(), IntegrationConfig.GenesysClientFactory.SETTINGS_GENESYS))) {
genesysClientFactory.clearToken();
}
return joinPoint.proceed();
}
}