Skip to content
Snippets Groups Projects
Commit 2f30a26a authored by OZGCloud's avatar OZGCloud
Browse files

OZG-986 EA Filesystem Reader: add file import scheduler

parent 6158cb27
Branches
Tags
No related merge requests found
......@@ -2,8 +2,10 @@ package de.itvsh.ozg.adapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
......
......@@ -20,10 +20,8 @@ import de.itvsh.ozg.adapter.common.errorhandling.TechnicalException;
import de.itvsh.ozg.adapter.formdata.FormData;
import de.itvsh.ozg.adapter.formdata.IncomingFile;
import de.itvsh.ozg.adapter.formdata.IncomingFileGroup;
import lombok.extern.log4j.Log4j2;
@Service
@Log4j2
class FileReaderAntragDirectoryParser {
private static final String GESCHAEFTSGANG_FILE_PATTERN = "_Geschaeftsgang.Geschaeftsgang.0201.xml";
......@@ -49,7 +47,7 @@ class FileReaderAntragDirectoryParser {
@Autowired
FileReaderAttachmentContentAdder fileReaderAttachmentContentAdder;
FormData readPath(String path) {
FormData readPath(Resource path) {
Supplier<Stream<Resource>> files = () -> loadFormDataFiles(path);
......@@ -129,13 +127,13 @@ class FileReaderAntragDirectoryParser {
}
}
Stream<Resource> loadFormDataFiles(String filePath) {
Stream<Resource> loadFormDataFiles(Resource path) {
try {
String pattern = filePath + "/*";
String pattern = path.getURI() + "/*";
return Arrays.stream(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern));
} catch (IOException e) {
throw new TechnicalException(String.format("Failed reading Eingang files from directory '%s'", filePath), e);
throw new TechnicalException(String.format("Failed reading Eingang files from directory '%s'", path), e);
}
}
......
package de.itvsh.ozg.adapter.intelliform;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import de.itvsh.ozg.adapter.VorgangService;
import de.itvsh.ozg.adapter.common.errorhandling.TechnicalException;
import de.itvsh.ozg.adapter.formdata.FormData;
import lombok.extern.log4j.Log4j2;
@Service
@Log4j2
class FileReaderScheduler {
private static final int MINUTE = 60 * 1000;
static final String STATUS_FILENAME = "kop_hat_das_hier_eingelesen.txt";
@Value("kop.adapter.intelliform.filereader.path")
String afmRootPath;
@Autowired
ResourceLoader resourceLoader;
@Autowired
FileReaderAntragDirectoryParser fileReaderAntragDirectoryParser;
@Autowired
private VorgangService vorgangService;
@Scheduled(initialDelay = 1 * MINUTE, fixedDelay = 5 * MINUTE)
void runImportTask() {
if (afmRootPath == null) {
LOG.info("FileReader is not configured. Imports will be skipped.");
return;
}
Stream<Resource> antragDirectories = getAntragDirectories();
antragDirectories.forEach(antragDirectory -> processAntrag(antragDirectory));
}
private void processAntrag(Resource antragDirectory) {
try {
FormData formData = fileReaderAntragDirectoryParser.readPath(antragDirectory);
createReaderStatusFile(antragDirectory, "Einlesen des Antrags war erfolgreich");
vorgangService.createVorgang(formData);
} catch (Exception e) {
LOG.error("Could not import Antrag from directory " + antragDirectory.getFilename());
createReaderStatusFile(antragDirectory, "Fehler beim Einlesen des Antrags: " + e.getMessage());
}
}
Stream<Resource> getAntragDirectories() {
try {
String pattern = afmRootPath + "/*";
return Arrays.stream(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern));
} catch (IOException e) {
throw new TechnicalException(String.format("Failed reading Eingang directories from path '%s'", afmRootPath), e);
}
}
void createReaderStatusFile(Resource antragDirectory, String message) {
try {
Files.write(Paths.get(new PathResource(antragDirectory.getURI()).getPath() + "/" + STATUS_FILENAME), message.getBytes());
} catch (IOException e) {
throw new TechnicalException("Could not create status file for directory " + antragDirectory.getFilename(), e);
}
}
}
......@@ -19,6 +19,8 @@ class FileReaderAntragDirectoryParserTest {
private FileReaderAntragDirectoryParser service;
private Resource testDataPath;
@BeforeEach
void init() {
service = new FileReaderAntragDirectoryParser();
......@@ -29,12 +31,14 @@ class FileReaderAntragDirectoryParserTest {
service.xmlToJavaMapsMapper = new XmlToJavaMapsMapper();
service.formDataIncomingFileMapper = new FormDataIncomingFileMapper();
service.fileReaderAttachmentContentAdder = new FileReaderAttachmentContentAdder();
testDataPath = new DefaultResourceLoader().getResource(TESTDATA_PATH);
}
@Test
void readEingangShouldReturnFormData() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData).isNotNull();
}
......@@ -42,7 +46,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void loadFormDataFilesShouldReturnFilesInTestpath() {
Stream<Resource> files = service.loadFormDataFiles(TESTDATA_PATH);
Stream<Resource> files = service.loadFormDataFiles(testDataPath);
assertThat(files).hasSize(4);
}
......@@ -50,7 +54,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void filterGeschaeftsgangFileShouldReturnMatchingResource() {
Stream<Resource> files = service.loadFormDataFiles(TESTDATA_PATH);
Stream<Resource> files = service.loadFormDataFiles(testDataPath);
assertThat(service.filterGeschaeftsgangFile(files).getFilename())
.isEqualTo("60c44129-de4b-11eb-b18f-005056ba7f94_Geschaeftsgang.Geschaeftsgang.0201.xml");
......@@ -62,7 +66,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void formDataShouldHaveId() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getId()).isNotNull();
}
......@@ -70,7 +74,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void formDataShouldHaveFormId() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getHeader().getFormId()).isEqualTo("EA-Antragsassistent/EA-Antragsassistent");
}
......@@ -78,7 +82,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void formDataShouldHaveAntragsteller() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getAntragsteller().getVorname()).isEqualTo("Projekt");
}
......@@ -86,7 +90,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void formDataShouldHaveZustaendigeStelle() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getZustaendigeStelle().getOrganisationseinheitenId()).isEqualTo("10363455");
}
......@@ -98,7 +102,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void shouldHaveNumberOfRepresentations() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getNumberOfRepresentations()).isEqualTo(2);
}
......@@ -106,7 +110,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void shouldHaveXmlRepresentation() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
IncomingFile representation = formData.getRepresentations().stream().filter(r -> r.getName().endsWith(".xml")).findFirst()
.orElseThrow();
......@@ -122,7 +126,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void shouldHavePdfRepresentation() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
IncomingFile representation = formData.getRepresentations().stream().filter(r -> r.getName().endsWith(".pdf")).findFirst()
.orElseThrow();
......@@ -142,7 +146,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void shouldHaveNumberOfAttachments() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
assertThat(formData.getNumberOfAttachments()).isEqualTo(1);
}
......@@ -150,7 +154,7 @@ class FileReaderAntragDirectoryParserTest {
@Test
void shouldHaveAttachment() {
FormData formData = service.readPath(TESTDATA_PATH);
FormData formData = service.readPath(testDataPath);
IncomingFile attachment = formData.getAttachments().get(0).getFiles().get(0);
......
package de.itvsh.ozg.adapter.intelliform;
import static org.assertj.core.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.PathResource;
class FileReaderSchedulerTest {
private static final String TEST_FILE_CONTENT = "TestFileContent";
private FileReaderScheduler service;
@BeforeEach
void init() {
service = new FileReaderScheduler();
}
@Test
void testCreateReaderStatusFile() throws IOException {
Path tmpPath = Files.createTempDirectory("kop");
service.createReaderStatusFile(new PathResource(tmpPath), TEST_FILE_CONTENT);
assertThat(new String(Files.readAllBytes(tmpPath.resolve(FileReaderScheduler.STATUS_FILENAME)))).isEqualTo(TEST_FILE_CONTENT);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment