|
| 1 | +package fr.insee.onyxia.api.services; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import javax.annotation.PostConstruct; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | +import org.springframework.beans.factory.annotation.Value; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +@Service |
| 16 | +public class JsonSchemaProfileRegistryService { |
| 17 | + |
| 18 | + private static final Logger LOGGER = |
| 19 | + LoggerFactory.getLogger(JsonSchemaProfileRegistryService.class); |
| 20 | + |
| 21 | + @Value("${profile.schema.directory:/userProfile}") // External directory path |
| 22 | + private String userProfileDirectory; |
| 23 | + |
| 24 | + private JsonNode defaultProfileSchema; |
| 25 | + |
| 26 | + private ObjectMapper objectMapper = new ObjectMapper(); |
| 27 | + |
| 28 | + public JsonSchemaProfileRegistryService() {} |
| 29 | + |
| 30 | + public JsonNode getProfileSchema() { |
| 31 | + return defaultProfileSchema; |
| 32 | + } |
| 33 | + |
| 34 | + @PostConstruct |
| 35 | + private void loadProfileSchema() { |
| 36 | + Path roleSchemaPath = Paths.get(userProfileDirectory); |
| 37 | + if (Files.exists(roleSchemaPath) && Files.exists(roleSchemaPath.resolve("default"))) { |
| 38 | + try { |
| 39 | + JsonNode schema = objectMapper.readTree(roleSchemaPath.resolve("default").toFile()); |
| 40 | + defaultProfileSchema = schema; |
| 41 | + } catch (IOException e) { |
| 42 | + LOGGER.error("User profile : failed to load default profile", e); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments