diff --git a/src/main/java/de/itvsh/ozg/adapter/Application.java b/src/main/java/de/itvsh/ozg/adapter/Application.java index d102fc40722d8db8381949e6f56a12673b2fb131..59052eb684830051e484cab9c779996b6c931464 100644 --- a/src/main/java/de/itvsh/ozg/adapter/Application.java +++ b/src/main/java/de/itvsh/ozg/adapter/Application.java @@ -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) { diff --git a/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParser.java b/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParser.java index 23cbcf70caf875d1df32e2c847e3543bfc48a339..4bcc4778d94ff613b3cf7c74183c93e88fc252a9 100644 --- a/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParser.java +++ b/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParser.java @@ -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); } } diff --git a/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderScheduler.java b/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderScheduler.java new file mode 100644 index 0000000000000000000000000000000000000000..361432e4d5c8c87a66724beede35295112f34666 --- /dev/null +++ b/src/main/java/de/itvsh/ozg/adapter/intelliform/FileReaderScheduler.java @@ -0,0 +1,92 @@ +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); + } + } +} diff --git a/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParserTest.java b/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParserTest.java index fdc4bafa8f68b8872dd62b0459b223d094630a13..086d1d5dd7da6af277678c951fe3798041a98591 100644 --- a/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParserTest.java +++ b/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderAntragDirectoryParserTest.java @@ -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); diff --git a/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderSchedulerTest.java b/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderSchedulerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9d361b7b490468c6135e1c2d6b495d59701cd583 --- /dev/null +++ b/src/test/java/de/itvsh/ozg/adapter/intelliform/FileReaderSchedulerTest.java @@ -0,0 +1,34 @@ +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); + } +}