use of nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection in project timbuctoo by HuygensING.
the class AutocompleteServiceTest method searchRequests1000ResultsWhenTheQueryIsEmpty.
@Test
public void searchRequests1000ResultsWhenTheQueryIsEmpty() throws Exception {
UUID id = UUID.randomUUID();
String collectionName = "wwpersons";
QuickSearchResult entity = QuickSearchResult.create("[TEMP] An author", id, 2);
TimbuctooActions timbuctooActions = mock(TimbuctooActions.class);
given(timbuctooActions.getCollectionMetadata(anyString())).willReturn(collWithCollectionName(collectionName));
given(timbuctooActions.doQuickSearch(any(), any(), anyString(), anyInt())).willReturn(Lists.newArrayList(entity));
AutocompleteService instance = new AutocompleteService((collection, id1, rev) -> URI.create("http://example.com/" + collection + "/" + id1 + "?rev=" + rev), timbuctooActions);
instance.search(collectionName, Optional.empty(), Optional.empty());
verify(timbuctooActions).doQuickSearch(any(Collection.class), any(QuickSearch.class), any(), intThat(is(1000)));
}
use of nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection 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.core.dto.dataset.Collection in project timbuctoo by HuygensING.
the class TinkerPopOperations method acceptRelation.
@Override
public UUID acceptRelation(Collection collection, CreateRelation createRelation) throws RelationNotPossibleException {
UUID typeId = createRelation.getTypeId();
String userId = createRelation.getCreated().getUserId();
Instant instant = Instant.ofEpochMilli(createRelation.getCreated().getTimeStamp());
requireCommit = true;
RelationType descs = getRelationDescription(traversal, typeId).orElseThrow(notPossible("Relation type " + typeId + " does not exist"));
Vertex sourceV = getEntityByFullIteration(traversal, createRelation.getSourceId()).orElseThrow(notPossible("source is not present"));
Vertex targetV = getEntityByFullIteration(traversal, createRelation.getTargetId()).orElseThrow(notPossible("target is not present"));
// check if the relation already exists
final Optional<EntityRelation> existingEdgeOpt = getEntityRelation(sourceV, targetV, typeId, collection);
if (existingEdgeOpt.isPresent()) {
final EntityRelation existingEdge = existingEdgeOpt.get();
if (!existingEdge.isAccepted()) {
// if not already an active relation
try {
replaceRelation(collection, existingEdge.getTimId(), existingEdge.getRevision(), true, userId, instant);
} catch (NotFoundException e) {
LOG.error("Relation with id '{}' not found. This should not happen.", existingEdge.getTimId());
}
}
return existingEdge.getTimId();
} else {
Collection sourceCollection = getOwnCollectionOfElement(collection.getVre(), sourceV).orElseThrow(notPossible("Source vertex is not part of the VRE of " + collection.getCollectionName()));
Collection targetCollection = getOwnCollectionOfElement(collection.getVre(), targetV).orElseThrow(notPossible("Target vertex is not part of the VRE of " + collection.getCollectionName()));
DirectionalRelationType desc = descs.getForDirection(sourceCollection, targetCollection).orElseThrow(notPossible("You can't have a " + descs.getOutName() + " relation from " + sourceCollection.getEntityTypeName() + " to " + targetCollection.getEntityTypeName() + " or vice versa"));
return createRelation(sourceV, targetV, desc, userId, collection, true, instant);
}
}
use of nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection in project timbuctoo by HuygensING.
the class TinkerPopOperations method deleteEntity.
@Override
public int deleteEntity(Collection collection, UUID id, Change modified) throws NotFoundException {
requireCommit = true;
GraphTraversal<Vertex, Vertex> entityTraversal = entityFetcher.getEntity(traversal, id, null, collection.getCollectionName());
if (!entityTraversal.hasNext()) {
throw new NotFoundException();
}
Vertex entity = entityTraversal.next();
String entityTypesStr = getProp(entity, "types", String.class).orElse("[]");
boolean wasRemoved = false;
if (entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
try {
ArrayNode entityTypes = arrayToEncodedArray.tinkerpopToJson(entityTypesStr);
if (entityTypes.size() == 1) {
entity.property("deleted", true);
wasRemoved = true;
} else {
for (int i = entityTypes.size() - 1; i >= 0; i--) {
JsonNode val = entityTypes.get(i);
if (val != null && val.asText("").equals(collection.getEntityTypeName())) {
entityTypes.remove(i);
wasRemoved = true;
}
}
entity.property("types", entityTypes.toString());
}
} catch (IOException e) {
LOG.error(Logmarkers.databaseInvariant, "property 'types' was not parseable: " + entityTypesStr);
}
} else {
throw new NotFoundException();
}
int newRev = getProp(entity, "rev", Integer.class).orElse(1) + 1;
entity.property("rev", newRev);
entity.edges(Direction.BOTH).forEachRemaining(edge -> {
// Skip the hasEntity and the VERSION_OF edge, which are not real relations, but system edges
if (edge.label().equals(HAS_ENTITY_RELATION_NAME) || edge.label().equals(VERSION_OF)) {
return;
}
Optional<Collection> ownEdgeCol = getOwnCollectionOfElement(collection.getVre(), edge);
if (ownEdgeCol.isPresent()) {
edge.property(ownEdgeCol.get().getEntityTypeName() + "_accepted", false);
}
});
setModified(entity, modified);
entity.property("pid").remove();
Vertex duplicate = duplicateVertex(traversal, entity, indexHandler);
if (wasRemoved) {
listener.onRemoveFromCollection(collection, Optional.of(entity), duplicate);
}
return newRev;
}
use of nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection in project timbuctoo by HuygensING.
the class TinkerPopOperations method retrieveProperty.
@Override
public Optional<RdfReadProperty> retrieveProperty(Vre vre, String entityRdfUri, String propertyUri) {
Optional<Vertex> entityOpt = indexHandler.findVertexInRdfIndex(vre, entityRdfUri);
if (entityOpt.isPresent()) {
Vertex entity = entityOpt.get();
Iterable<Vertex> collectionOfEntity = () -> collectionsFor(entity);
Optional<Collection> colOpt = StreamSupport.stream(collectionOfEntity.spliterator(), false).map(col -> getProp(col, COLLECTION_NAME_PROPERTY_NAME, String.class)).filter(colNameProp -> colNameProp.isPresent()).map(colNameProp -> colNameProp.get()).map(vre::getCollectionForCollectionName).filter(Optional::isPresent).map(Optional::get).findFirst();
if (colOpt.isPresent()) {
Collection collection = colOpt.get();
VertexProperty<String> property = entity.property(createPropName(collection.getEntityTypeName(), propertyUri));
if (property.isPresent()) {
return Optional.of(new RdfReadProperty(propertyUri, property.value()));
}
}
}
return Optional.empty();
}
Aggregations