public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            SignatureDocumentService signatureDocumentService = new SignatureDocumentService(rockContext);
            BinaryFileService        binaryfileService        = new BinaryFileService(rockContext);
            PersonAliasService       personAliasService       = new PersonAliasService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var        mergeFields        = GetMergeFields(action);
            int        documentTemplateId = GetAttributeValue(action, "DocumentTemplateId", true).AsInteger();
            Guid       documentGuid       = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "Document").AsGuid()).AsGuid();
            BinaryFile binaryFile         = binaryfileService.Get(documentGuid);
            string     documentKey        = GetAttributeValue(action, "DocumentKey", true).ResolveMergeFields(mergeFields);
            string     documentName       = GetAttributeValue(action, "DocumentName", true).ResolveMergeFields(mergeFields);
            Guid       assignedTo         = GetAttributeValue(action, "AssignedTo", true).AsGuid();
            Guid       appliesTo          = GetAttributeValue(action, "AppliesTo", true).AsGuid();
            Guid       signedBy           = GetAttributeValue(action, "SignedBy", true).AsGuid();

            if (binaryFile != null)
            {
                // Create the signature document
                Rock.Model.SignatureDocument document = new Rock.Model.SignatureDocument();
                document.AssignedToPersonAliasId = personAliasService.Get(assignedTo).Id;
                document.AppliesToPersonAliasId  = personAliasService.Get(appliesTo).Id;
                document.SignedByPersonAliasId   = personAliasService.Get(signedBy).Id;
                document.Name         = documentName;
                document.DocumentKey  = documentKey;
                document.BinaryFileId = binaryFile.Id;
                document.SignatureDocumentTemplateId = documentTemplateId;
                document.LastInviteDate = RockDateTime.Now;
                document.LastStatusDate = RockDateTime.Now;
                document.Status         = SignatureDocumentStatus.Signed;

                // Add the signature document to the service
                signatureDocumentService.Add(document);
            }

            return(true);
        }
Example #2
0
 /// <summary>
 /// Copies the properties from another SignatureDocument object to this SignatureDocument object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this SignatureDocument target, SignatureDocument source)
 {
     target.Id = source.Id;
     target.AppliesToPersonAliasId  = source.AppliesToPersonAliasId;
     target.AssignedToPersonAliasId = source.AssignedToPersonAliasId;
     target.BinaryFileId            = source.BinaryFileId;
     target.DocumentKey             = source.DocumentKey;
     target.ForeignGuid             = source.ForeignGuid;
     target.ForeignKey     = source.ForeignKey;
     target.InviteCount    = source.InviteCount;
     target.LastInviteDate = source.LastInviteDate;
     target.LastStatusDate = source.LastStatusDate;
     target.Name           = source.Name;
     target.SignatureDocumentTemplateId = source.SignatureDocumentTemplateId;
     target.SignedByPersonAliasId       = source.SignedByPersonAliasId;
     target.Status                  = source.Status;
     target.CreatedDateTime         = source.CreatedDateTime;
     target.ModifiedDateTime        = source.ModifiedDateTime;
     target.CreatedByPersonAliasId  = source.CreatedByPersonAliasId;
     target.ModifiedByPersonAliasId = source.ModifiedByPersonAliasId;
     target.Guid      = source.Guid;
     target.ForeignId = source.ForeignId;
 }
        /// <summary>
        /// Sends the document.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="signatureDocumentTemplate">Type of the signature document.</param>
        /// <param name="appliesToPerson">The person.</param>
        /// <param name="assignedToPerson">The assigned to person.</param>
        /// <param name="documentName">Name of the document.</param>
        /// <param name="alternateEmail">The alternate email.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        private bool SendDocument(SignatureDocument document, SignatureDocumentTemplate signatureDocumentTemplate, Person appliesToPerson, Person assignedToPerson, string documentName, string alternateEmail, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // If document was passed and other values were not, set them from the document
            if (document != null)
            {
                signatureDocumentTemplate = signatureDocumentTemplate ?? document.SignatureDocumentTemplate;
                if (document.AppliesToPersonAlias != null && document.AppliesToPersonAlias.Person != null)
                {
                    appliesToPerson = appliesToPerson ?? document.AppliesToPersonAlias.Person;
                }
                if (document.AssignedToPersonAlias != null && document.AssignedToPersonAlias.Person != null)
                {
                    assignedToPerson = assignedToPerson ?? document.AssignedToPersonAlias.Person;
                    alternateEmail   = !string.IsNullOrWhiteSpace(alternateEmail) ? alternateEmail : document.AssignedToPersonAlias.Person.Email;
                }

                documentName = !string.IsNullOrWhiteSpace(documentName) ? documentName : document.Name;
            }

            if (signatureDocumentTemplate == null)
            {
                errorMessages.Add("Invalid Document Type.");
            }

            if (appliesToPerson == null)
            {
                errorMessages.Add("Invalid Applies To Person.");
            }

            if (assignedToPerson == null)
            {
                errorMessages.Add("Invalid Assigned To Person.");
            }

            if (!errorMessages.Any())
            {
                var provider = DigitalSignatureContainer.GetComponent(signatureDocumentTemplate.ProviderEntityType.Name);
                if (provider == null || !provider.IsActive)
                {
                    errorMessages.Add("Digital Signature provider was not found or is not active.");
                }
                else
                {
                    string email = string.IsNullOrWhiteSpace(alternateEmail) ? assignedToPerson.Email : alternateEmail;
                    if (string.IsNullOrWhiteSpace(email))
                    {
                        errorMessages.Add(string.Format("There is no email address for {0}.", assignedToPerson.FullName));
                    }
                    else
                    {
                        var sendErrors = new List <string>();

                        var rockContext     = this.Context as RockContext;
                        var documentService = new SignatureDocumentService(rockContext);

                        if (document == null)
                        {
                            document = documentService.Queryable()
                                       .Where(d =>
                                              d.SignatureDocumentTemplateId == signatureDocumentTemplate.Id &&
                                              d.AppliesToPersonAlias.PersonId == appliesToPerson.Id &&
                                              d.AssignedToPersonAlias.PersonId == assignedToPerson.Id &&
                                              d.Status != SignatureDocumentStatus.Signed)
                                       .OrderByDescending(d => d.CreatedDateTime)
                                       .FirstOrDefault();
                        }

                        string documentKey = document?.DocumentKey;
                        if (document == null || string.IsNullOrWhiteSpace(documentKey))
                        {
                            documentKey = provider.CreateDocument(signatureDocumentTemplate, appliesToPerson, assignedToPerson, documentName, out sendErrors, true);
                        }
                        else
                        {
                            provider.ResendDocument(document, out sendErrors);
                        }

                        if (document == null)
                        {
                            document = new SignatureDocument();
                            document.SignatureDocumentTemplate   = signatureDocumentTemplate;
                            document.SignatureDocumentTemplateId = signatureDocumentTemplate.Id;
                            document.Name = documentName;
                            document.AppliesToPersonAliasId  = appliesToPerson.PrimaryAliasId;
                            document.AssignedToPersonAliasId = assignedToPerson.PrimaryAliasId;
                            documentService.Add(document);
                        }

                        if (!sendErrors.Any())
                        {
                            document.DocumentKey    = documentKey;
                            document.LastInviteDate = RockDateTime.Now;
                            document.InviteCount    = document.InviteCount + 1;
                            if (document.Status != SignatureDocumentStatus.Sent)
                            {
                                document.LastStatusDate = document.LastInviteDate;
                            }
                            document.Status = SignatureDocumentStatus.Sent;

                            return(true);
                        }
                        else
                        {
                            errorMessages.AddRange(sendErrors);
                        }
                    }
                }
            }

            return(false);
        }
 /// <summary>
 /// Sends the document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="alternateEmail">The alternate email.</param>
 /// <param name="errorMessages">The error messages.</param>
 /// <returns></returns>
 public bool SendDocument(SignatureDocument document, string alternateEmail, out List <string> errorMessages)
 {
     return(SendDocument(document, null, null, null, string.Empty, alternateEmail, out errorMessages));
 }
        /// <summary>
        /// Updates the document status.
        /// </summary>
        /// <param name="signatureDocument">The signature document.</param>
        /// <param name="tempFolderPath">The temporary folder path.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public bool UpdateDocumentStatus(SignatureDocument signatureDocument, string tempFolderPath, out List <string> errorMessages)
        {
            errorMessages = new List <string>();
            if (signatureDocument == null)
            {
                errorMessages.Add("Invalid Document.");
            }

            if (signatureDocument.SignatureDocumentTemplate == null)
            {
                errorMessages.Add("Document has an invalid document type.");
            }

            if (!errorMessages.Any())
            {
                var provider = DigitalSignatureContainer.GetComponent(signatureDocument.SignatureDocumentTemplate.ProviderEntityType.Name);
                if (provider == null || !provider.IsActive)
                {
                    errorMessages.Add("Digital Signature provider was not found or is not active.");
                }
                else
                {
                    var originalStatus = signatureDocument.Status;
                    if (provider.UpdateDocumentStatus(signatureDocument, out errorMessages))
                    {
                        if (signatureDocument.Status == SignatureDocumentStatus.Signed && !signatureDocument.BinaryFileId.HasValue)
                        {
                            using (var rockContext = new RockContext())
                            {
                                string documentPath = provider.GetDocument(signatureDocument, tempFolderPath, out errorMessages);
                                if (!string.IsNullOrWhiteSpace(documentPath))
                                {
                                    var        binaryFileService = new BinaryFileService(rockContext);
                                    BinaryFile binaryFile        = new BinaryFile();
                                    binaryFile.Guid             = Guid.NewGuid();
                                    binaryFile.IsTemporary      = false;
                                    binaryFile.BinaryFileTypeId = signatureDocument.SignatureDocumentTemplate.BinaryFileTypeId;
                                    binaryFile.MimeType         = "application/pdf";
                                    var fi = new FileInfo(documentPath);
                                    binaryFile.FileName      = fi.Name;
                                    binaryFile.FileSize      = fi.Length;
                                    binaryFile.ContentStream = new FileStream(documentPath, FileMode.Open);
                                    binaryFileService.Add(binaryFile);
                                    rockContext.SaveChanges();

                                    signatureDocument.BinaryFileId = binaryFile.Id;

                                    File.Delete(documentPath);
                                }
                            }
                        }

                        if (signatureDocument.Status != originalStatus)
                        {
                            signatureDocument.LastStatusDate = RockDateTime.Now;
                        }
                    }
                }
            }

            return(!errorMessages.Any());
        }
        /// <summary>
        /// Sends the invite.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="component">The component.</param>
        /// <param name="document">The document.</param>
        /// <param name="person">The person.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        private bool SendInvite(RockContext rockContext, DigitalSignatureComponent component, SignatureDocument document, Person person, out List <string> errors)
        {
            errors = new List <string>();
            if (document != null &&
                document.SignatureDocumentTemplate != null &&
                document.SignatureDocumentTemplate.InviteSystemCommunicationId.HasValue &&
                person != null &&
                !string.IsNullOrWhiteSpace(person.Email))
            {
                string inviteLink = component.GetInviteLink(document, person, out errors);
                if (!errors.Any())
                {
                    var systemEmail = new SystemCommunicationService(rockContext).Get(document.SignatureDocumentTemplate.InviteSystemCommunicationId.Value);
                    if (systemEmail != null)
                    {
                        var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null, person);
                        mergeFields.Add("SignatureDocument", document);
                        mergeFields.Add("InviteLink", inviteLink);

                        var emailMessage = new RockEmailMessage(systemEmail);
                        emailMessage.AddRecipient(new RockEmailMessageRecipient(person, mergeFields));
                        emailMessage.Send();
                    }
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Example #7
0
        /// <summary>
        /// Sends the invite.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="component">The component.</param>
        /// <param name="document">The document.</param>
        /// <param name="person">The person.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        private bool SendInvite(RockContext rockContext, DigitalSignatureComponent component, SignatureDocument document, Person person, out List <string> errors)
        {
            errors = new List <string>();
            if (document != null &&
                document.SignatureDocumentTemplate != null &&
                document.SignatureDocumentTemplate.InviteSystemEmailId.HasValue &&
                person != null &&
                !string.IsNullOrWhiteSpace(person.Email))
            {
                string inviteLink = component.GetInviteLink(document, person, out errors);
                if (!errors.Any())
                {
                    var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null, person);
                    mergeFields.Add("SignatureDocument", document);
                    mergeFields.Add("InviteLink", inviteLink);

                    var recipients = new List <RecipientData>();
                    recipients.Add(new RecipientData(person.Email, mergeFields));

                    var systemEmail = new SystemEmailService(rockContext).Get(document.SignatureDocumentTemplate.InviteSystemEmailId.Value);
                    if (systemEmail != null)
                    {
                        var appRoot = Rock.Web.Cache.GlobalAttributesCache.Read(rockContext).GetValue("InternalApplicationRoot");
                        Email.Send(systemEmail.Guid, recipients, appRoot, string.Empty, false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }