/// <summary>
        /// This method gets invoked when the command is called
        /// </summary>
        protected override void InternalProcessRecord()
        {
            SPIisWebServiceApplicationPool resolvedApplicationPool = this.ApplicationPool.Read();

            if (resolvedApplicationPool == null)
            {
                this.ThrowTerminatingError(new InvalidOperationException("Could not find the specified application pool."), ErrorCategory.InvalidOperation, this);
            }

            if (this.ShouldProcess(this.Name))
            {
                // Get or create the service
                ClubCloudService service = ClubCloudService.GetOrCreateService();

                // Get or create the service proxy
                ClubCloudServiceProxy.GetOrCreateServiceProxy();

                // Install the service instances to servers in this farm
                ClubCloudServiceInstance.CreateServiceInstances(service);

                // Create the service application
                ClubCloudServiceApplication application = new ClubCloudServiceApplication(this.Name, service, resolvedApplicationPool);
                application.Update();
                application.Provision();

                // Database settings
                if (string.Equals(this.ParameterSetName, "DB", StringComparison.OrdinalIgnoreCase))
                {
                    NetworkCredential databaseCredentials = null;

                    if (this.DatabaseCredentials != null)
                    {
                        databaseCredentials = (NetworkCredential)this.DatabaseCredentials;
                    }

                    SPDatabaseParameters databaseParameters = SPDatabaseParameters.CreateParameters(this.DatabaseName, this.DatabaseServerName, databaseCredentials, this.DatabaseFailoverServerName, SPDatabaseParameterOptions.None);

                    // Create the database
                    ClubCloudDatabase database = new ClubCloudDatabase(databaseParameters);

                    // Provision the database (runs the Create scripts)
                    database.Provision();

                    // Grant the database the proper permissions
                    database.GrantApplicationPoolAccess(resolvedApplicationPool.ProcessAccount.SecurityIdentifier);

                    // Add the failover server instance (the base class does not do this for you)
                    if (!string.IsNullOrEmpty(this.DatabaseFailoverServerName))
                    {
                        database.AddFailoverServiceInstance(this.DatabaseFailoverServerName);
                    }

                    // Establish a relationship between the service application and the database
                    application.Database = database;
                    application.Update();
                }

                this.WriteObject(application);
            }
        }
        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">The Sender.</param>
        /// <param name="e">The EventArgs.</param>
        protected void ButtonOk_Click(object sender, EventArgs e)
        {
            // Validate
            this.Validate();

            if (!this.IsValid)
            {
                return;
            }

            ContentDatabaseSection databaseSectionControl = this.databaseSection as ContentDatabaseSection;

            // We are valid
            // Register the database
            using (SPLongOperation operation = new SPLongOperation(this))
            {
                operation.LeadingHTML  = HttpContext.GetGlobalResourceObject("ClubCloud.Service.ServiceAdminResources", "DatabaseSettingsCreateOperationLeadingHtml", CultureInfo.CurrentCulture).ToString();
                operation.TrailingHTML = HttpContext.GetGlobalResourceObject("ClubCloud.Service.ServiceAdminResources", "DatabaseSettingsCreateOperationTrailingHtml", CultureInfo.CurrentCulture).ToString();
                operation.Begin();

                if (databaseSectionControl.DisplayMode == ContentDatabaseSectionMode.AuthenticationEdit)
                {
                    // We are only changing credentials here, do not reprovision.
                    if (string.Equals(this.ServiceApplication.Database.DatabaseConnectionString, databaseSectionControl.ConnectionString, StringComparison.OrdinalIgnoreCase))
                    {
                        // No change made, exit the application.
                        operation.End(string.Format(CultureInfo.InvariantCulture, "/_admin/ClubCloud.Service/ManageApplication.aspx?id={0}", SPHttpUtility.UrlKeyValueEncode(this.ServiceApplication.Id)));
                    }
                    else
                    {
                        if (databaseSectionControl.UseWindowsAuthentication)
                        {
                            // Switching to windows authentication.
                            this.ServiceApplication.Database.Username = string.Empty;
                            this.ServiceApplication.Database.Password = string.Empty;
                        }
                        else
                        {
                            this.ServiceApplication.Database.Username = databaseSectionControl.DatabaseUserName;
                            this.ServiceApplication.Database.Password = databaseSectionControl.DatabasePassword;
                        }

                        this.ServiceApplication.Database.GrantOwnerAccessToDatabaseAccount();
                    }

                    // Set the failover instance
                    this.ServiceApplication.Database.AddFailoverServiceInstance(databaseSectionControl.FailoverDatabaseServer);

                    this.ServiceApplication.Database.Update();
                    operation.End(string.Format(CultureInfo.InvariantCulture, "/_admin/ClubCloud.Service/ManageApplication.aspx?id={0}", SPHttpUtility.UrlKeyValueEncode(this.ServiceApplication.Id)));
                }

                // Create the database
                ClubCloudDatabase database = new ClubCloudDatabase(this.databaseParameters);

                // Provision the database (runs the Create scripts)
                database.Provision();

                // Grant the database the proper permissions
                database.GrantApplicationPoolAccess(this.ServiceApplication.ApplicationPool.ProcessAccount.SecurityIdentifier);

                // Add the failover server instance (the base class does not do this for you)
                if (!string.IsNullOrEmpty(this.databaseParameters.FailoverPartner))
                {
                    database.AddFailoverServiceInstance(this.databaseParameters.FailoverPartner);
                }

                // Establish a relationship between the service application and the database
                this.ServiceApplication.Database = database;
                this.ServiceApplication.Update();

                operation.End(string.Format(CultureInfo.InvariantCulture, "/_admin/ClubCloud.Service/ManageApplication.aspx?id={0}", SPHttpUtility.UrlKeyValueEncode(this.ServiceApplication.Id)));
            }
        }