Example #1
0
        public void can_fetch_items_sorted_descending_by_given_attribute()
        {
            var sorting = new SortingInstructions(
                RmResource.AttributeNames.DisplayName.Name
                , SortOrder.Descending
                );
            var page = _client.EnumeratePage <RmPerson>("/Person", Pagination.FirstPageOfSize(3), sorting);

            var sorteredLocally = page.Items.OrderByDescending(x => x.DisplayName).ToList();
            var fromFIM         = page.Items.ToList();

            Assert.Equal(sorteredLocally, fromFIM);
        }
Example #2
0
        public void the_same_page_with_different_sorting_should_return_different_results()
        {
            var sorting1 = new SortingInstructions(
                RmResource.AttributeNames.DisplayName.Name
                , SortOrder.Descending
                );
            var page1 = _client.EnumeratePage <RmPerson>("/Person", Pagination.FirstPageOfSize(3), sorting1);

            var sorting2 = new SortingInstructions(
                RmResource.AttributeNames.DisplayName.Name
                , SortOrder.Ascending
                );
            var page2 = _client.EnumeratePage <RmPerson>("/Person", Pagination.FirstPageOfSize(3), sorting2);

            Assert.NotEqual(page1.Items, page2.Items);
        }
Example #3
0
        private DataPage <TResource> ExecuteAdjustedQuery <TResource>(string query, Pagination pagination, SortingInstructions sorting, AttributesToFetch attributes = null) where TResource : RmResource
        {
            attributes = attributes ?? AttributesToFetch.All;

            Initialize();

            var ctx = LogContext.WithConfigFormat();

            _log.Debug(ctx.Format("Executing query {0} with paging: {1}, sorting {2} and attributes {3}"), query, pagination.ToJSON(), sorting.ToJSON(), attributes.GetNames().ToJSON());

            // first request - only to get enumeration context and total rows count
            var enumerationRequest = new EnumerationRequest(query);

            // do not fetch any items - total count only; however this cannot be set to 0 because then total count is not returned (is always set to 0)
            enumerationRequest.MaxElements = 1;
            // get only ObjectID attribute to minimize overhead caused by this operation which returns elements not used for further processing
            enumerationRequest.Selection = new List <string>
            {
                RmResource.AttributeNames.ObjectID.Name
            };
            var enumerateResponse = _pagedQueriesClient.Enumerate(enumerationRequest);

            long totalCount = enumerateResponse.Count ?? 0;

            // second request - to actually get desired data
            var pullRequest = new PullRequest();

            // set enumeration context from previous query
            pullRequest.EnumerationContext = enumerateResponse.EnumerationContext;

            // if attributes names to fetch defined...
            var attributeNames = attributes.GetNames();

            if (attributeNames != null)
            {
                // ... set them to context
                pullRequest.EnumerationContext.Selection         = new Selection();
                pullRequest.EnumerationContext.Selection.@string = attributeNames.ToList();
            }
            else
            {
                pullRequest.EnumerationContext.Selection = null;
            }

            // if paging defined...
            if (pagination.PageSize != Pagination.AllPagesSize)
            {
                // set current page's first row index...
                pullRequest.EnumerationContext.CurrentIndex = pagination.GetFirstRowIndex();
                // ...and page size
                pullRequest.MaxElements = pagination.PageSize;
            }
            // ... if not - get all elements
            else
            {
                // reset current row index...
                pullRequest.EnumerationContext.CurrentIndex = 0;
                // ... page size to max (this may throw if message size exceeds configured maximum, but this situation - getting all items using this method - is not likely to happen)
                pullRequest.MaxElements = int.MaxValue;
            }

            // if sorting defined...
            if (sorting != SortingInstructions.None)
            {
                var sortingAttribute = new SortingAttribute()
                {
                    Ascending = sorting.Order == SortOrder.Ascending,
                    Value     = sorting.AttributeName
                };
                // due to implementation details of these classes, new instances of each of them needs to be created
                pullRequest.EnumerationContext.Sorting = new Sorting
                {
                    SortingAttribute = new List <SortingAttribute>
                    {
                        sortingAttribute
                    }
                };
            }

            var pullResponse = _pagedQueriesClient.Pull(pullRequest);
            var results      = _defaultClient.ResourceFactory.CreateResource(pullResponse)
                               .Cast <TResource>()
                               .ToList();

            log_query_executed(ctx, "Paged query {0} returned {1} results and {2} total count", query, results.Count, totalCount);

            return(new DataPage <TResource>(results, totalCount));
        }
Example #4
0
        public DataPage <TResource> EnumeratePage <TResource>(string query, Pagination pagination, SortingInstructions sorting, AttributesToFetch attributes = null) where TResource : RmResource
        {
            if (pagination == null)
            {
                throw new ArgumentNullException("pagination");
            }
            if (sorting == null)
            {
                throw new ArgumentNullException("sorting");
            }

            try
            {
                return(ExecuteAdjustedQuery <TResource>(query, pagination, sorting, attributes));
            }
            catch (Exception exc)
            {
                var qee = new QueryExecutionException(query, exc);

                _log.ErrorException("Error when trying to execute query " + query, qee);

                throw qee;
            }
        }