diff --git a/Dockerfile b/Dockerfile index 6040315ec..592745849 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,5 +27,6 @@ WORKDIR /fdp COPY --from=builder /builder/target/fdp-spring-boot.jar /fdp/app.jar COPY --from=builder /builder/fixtures /fdp/fixtures +# TODO: copy the (rdf) "data" dir as well, or move that into the fixtures dir, e.g. fixtures/rdf and fixtures/db ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/data/records/records.json b/data/records/records.json new file mode 100644 index 000000000..46c9c64b7 --- /dev/null +++ b/data/records/records.json @@ -0,0 +1,10 @@ +{ + "records": [ + { + "file": "repository.ttl", + "repository": "main", + "uri": "{{ persistentUrl }}" + } + ], + "persistentUrlVar": "{{ persistentUrl }}" +} diff --git a/data/records/repository.ttl b/data/records/repository.ttl new file mode 100644 index 000000000..3908c213f --- /dev/null +++ b/data/records/repository.ttl @@ -0,0 +1,28 @@ +@prefix dcterms: . +@prefix dcat: . +@prefix foaf: . +@prefix xsd: . +@prefix ldp: . + +<{{ persistentUrl }}> a dcat:Resource, dcat:DataService, , + ; + dcterms:title "My FAIR Data Point"; + "My FAIR Data Point"; + dcat:version "1.0"; + dcterms:license ; + dcterms:description "Duis pellentesque, nunc a fringilla varius, magna dui porta quam, nec ultricies augue turpis sed velit. Donec id consectetur ligula. Suspendisse pharetra egestas massa, vel varius leo viverra at. Donec scelerisque id ipsum id semper. Maecenas facilisis augue vel justo molestie aliquet. Maecenas sed mattis lacus, sed viverra risus. Donec iaculis quis lacus vitae scelerisque. Nullam fermentum lectus nisi, id vulputate nisi congue nec. Morbi fermentum justo at justo bibendum, at tempus ipsum tempor. Donec facilisis nibh sed lectus blandit venenatis. Cras ullamcorper, justo vitae feugiat commodo, orci metus suscipit purus, quis sagittis turpis ante eget ex. Pellentesque malesuada a metus eu pulvinar. Morbi rutrum euismod eros at varius. Duis finibus dapibus ex, a hendrerit mauris efficitur at."; + dcterms:language ; + <{{ persistentUrl }}#identifier>; + <{{ persistentUrl }}#identifier>; + dcterms:accessRights <{{ persistentUrl }}#accessRights>; + dcterms:publisher <{{ persistentUrl }}#publisher>; + dcat:endpointURL <{{ persistentUrl }}> . + +<{{ persistentUrl }}#identifier> a ; + dcterms:identifier "{{ persistentUrl }}" . + +<{{ persistentUrl }}#accessRights> a dcterms:RightsStatement; + dcterms:description "This resource has no access restriction" . + +<{{ persistentUrl }}#publisher> a foaf:Agent; + foaf:name "Default Publisher" . diff --git a/pom.xml b/pom.xml index 361daa58d..733440f45 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,6 @@ 21 - 2.0.0 0.2.0 @@ -81,11 +80,6 @@ - - org.fairdatateam.rdf - spring-rdf-migration - ${spring.rdf.migration.version} - org.fairdatateam.rdf rdf-resource-resolver-core diff --git a/src/main/java/org/fairdatapoint/config/RepositoryMigrationConfig.java b/src/main/java/org/fairdatapoint/config/RepositoryMigrationConfig.java deleted file mode 100644 index 9c7f96a32..000000000 --- a/src/main/java/org/fairdatapoint/config/RepositoryMigrationConfig.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * The MIT License - * Copyright © 2016-2024 FAIR Data Team - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.fairdatapoint.config; - -import org.fairdatapoint.Profiles; -import org.fairdatateam.rdf.migration.database.RdfMigrationRepository; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigrationRunner; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; - -@Configuration -public class RepositoryMigrationConfig { - - @Bean - @Profile(Profiles.PRODUCTION) - public RdfProductionMigrationRunner rdfProductionMigrationRunner( - RdfMigrationRepository rdfMigrationRepository, - ApplicationContext appContext - ) { - final RdfProductionMigrationRunner mr = - new RdfProductionMigrationRunner(rdfMigrationRepository, appContext); - mr.run(); - return mr; - } - -} diff --git a/src/main/java/org/fairdatapoint/config/properties/BootstrapProperties.java b/src/main/java/org/fairdatapoint/config/properties/BootstrapProperties.java index efe2d084d..bb5a19091 100644 --- a/src/main/java/org/fairdatapoint/config/properties/BootstrapProperties.java +++ b/src/main/java/org/fairdatapoint/config/properties/BootstrapProperties.java @@ -35,6 +35,7 @@ @ConfigurationProperties(prefix = "bootstrap") public class BootstrapProperties { private boolean enabled; + private String dataPath; // directories relative to project root private String dbFixturesDir; } diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/RdfDevelopmentMigrationRunner.java b/src/main/java/org/fairdatapoint/database/rdf/migration/RdfDevelopmentMigrationRunner.java index 59c4aeb2c..3b7cdf390 100644 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/RdfDevelopmentMigrationRunner.java +++ b/src/main/java/org/fairdatapoint/database/rdf/migration/RdfDevelopmentMigrationRunner.java @@ -35,6 +35,12 @@ import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; +/** + * Loads initial RDF data for development. + * TODO: remove all classes and files related to RDF migration, in favor of the fixtures-based approach. + * @deprecated use RDF fixtures instead, see {@link org.fairdatapoint.service.bootstrap bootstrap package} + */ +@Deprecated(forRemoval = true, since = "1") @Service @Profile(Profiles.NON_PRODUCTION) public class RdfDevelopmentMigrationRunner { diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0001_Init.java b/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0001_Init.java deleted file mode 100644 index e2313a4e3..000000000 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0001_Init.java +++ /dev/null @@ -1,97 +0,0 @@ -/** - * The MIT License - * Copyright © 2016-2024 FAIR Data Team - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.fairdatapoint.database.rdf.migration.production; - -import lombok.extern.slf4j.Slf4j; -import org.fairdatapoint.service.reset.FactoryDefaults; -import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigration; -import org.bson.Document; -import org.eclipse.rdf4j.model.IRI; -import org.eclipse.rdf4j.model.Statement; -import org.eclipse.rdf4j.repository.Repository; -import org.eclipse.rdf4j.repository.RepositoryConnection; -import org.eclipse.rdf4j.repository.RepositoryException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -import java.util.List; - -@RdfMigrationAnnotation( - number = 1, - name = "Init migration", - description = "Load basic fixtures for repository, catalog, dataset and distribution") -@Slf4j -@Service -public class Rdf_Migration_0001_Init implements RdfProductionMigration { - // TODO: use data specs for initial data - - @Autowired - private Repository mainRepository; - - @Autowired - @Qualifier("persistentUrl") - private String persistentUrl; - - @Value("${metadataProperties.accessRightsDescription:This resource has no access restriction}") - private String accessRightsDescription; - - @Autowired - private IRI license; - - @Autowired - private IRI language; - - public void runMigration() { - createRepositoryInTripleStore(); - storePermissionForRepository(); - } - - private void createRepositoryInTripleStore() { - try (RepositoryConnection conn = mainRepository.getConnection()) { - final List statements = FactoryDefaults.repositoryStatements( - persistentUrl, - license, - language, - accessRightsDescription - ); - conn.add(statements); - } - catch (RepositoryException exception) { - log.error(exception.getMessage(), exception); - } - } - - private void storePermissionForRepository() { - // TODO - return; - } - - private Document repositoryPermission() { - // TODO - return null; - } - -} diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0005_FixMetadataVersion.java b/src/main/java/org/fairdatapoint/service/bootstrap/BootstrapRunner.java similarity index 62% rename from src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0005_FixMetadataVersion.java rename to src/main/java/org/fairdatapoint/service/bootstrap/BootstrapRunner.java index f6ef3d14c..b269e6239 100644 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0005_FixMetadataVersion.java +++ b/src/main/java/org/fairdatapoint/service/bootstrap/BootstrapRunner.java @@ -20,22 +20,24 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.fairdatapoint.database.rdf.migration.production; +package org.fairdatapoint.service.bootstrap; -import lombok.extern.slf4j.Slf4j; -import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigration; -import org.springframework.stereotype.Service; +import lombok.RequiredArgsConstructor; +import org.fairdatapoint.config.properties.BootstrapProperties; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; -@RdfMigrationAnnotation( - number = 5, - name = "Fix Metadata Version", - description = "Use dcat:version instead of dcterms:hasVersion") -@Slf4j -@Service -public class Rdf_Migration_0005_FixMetadataVersion implements RdfProductionMigration { - // TODO: remove (use seed) +@Component +@RequiredArgsConstructor +public class BootstrapRunner implements ApplicationRunner { + private final BootstrapProperties bootstrapProperties; + private final BootstrapService bootstrapService; - public void runMigration() { + @Override + public void run(final org.springframework.boot.ApplicationArguments args) { + if (bootstrapProperties.isEnabled()) { + bootstrapService.bootstrapFromDir(bootstrapProperties.getDataPath()); + } } + } diff --git a/src/main/java/org/fairdatapoint/service/bootstrap/BootstrapService.java b/src/main/java/org/fairdatapoint/service/bootstrap/BootstrapService.java new file mode 100644 index 000000000..96636cf4e --- /dev/null +++ b/src/main/java/org/fairdatapoint/service/bootstrap/BootstrapService.java @@ -0,0 +1,59 @@ +/** + * The MIT License + * Copyright © 2016-2024 FAIR Data Team + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.fairdatapoint.service.bootstrap; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.fairdatapoint.service.bootstrap.components.*; +import org.springframework.stereotype.Service; + +import java.nio.file.Path; + +@Slf4j +@Service +@RequiredArgsConstructor +public class BootstrapService { + private final MetadataRecordsBootstrapper metadataRecordsBootstrapper; + + @Transactional + public void bootstrapFromDir(String dataPath) { + final Path basePath = Path.of(dataPath); + log.info("Bootstrap process started"); + + if (!basePath.toFile().exists() || !basePath.toFile().isDirectory()) { + log.warn("Bootstrap directory {} does not exist or is not a directory, skipping bootstrapping", dataPath); + return; + } + + // RDF Records + if (metadataRecordsBootstrapper.shouldBootstrap()) { + metadataRecordsBootstrapper.bootstrapAllFromDir(basePath.resolve("records")); + } + else { + log.info("Metadata Records already exist, skipping metadata records bootstrapping"); + } + + log.info("Bootstrap process finished"); + } +} diff --git a/src/main/java/org/fairdatapoint/service/bootstrap/components/AbstractBootstrapper.java b/src/main/java/org/fairdatapoint/service/bootstrap/components/AbstractBootstrapper.java new file mode 100644 index 000000000..87261d84a --- /dev/null +++ b/src/main/java/org/fairdatapoint/service/bootstrap/components/AbstractBootstrapper.java @@ -0,0 +1,76 @@ +/** + * The MIT License + * Copyright © 2016-2024 FAIR Data Team + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.fairdatapoint.service.bootstrap.components; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; + +@Slf4j +public abstract class AbstractBootstrapper implements IBootstrapper { + private final ObjectMapper objectMapper; + + protected AbstractBootstrapper(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + @Override + public void bootstrapAllFromDir(Path dirPath) { + if (!Files.isDirectory(dirPath)) { + log.info("Directory {} does not exist, nothing to bootstrap", dirPath); + return; + } + try (Stream paths = Files.walk(dirPath)) { + initBootstrap(); + paths.filter(Files::isRegularFile) + .filter(path -> path.toString().endsWith(".json")) + .forEach(path -> bootstrapFromJson(path)); + finalizeBootstrap(); + } + catch (IOException exception) { + throw new RuntimeException("Error loading entities", exception); + } + } + + protected ObjectMapper getObjectMapper() { + return objectMapper; + } + + protected void initBootstrap() { + } + + protected void finalizeBootstrap() { + getRepository().flush(); + } + + public boolean shouldBootstrap() { + return getRepository().count() == 0; + } + + protected abstract JpaRepository getRepository(); +} diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0003_FDPO.java b/src/main/java/org/fairdatapoint/service/bootstrap/components/IBootstrapper.java similarity index 66% rename from src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0003_FDPO.java rename to src/main/java/org/fairdatapoint/service/bootstrap/components/IBootstrapper.java index 1e7bca1b5..83414e2c1 100644 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0003_FDPO.java +++ b/src/main/java/org/fairdatapoint/service/bootstrap/components/IBootstrapper.java @@ -20,22 +20,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.fairdatapoint.database.rdf.migration.production; +package org.fairdatapoint.service.bootstrap.components; -import lombok.extern.slf4j.Slf4j; -import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigration; -import org.springframework.stereotype.Service; +import java.nio.file.Path; -@RdfMigrationAnnotation( - number = 3, - name = "FDPO Compliance", - description = "Comply with FDP-O Metadata Service") -@Slf4j -@Service -public class Rdf_Migration_0003_FDPO implements RdfProductionMigration { - // TODO: remove (use seed) +public interface IBootstrapper { - public void runMigration() { - } + void bootstrapAllFromDir(Path dirPath); + + void bootstrapFromJson(Path resourcePath); } diff --git a/src/main/java/org/fairdatapoint/service/bootstrap/components/MetadataRecordsBootstrapper.java b/src/main/java/org/fairdatapoint/service/bootstrap/components/MetadataRecordsBootstrapper.java new file mode 100644 index 000000000..560a426cb --- /dev/null +++ b/src/main/java/org/fairdatapoint/service/bootstrap/components/MetadataRecordsBootstrapper.java @@ -0,0 +1,112 @@ +/** + * The MIT License + * Copyright © 2016-2024 FAIR Data Team + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.fairdatapoint.service.bootstrap.components; + +import lombok.extern.slf4j.Slf4j; +import org.eclipse.rdf4j.model.Model; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.Rio; +import org.fairdatapoint.database.rdf.repository.RepositoryMode; +import org.fairdatapoint.database.rdf.repository.generic.GenericMetadataRepository; +import org.fairdatapoint.service.bootstrap.fixtures.RecordsFixture; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Component; + +import java.io.StringReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Locale; + +import static org.fairdatapoint.util.ValueFactoryHelper.i; + +@Slf4j +@Component +public class MetadataRecordsBootstrapper extends AbstractBootstrapper { + private final GenericMetadataRepository genericMetadataRepository; + private final String persistentUrl; + + public MetadataRecordsBootstrapper(GenericMetadataRepository genericMetadataRepository, + String persistentUrl) { + super(null); + this.genericMetadataRepository = genericMetadataRepository; + this.persistentUrl = persistentUrl; + } + + @Override + protected JpaRepository getRepository() { + return null; + } + + @Override + public void bootstrapFromJson(Path resourcePath) { + if (!resourcePath.getFileName().toString().equals("records.json")) { + log.warn("Skipping file {}: only records.json is supported for records bootstrapping", resourcePath); + return; + } + try { + final RecordsFixture recordsFixture = + getObjectMapper().readValue(resourcePath.toFile(), RecordsFixture.class); + final Path resourceDir = resourcePath.getParent(); + recordsFixture.getRecords().forEach(record -> { + final Path recordPath = resourceDir.resolve(record.getFilename()); + try { + final String rdfContent = + Files + .readString(recordPath) + .replaceAll(recordsFixture.getPersistentUrlVar(), persistentUrl); + final String baseUri = + record.getUri().replaceAll(recordsFixture.getPersistentUrlVar(), persistentUrl); + final RepositoryMode repositoryMode = getRepositoryMode(record.getRepository()); + storeRecord(rdfContent, repositoryMode, baseUri); + log.info("Created metadata record {}", record.getUri()); + } + catch (Exception exception) { + log.warn("Failed to read record file {}: {}", recordPath, exception.getMessage()); + } + }); + } + catch (Exception exception) { + throw new RuntimeException(exception); + } + } + + private void storeRecord(String rdfContent, RepositoryMode repositoryMode, String baseUri) { + try { + final Model model = Rio.parse(new StringReader(rdfContent), baseUri, RDFFormat.TURTLE); + final List statements = model.stream().toList(); + genericMetadataRepository.save(statements, i(baseUri), repositoryMode); + } + catch (Exception exception) { + log.warn("Failed to parse RDF content: {}", exception.getMessage()); + } + } + + private RepositoryMode getRepositoryMode(String repository) { + if (repository.toLowerCase(Locale.ROOT).equals("drafts")) { + return RepositoryMode.DRAFTS; + } + return RepositoryMode.MAIN; + } +} diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0004_Cleanup_Index.java b/src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordFixture.java similarity index 65% rename from src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0004_Cleanup_Index.java rename to src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordFixture.java index b9635c9c2..6100b7e9b 100644 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0004_Cleanup_Index.java +++ b/src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordFixture.java @@ -20,22 +20,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.fairdatapoint.database.rdf.migration.production; +package org.fairdatapoint.service.bootstrap.fixtures; -import lombok.extern.slf4j.Slf4j; -import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigration; -import org.springframework.stereotype.Service; +import lombok.Data; -@RdfMigrationAnnotation( - number = 4, - name = "Cleanup Index", - description = "Cleanup harvested record stored in separate named graphs") -@Slf4j -@Service -public class Rdf_Migration_0004_Cleanup_Index implements RdfProductionMigration { - // TODO: remove (use seed) - - public void runMigration() { - } +@Data +public class RecordFixture { + private final String filename; + private final String repository; + private final String uri; } diff --git a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0002_Metadata_Draft.java b/src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordsFixture.java similarity index 66% rename from src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0002_Metadata_Draft.java rename to src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordsFixture.java index 908cd5f52..240f13d8b 100644 --- a/src/main/java/org/fairdatapoint/database/rdf/migration/production/Rdf_Migration_0002_Metadata_Draft.java +++ b/src/main/java/org/fairdatapoint/service/bootstrap/fixtures/RecordsFixture.java @@ -20,23 +20,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.fairdatapoint.database.rdf.migration.production; +package org.fairdatapoint.service.bootstrap.fixtures; -import lombok.extern.slf4j.Slf4j; -import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; -import org.fairdatateam.rdf.migration.runner.RdfProductionMigration; -import org.springframework.stereotype.Service; +import lombok.Data; -@RdfMigrationAnnotation( - number = 2, - name = "Metadata Draft", - description = "Support metadata in DRAFT state") -@Slf4j -@Service -public class Rdf_Migration_0002_Metadata_Draft implements RdfProductionMigration { - // TODO: remove (use seed) +import java.util.ArrayList; +import java.util.List; - public void runMigration() { - - } +@Data +public class RecordsFixture { + private List records = new ArrayList<>(); + private String persistentUrlVar = "{{ persistentUrl }}"; } diff --git a/src/main/java/org/fairdatapoint/service/settings/SettingsMapper.java b/src/main/java/org/fairdatapoint/service/settings/SettingsMapper.java index af5d55b49..73819df1d 100644 --- a/src/main/java/org/fairdatapoint/service/settings/SettingsMapper.java +++ b/src/main/java/org/fairdatapoint/service/settings/SettingsMapper.java @@ -31,6 +31,7 @@ import org.fairdatapoint.config.properties.RepositoryConnectionProperties; import org.fairdatapoint.config.properties.RepositoryProperties; import org.fairdatapoint.entity.settings.*; +import org.fairdatapoint.service.bootstrap.fixtures.*; import org.springframework.stereotype.Component; import java.time.Instant; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7e3d89ec7..191da5a76 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -113,4 +113,6 @@ server: forward-headers-strategy: framework bootstrap: + # TODO: consistent naming for rdf data path and db fixtures dir + data-path: '/data' db-fixtures-dir: "fixtures"