Search in sources :

Example 1 with AllowedPermissions

use of io.lumeer.api.model.AllowedPermissions 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 AllowedPermissions

use of io.lumeer.api.model.AllowedPermissions 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 3 with AllowedPermissions

use of io.lumeer.api.model.AllowedPermissions in project engine by Lumeer.

the class CollectionFacade method runRule.

public void runRule(final Collection collection, final String ruleId) {
    if (adapter.getDocumentsCountByCollection(collection.getId()) > 2_000) {
        throw new UnsuccessfulOperationException("Too many documents in the source collection");
    }
    final Rule rule = collection.getRules().get(ruleId);
    if (rule != null && rule.getType() == Rule.RuleType.AUTO_LINK) {
        final AutoLinkRule autoLinkRule = new AutoLinkRule(rule);
        final String otherCollectionId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getCollection1() : autoLinkRule.getCollection2();
        final String attributeId = autoLinkRule.getCollection1().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
        final Attribute attribute = collection.getAttributes().stream().filter(a -> a.getId().equals(attributeId)).findFirst().orElse(null);
        final Collection otherCollection = getCollection(otherCollectionId);
        final String otherAttributeId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
        final Attribute otherAttribute = otherCollection.getAttributes().stream().filter(a -> a.getId().equals(otherAttributeId)).findFirst().orElse(null);
        final Map<String, AllowedPermissions> permissions = permissionsChecker.getCollectionsPermissions(List.of(collection, otherCollection));
        if (adapter.getDocumentsCountByCollection(otherCollectionId) > 10_000) {
            throw new UnsuccessfulOperationException("Too many documents in the target collection");
        }
        final LinkType linkType = linkTypeDao.getLinkType(autoLinkRule.getLinkType());
        final AutoLinkBatchTask task = taskFactory.getInstance(AutoLinkBatchTask.class);
        task.setupBatch(autoLinkRule, linkType, collection, attribute, otherCollection, otherAttribute, getCurrentUser(), permissions);
        taskExecutor.submitTask(task);
    } else if (rule != null && rule.getType() == Rule.RuleType.CRON) {
        final CronRule cronRule = new CronRule(rule);
        List<Document> documents = new ArrayList<>();
        if (cronRule.getViewId() != null) {
            try {
                final View view = viewDao.getViewById(cronRule.getViewId());
                final User user = AuthenticatedUser.getMachineUser();
                final AllowedPermissions allowedPermissions = AllowedPermissions.allAllowed();
                documents = DocumentUtils.getDocuments(collectionDao, documentDao, dataDao, userDao, groupDao, selectionListDao, getOrganization(), getProject(), view.getQuery(), user, cronRule.getLanguage(), allowedPermissions, null);
                documents = documents.stream().filter(document -> document.getCollectionId().equals(collection.getId())).collect(Collectors.toList());
            } catch (ResourceNotFoundException ignore) {
            }
        }
        RuleTask task = taskFactory.getInstance(RuleTask.class);
        task.setRule(rule.getName(), rule, collection, documents);
        taskExecutor.submitTask(task);
    }
}
Also used : AutoLinkBatchTask(io.lumeer.core.task.AutoLinkBatchTask) TaskExecutor(io.lumeer.core.task.TaskExecutor) CollectionUtil(io.lumeer.api.util.CollectionUtil) ZonedDateTime(java.time.ZonedDateTime) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) LinkTypeDao(io.lumeer.storage.api.dao.LinkTypeDao) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) CollectionDao(io.lumeer.storage.api.dao.CollectionDao) ResourceType(io.lumeer.api.model.ResourceType) DefaultViewConfigDao(io.lumeer.storage.api.dao.DefaultViewConfigDao) Map(java.util.Map) ResourceCommentDao(io.lumeer.storage.api.dao.ResourceCommentDao) CollectionAdapter(io.lumeer.core.adapter.CollectionAdapter) Permission(io.lumeer.api.model.Permission) RoleType(io.lumeer.api.model.RoleType) View(io.lumeer.api.model.View) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) Document(io.lumeer.api.model.Document) Set(java.util.Set) NoResourcePermissionException(io.lumeer.core.exception.NoResourcePermissionException) Collectors(java.util.stream.Collectors) LinkType(io.lumeer.api.model.LinkType) Objects(java.util.Objects) AttributesResource(io.lumeer.api.model.common.AttributesResource) List(java.util.List) ResourceUtils(io.lumeer.api.util.ResourceUtils) PostConstruct(javax.annotation.PostConstruct) FavoriteItemDao(io.lumeer.storage.api.dao.FavoriteItemDao) Attribute(io.lumeer.api.model.Attribute) ContextualTaskFactory(io.lumeer.core.task.ContextualTaskFactory) UserDao(io.lumeer.storage.api.dao.UserDao) HashMap(java.util.HashMap) SelectionListDao(io.lumeer.storage.api.dao.SelectionListDao) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) Function(java.util.function.Function) CollectionPurpose(io.lumeer.api.model.CollectionPurpose) GroupDao(io.lumeer.storage.api.dao.GroupDao) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) DataDao(io.lumeer.storage.api.dao.DataDao) ViewDao(io.lumeer.storage.api.dao.ViewDao) CodeGenerator(io.lumeer.core.util.CodeGenerator) Rule(io.lumeer.api.model.Rule) DocumentDao(io.lumeer.storage.api.dao.DocumentDao) Permissions(io.lumeer.api.model.Permissions) ResourceAdapter(io.lumeer.core.adapter.ResourceAdapter) DocumentUtils(io.lumeer.core.util.DocumentUtils) FileAttachment(io.lumeer.api.model.FileAttachment) Project(io.lumeer.api.model.Project) LinkInstanceDao(io.lumeer.storage.api.dao.LinkInstanceDao) UnsuccessfulOperationException(io.lumeer.engine.api.exception.UnsuccessfulOperationException) ConversionFacade(io.lumeer.core.facade.conversion.ConversionFacade) RuleTask(io.lumeer.core.task.RuleTask) RequestScoped(javax.enterprise.context.RequestScoped) CronRule(io.lumeer.api.model.rule.CronRule) AutoLinkBatchTask(io.lumeer.core.task.AutoLinkBatchTask) Collection(io.lumeer.api.model.Collection) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) Attribute(io.lumeer.api.model.Attribute) View(io.lumeer.api.model.View) AllowedPermissions(io.lumeer.api.model.AllowedPermissions) UnsuccessfulOperationException(io.lumeer.engine.api.exception.UnsuccessfulOperationException) Collection(io.lumeer.api.model.Collection) List(java.util.List) ArrayList(java.util.ArrayList) RuleTask(io.lumeer.core.task.RuleTask) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Rule(io.lumeer.api.model.Rule) CronRule(io.lumeer.api.model.rule.CronRule) LinkType(io.lumeer.api.model.LinkType) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) CronRule(io.lumeer.api.model.rule.CronRule)

Example 4 with AllowedPermissions

use of io.lumeer.api.model.AllowedPermissions in project engine by Lumeer.

the class PermissionsChecker method getCollectionPermissions.

public AllowedPermissions getCollectionPermissions(final Collection collection) {
    User user = authenticatedUser.getCurrentUser();
    Set<RoleType> roles = permissionAdapter.getUserRolesInResource(getOrganization(), getProject(), collection, user);
    Set<RoleType> rolesWithView = new HashSet<>(permissionAdapter.getUserRolesInCollectionWithView(getOrganization(), getProject(), collection, user));
    rolesWithView.addAll(roles);
    return new AllowedPermissions(roles, rolesWithView);
}
Also used : AllowedPermissions(io.lumeer.api.model.AllowedPermissions) User(io.lumeer.api.model.User) RoleType(io.lumeer.api.model.RoleType) HashSet(java.util.HashSet)

Example 5 with AllowedPermissions

use of io.lumeer.api.model.AllowedPermissions in project engine by Lumeer.

the class PermissionsChecker method getLinkTypePermissions.

public AllowedPermissions getLinkTypePermissions(final LinkType linkType) {
    User user = authenticatedUser.getCurrentUser();
    Set<RoleType> roles = permissionAdapter.getUserRolesInLinkType(getOrganization(), getProject(), linkType, user);
    Set<RoleType> rolesWithView = new HashSet<>(permissionAdapter.getUserRolesInLinkTypeWithView(getOrganization(), getProject(), linkType, user));
    rolesWithView.addAll(roles);
    return new AllowedPermissions(roles, rolesWithView);
}
Also used : AllowedPermissions(io.lumeer.api.model.AllowedPermissions) User(io.lumeer.api.model.User) RoleType(io.lumeer.api.model.RoleType) HashSet(java.util.HashSet)

Aggregations

AllowedPermissions (io.lumeer.api.model.AllowedPermissions)8 RoleType (io.lumeer.api.model.RoleType)6 Document (io.lumeer.api.model.Document)5 HashSet (java.util.HashSet)5 Collection (io.lumeer.api.model.Collection)4 LinkType (io.lumeer.api.model.LinkType)4 Query (io.lumeer.api.model.Query)4 User (io.lumeer.api.model.User)4 LinkInstance (io.lumeer.api.model.LinkInstance)3 View (io.lumeer.api.model.View)3 Attribute (io.lumeer.api.model.Attribute)2 FileAttachment (io.lumeer.api.model.FileAttachment)2 Language (io.lumeer.api.model.Language)2 ResourceType (io.lumeer.api.model.ResourceType)2 CollectionUtil (io.lumeer.api.util.CollectionUtil)2 TaskExecutor (io.lumeer.core.task.TaskExecutor)2 DocumentUtils (io.lumeer.core.util.DocumentUtils)2 Tuple (io.lumeer.core.util.Tuple)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2