//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Configuration;
using Config = System.Configuration.Configuration;
using Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration.Helpers; 
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration
{
    /// <summary>
    /// Action that encrypts a configuration section
    /// </summary>
    public class EncryptConfigurationSectionAction : ConfigurableAction
    {
        #region Input Properties

        string sectionName;
        /// <summary>
        /// Gets or sets the name of the section.
        /// </summary>
        /// <value>The name of the section.</value>
        [Input(Required = true)]
        public string SectionName
        {
            get { return sectionName; }
            set { sectionName = value; }
        }

        Config configuration;
        /// <summary>
        /// Gets or sets the configuration.
        /// </summary>
        /// <value>The configuration.</value>
        [Input(Required = true)]
        public Config Configuration
        {
            get
            {
                return configuration;
            }
            set
            {
                configuration = value;
            }
        }

        ProtectionConfigurationProvider protectionType;
        /// <summary>
        /// Gets or sets the type of the protection.
        /// </summary>
        /// <value>The type of the protection.</value>
        [Input(Required = true)]
        public ProtectionConfigurationProvider ProtectionType
        {
            get
            {
                return protectionType;
            }
            set
            {
                protectionType = value;
            }
        } 
        #endregion

        #region Output Properties
        #endregion

        #region ConfigurableAction Implementation
        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            ConfigurationSection sectionToEncrypt =
                configuration.GetSection(sectionName);

            if (sectionToEncrypt.SectionInformation.IsProtected)
            {
                sectionToEncrypt.SectionInformation.UnprotectSection();
            }

            sectionToEncrypt.SectionInformation.ProtectSection(
                    Enum.GetName(
                        typeof(ProtectionConfigurationProvider),
                        protectionType));
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            //Not Implemented
        } 
        #endregion
    }
}
