Service/Data Access class for Rock.Model.SignatureDocumentTemplate entity objects.
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            using ( var rockContext = new RockContext() )
            {
                var docTypeService = new SignatureDocumentTemplateService( rockContext );
                var docService = new SignatureDocumentService( rockContext );

                var document = docService.Get( SignatureDocumentId );
                if ( document != null )
                {
                    var status = document.Status;
                    int? binaryFileId = document.BinaryFileId;
                    string folderPath = System.Web.Hosting.HostingEnvironment.MapPath( "~/App_Data/Cache/SignNow" );
                    var updateErrorMessages = new List<string>();

                    if ( docTypeService.UpdateDocumentStatus( document, folderPath, out updateErrorMessages ) )
                    {
                        if ( status != document.Status || !binaryFileId.Equals( document.BinaryFileId ) )
                        {
                            rockContext.SaveChanges();
                        }
                    }
                }
            }
        }
        /// <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();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <remarks>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </remarks>
        public virtual void Execute( IJobExecutionContext context )
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            int resendDays = dataMap.GetString( "ResendInviteAfterNumberDays" ).AsIntegerOrNull() ?? 5;
            int maxInvites = dataMap.GetString( "MaxInvites" ).AsIntegerOrNull() ?? 2;
            int checkDays = dataMap.GetString( "CheckForSignatureDays" ).AsIntegerOrNull() ?? 30;
            string folderPath = System.Web.Hosting.HostingEnvironment.MapPath( "~/App_Data/Cache/SignNow" );

            var errorMessages = new List<string>();
            int signatureRequestsSent = 0;
            int documentsUpdated = 0;

            // Send documents
            using ( var rockContext = new RockContext() )
            {
                var maxInviteDate = RockDateTime.Today.AddDays( 0 - resendDays );
                var maxCheckDays = RockDateTime.Today.AddDays( 0 - checkDays );
                var docTypeService = new SignatureDocumentTemplateService( rockContext );
                var docService = new SignatureDocumentService( rockContext );

                // Check for status updates
                foreach ( var document in new SignatureDocumentService( rockContext ).Queryable()
                    .Where( d =>
                        (
                            d.Status == SignatureDocumentStatus.Sent ||
                            ( d.Status == SignatureDocumentStatus.Signed && !d.BinaryFileId.HasValue )
                        ) &&
                        d.LastInviteDate.HasValue &&
                        d.LastInviteDate.Value > maxCheckDays )
                    .ToList() )
                {
                    var updateErrorMessages = new List<string>();
                    var status = document.Status;
                    int? binaryFileId = document.BinaryFileId;
                    if ( docTypeService.UpdateDocumentStatus( document, folderPath, out updateErrorMessages ) )
                    {
                        if ( status != document.Status || !binaryFileId.Equals( document.BinaryFileId )  )
                        {
                            rockContext.SaveChanges();
                            documentsUpdated++;
                        }
                    }
                    else
                    {
                        errorMessages.AddRange( updateErrorMessages );
                    }
                }

                // Send any needed signature requests
                var docsSent = new Dictionary<int, List<int>>();
                foreach ( var gm in new GroupMemberService( rockContext ).Queryable()
                    .Where( m =>
                        m.GroupMemberStatus == GroupMemberStatus.Active &&
                        m.Group.IsActive &&
                        m.Person.Email != null &&
                        m.Person.Email != "" &&
                        m.Group.RequiredSignatureDocumentTemplate != null &&
                        !m.Group.RequiredSignatureDocumentTemplate.Documents.Any( d =>
                            d.AppliesToPersonAlias.PersonId == m.PersonId &&
                            d.Status == SignatureDocumentStatus.Signed
                        )
                    )
                    .Select( m => new
                    {
                        GroupName = m.Group.Name,
                        Person = m.Person,
                        DocumentType = m.Group.RequiredSignatureDocumentTemplate
                    } )
                    .ToList() )
                {
                    if ( docsSent.ContainsKey( gm.Person.Id ) )
                    {
                        if ( docsSent[gm.Person.Id].Contains( gm.DocumentType.Id ) )
                        {
                            continue;
                        }
                        else
                        {
                            docsSent[gm.Person.Id].Add( gm.DocumentType.Id );
                        }
                    }
                    else
                    {
                        docsSent.Add( gm.Person.Id, new List<int> { gm.DocumentType.Id } );
                    }

                    var document = docService.Queryable()
                        .Where( d =>
                            d.SignatureDocumentTemplateId == gm.DocumentType.Id &&
                            d.AppliesToPersonAlias.PersonId == gm.Person.Id &&
                            d.AssignedToPersonAlias.PersonId == gm.Person.Id &&
                            d.Status != SignatureDocumentStatus.Signed
                        )
                        .OrderByDescending( d => d.CreatedDateTime )
                        .FirstOrDefault();

                    if ( document == null || ( document.InviteCount < maxInvites && document.LastInviteDate < maxInviteDate ) )
                    {
                        string documentName = string.Format( "{0}_{1}", gm.GroupName.RemoveSpecialCharacters(), gm.Person.FullName.RemoveSpecialCharacters() );

                        var sendErrorMessages = new List<string>();
                        if ( document != null )
                        {
                            docTypeService.SendDocument( document, gm.Person.Email, out sendErrorMessages );
                        }
                        else
                        {
                            docTypeService.SendDocument( gm.DocumentType, gm.Person, gm.Person, documentName, gm.Person.Email, out sendErrorMessages );
                        }

                        if ( !errorMessages.Any() )
                        {
                            rockContext.SaveChanges();
                            signatureRequestsSent++;
                        }
                        else
                        {
                            errorMessages.AddRange( sendErrorMessages );
                        }
                    }
                }
            }

            if ( errorMessages.Any() )
            {
                throw new Exception( "One or more exceptions occurred processing signature documents..." + Environment.NewLine + errorMessages.AsDelimited( Environment.NewLine ) );
            }

            context.Result = string.Format( "{0} signature requests sent; {1} existing document's status updated", signatureRequestsSent, documentsUpdated );
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="signatureDocumentTemplateId">The signature document type identifier.</param>
        public void ShowDetail( int signatureDocumentTemplateId )
        {
            pnlDetails.Visible = true;
            SignatureDocumentTemplate signatureDocumentTemplate = null;

            using ( var rockContext = new RockContext() )
            {
                if ( !signatureDocumentTemplateId.Equals( 0 ) )
                {
                    signatureDocumentTemplate = new SignatureDocumentTemplateService( rockContext ).Get( signatureDocumentTemplateId );
                }

                if ( signatureDocumentTemplate == null )
                {
                    signatureDocumentTemplate = new SignatureDocumentTemplate { Id = 0 };
                    var components = DigitalSignatureContainer.Instance.Components;
                    var entityType = components.Where( c => c.Value.Value.IsActive ).OrderBy( c => c.Value.Value.Order ).Select( c => c.Value.Value.EntityType ).FirstOrDefault();
                    if ( entityType != null )
                    {
                        signatureDocumentTemplate.ProviderEntityType = new EntityTypeService( rockContext ).Get( entityType.Id );
                    }

                    Guid? fileTypeGuid = GetAttributeValue( "DefaultFileType" ).AsGuidOrNull();
                    if ( fileTypeGuid.HasValue )
                    {
                        var binaryFileType = new BinaryFileTypeService( rockContext ).Get( fileTypeGuid.Value );
                        if ( binaryFileType != null )
                        {
                            signatureDocumentTemplate.BinaryFileType = binaryFileType;
                            signatureDocumentTemplate.BinaryFileTypeId = binaryFileType.Id;
                        }
                    }

                    Guid? inviteEmailGuid = GetAttributeValue( "DefaultInviteEmail" ).AsGuidOrNull();
                    if ( inviteEmailGuid.HasValue )
                    {
                        var systemEmail = new SystemEmailService( rockContext ).Get( inviteEmailGuid.Value );
                        if ( systemEmail != null )
                        {
                            signatureDocumentTemplate.InviteSystemEmail = systemEmail;
                            signatureDocumentTemplate.InviteSystemEmailId = systemEmail.Id;
                        }
                    }

                }

                hfSignatureDocumentTemplateId.SetValue( signatureDocumentTemplate.Id );

                // render UI based on Authorized and IsSystem
                bool readOnly = false;

                nbEditModeMessage.Text = string.Empty;
                bool canEdit = UserCanEdit || signatureDocumentTemplate.IsAuthorized( Authorization.EDIT, CurrentPerson );
                bool canView = canEdit || signatureDocumentTemplate.IsAuthorized( Authorization.VIEW, CurrentPerson );

                if ( !canView )
                {
                    pnlDetails.Visible = false;
                }
                else
                {
                    pnlDetails.Visible = true;

                    if ( !canEdit )
                    {
                        readOnly = true;
                        nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( SignatureDocumentTemplate.FriendlyTypeName );
                    }

                    if ( readOnly )
                    {
                        btnEdit.Visible = false;
                        btnDelete.Visible = false;
                        ShowReadonlyDetails( signatureDocumentTemplate );
                    }
                    else
                    {
                        btnEdit.Visible = true;
                        btnDelete.Visible = false;
                        if ( signatureDocumentTemplate.Id > 0 )
                        {
                            ShowReadonlyDetails( signatureDocumentTemplate );
                        }
                        else
                        {
                            ShowEditDetails( signatureDocumentTemplate );
                        }
                    }
                }
            }
        }
        /// <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();
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="signatureDocumentId">The signature document type identifier.</param>
        public void ShowDetail( int signatureDocumentId )
        {
            pnlDetails.Visible = true;
            SignatureDocument signatureDocument = null;

            using ( var rockContext = new RockContext() )
            {
                if ( !signatureDocumentId.Equals( 0 ) )
                {
                    signatureDocument = new SignatureDocumentService( rockContext ).Get( signatureDocumentId );
                }

                if ( signatureDocument == null )
                {
                    signatureDocument = new SignatureDocument { Id = 0 };

                    int? personId = PageParameter( "personId" ).AsIntegerOrNull();
                    if ( personId.HasValue )
                    {
                        var person = new PersonService( rockContext ).Get( personId.Value );
                        if ( person != null )
                        {
                            var personAlias = person.PrimaryAlias;
                            if ( personAlias != null )
                            {
                                signatureDocument.AppliesToPersonAlias = personAlias;
                                signatureDocument.AppliesToPersonAliasId = personAlias.Id;
                                signatureDocument.AssignedToPersonAlias = personAlias;
                                signatureDocument.AssignedToPersonAliasId = personAlias.Id;
                            }
                        }
                    }

                    int? documentTypeId = PageParameter( "SignatureDocumentTemplateId" ).AsIntegerOrNull();
                    if ( documentTypeId.HasValue )
                    {
                        var documentType = new SignatureDocumentTemplateService( rockContext ).Get( documentTypeId.Value );
                        if ( documentType != null )
                        {
                            signatureDocument.SignatureDocumentTemplate = documentType;
                            signatureDocument.SignatureDocumentTemplateId = documentType.Id;
                        }
                    }
                }

                hfSignatureDocumentId.SetValue( signatureDocument.Id );

                // render UI based on Authorized and IsSystem
                bool readOnly = false;

                nbEditModeMessage.Text = string.Empty;
                bool canEdit = UserCanEdit || signatureDocument.IsAuthorized( Authorization.EDIT, CurrentPerson );
                bool canView = canEdit || signatureDocument.IsAuthorized( Authorization.VIEW, CurrentPerson );

                if ( !canView )
                {
                    pnlDetails.Visible = false;
                }
                else
                {
                    pnlDetails.Visible = true;

                    if ( !canEdit )
                    {
                        readOnly = true;
                        nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( SignatureDocument.FriendlyTypeName );
                    }

                    if ( readOnly )
                    {
                        ShowReadonlyDetails( signatureDocument );
                    }
                    else
                    {
                        ShowEditDetails( signatureDocument, false );
                    }
                }
            }
        }
        /// <summary>
        /// Binds the grid for signature document templates.
        /// </summary>
        private void BindGrid()
        {
            var queryable = new SignatureDocumentTemplateService( new RockContext() ).Queryable();

            SortProperty sortProperty = gSignatureDocumentTemplate.SortProperty;
            if ( sortProperty != null )
            {
                if ( sortProperty.Property == "Documents" )
                {
                    if ( sortProperty.Direction == SortDirection.Ascending )
                    {
                        queryable = queryable.OrderBy( a => a.Documents.Count() );
                    }
                    else
                    {
                        queryable = queryable.OrderByDescending( a => a.Documents.Count() );
                    }
                }
                queryable = queryable.Sort( sortProperty );
            }
            else
            {
                queryable = queryable.OrderBy( a => a.Name );
            }

            var types = new List<SignatureDocumentTemplate>();
            foreach( var type in queryable )
            {
                if ( type.IsAuthorized( Authorization.VIEW, CurrentPerson))
                {
                    types.Add( type );
                }
            }

            gSignatureDocumentTemplate.DataSource = types
                .Select( a =>
                new
                {
                    a.Id,
                    a.Name,
                    a.Description,
                    BinaryFileType = a.BinaryFileType.Name,
                    a.ProviderTemplateKey,
                    Documents = a.Documents.Count()
                } )
                .ToList();
            gSignatureDocumentTemplate.DataBind();
        }