Search in sources :

Example 6 with ImportStatus

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.

the class JsonProvenanceToRdfPatch method sendQuads.

@Override
public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatus, DataSet dataSet) throws LogStorageFailedException {
    for (JsonNode revision : activity.get(PROV_GENERATES)) {
        final String entityUri = revision.get(PROV_SPECIALIZATION_OF).get(0).get("@id").asText();
        final String revisionUri = revision.get("@id").asText();
        String wasRevisionOf = null;
        if (revision.get(PROV_REVISION_OF) != null) {
            wasRevisionOf = revision.get(PROV_REVISION_OF).get(0).get("@id").asText();
        }
        generateRevisionInfo(saver, revisionUri, entityUri, wasRevisionOf);
        generatePatch(saver, revision.get(TIM_ADDITIONS), entityUri, true);
        for (CursorQuad quad : toReplace.getOrDefault(revisionUri, new ArrayList<>())) {
            saver.delQuad(quad.getSubject(), quad.getPredicate(), quad.getObject(), quad.getValuetype().orElse(null), quad.getLanguage().orElse(null), null);
        }
        generatePatch(saver, revision.get(TIM_REPLACEMENTS), entityUri, true);
        generatePatch(saver, revision.get(TIM_DELETIONS), entityUri, false);
    }
    try {
        final HashMap map = OBJECT_MAPPER.treeToValue(activity, HashMap.class);
        final RDFDataset dataset = (RDFDataset) JsonLdProcessor.toRDF(map);
        for (String graphName : dataset.graphNames()) {
            for (RDFDataset.Quad quad : dataset.getQuads(graphName)) {
                saver.onQuad(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad.getObject().getValue(), quad.getObject().isLiteral() ? quad.getObject().getDatatype() : null, quad.getObject().getLanguage(), quad.getGraph() == null ? null : quad.getGraph().getValue());
            }
        }
    } catch (JsonProcessingException | JsonLdError e) {
        throw new LogStorageFailedException(e);
    }
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) HashMap(java.util.HashMap) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonLdError(com.github.jsonldjava.core.JsonLdError)

Example 7 with ImportStatus

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.

the class ImportStatusTest method jsonSerialization.

@Test
public void jsonSerialization() throws Exception {
    LogList logList = new LogList();
    LogEntry entry = LogEntry.create("baseUri", "defaultGraph", "token");
    logList.addEntry(entry);
    ImportStatus status = new ImportStatus(logList);
    status.start("method", "baseUri");
    assertThat(status.getStatus(), is("Started method"));
    status.addError("This error is recorded in logList", new RuntimeException("list"));
    assertThat(status.getStatus().contains("This error is recorded in logList"), is(true));
    assertThat(logList.getListErrors().get(0).contains("This error is recorded in logList"), is(true));
    status.startEntry(entry);
    assertThat(status.getStatus(), is("Adding entry with token token"));
    status.addError("This error is recorded in logEntry", new RuntimeException("entry"));
    assertThat(status.getStatus().contains("This error is recorded in logEntry"), is(true));
    assertThat(entry.getImportStatus().get().getErrors().get(0).contains("This error is recorded in logEntry"), is(true));
    status.finishEntry();
    status.finishList();
    String json = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(status);
    // System.out.println(json);
    assertThat(json.contains("\"@type\" : \"ImportStatus\""), is(true));
}
Also used : LogList(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList) LogEntry(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with ImportStatus

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.

the class BdbRmlDataSourceStoreTest method itWorks.

@Test
public void itWorks() throws Exception {
    BdbNonPersistentEnvironmentCreator dbCreator = new BdbNonPersistentEnvironmentCreator();
    DataSetMetaData dataSetMetadata = new BasicDataSetMetaData("userid", "datasetid", "http://timbuctoo.huygens.knaw.nl/v5/userid/datasetid", "http://example.org/prefix/", false, false);
    final RmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(dbCreator.getDatabase("userid", "datasetid", "rmlSource", true, TupleBinding.getPrimitiveBinding(String.class), TupleBinding.getPrimitiveBinding(String.class), new StringStringIsCleanHandler()), new ImportStatus(new LogList()));
    RdfSerializer rdfSerializer = new RmlDataSourceRdfSerializer(rmlDataSourceStore);
    RawUploadRdfSaver rawUploadRdfSaver = new RawUploadRdfSaver(dataSetMetadata, "fileName", APPLICATION_OCTET_STREAM_TYPE, rdfSerializer, "origFileName", Clock.systemUTC());
    final String inputCol1 = rawUploadRdfSaver.addCollection("collection1");
    ImportPropertyDescriptions importPropertyDescriptions = new ImportPropertyDescriptions();
    importPropertyDescriptions.getOrCreate(1).setPropertyName("propName1");
    importPropertyDescriptions.getOrCreate(2).setPropertyName("propName2");
    rawUploadRdfSaver.addPropertyDescriptions(inputCol1, importPropertyDescriptions);
    rawUploadRdfSaver.addEntity(inputCol1, ImmutableMap.of("propName1", "value1", "propName2", "val2"));
    rawUploadRdfSaver.addEntity(inputCol1, ImmutableMap.of("propName1", "entVal1", "propName2", "entVal2"));
    final String inputCol2 = rawUploadRdfSaver.addCollection("collection2");
    ImportPropertyDescriptions importPropertyDescriptions1 = new ImportPropertyDescriptions();
    importPropertyDescriptions1.getOrCreate(1).setPropertyName("prop3");
    importPropertyDescriptions1.getOrCreate(2).setPropertyName("prop4");
    rawUploadRdfSaver.addPropertyDescriptions(inputCol2, importPropertyDescriptions1);
    rawUploadRdfSaver.addEntity(inputCol2, ImmutableMap.of("prop3", "value1", "prop4", "val2"));
    rawUploadRdfSaver.addEntity(inputCol2, ImmutableMap.of("prop3", "entVal1", "prop4", "entVal2"));
    rdfSerializer.close();
    RdfDataSource rdfDataSource = new RdfDataSource(rmlDataSourceStore, inputCol1, new JexlRowFactory(ImmutableMap.of(), new HashMapBasedJoinHandler()));
    RdfDataSource rdfDataSource2 = new RdfDataSource(rmlDataSourceStore, inputCol2, new JexlRowFactory(ImmutableMap.of(), new HashMapBasedJoinHandler()));
    final List<String> collection1;
    final List<String> collection2;
    try (Stream<Row> stream = rdfDataSource.getRows(new ThrowingErrorHandler())) {
        collection1 = stream.map(x -> x.getRawValue("propName1") + ":" + x.getRawValue("propName2")).collect(toList());
    }
    try (Stream<Row> stream = rdfDataSource2.getRows(new ThrowingErrorHandler())) {
        collection2 = stream.map(x -> x.getRawValue("prop3") + ":" + x.getRawValue("prop4")).collect(toList());
    }
    assertThat(collection1, contains("value1:val2", "entVal1:entVal2"));
    assertThat(collection2, contains("value1:val2", "entVal1:entVal2"));
    dbCreator.close();
}
Also used : RawUploadRdfSaver(nl.knaw.huygens.timbuctoo.v5.bulkupload.RawUploadRdfSaver) ImportPropertyDescriptions(nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ImportPropertyDescriptions) StringStringIsCleanHandler(nl.knaw.huygens.timbuctoo.v5.berkeleydb.isclean.StringStringIsCleanHandler) RmlDataSourceStore(nl.knaw.huygens.timbuctoo.v5.datastores.rmldatasource.RmlDataSourceStore) BdbNonPersistentEnvironmentCreator(nl.knaw.huygens.timbuctoo.v5.dropwizard.BdbNonPersistentEnvironmentCreator) RdfSerializer(nl.knaw.huygens.timbuctoo.v5.rdfio.RdfSerializer) JexlRowFactory(nl.knaw.huygens.timbuctoo.rml.datasource.jexl.JexlRowFactory) LogList(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) HashMapBasedJoinHandler(nl.knaw.huygens.timbuctoo.rml.datasource.joinhandlers.HashMapBasedJoinHandler) BasicDataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData) ThrowingErrorHandler(nl.knaw.huygens.timbuctoo.rml.ThrowingErrorHandler) Row(nl.knaw.huygens.timbuctoo.rml.Row) DataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSetMetaData) BasicDataSetMetaData(nl.knaw.huygens.timbuctoo.v5.dataset.dto.BasicDataSetMetaData) RdfDataSource(nl.knaw.huygens.timbuctoo.v5.rml.RdfDataSource) Test(org.junit.Test)

Example 9 with ImportStatus

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.

the class ImportManager method generateLog.

public Future<ImportStatus> generateLog(String baseUri, String defaultGraph, RdfCreator creator) throws LogStorageFailedException {
    importStatus.start(this.getClass().getSimpleName() + ".generateLog", baseUri);
    try {
        // add to the log structure
        int[] index = new int[1];
        logListStore.updateData(logList -> {
            index[0] = logList.addEntry(LogEntry.create(baseUri, defaultGraph, creator));
            return logList;
        });
        // schedule processing
        return executorService.submit(() -> processLogsUntil(index[0]));
    } catch (IOException e) {
        importStatus.addError("Could not update logList", e);
        throw new LogStorageFailedException(e);
    }
}
Also used : LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) IOException(java.io.IOException)

Example 10 with ImportStatus

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.

the class JsonLdEditEndpoint method submitChanges.

@PUT
public Response submitChanges(String jsonLdImport, @PathParam("user") String ownerId, @PathParam("dataset") String dataSetId, @HeaderParam("authorization") String authHeader) throws LogStorageFailedException {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user.get(), ownerId, dataSetId);
    if (!dataSetOpt.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    final DataSet dataSet = dataSetOpt.get();
    final QuadStore quadStore = dataSet.getQuadStore();
    final ImportManager importManager = dataSet.getImportManager();
    final Response response = checkWriteAccess(dataSet, user, permissionFetcher);
    if (response != null) {
        return response;
    }
    try {
        final Future<ImportStatus> promise = importManager.generateLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), fromCurrentState(documentLoader, jsonLdImport, quadStore, TIM_USERS + user.get().getPersistentId(), UUID.randomUUID().toString(), Clock.systemUTC()));
        return handleImportManagerResult(promise);
    } catch (IOException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (ConcurrentUpdateException e) {
        return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) IOException(java.io.IOException) ConcurrentUpdateException(nl.knaw.huygens.timbuctoo.v5.jsonldimport.ConcurrentUpdateException) PUT(javax.ws.rs.PUT)

Aggregations

ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)8 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 File (java.io.File)4 ImportManager (nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager)4 LogEntry (nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry)4 LogList (nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList)4 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)4 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Optional (java.util.Optional)3 POST (javax.ws.rs.POST)3 MediaType (javax.ws.rs.core.MediaType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Response (javax.ws.rs.core.Response)2