package de.mummeit.pmg.service.config; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import de.mummeit.pmg.api.model.integration.Integration; import de.mummeit.pmg.service.PermissionManager; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * Abstract configuration class for Permission Manager integration. * Implementing classes can provide integrations either by overriding getIntegrations() * or by providing a JSON file path in getIntegrationsJsonPath(). */ @Slf4j @RequiredArgsConstructor public abstract class AbstractPermissionManagerConfiguration { private final PermissionManager permissionManager; private final ResourceLoader resourceLoader; private final ObjectMapper objectMapper; /** * Override this method to provide integrations programmatically. * By default, returns null, which means no integrations will be performed unless * getIntegrationsJsonPath() returns a valid path. * * @return List of integrations to perform, or null if no integrations should be performed */ protected List> getIntegrations() { return null; } /** * Override this method to provide the path to a JSON file containing integrations. * The JSON file should contain an array of Integration objects. * By default, returns null, which means no JSON file will be loaded. * * @return Path to the JSON file, or null if no file should be loaded */ protected String getIntegrationsJsonPath() { return null; } /** * Loads integrations from a JSON file. * * @param path Path to the JSON file * @return List of integrations * @throws IOException if the file cannot be read or parsed */ protected List> loadIntegrationsFromJson(String path) throws IOException { Resource resource = resourceLoader.getResource(path); try (InputStream inputStream = resource.getInputStream()) { return objectMapper.readValue(inputStream, new TypeReference>>() {}); } } /** * Event listener that performs integrations when the application is ready. * This method will first try to get integrations from getIntegrations(), * and if that returns null, it will try to load integrations from the JSON file * specified by getIntegrationsJsonPath(). */ @EventListener(ApplicationReadyEvent.class) public void performIntegrationsOnStartup() { try { List> integrations = getIntegrations(); if (integrations == null) { String jsonPath = getIntegrationsJsonPath(); if (jsonPath != null) { integrations = loadIntegrationsFromJson(jsonPath); } } if (integrations != null && !integrations.isEmpty()) { log.info("Performing {} integrations on startup", integrations.size()); permissionManager.performIntegration(integrations); log.info("Successfully performed integrations"); } else { log.info("No integrations to perform"); } } catch (Exception e) { log.error("Failed to perform integrations on startup", e); throw new RuntimeException("Failed to perform integrations on startup", e); } } }