Пример #1
0
        /// <summary>
        /// Perform an Item search by SearchName
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="searchCriteria">SearchCriteria object</param>
        /// <param name="startOffset">Search start offset</param>
        /// <param name="pageSize">Page Size</param>
        /// <returns></returns>
        public async Task <ItemNameSearchResultSet> SearchName(
            int userId,
            ItemNameSearchCriteria searchCriteria,
            int startOffset,
            int pageSize)
        {
            var param = new DynamicParameters();

            param.Add("@userId", userId);
            param.Add("@query", searchCriteria.Query);
            param.Add("@projectIds", SqlConnectionWrapper.ToDataTable(searchCriteria.ProjectIds));
            if (searchCriteria.PredefinedTypeIds != null && searchCriteria.PredefinedTypeIds.Any())
            {
                param.Add("@predefinedTypeIds", SqlConnectionWrapper.ToDataTable(searchCriteria.PredefinedTypeIds));
            }
            if (searchCriteria.ItemTypeIds != null && searchCriteria.ItemTypeIds.Any())
            {
                param.Add("@itemTypeIds", SqlConnectionWrapper.ToDataTable(searchCriteria.ItemTypeIds));
            }
            param.Add("@excludedPredefineds", SqlConnectionWrapper.ToDataTable(GetExcludedPredefineds(searchCriteria)));
            param.Add("@startOffset", startOffset);
            param.Add("@pageSize", pageSize);
            param.Add("@maxSearchableValueStringSize", _searchConfigurationProvider.MaxSearchableValueStringSize);

            List <ItemNameSearchResult> items;

            try
            {
                items = (await _connectionWrapper.QueryAsync <ItemNameSearchResult>("SearchItemNameByItemTypes",
                                                                                    param,
                                                                                    commandType: CommandType.StoredProcedure,
                                                                                    commandTimeout: _searchConfigurationProvider.SearchTimeout)).ToList();
            }
            catch (SqlException sqlException)
            {
                switch (sqlException.Number)
                {
                // Sql timeout error
                case ErrorCodes.SqlTimeoutNumber:
                    throw new SqlTimeoutException("Server did not respond with a response in the allocated time. Please try again later.", ErrorCodes.Timeout);
                }
                throw;
            }

            var itemIds = items.Select(i => i.ItemId).ToList();

            var itemIdsPermissions =
                // Always getting permissions for the Head version of an artifact.
                await _artifactPermissionsRepository.GetArtifactPermissions(itemIds, userId);

            IDictionary <int, IEnumerable <Artifact> > itemsNavigationPaths;

            if (searchCriteria.IncludeArtifactPath)
            {
                itemsNavigationPaths =
                    await
                    _artifactRepository.GetArtifactsNavigationPathsAsync(userId, itemIds, false);
            }
            else
            {
                itemsNavigationPaths = new Dictionary <int, IEnumerable <Artifact> >();
            }

            // items without permission should be removed
            items.RemoveAll(item => !itemIdsPermissions.ContainsKey(item.ItemId) || !itemIdsPermissions[item.ItemId].HasFlag(RolePermissions.Read));

            var joinedResult = from item in items
                               join permission in itemIdsPermissions.AsEnumerable()
                               on item.ItemId equals permission.Key
                               join path in itemsNavigationPaths
                               on item.Id equals path.Key into paths
                               from lpath in paths.DefaultIfEmpty()
                               select new { item, permission, lpath };

            foreach (var result in joinedResult)
            {
                result.item.Permissions = result.permission.Value;
                if (searchCriteria.IncludeArtifactPath)
                {
                    result.item.ArtifactPath         = result.lpath.Value.Select(a => a.Name).ToList();
                    result.item.IdPath               = result.lpath.Value.Select(a => a.Id).ToList();
                    result.item.ParentPredefinedType = result.lpath.Value.Select(a => a.PredefinedType).FirstOrDefault();
                }
            }

            return(new ItemNameSearchResultSet
            {
                Items = items,
                PageItemCount = items.Count
            });
        }