public void SearchTestPagedResultsSortDesc() { ResourceManagementClient c = new ResourceManagementClient(); string query = String.Format("/{0}[starts-with('{1}', 'reftest')]", UnitTestHelper.ObjectTypeUnitTestObjectName, AttributeNames.AccountName); List <string> attributesToGet = new List <string>() { AttributeNames.AccountName }; List <SortingAttribute> sortAttributes = new List <SortingAttribute>(); sortAttributes.Add(new SortingAttribute("AccountName", false)); SearchResultPager pager = c.GetResourcesPaged(query, 2, attributesToGet, sortAttributes); Assert.AreEqual(pager.TotalCount, 6); ResourceObject[] results = pager.GetNextPage().ToArray(); // Move forward through the result set, getting two items per page Assert.AreEqual(2, results.Length); Assert.AreEqual(true, pager.HasMoreItems); Assert.AreEqual("reftest6", results[0].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest5", results[1].Attributes[AttributeNames.AccountName].StringValue); results = pager.GetNextPage().ToArray(); Assert.AreEqual(2, results.Length); Assert.AreEqual(true, pager.HasMoreItems); Assert.AreEqual("reftest4", results[0].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest3", results[1].Attributes[AttributeNames.AccountName].StringValue); results = pager.GetNextPage().ToArray(); Assert.AreEqual(2, results.Length); Assert.AreEqual(false, pager.HasMoreItems); Assert.AreEqual("reftest2", results[0].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest1", results[1].Attributes[AttributeNames.AccountName].StringValue); /// Jump back in the result set and change the page size pager.CurrentIndex = 2; pager.PageSize = 4; results = pager.GetNextPage().ToArray(); Assert.AreEqual(4, results.Length); Assert.AreEqual(false, pager.HasMoreItems); Assert.AreEqual("reftest4", results[0].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest3", results[1].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest2", results[2].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest1", results[3].Attributes[AttributeNames.AccountName].StringValue); }
public DSResourceSet GetResourceByQuery(string query, string[] attributes, int pageSize = 0, int index = 0, ResourceOption resourceOption = null) { ResourceOption option = resourceOption == null ? new ResourceOption() : resourceOption; ResourceManagementClient client = getClient(option.ConnectionInfo); client.RefreshSchema(); DSResourceSet retVal = new DSResourceSet(); List <SortingAttribute> sortingAttributes = getSortingAttributes(option.SortingAttributes); if (pageSize == 0) { SearchResultCollection src = sortingAttributes.Count == 0 ? client.GetResources(query, attributes) as SearchResultCollection : client.GetResources(query, attributes, sortingAttributes) as SearchResultCollection; if (src != null) { retVal.TotalCount = src.Count; foreach (ResourceObject resource in src) { retVal.Resources.Add(convertToDSResource(client, resource, attributes, false, option)); } } } else { SearchResultPager srp = sortingAttributes.Count == 0 ? client.GetResourcesPaged(query, pageSize, attributes) : client.GetResourcesPaged(query, pageSize, attributes, sortingAttributes); if (index >= 0) { srp.CurrentIndex = index; } srp.PageSize = pageSize; foreach (ResourceObject resource in srp.GetNextPage()) { retVal.Resources.Add(convertToDSResource(client, resource, attributes, false, option)); } retVal.TotalCount = srp.TotalCount; retVal.HasMoreItems = srp.HasMoreItems; } return(retVal); }
public void SearchTestPagedSortedDescending() { ResourceManagementClient c = new ResourceManagementClient(); var query = String.Format("/{0}[starts-with('{1}', 'reftest')]", UnitTestHelper.ObjectTypeUnitTestObjectName, AttributeNames.AccountName); var attributesToGet = new List <string>(); attributesToGet.Add(AttributeNames.AccountName); SearchResultPager results = c.GetResourcesPaged(query, 2, attributesToGet, AttributeNames.AccountName, false); Assert.AreEqual(6, results.TotalCount); results.CurrentIndex = 3; ResourceObject[] arrayResults = results.GetNextPage().ToArray(); Assert.AreEqual(2, arrayResults.Length); Assert.AreEqual("reftest3", arrayResults[0].Attributes[AttributeNames.AccountName].StringValue); Assert.AreEqual("reftest2", arrayResults[1].Attributes[AttributeNames.AccountName].StringValue); }
public Stream GetResourcesPaged() { try { IncomingWebRequestContext context = WebOperationContext.Current?.IncomingRequest; if (context == null) { throw new InvalidOperationException(); } string currentIndexParam = context.UriTemplateMatch.QueryParameters["index"]; string token = context.UriTemplateMatch.QueryParameters["token"]; int pageSize = WebResponseHelper.GetPageSize(context); int index = currentIndexParam == null ? -1 : Convert.ToInt32(currentIndexParam); SearchResultPager p = ResourceManagementWebServicev2.GetSearchResultPager(context, pageSize, token); token = token ?? Guid.NewGuid().ToString(); if (index >= 0) { p.CurrentIndex = index; } p.PageSize = pageSize; int oldIndex = p.CurrentIndex; PagedResultSet results = new PagedResultSet { Results = p.GetNextPage().ToList(), }; ResourceManagementWebServicev2.GetPageUris(context, oldIndex, pageSize, token, p, out Uri previousPageUri, out Uri nextPageUri); results.NextPage = nextPageUri?.ToString(); results.PreviousPage = previousPageUri?.ToString(); results.TotalCount = p.TotalCount; results.HasMoreItems = results.NextPage != null; ResourceManagementWebServicev2.searchCache.Add(ResourceManagementWebServicev2.BuildCacheKey(token), p, new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0, 5, 0) }); return(WebResponseHelper.GetResponse(results, false)); } catch (WebFaultException) { throw; } catch (WebFaultException <Error> ) { throw; } catch (Exception ex) { ResourceManagementWebServicev2.HandleException(ex); throw; } }
static void Main(string[] args) { try { string filter = args.Length > 0 ? args[0] : "/*[starts-with(DisplayName,'%')]"; int index = args.Length > 1 ? Convert.ToInt32(args[1]) : -1; string token = args.Length > 2 ? args[2] : null; int pageSize = 100; ResourceManagementClient client = new ResourceManagementClient(); client.RefreshSchema(); ResourceObject resourceObject = client.GetResource( "418fc8b4-d5b3-4a0a-87c6-0984812a6781", new string[] { "DisplayName", "CreatedTime", "Manager" }, new CultureInfo("de-DE"), true); SearchResultPager p = getSearchResultPager(client, filter, pageSize, token); token = token ?? Guid.NewGuid().ToString(); if (index >= 0) { p.CurrentIndex = index; } p.PageSize = pageSize; int oldIndex = p.CurrentIndex; PagedResultSet results = new PagedResultSet { Results = p.GetNextPage().ToList(), }; results.TotalCount = p.TotalCount; results.HasMoreItems = index + pageSize < p.TotalCount; searchCache.Add(buildCacheKey(token), p, new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0, 5, 0) }); foreach (ResourceObject resource in results.Results) { Console.WriteLine(resource.DisplayName); } Console.WriteLine("Total Count: " + results.TotalCount); Console.WriteLine("More Items to Load: " + results.HasMoreItems); //Uri nextPageUri; //Uri previousPageUri; //ResourceManagementWebServicev2.GetPageUris(context, oldIndex, pageSize, token, p, out previousPageUri, out nextPageUri); //results.NextPage = nextPageUri?.ToString(); //results.PreviousPage = previousPageUri?.ToString(); //results.TotalCount = p.TotalCount; //results.HasMoreItems = results.NextPage != null; //ResourceManagementWebServicev2.searchCache.Add(ResourceManagementWebServicev2.BuildCacheKey(token), p, new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0, 5, 0) }); //return WebResponseHelper.GetResponse(results, false); } catch (Exception ex) { throw ex; } }