//===============================================================================
// 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 System.Web.Configuration;
using Config = System.Configuration.Configuration;
using Identity = System.Web.Configuration.IdentitySection;
#endregion

namespace Microsoft.Practices.SoftwareFactories.ServiceFactory.Actions.Configuration
{
    /// <summary>
    /// Action thats set the connection string entry on the Configuration File
    /// </summary>
    public class SetConnectionStringEntryAction : ConfigurableAction
    {
        #region Input Properties
        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; }
        }

        private string connectionStringEntryName;

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

        private string connectionStringEntryValue;

        /// <summary>
        /// Gets or sets the connection string entry value.
        /// </summary>
        /// <value>The connection string entry value.</value>
        [Input(Required = true)]
        public string ConnectionStringEntryValue
        {
            get { return connectionStringEntryValue; }
            set { connectionStringEntryValue = value; }
        }

        private string providerName;

        /// <summary>
        /// Gets or sets the name of the provider.
        /// </summary>
        /// <value>The name of the provider.</value>
        [Input(Required = true)]
        public string ProviderName
        {
            get { return providerName; }
            set { providerName = value; }
        }
        #endregion

        #region Action Implementation
        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

            if(connectionStringsSection.ConnectionStrings[connectionStringEntryName] == null)
            {
                //Add Entry
                connectionStringsSection.ConnectionStrings.Add(
                    new ConnectionStringSettings(
                        connectionStringEntryName, 
                        connectionStringEntryValue,
                        providerName));
            }
            else
            { 
                //Update Entry
                connectionStringsSection.ConnectionStrings[connectionStringEntryName].ConnectionString = 
                    ConnectionStringEntryValue;
            }
        }

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