/// <summary>
 /// Gets the health record item specified by its ID.
 /// </summary>
 /// 
 /// <param name="itemId">
 /// The ID of the health record item to retrieve.
 /// </param>
 /// 
 /// <param name="sections">
 /// The data sections of the health record item to retrieve.
 /// </param>
 /// 
 /// <returns>
 /// A <see cref="HealthRecordItem"/> with the specified data sections 
 /// filled out.
 /// </returns>
 /// 
 /// <remarks>
 /// This method accesses the HealthVault service across the network.
 /// </remarks>
 /// 
 /// <exception cref="HealthServiceException">
 /// The HealthVault service returned an error.
 /// </exception>
 /// 
 public HealthRecordItem GetItem(
     Guid itemId,
     HealthRecordItemSections sections)
 {
     HealthRecordSearcher searcher = CreateSearcher();
     return searcher.GetSingleItem(itemId, sections);
 }
        /// <summary>
        /// Gets the health record items related to this record filtered on the
        /// specified type.
        /// </summary>
        /// 
        /// <param name="typeId">
        /// A unique identifier for the type of health record item to filter 
        /// on.
        /// </param>
        /// 
        /// <param name="sections">
        /// The data sections of the health record item to retrieve.
        /// </param>
        /// 
        /// <returns>
        /// A collection of the health record items related to this record
        /// that match the specified type identifier.
        /// </returns>
        /// 
        /// <remarks>
        /// This method accesses the HealthVault service across the network.
        /// </remarks>
        /// 
        /// <exception cref="HealthServiceException">
        /// The HealthVault service returned an error.
        /// </exception>
        /// 
        public HealthRecordItemCollection GetItemsByType(
            Guid typeId,
            HealthRecordItemSections sections)
        {
            HealthRecordSearcher searcher = CreateSearcher(typeId);
            searcher.Filters[0].View.Sections = sections;

            ReadOnlyCollection<HealthRecordItemCollection> results =
                searcher.GetMatchingItems();

            // Since we only applied a single filter we should
            // only have a single group

            return results[0];
        }
        /// <summary>
        /// Gets a single health record item from the associated record by 
        /// using the item identifier.
        /// </summary>
        /// 
        /// <param name="itemId">
        /// The unique identifier for the health record item.
        /// </param>
        /// 
        /// <param name="sections">
        /// The data sections of the health record item that should be retrieved.
        /// </param>
        /// 
        /// <returns>
        /// An instance of a <see cref="Microsoft.Health.HealthRecordItem"/> 
        /// representing the health record item with the specified identifier.
        /// </returns>
        /// 
        /// <remarks>
        /// This method accesses the HealthVault service across the network.
        /// <br/><br/>
        /// All filters are cleared and replaced with a single filter
        /// for the specified item.
        /// </remarks>
        /// 
        /// <exception cref="HealthServiceException">
        /// The server returned something other than a code of 
        /// HealthServiceStatusCode.OK, or the result count did not equal one (1).
        /// -or-
        /// <see cref="Microsoft.Health.HealthRecordSearcher.Filters"/> is empty
        /// or contains invalid filters.
        /// </exception>
        /// 
        public HealthRecordItem GetSingleItem(
            Guid itemId,
            HealthRecordItemSections sections)
        {
            // Create a new searcher to get the item.
            HealthRecordSearcher searcher = new HealthRecordSearcher(Record);

            HealthRecordFilter filter = new HealthRecordFilter();
            filter.ItemIds.Add(itemId);
            filter.View.Sections = sections;
            filter.CurrentVersionOnly = true;

            searcher.Filters.Add(filter);

            ReadOnlyCollection<HealthRecordItemCollection> resultSet =
                HealthVaultPlatform.GetMatchingItems(Record.Connection, Record, searcher);

            // Check in case HealthVault returned invalid data.
            if (resultSet.Count > 1)
            {
                HealthServiceResponseError error = new HealthServiceResponseError();
                error.Message =
                    ResourceRetriever.GetResourceString(
                        "GetSingleThingTooManyResults");

                HealthServiceException e =
                    HealthServiceExceptionHelper.GetHealthServiceException(
                        HealthServiceStatusCode.MoreThanOneThingReturned,
                        error);
                throw e;
            }

            HealthRecordItem result = null;
            if (resultSet.Count == 1)
            {
                HealthRecordItemCollection resultGroup = resultSet[0];

                if (resultGroup.Count > 1)
                {
                    HealthServiceResponseError error = new HealthServiceResponseError();
                    error.Message =
                        ResourceRetriever.GetResourceString(
                            "GetSingleThingTooManyResults");

                    HealthServiceException e =
                        HealthServiceExceptionHelper.GetHealthServiceException(
                            HealthServiceStatusCode.MoreThanOneThingReturned,
                            error);
                    throw e;
                }

                if (resultGroup.Count == 1)
                {
                    result = resultGroup[0];
                }
            }
            return result;
        }