Search in sources :

Example 1 with Tuple

use of io.lumeer.core.util.Tuple in project engine by Lumeer.

the class SearchFacade method searchDocumentsAndLinksInStem.

private Tuple<? extends java.util.Collection<Document>, ? extends java.util.Collection<LinkInstance>> searchDocumentsAndLinksInStem(final QueryStem stem, final Set<String> fulltexts, final Map<String, Collection> collectionsMap, final Map<String, LinkType> linkTypesMap, @Nullable final Function<Document, Boolean> documentFilter, final ConstraintData constraintData, boolean includeChildDocuments) {
    final Set<Document> allDocuments = new HashSet<>();
    final Set<LinkInstance> allLinkInstances = new HashSet<>();
    var resources = getResourcesFromStem(stem, collectionsMap, linkTypesMap);
    final List<Collection> allCollections = resources.getFirst();
    final List<LinkType> allLinkTypes = resources.getSecond();
    if (allCollections.isEmpty()) {
        return new Tuple<>(allDocuments, allLinkInstances);
    }
    final Map<String, AllowedPermissions> collectionsPermissions = permissionsChecker.getCollectionsPermissions(allCollections);
    final Map<String, AllowedPermissions> linkTypesPermissions = permissionsChecker.getLinkTypesPermissions(allLinkTypes);
    final Query query = new Query(Collections.singletonList(stem), fulltexts, null, null);
    var hasMoreDocuments = true;
    var page = 0;
    while (hasMoreDocuments) {
        var previousCollection = allCollections.get(0);
        var firstCollectionDocuments = getDocumentsByCollection(previousCollection, page, FETCH_SIZE);
        var previousDocuments = filterDocumentsByDocumentFilter(firstCollectionDocuments, documentFilter);
        final Set<Document> currentDocuments = new HashSet<>(previousDocuments);
        final Set<LinkInstance> currentLinkInstances = new HashSet<>();
        for (String linkTypeId : stem.getLinkTypeIds()) {
            var linkType = linkTypesMap.get(linkTypeId);
            var collection = getOtherCollection(linkType, collectionsMap, Utils.computeIfNotNull(previousCollection, Collection::getId));
            if (linkType != null && previousCollection != null) {
                var links = getLinkInstancesByLinkType(linkType, getDocumentsIds(previousDocuments));
                var documents = getDocumentsByCollection(collection, getLinkDocumentsIds(links), documentFilter);
                currentDocuments.addAll(documents);
                currentLinkInstances.addAll(links);
                previousCollection = collection;
                previousDocuments = documents;
            }
        }
        if (!currentDocuments.isEmpty()) {
            var result = DataFilter.filterDocumentsAndLinksByQueryFromJson(new ArrayList<>(currentDocuments), allCollections, allLinkTypes, new ArrayList<>(currentLinkInstances), query, collectionsPermissions, linkTypesPermissions, constraintData, includeChildDocuments, language);
            allDocuments.addAll(result.getFirst());
            allLinkInstances.addAll(result.getSecond());
        }
        page++;
        hasMoreDocuments = !firstCollectionDocuments.isEmpty();
    }
    return new Tuple<>(allDocuments, allLinkInstances);
}
Also used : Query(io.lumeer.api.model.Query) LinkInstance(io.lumeer.api.model.LinkInstance) Document(io.lumeer.api.model.Document) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) Collection(io.lumeer.api.model.Collection) LinkType(io.lumeer.api.model.LinkType) Tuple(io.lumeer.core.util.Tuple) HashSet(java.util.HashSet)

Example 2 with Tuple

use of io.lumeer.core.util.Tuple in project engine by Lumeer.

the class SearchFacade method searchTasksDocumentsAndLinks.

private Tuple<List<Document>, List<LinkInstance>> searchTasksDocumentsAndLinks(final Query query, boolean isPublic, boolean includeChildDocuments) {
    final List<Collection> collections = collectionDao.getCollectionsByPurpose(CollectionPurposeType.Tasks);
    // we don't fetch links for tasks
    final List<LinkType> linkTypes = Collections.emptyList();
    final Query tasksQuery = modifyQueryForTasks(isPublic, query, collections);
    if (tasksQuery == null) {
        return new Tuple<>(Collections.emptyList(), Collections.emptyList());
    }
    final Map<String, Collection> collectionsMap = getCollectionsMap(collections);
    final Map<String, LinkType> linkTypesMap = getLinkTypeMap(linkTypes);
    final Function<Document, Boolean> documentFilter = query.isEmpty() ? document -> !CollectionPurposeUtils.isDoneState(document.getData(), collectionsMap.get(document.getCollectionId())) : null;
    return searchDocumentsAndLinks(tasksQuery, includeChildDocuments, !isPublic && query.isEmpty(), collectionsMap, linkTypesMap, documentFilter, isPublic);
}
Also used : Query(io.lumeer.api.model.Query) Collection(io.lumeer.api.model.Collection) LinkType(io.lumeer.api.model.LinkType) Document(io.lumeer.api.model.Document) Tuple(io.lumeer.core.util.Tuple)

Example 3 with Tuple

use of io.lumeer.core.util.Tuple in project engine by Lumeer.

the class SearchFacade method searchDocumentsAndLinksByFulltexts.

private Tuple<? extends java.util.Collection<Document>, ? extends java.util.Collection<LinkInstance>> searchDocumentsAndLinksByFulltexts(final Set<String> fulltexts, final Map<String, Collection> collectionsMap, final Map<String, LinkType> linkTypesMap, @Nullable final Function<Document, Boolean> documentFilter, final ConstraintData constraintData, boolean includeChildDocuments) {
    final Set<Document> allDocuments = new HashSet<>();
    final Set<LinkInstance> allLinkInstances = new HashSet<>();
    // because we are filtering documents (or links) without linked documents, so it is safe to fetch more
    var fetchSizeMultiplier = 3;
    var fetchSize = FETCH_SIZE * fetchSizeMultiplier;
    collectionsMap.values().forEach(collection -> {
        final List<Collection> collections = Collections.singletonList(collection);
        final Map<String, AllowedPermissions> collectionsPermissions = permissionsChecker.getCollectionsPermissions(collections);
        final Map<String, AllowedPermissions> linkTypesPermissions = Collections.emptyMap();
        final Query query = new Query(Collections.emptyList(), fulltexts, null, null);
        var hasMoreDocuments = true;
        var page = 0;
        while (hasMoreDocuments) {
            final List<Document> pagedDocuments = getDocumentsByCollection(collection, page, fetchSize);
            final List<Document> filteredDocuments = filterDocumentsByDocumentFilter(pagedDocuments, documentFilter);
            if (!filteredDocuments.isEmpty()) {
                var result = DataFilter.filterDocumentsAndLinksByQueryFromJson(new ArrayList<>(filteredDocuments), collections, Collections.emptyList(), new ArrayList<>(), query, collectionsPermissions, linkTypesPermissions, constraintData, includeChildDocuments, language);
                allDocuments.addAll(result.getFirst());
            }
            hasMoreDocuments = !pagedDocuments.isEmpty();
            page++;
        }
    });
    linkTypesMap.values().forEach(linkType -> {
        final List<LinkType> linkTypes = Collections.singletonList(linkType);
        final List<Collection> collections = linkType.getCollectionIds().stream().map(collectionsMap::get).filter(Objects::nonNull).collect(Collectors.toList());
        final Map<String, AllowedPermissions> collectionsPermissions = permissionsChecker.getCollectionsPermissions(collections);
        final Map<String, AllowedPermissions> linkTypesPermissions = permissionsChecker.getLinkTypesPermissions(linkTypes);
        final Query query = new Query(Collections.emptyList(), fulltexts, null, null);
        var hasMoreLinks = true;
        var page = 0;
        while (hasMoreLinks) {
            final List<LinkInstance> linkInstances = getLinkInstancesByLinkType(linkType, page, fetchSize);
            if (!linkInstances.isEmpty()) {
                var result = DataFilter.filterDocumentsAndLinksByQueryFromJson(new ArrayList<>(), collections, linkTypes, linkInstances, query, collectionsPermissions, linkTypesPermissions, constraintData, true, language);
                allLinkInstances.addAll(result.getSecond());
            }
            hasMoreLinks = !linkInstances.isEmpty();
            page++;
        }
    });
    return new Tuple<>(allDocuments, allLinkInstances);
}
Also used : Query(io.lumeer.api.model.Query) LinkInstance(io.lumeer.api.model.LinkInstance) Document(io.lumeer.api.model.Document) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) Collection(io.lumeer.api.model.Collection) LinkType(io.lumeer.api.model.LinkType) Tuple(io.lumeer.core.util.Tuple) HashSet(java.util.HashSet)

Example 4 with Tuple

use of io.lumeer.core.util.Tuple in project engine by Lumeer.

the class SearchFacade method searchDocumentsAndLinks.

private Tuple<List<Document>, List<LinkInstance>> searchDocumentsAndLinks(final Query query, boolean includeChildDocuments, boolean shouldCheckQuery, final Map<String, Collection> collectionsMap, final Map<String, LinkType> linkTypesMap, @Nullable final Function<Document, Boolean> documentFilter, boolean isPublic) {
    final Query encodedQuery = checkQuery(query, collectionsMap, linkTypesMap, shouldCheckQuery);
    final Set<Document> allDocuments = new HashSet<>();
    final Set<LinkInstance> allLinkInstances = new HashSet<>();
    if (encodedQuery.containsStems()) {
        ConstraintData constraintData = createConstraintData();
        encodedQuery.getStems().forEach(stem -> {
            var result = stem.containsAnyFilter() || encodedQuery.getFulltexts().size() > 0 ? searchDocumentsAndLinksInStem(stem, encodedQuery.getFulltexts(), collectionsMap, linkTypesMap, documentFilter, constraintData, includeChildDocuments) : searchDocumentsAndLinksInStemWithoutFilters(stem, collectionsMap, linkTypesMap, documentFilter, isPublic);
            allDocuments.addAll(result.getFirst());
            allLinkInstances.addAll(result.getSecond());
        });
    } else if (encodedQuery.getFulltexts().size() > 0) {
        var result = searchDocumentsAndLinksByFulltexts(encodedQuery.getFulltexts(), collectionsMap, linkTypesMap, documentFilter, createConstraintData(), includeChildDocuments);
        allDocuments.addAll(result.getFirst());
        allLinkInstances.addAll(result.getSecond());
    } else {
        var result = searchDocumentsAndLinksByEmptyQuery(collectionsMap, linkTypesMap, documentFilter, isPublic);
        allDocuments.addAll(result.getFirst());
        allLinkInstances.addAll(result.getSecond());
    }
    var mappedDocuments = documentAdapter.mapDocumentsData(new ArrayList<>(allDocuments), getCurrentUserId(), workspaceKeeper.getProjectId());
    var mappedLinkInstances = linkInstanceAdapter.mapLinkInstancesData(new ArrayList<>(allLinkInstances));
    return new Tuple<>(mappedDocuments, mappedLinkInstances);
}
Also used : ConstraintData(io.lumeer.api.model.ConstraintData) Query(io.lumeer.api.model.Query) LinkInstance(io.lumeer.api.model.LinkInstance) Document(io.lumeer.api.model.Document) Tuple(io.lumeer.core.util.Tuple) HashSet(java.util.HashSet)

Example 5 with Tuple

use of io.lumeer.core.util.Tuple in project engine by Lumeer.

the class DocumentFacade method readCollectionAndDocument.

private Tuple<Collection, Document> readCollectionAndDocument(String documentId) {
    final Document document = DocumentUtils.loadDocumentWithData(documentDao, dataDao, documentId);
    Collection collection = collectionDao.getCollectionById(document.getCollectionId());
    return new Tuple<>(collection, document);
}
Also used : Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) Tuple(io.lumeer.core.util.Tuple)

Aggregations

Tuple (io.lumeer.core.util.Tuple)11 Document (io.lumeer.api.model.Document)9 LinkInstance (io.lumeer.api.model.LinkInstance)7 Collection (io.lumeer.api.model.Collection)6 LinkType (io.lumeer.api.model.LinkType)5 Query (io.lumeer.api.model.Query)4 CreateDocument (io.lumeer.engine.api.event.CreateDocument)4 UpdateDocument (io.lumeer.engine.api.event.UpdateDocument)4 HashSet (java.util.HashSet)4 DataDocument (io.lumeer.engine.api.data.DataDocument)3 AllowedPermissions (io.lumeer.api.model.AllowedPermissions)2 ResourceType (io.lumeer.api.model.ResourceType)2 ConstraintManager (io.lumeer.core.constraint.ConstraintManager)2 DefaultConfigurationProducer (io.lumeer.core.facade.configuration.DefaultConfigurationProducer)2 CreateLinkInstance (io.lumeer.engine.api.event.CreateLinkInstance)2 UpdateLinkInstance (io.lumeer.engine.api.event.UpdateLinkInstance)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2