public RmResource FindById(string id, AttributesToFetch attributes = null) { Initialize(); attributes = attributes ?? AttributesToFetch.All; var ctx = LogContext.WithConfigFormat(); _log.Debug(ctx.Format("Finding object by id {0} with attributes {1}"), id, attributes.GetNames().ToJSON()); RmResource resource = null; try { var reference = new RmReference(id); if (attributes != AttributesToFetch.All) { attributes = attributes.AppendAttribute(RmResource.AttributeNames.ObjectType.Name); } resource = _defaultClient.Get(reference, attributes.GetNames()); _log.Debug(ctx.Format("Found object with id {0}, type {1}"), id, resource.ObjectType); } catch { // catching exc - meaning that object not found _log.Warn(ctx.Format("Object with id {0} not found"), id); } return(resource); }
public IEnumerable <TResource> EnumerateAll <TResource>(string query, AttributesToFetch attributes = null) where TResource : RmResource { attributes = attributes ?? AttributesToFetch.All; var ctx = LogContext.WithConfigFormat(); Initialize(); try { _log.Debug(ctx.Format("Executing simple enumeration: {0} with attributes {1}"), query, attributes.GetNames().ToJSON()); var results = _defaultClient.Enumerate(query, attributes.GetNames()) .Cast <TResource>() .ToList(); log_query_executed(ctx, "Simple enumeration {0} returned {1} results", query, results.Count); return(results); } catch (Exception exc) { var qee = new QueryExecutionException(query, exc); _log.ErrorException(ctx.Format("Error when trying to execute query " + query), qee); throw qee; } }
public void returns_given_strings_as_array() { var attributes = new AttributesToFetch("attr1", "attr2", "attr3"); var names = attributes.GetNames(); Assert.Equal(new[] { "attr1", "attr2", "attr3" }, names); }
public void returns_null_if_no_names_passed___required_for_underlying_fim2010client_mechanisms__must_be_null_not_empty_collection() { var attributes = new AttributesToFetch(); var names = attributes.GetNames(); Assert.Null(names); }
public void does_not_modify_original_instance() { var attributes = new AttributesToFetch("attr1"); attributes.AppendAttribute("attr2"); var names = attributes.GetNames(); Assert.Equal(new[] { "attr1" }, names); }
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)); }