61 lines
2.1 KiB
Java
61 lines
2.1 KiB
Java
package org.lucares.pdbui;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
import org.lucares.pdbui.domain.FileDropConfig;
|
|
import org.lucares.pdbui.domain.FileDropSettings;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.AntPathMatcher;
|
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
@Component
|
|
public class FileDropConfigProvider {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(FileDropConfigProvider.class);
|
|
|
|
private final FileDropConfig config;
|
|
|
|
public FileDropConfigProvider(@Value("${path.fileDropConfig}") final String fileDropConfig)
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
final Path configPath = Path.of(fileDropConfig);
|
|
|
|
if (Files.exists(configPath)) {
|
|
|
|
final ObjectMapper objectMapper = new ObjectMapper();
|
|
config = objectMapper.readValue(configPath.toFile(), FileDropConfig.class);
|
|
LOGGER.info("File drop config: {}", objectMapper.writeValueAsString(config));
|
|
} else {
|
|
config = new FileDropConfig();
|
|
}
|
|
}
|
|
|
|
public FileDropConfig getConfig() {
|
|
return config;
|
|
}
|
|
|
|
public Optional<CsvReaderSettings> provideCsvReaderSettings(final String file) {
|
|
for (final FileDropSettings settings : config.getSettings()) {
|
|
final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
|
if (antPathMatcher.match(settings.match(), file)) {
|
|
|
|
final Map<String, String> variables = antPathMatcher.extractUriTemplateVariables(settings.match(),
|
|
file);
|
|
|
|
final CsvReaderSettings csvSettings = settings.csvSettings().copy();
|
|
csvSettings.putAdditionalTag(variables);
|
|
return Optional.of(csvSettings);
|
|
}
|
|
}
|
|
return Optional.empty();
|
|
}
|
|
}
|