public string GetSearchRequest(ISearchExpression expression, bool onlyMy = true)
        {
            var queryBuilder = QueryBuilderImpl.CreateEmptyQueryBuilder();

            var currentUser          = _context.GetCurrentPerson();
            var currentUserPositions = currentUser.Positions.ToArray();
            var allTasksTypes        = _taskTypesService.AllTaskTypes
                                       .Union(_taskTypesService.AllWorkflowTypes)
                                       .ToArray();

            var types = GetTypesFromExpression <NextTaskTypeArgumentToken>(expression, allTasksTypes, out _);

            queryBuilder.Must(ObjectFields.TypeId.BeAnyOf(types.Select(x => x.Id).ToArray()));

            AddAttributesSearch(expression, queryBuilder);

            var myToken = expression.Tokens.OfType <NextTaskMySearchToken>().ToList();

            if ((!currentUser.IsAdmin && onlyMy) || (!currentUser.IsAdmin && !onlyMy && myToken.Any()) || (currentUser.IsAdmin && myToken.Any()))
            {
                var occur          = myToken.FirstOrDefault()?.Context?.GetTermOccur() ?? TermOccur.Must;
                var searchObjects  = new List <ISearchTerm>();
                var attributeNames = new HashSet <string>();

                foreach (var name in allTasksTypes)
                {
                    attributeNames.UnionWith(name.Attributes.Where(x => x.Type == MAttrType.OrgUnit).Select(x => x.Name));
                }

                foreach (var attribute in attributeNames)
                {
                    var field = AttributeFields.OrgUnit(attribute);
                    searchObjects.Add(field.BeAnyOf(currentUserPositions));
                }

                queryBuilder.AddAnyOf(searchObjects.ToArray(), occur);
            }

            var searchPhraseTokens = expression.Tokens.OfType <NextTaskSearchPhraseToken>();

            foreach (var tokenGroup in searchPhraseTokens.GroupBy(x => x.Context.GetTermOccur()))
            {
                var keywords = tokenGroup.Select(x => EscapeKeyword(x.Value)).ToList();
                foreach (var keyword in keywords)
                {
                    queryBuilder.Add(ObjectFields.AllText.Be(keyword), tokenGroup.Key);
                }
            }
            var quotedSearchPhraseTokens = expression.Tokens.OfType <NextTaskQuotedSearchPhraseToken>();

            foreach (var tokenGroup in quotedSearchPhraseTokens.GroupBy(x => x.Context.GetTermOccur()))
            {
                var keywords = tokenGroup.Select(x => EscapeSearchPhrase(x.Value)).ToList();
                foreach (var keyword in keywords)
                {
                    queryBuilder.Add(ObjectFields.AllText.Be(keyword), tokenGroup.Key);
                }
            }
            return(queryBuilder.ToString());
        }
        public IQueryBuilder GetSearchRequest(SearchKind searchKind)
        {
            var builder = QueryBuilderImpl.CreateEmptyQueryBuilder();

            if (_context == null)
            {
                return(builder);
            }

            if (_taskTypesService == null)
            {
                return(builder);
            }

            var allTaskTypes = _taskTypesService.AllTaskTypes;

            if (!allTaskTypes.Any())
            {
                return(builder);
            }

            var allWorkflowTypes = _taskTypesService.AllWorkflowTypes;

            if (!allWorkflowTypes.Any())
            {
                return(builder);
            }

            var notRootTypes = allTaskTypes.Select(x => x.Id).ToList();

            notRootTypes.AddRange(allWorkflowTypes.Select(t => t.Id));
            builder.Must(ObjectFields.TypeId.BeAnyOf(notRootTypes.ToArray()));
            return(builder);
        }
예제 #3
0
        public Task <DSearchResult> SearchObjects(string searchString, bool isContextSearch, Guid searchContextObjectId)
        {
            var request = CreateRequestForObject(searchString);

            // context search
            if (isContextSearch && searchContextObjectId != Guid.Empty && searchContextObjectId != DObject.RootId)
            {
                var queryBuilder = QueryBuilderImpl.CreateEmptyQueryBuilder();
                queryBuilder.Must(ObjectFields.Context.Be(searchContextObjectId));
                //queryBuilder.Must(ObjectFields.SearchContextObjectId.Be(_searchContextObjectId));
                var str = request.SearchString;
                str     = $"{str} {queryBuilder}";
                request = new SearchRequest(str);
            }

            // todo for files?
            var searchDefinition       = CreateSearchDefinition(request.SearchString);
            var searchCompletionSource = new TaskCompletionSource <DSearchResult>();

            _searchKeeper[searchDefinition.Id] = searchCompletionSource;
            _apiService.AddSearch(searchDefinition);
            return(searchCompletionSource.Task);
        }
예제 #4
0
        public SearchRequest GetSearchRequest(ISearchExpression expression)
        {
            var searchTargets = GetSearchTargets(expression);

            var attributesQueryBuilder = searchTargets.HasFlag(SearchTarget.Attributes)
                ? QueryBuilderImpl.CreateEmptyQueryBuilder()
                : null;

            var filesQueryBuilder = searchTargets.HasFlag(SearchTarget.Files)
                ? QueryBuilderImpl.CreateFilesQueryBuilder(_context)
                : null;

            var userTypes = QueryBuilderImpl.GetUserTypes(_context);
            var types     = GetTypesFromExpression <TypeArgumentToken>(expression, userTypes.ToList(), out var isFiltered).ToList();
            var typeIds   = types.Select(x => x.Id).ToArray();

            attributesQueryBuilder?.Must(ObjectFields.TypeId.BeAnyOf(typeIds));
            filesQueryBuilder?.Must(ObjectFields.TypeId.BeAnyOf(typeIds));

            if (isFiltered && types.All(x => !x.HasFiles))
            {
                filesQueryBuilder = null;
            }

            var objectStates = GetObjectStates(expression);

            attributesQueryBuilder?.Must(ObjectFields.ObjectState.BeAnyOf(objectStates));

            AddAttributesSearch(expression, attributesQueryBuilder);

            var searchPhraseTokens = expression.Tokens.OfType <SearchPhraseToken>();

            foreach (var tokenGroup in searchPhraseTokens.GroupBy(x => x.Context.GetTermOccur()))
            {
                var keywords = tokenGroup.Select(x => EscapeKeyword(x.Value)).ToList();
                foreach (var keyword in keywords)
                {
                    attributesQueryBuilder?.Add(ObjectFields.AllText.Be(keyword), tokenGroup.Key);
                    filesQueryBuilder?.Add(FileFields.FileContent.Be(keyword), tokenGroup.Key);
                }
            }

            var quotedSearchPhraseTokens = expression.Tokens.OfType <QuotedSearchPhraseToken>();

            foreach (var tokenGroup in quotedSearchPhraseTokens.GroupBy(x => x.Context.GetTermOccur()))
            {
                var keywords = tokenGroup.Select(x => EscapeSearchPhrase(x.Value)).ToList();
                foreach (var keyword in keywords)
                {
                    attributesQueryBuilder?.Add(ObjectFields.AllText.Be(keyword), tokenGroup.Key);
                    filesQueryBuilder?.Add(FileFields.FileContent.Be(keyword), tokenGroup.Key);
                }
            }

            var snapshotReasonTokens = expression.Tokens.OfType <SnapshotReasonArgumentToken>();

            foreach (var tokenGroup in snapshotReasonTokens.GroupBy(x => x.Context.GetTermOccur()))
            {
                var snapshotReasons = tokenGroup.Select(x => x.Value).ToList();
                if (snapshotReasons.Any())
                {
                    attributesQueryBuilder?.Add(ObjectFields.AllSnapshotsReason.ContainsAll(snapshotReasons.Select(r => EscapeStringValue(r)).ToArray()), tokenGroup.Key);
                }
            }

            AddIntFieldSearch <AuthorArgumentToken>(ids => ids, (builder, personIds, occur) => builder.Add(ObjectFields.CreatorId.BeAnyOf(personIds), occur), expression, attributesQueryBuilder);

            AddIntFieldSearch <SignatureAwaitingArgumentToken>(ids => GetPositions(_context, ids), (builder, personIds, occur) => builder.Add(ObjectFields.SignatureAwaitingBy.BeAnyOf(personIds), occur), expression, attributesQueryBuilder);
            AddIntFieldSearch <SignedArgumentToken>(ids => GetPositions(_context, ids), (builder, personIds, occur) => builder.Add(ObjectFields.SignedBy.BeAnyOf(personIds), occur), expression, attributesQueryBuilder);


            AddDateSearch(
                token => token.Context[TokenBase.GroupParentKey].Value is CreatedKeywordToken,
                datesRange => ObjectFields.CreatedDate.BeInRange(datesRange.FromUtc, datesRange.ToUtc),
                datesRange => FileFields.CreatedDate.BeInRange(datesRange.FromUtc, datesRange.ToUtc),
                expression, attributesQueryBuilder, filesQueryBuilder);

            AddDateSearch(
                token => token.Context[TokenBase.GroupParentKey].Value is SnapshotCreatedKeywordToken,
                datesRange => ObjectFields.SnapshotsCreated.BeInRange(datesRange.FromUtc, datesRange.ToUtc),
                datesRange => FileFields.CreatedDate.BeInRange(datesRange.FromUtc, datesRange.ToUtc),
                expression, attributesQueryBuilder, filesQueryBuilder);

            var attributesSearchString = attributesQueryBuilder != null
                ? attributesQueryBuilder.ToString()
                : null;

            var filesSearchString = filesQueryBuilder != null
                                    //&& (_context.GetClientFeatures() == ClientFeatures.Enterprise || _context.GetClientFeatures() == ClientFeatures.Bim)
                                    //&& _connectionListener.IsOnline
                ? filesQueryBuilder.ToString()
                : null;

            var searchContextValueToken = expression.Tokens.FirstOrDefault(x => x is SearchContextArgumentToken) as SearchContextArgumentToken;
            var isContextSearch         = searchContextValueToken == null || searchContextValueToken.IsContextSearch;

            return(new SearchRequest(attributesSearchString, filesSearchString, isContextSearch));
        }