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());
}
});
}
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)));
}
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());
}
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()))));
}
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());
}
});
}
}
Aggregations