/// <summary>
        /// Handles the Delete event of the gSignatureDocumentTemplate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gSignatureDocumentTemplate_Delete( object sender, RowEventArgs e )
        {
            var rockContext = new RockContext();
            var signatureDocumentService = new SignatureDocumentService( rockContext );
            var signatureDocumentTemplateService = new SignatureDocumentTemplateService( rockContext );

            SignatureDocumentTemplate type = signatureDocumentTemplateService.Get( e.RowKeyId );

            if ( type != null )
            {
                if ( !UserCanEdit && !type.IsAuthorized( Authorization.EDIT, CurrentPerson ) )
                {
                    mdGridWarning.Show( "Sorry, you're not authorized to delete this signature document template.", ModalAlertType.Alert );
                    return;
                }

                string errorMessage;
                if ( !signatureDocumentTemplateService.CanDelete( type, out errorMessage ) )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                signatureDocumentTemplateService.Delete( type );

                rockContext.SaveChanges();
            }

            BindGrid();
        }
        /// <summary>
        /// Handles the Click event of the btnSaveType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSaveType_Click( object sender, EventArgs e )
        {
            var rockContext = new RockContext();

            SignatureDocumentTemplate signatureDocumentTemplate = null;
            SignatureDocumentTemplateService typeService = new SignatureDocumentTemplateService( rockContext );

            int SignatureDocumentTemplateId = hfSignatureDocumentTemplateId.ValueAsInt();

            if ( SignatureDocumentTemplateId == 0 )
            {
                signatureDocumentTemplate = new SignatureDocumentTemplate();
                typeService.Add( signatureDocumentTemplate );
            }
            else
            {
                signatureDocumentTemplate = typeService.Get( SignatureDocumentTemplateId );
            }

            signatureDocumentTemplate.Name = tbTypeName.Text;
            signatureDocumentTemplate.Description = tbTypeDescription.Text;
            signatureDocumentTemplate.BinaryFileTypeId = bftpFileType.SelectedValueAsInt();
            signatureDocumentTemplate.ProviderEntityTypeId = cpProvider.SelectedEntityTypeId;
            signatureDocumentTemplate.ProviderTemplateKey = ddlTemplate.SelectedValue;
            signatureDocumentTemplate.InviteSystemEmailId = ddlSystemEmail.SelectedValueAsInt();

            if ( !signatureDocumentTemplate.IsValid )
            {
                // Controls will render the error messages
                return;
            }

            rockContext.SaveChanges();

            var qryParams = new Dictionary<string, string>();
            qryParams["SignatureDocumentTemplateId"] = signatureDocumentTemplate.Id.ToString();
            NavigateToCurrentPage( qryParams );
        }
 /// <summary>
 /// Handles the Click event of the btnEdit control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
 protected void btnEdit_Click( object sender, EventArgs e )
 {
     SignatureDocumentTemplateService signatureDocumentTemplateService = new SignatureDocumentTemplateService( new RockContext() );
     SignatureDocumentTemplate SignatureDocumentTemplate = signatureDocumentTemplateService.Get( hfSignatureDocumentTemplateId.ValueAsInt() );
     ShowEditDetails( SignatureDocumentTemplate );
 }
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnDelete_Click( object sender, EventArgs e )
        {
            RockContext rockContext = new RockContext();
            SignatureDocumentTemplateService signatureDocumentTemplateService = new SignatureDocumentTemplateService( rockContext );
            SignatureDocumentTemplate signatureDocumentTemplate = signatureDocumentTemplateService.Get( int.Parse( hfSignatureDocumentTemplateId.Value ) );

            if ( signatureDocumentTemplate != null )
            {
                if ( !signatureDocumentTemplate.IsAuthorized( Authorization.EDIT, this.CurrentPerson ) )
                {
                    mdDeleteWarning.Show( "Sorry, You are not authorized to delete this document template.", ModalAlertType.Information );
                    return;
                }

                string errorMessage;
                if ( !signatureDocumentTemplateService.CanDelete( signatureDocumentTemplate, out errorMessage ) )
                {
                    mdDeleteWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                signatureDocumentTemplateService.Delete( signatureDocumentTemplate );

                rockContext.SaveChanges();

            }

            NavigateToParentPage();
        }
 /// <summary>
 /// Handles the Click event of the btnCancelType control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
 protected void btnCancelType_Click( object sender, EventArgs e )
 {
     if ( hfSignatureDocumentTemplateId.IsZero() )
     {
         // Cancelling on Add.  Return to Grid
         NavigateToParentPage();
     }
     else
     {
         // Cancelling on Edit.  Return to Details
         SignatureDocumentTemplateService signatureDocumentTemplateService = new SignatureDocumentTemplateService(new RockContext());
         SignatureDocumentTemplate signatureDocumentTemplate = signatureDocumentTemplateService.Get( hfSignatureDocumentTemplateId.ValueAsInt() );
         ShowReadonlyDetails( signatureDocumentTemplate );
     }
 }
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            using ( var rockContext = new RockContext() )
            {
                var documentService = new SignatureDocumentService( rockContext );
                var personAliasService = new PersonAliasService( rockContext );
                var appliesPerson = personAliasService.GetPerson( AppliesToPersonAliasId );
                var assignedPerson = personAliasService.GetPerson( AssignedToPersonAliasId );

                if ( !documentService.Queryable().Any( d =>
                        d.AppliesToPersonAliasId.HasValue &&
                        d.AppliesToPersonAliasId.Value == AppliesToPersonAliasId &&
                        d.Status == SignatureDocumentStatus.Signed ) )
                {
                    var documentTypeService = new SignatureDocumentTemplateService( rockContext );
                    var SignatureDocumentTemplate = documentTypeService.Get( SignatureDocumentTemplateId );

                    var errorMessages = new List<string>();
                    if ( documentTypeService.SendDocument( SignatureDocumentTemplate, appliesPerson, assignedPerson, DocumentName, Email, out errorMessages ) )
                    {
                        rockContext.SaveChanges();
                    }
                }
            }
        }