/// <summary>
 /// Creates a new instance of the <see cref="AuthorizationRule"/> class 
 /// with the specified permissions, target and exception sets.
 /// </summary>
 /// 
 /// <param name="permissions">
 /// The permissions granted. 
 /// </param>
 /// 
 /// <param name="targetSets">
 /// The set or sets of health record items to which this rule applies.
 /// </param>
 /// 
 /// <param name="exceptionSets">
 /// The set or sets of health record items to which this rule does not 
 /// apply even if contained in a set defined by 
 /// <paramref name="targetSets"/>.
 /// </param>
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="permissions"/> parameter is 
 /// <see cref="Microsoft.Health.HealthRecordItemPermissions.None"/>.
 /// </exception>
 ///         
 public AuthorizationRule(
     HealthRecordItemPermissions permissions,
     IList<AuthorizationSetDefinition> targetSets,
     IList<AuthorizationSetDefinition> exceptionSets)
     : this(null, null, permissions, targetSets, exceptionSets, false,
         AuthorizationRuleDisplayFlags.None)
 {
 }
        /// <summary>
        /// Creates a new instance of the <see cref="AuthorizationRule"/> class 
        /// with the specified name, reason, permissions, target, exception sets,
        /// optional and display flags.
        /// </summary>
        /// 
        /// <param name="name">
        /// The name uniquely identifying this rule in a set
        /// </param>
        ///
        /// <param name="reason">
        /// The reason why an application wants this access
        /// </param>
        ///
        /// <param name="permissions">
        /// The permissions granted. 
        /// </param>
        /// 
        /// <param name="targetSets">
        /// The set or sets of health record items to which this rule applies.
        /// </param>
        /// 
        /// <param name="exceptionSets">
        /// The set or sets of health record items to which this rule does not 
        /// apply even if contained in a set defined by 
        /// <paramref name="targetSets"/>.
        /// </param>
        ///
        /// <param name="isOptional">
        /// Flag indicating whether or not this rule is optional
        /// </param>
        ///
        /// <param name="displayFlags">
        /// Flags controlling how to display this rule
        /// </param>
        /// 
        /// <exception cref="ArgumentException">
        /// The <paramref name="permissions"/> parameter is 
        /// <see cref="Microsoft.Health.HealthRecordItemPermissions.None"/>.
        /// </exception>
        ///         
        public AuthorizationRule(
            string name,
            string reason, 
            HealthRecordItemPermissions permissions,
            IList<AuthorizationSetDefinition> targetSets,
            IList<AuthorizationSetDefinition> exceptionSets,
            bool isOptional,
            AuthorizationRuleDisplayFlags displayFlags)
        {
            _name = name;
            _isOptional = isOptional;
            _displayFlags = displayFlags;

            if (!String.IsNullOrEmpty(reason))
            {
                CultureSpecificReasons.DefaultValue = reason;
            }

            Validator.ThrowArgumentExceptionIf(
                permissions == HealthRecordItemPermissions.None,
                "permissions",
                "AuthorizationRuleBadPermissions");

            _permissions = permissions;

            if (targetSets != null)
            {
                _targetSets =
                    new ReadOnlyCollection<AuthorizationSetDefinition>(
                        targetSets);
            }
            else
            {
                _targetSets =
                    new ReadOnlyCollection<AuthorizationSetDefinition>(
                        new AuthorizationSetDefinition[0]);
            }

            if (exceptionSets != null)
            {
                _exceptionSets =
                    new ReadOnlyCollection<AuthorizationSetDefinition>(
                        exceptionSets);
            }
            else
            {
                _exceptionSets =
                    new ReadOnlyCollection<AuthorizationSetDefinition>(
                        new AuthorizationSetDefinition[0]);
            }
        }
 /// <summary>
 /// Creates a new instance of the <see cref="AuthorizationRule"/> class
 /// with the specified permissions.
 /// </summary>
 /// 
 /// <param name="permissions">
 /// The permissions granted.
 /// </param>
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="permissions"/> parameter is 
 /// <see cref="Microsoft.Health.HealthRecordItemPermissions.None"/>.
 /// </exception>
 /// 
 public AuthorizationRule(
     HealthRecordItemPermissions permissions)
     : this(permissions, null, null)
 {
 }
        internal void ParseXml(XPathNavigator navigator)
        {
            _typeId = new Guid(navigator.SelectSingleNode(
                            "thing-type-id").Value);

            XPathNavigator onlinePermissions
                = navigator.SelectSingleNode("online-access-permissions");
            _onlineAccessPermissions = HealthRecordItemPermissions.None;

            if (onlinePermissions != null)
            {
                XPathNodeIterator nodes
                    = onlinePermissions.Select("permission");
                try
                {
                    foreach (XPathNavigator navPerms in nodes)
                    {
                        _onlineAccessPermissions
                            |= (HealthRecordItemPermissions)Enum.Parse(
                                    typeof(HealthRecordItemPermissions),
                                    navPerms.Value);
                    }
                }
                catch (ArgumentException)
                {
                    _onlineAccessPermissions
                        = HealthRecordItemPermissions.None;
                }
            }

            XPathNavigator offlinePermissions
                = navigator.SelectSingleNode("offline-access-permissions");
            _offlineAccessPermissions = HealthRecordItemPermissions.None;

            if (offlinePermissions != null)
            {
                XPathNodeIterator nodes
                    = offlinePermissions.Select("permission");
                try
                {
                    foreach (XPathNavigator navPerms in nodes)
                    {
                        _offlineAccessPermissions
                            |= (HealthRecordItemPermissions)Enum.Parse(
                                    typeof(HealthRecordItemPermissions),
                                    navPerms.Value);
                    }
                }
                catch (ArgumentException)
                {
                    _offlineAccessPermissions
                        = HealthRecordItemPermissions.None;
                }
            }
        }