/// <summary>
 /// Returns a new data masking rule model
 /// </summary>
 /// <param name="rules">The database's data masking rules</param>
 /// <returns>A data masking rule object, initialized for the user provided rule identity</returns>
 protected override DatabaseDataMaskingRuleModel GetRule(IEnumerable<DatabaseDataMaskingRuleModel> rules)
 {
     DatabaseDataMaskingRuleModel rule = new DatabaseDataMaskingRuleModel();
     rule.ResourceGroupName = ResourceGroupName;
     rule.ServerName = ServerName;
     rule.DatabaseName = DatabaseName;
     rule.RuleId = RuleId;
     return rule;
 }
 /// <summary>
 /// Adds the data masking rule that this cmdlet operated on to the list of rules of this database
 /// </summary>
 /// <param name="rules">The data masking rules already defined for this database</param>
 /// <param name="rule">The rule that this cmdlet operated on</param>
 /// <returns>The updated list of data masking rules</returns>
 protected override IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule)
 {
     List<DatabaseDataMaskingRuleModel> rulesList = rules.ToList();
     rulesList.Add(rule);
     return rulesList;
 }
 /// <summary>
 /// Update the rule that this cmdlet operates on based on the user provided values
 /// </summary>
 /// <param name="rules">The data masking rules of the database</param>
 /// <param name="rule">The rule that this cmdlet operates on</param>
 /// <returns>The update list of the database's data masking rules</returns>
 protected abstract IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule);
 /// <summary>
 /// Updates the data masking rule that this cmdlet operated in the list of rules of this database
 /// </summary>
 /// <param name="rules">The data masking rules already defined for this database</param>
 /// <param name="rule">The rule that this cmdlet operated on</param>
 /// <returns>The updated list of data masking rules</returns>
 protected override IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule)
 { 
     return rules;
 }
        /// <summary>
        /// Update the rule this cmdlet is operating on based on the values provided by the user
        /// </summary>
        /// <param name="rule">The rule this cmdlet operates on</param>
        /// <returns>An updated rule model</returns>
        protected DatabaseDataMaskingRuleModel UpdateRule(DatabaseDataMaskingRuleModel rule)
        {
            rule.TableName = TableName;
            rule.ColumnName = ColumnName;
            if(!string.IsNullOrEmpty(MaskingFunction)) // only update if the user provided this value
            {
                rule.MaskingFunction = ModelizeMaskingFunction();
            }

            if(rule.MaskingFunction == Model.MaskingFunction.Text)
            {
                if (PrefixSize != null) // only update if the user provided this value
                {
                    rule.PrefixSize = PrefixSize;
                }

                if (!string.IsNullOrEmpty(ReplacementString)) // only update if the user provided this value
                {
                    rule.ReplacementString = ReplacementString;
                }

                if (SuffixSize != null) // only update if the user provided this value
                {
                    rule.SuffixSize = SuffixSize;
                }

                if(rule.PrefixSize == null)
                {
                    rule.PrefixSize = SecurityConstants.PrefixSizeDefaultValue;
                }

                if (string.IsNullOrEmpty(rule.ReplacementString))
                {
                    rule.ReplacementString = SecurityConstants.ReplacementStringDefaultValue;
                }

                if (rule.SuffixSize == null)
                {
                    rule.SuffixSize = SecurityConstants.SuffixSizeDefaultValue;
                }
            }

            if (rule.MaskingFunction == Model.MaskingFunction.Number)
            {
                if (NumberFrom != null) // only update if the user provided this value
                {
                    rule.NumberFrom = NumberFrom;
                }

                if (NumberTo != null) // only update if the user provided this value
                {
                    rule.NumberTo = NumberTo;
                }

                if (rule.NumberFrom == null)
                {
                    rule.NumberFrom = SecurityConstants.NumberFromDefaultValue;
                }

                if (rule.NumberTo == null)
                {
                    rule.NumberTo = SecurityConstants.NumberToDefaultValue;
                }

                if(rule.NumberFrom > rule.NumberTo)
                {
                    throw new Exception(string.Format(CultureInfo.InvariantCulture, Resources.DataMaskingNumberRuleIntervalDefinitionError));
                }
            }
            return rule;
        }
 /// <summary>
 /// Sets a data masking rule based on the information provided by the model object
 /// </summary>
 public void SetDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     DataMaskingRuleCreateOrUpdateParameters parameters = PolicizeDatabaseDataRuleModel(model);
     Communicator.SetDatabaseDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, model.RuleId, clientId, parameters);
 }
        /// <summary>
        /// Transforms a data masking rule to its cmdlet model representation
        /// </summary>
        private DatabaseDataMaskingRuleModel ModelizeDatabaseDataMaskingRule(DataMaskingRule rule, string resourceGroup, string serverName, string databaseName)
        {
            DatabaseDataMaskingRuleModel dbRuleModel = new DatabaseDataMaskingRuleModel();
            DataMaskingRuleProperties properties = rule.Properties;
            dbRuleModel.ResourceGroupName = resourceGroup;
            dbRuleModel.ServerName = serverName;
            dbRuleModel.DatabaseName = databaseName;
            dbRuleModel.RuleId = properties.Id;
            dbRuleModel.ColumnName = properties.ColumnName;
            dbRuleModel.TableName = properties.TableName;
            dbRuleModel.MaskingFunction = ModelizeMaskingFunction(properties.MaskingFunction);
            dbRuleModel.PrefixSize = ModelizeNullableUint(properties.PrefixSize);
            dbRuleModel.ReplacementString = properties.ReplacementString;
            dbRuleModel.SuffixSize = ModelizeNullableUint(properties.SuffixSize);
            dbRuleModel.NumberFrom = ModelizeNullableDouble(properties.NumberFrom);
            dbRuleModel.NumberTo = ModelizeNullableDouble(properties.NumberTo);
            return dbRuleModel;

        }
 /// <summary>
 /// Takes the cmdlets model object and transform it to the policy as expected by the endpoint
 /// </summary>
 /// <param name="model">The data masking Policy model object</param>
 /// <returns>The communication model object</returns>
 private DataMaskingRuleCreateOrUpdateParameters PolicizeDatabaseDataRuleModel(DatabaseDataMaskingRuleModel model)
 {
     DataMaskingRuleCreateOrUpdateParameters updateParameters = new DataMaskingRuleCreateOrUpdateParameters();
     DataMaskingRuleProperties properties = new DataMaskingRuleProperties();
     updateParameters.Properties = properties;
     properties.Id = model.RuleId;
     properties.TableName = model.TableName;
     properties.ColumnName = model.ColumnName;
     properties.MaskingFunction = PolicizeMaskingFunction(model.MaskingFunction);
     properties.PrefixSize = (model.PrefixSize == null) ? null : model.PrefixSize.ToString();
     properties.ReplacementString = model.ReplacementString;
     properties.SuffixSize = (model.SuffixSize == null) ? null : model.SuffixSize.ToString();
     properties.NumberFrom = (model.NumberFrom == null) ? null : model.NumberFrom.ToString();
     properties.NumberTo = (model.NumberTo == null) ? null : model.NumberTo.ToString();
     return updateParameters;
 }
 /// <summary>
 /// Removes a data masking rule based on the information provided by the model object
 /// </summary>
 public void RemoveDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     Communicator.DeleteDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, model.RuleId, clientId);
 }