Search in sources :

Example 6 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class SingleEntityNTriple method get.

@GET
public Response get(@PathParam("collection") String collectionName, @PathParam("id") UUIDParam id, @QueryParam("rev") Integer rev) {
    return transactionEnforcer.executeAndReturn(timbuctooActions -> {
        try {
            Collection collection = timbuctooActions.getCollectionMetadata(collectionName);
            ReadEntity entity = timbuctooActions.getEntity(collection, id.get(), rev);
            URI rdfUri = entity.getRdfUri();
            String rdfString = rdfUri == null ? uriHelper.fromResourceUri(makeUrl(collectionName, id.get())).toString() : rdfUri.toString();
            StringBuilder sb = new StringBuilder();
            addRdfProp(rdfString, sb, "id", entity.getId() + "", "https://www.ietf.org/rfc/rfc4122.txt");
            entity.getRdfAlternatives().forEach(alt -> sb.append(asNtriples(new LinkTriple(rdfString, SAME_AS_PRED, alt))));
            TriplePropertyConverter converter = new TriplePropertyConverter(collection, rdfString);
            for (TimProperty<?> timProperty : entity.getProperties()) {
                try {
                    timProperty.convert(converter).getRight().forEach(triple -> sb.append(asNtriples(triple)));
                } catch (IOException e) {
                    LOG.error("Could not convert property with name '{}' and value '{}'", timProperty.getName(), timProperty.getValue());
                }
            }
            entity.getRelations().forEach(rel -> sb.append(asNtriples(new LinkTriple(rdfString, getRelationRdfUri(rel), getEntityRdfUri(rel)))));
            return commitAndReturn(Response.ok(sb.toString()).build());
        } catch (InvalidCollectionException e) {
            return commitAndReturn(Response.status(BAD_REQUEST).entity(e.getMessage()).build());
        } catch (NotFoundException e) {
            return commitAndReturn(Response.status(NOT_FOUND).build());
        }
    });
}
Also used : ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) LinkTriple(nl.knaw.huygens.timbuctoo.rdf.LinkTriple) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) IOException(java.io.IOException) TriplePropertyConverter(nl.knaw.huygens.timbuctoo.rdf.conversion.TriplePropertyConverter) URI(java.net.URI) GET(javax.ws.rs.GET)

Example 7 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class AutocompleteServiceTest method searchFiltersKeywordsByType.

@Test
public void searchFiltersKeywordsByType() throws InvalidCollectionException {
    String query = "*foo bar*";
    String keywordType = "maritalStatus";
    String collectionName = "wwkeywords";
    UUID id = UUID.randomUUID();
    QuickSearchResult readEntity = QuickSearchResult.create("a keyword", id, 2);
    TimbuctooActions timbuctooActions = mock(TimbuctooActions.class);
    given(timbuctooActions.getCollectionMetadata(anyString())).willReturn(keywordCollWithCollectionName(collectionName));
    given(timbuctooActions.doQuickSearch(any(), any(), anyString(), anyInt())).willReturn(Lists.newArrayList(readEntity));
    UrlGenerator urlGenerator = (coll, id1, rev) -> URI.create("http://example.com/" + coll + "/" + id1 + "?rev=" + rev);
    AutocompleteService instance = new AutocompleteService(urlGenerator, timbuctooActions);
    JsonNode result = instance.search(collectionName, Optional.of(query), Optional.of(keywordType));
    assertThat(result.toString(), sameJSONAs(jsnA(jsnO("value", jsn("a keyword"), "key", jsn("http://example.com/wwkeywords/" + id.toString() + "?rev=2"))).toString()));
    verify(timbuctooActions).doQuickSearch(argThat(hasProperty("collectionName", equalTo(collectionName))), any(QuickSearch.class), argThat(is(keywordType)), intThat(is(50)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) UrlGenerator(nl.knaw.huygens.timbuctoo.crud.UrlGenerator) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Lists(com.google.common.collect.Lists) QuickSearch(nl.knaw.huygens.timbuctoo.core.dto.QuickSearch) BDDMockito.given(org.mockito.BDDMockito.given) MockitoHamcrest.argThat(org.mockito.hamcrest.MockitoHamcrest.argThat) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) Is.is(org.hamcrest.core.Is.is) TimbuctooActions(nl.knaw.huygens.timbuctoo.core.TimbuctooActions) Matchers.anyInt(org.mockito.Matchers.anyInt) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) QuickSearchResult(nl.knaw.huygens.timbuctoo.core.dto.QuickSearchResult) MockitoHamcrest.intThat(org.mockito.hamcrest.MockitoHamcrest.intThat) Test(org.junit.Test) UUID(java.util.UUID) Mockito.verify(org.mockito.Mockito.verify) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) CollectionStubs.collWithCollectionName(nl.knaw.huygens.timbuctoo.core.dto.dataset.CollectionStubs.collWithCollectionName) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) CollectionStubs.keywordCollWithCollectionName(nl.knaw.huygens.timbuctoo.core.dto.dataset.CollectionStubs.keywordCollWithCollectionName) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) QuickSearchResult(nl.knaw.huygens.timbuctoo.core.dto.QuickSearchResult) TimbuctooActions(nl.knaw.huygens.timbuctoo.core.TimbuctooActions) QuickSearch(nl.knaw.huygens.timbuctoo.core.dto.QuickSearch) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UUID(java.util.UUID) UrlGenerator(nl.knaw.huygens.timbuctoo.crud.UrlGenerator) Test(org.junit.Test)

Example 8 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class AutocompleteServiceTest method searchThrowsWhenTheCollectionNameDoesNotExist.

@Test(expected = InvalidCollectionException.class)
public void searchThrowsWhenTheCollectionNameDoesNotExist() throws InvalidCollectionException {
    TimbuctooActions timbuctooActions = mock(TimbuctooActions.class);
    given(timbuctooActions.getCollectionMetadata(anyString())).willThrow(new InvalidCollectionException(""));
    AutocompleteService underTest = new AutocompleteService(null, timbuctooActions);
    underTest.search("nonexistent", Optional.empty(), Optional.empty());
}
Also used : TimbuctooActions(nl.knaw.huygens.timbuctoo.core.TimbuctooActions) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) Test(org.junit.Test)

Example 9 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class AutocompleteService method search.

public JsonNode search(String collectionName, Optional<String> query, Optional<String> keywordType) throws InvalidCollectionException {
    final Collection collection = timbuctooActions.getCollectionMetadata(collectionName);
    int limit = query.isPresent() ? 50 : 1000;
    String queryString = query.orElse(null);
    QuickSearch quickSearch = QuickSearch.fromQueryString(queryString);
    List<QuickSearchResult> results = timbuctooActions.doQuickSearch(collection, quickSearch, keywordType.orElse(null), limit);
    return jsnA(results.stream().map(entity -> jsnO("value", jsn(entity.getIndexedValue()), "key", jsn(autoCompleteUrlFor.apply(collectionName, entity.getId(), entity.getRev()).toString()))));
}
Also used : JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) QuickSearchResult(nl.knaw.huygens.timbuctoo.core.dto.QuickSearchResult) UrlGenerator(nl.knaw.huygens.timbuctoo.crud.UrlGenerator) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) List(java.util.List) QuickSearch(nl.knaw.huygens.timbuctoo.core.dto.QuickSearch) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) TimbuctooActions(nl.knaw.huygens.timbuctoo.core.TimbuctooActions) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) QuickSearchResult(nl.knaw.huygens.timbuctoo.core.dto.QuickSearchResult) QuickSearch(nl.knaw.huygens.timbuctoo.core.dto.QuickSearch) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)

Example 10 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class SingleEntity method delete.

@DELETE
public Response delete(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id) {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<User> newUser = user;
    if (!newUser.isPresent()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    } else {
        return transactionEnforcer.executeAndReturn(timbuctooActions -> {
            JsonCrudService jsonCrudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
            try {
                jsonCrudService.delete(collectionName, id.get(), newUser.get());
                return commitAndReturn(Response.noContent().build());
            } catch (InvalidCollectionException e) {
                return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (NotFoundException e) {
                return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
            } catch (PermissionFetchingException e) {
                return rollbackAndReturn(Response.status(Response.Status.FORBIDDEN).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (IOException e) {
                return rollbackAndReturn(Response.status(Response.Status.BAD_REQUEST).entity(jsnO("message", jsn(e.getMessage()))).build());
            }
        });
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) IOException(java.io.IOException) DELETE(javax.ws.rs.DELETE)

Aggregations

InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)5 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)5 IOException (java.io.IOException)4 URI (java.net.URI)4 Optional (java.util.Optional)4 UUID (java.util.UUID)4 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)4 JsonBuilder.jsn (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn)4 JsonBuilder.jsnA (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA)4 JsonBuilder.jsnO (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO)4 Test (org.junit.Test)4 TimbuctooActions (nl.knaw.huygens.timbuctoo.core.TimbuctooActions)3 JsonCrudService (nl.knaw.huygens.timbuctoo.crud.JsonCrudService)3 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)3 PermissionFetchingException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Mockito.mock (org.mockito.Mockito.mock)3