Ejemplo n.º 1
0
 /// <summary>
 /// Clones this SignatureDocumentTemplate object to a new SignatureDocumentTemplate object
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="deepCopy">if set to <c>true</c> a deep copy is made. If false, only the basic entity properties are copied.</param>
 /// <returns></returns>
 public static SignatureDocumentTemplate Clone(this SignatureDocumentTemplate source, bool deepCopy)
 {
     if (deepCopy)
     {
         return(source.Clone() as SignatureDocumentTemplate);
     }
     else
     {
         var target = new SignatureDocumentTemplate();
         target.CopyPropertiesFrom(source);
         return(target);
     }
 }
 /// <summary>
 /// Copies the properties from another SignatureDocumentTemplate object to this SignatureDocumentTemplate object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this SignatureDocumentTemplate target, SignatureDocumentTemplate source)
 {
     target.Id = source.Id;
     target.BinaryFileTypeId    = source.BinaryFileTypeId;
     target.Description         = source.Description;
     target.ForeignGuid         = source.ForeignGuid;
     target.ForeignKey          = source.ForeignKey;
     target.InviteSystemEmailId = source.InviteSystemEmailId;
     target.Name = source.Name;
     target.ProviderEntityTypeId    = source.ProviderEntityTypeId;
     target.ProviderTemplateKey     = source.ProviderTemplateKey;
     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="signatureDocumentTemplate">The signature document template.</param>
 /// <param name="appliesToPerson">The applies to 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>
 public bool SendDocument(SignatureDocumentTemplate signatureDocumentTemplate, Person appliesToPerson, Person assignedToPerson, string documentName, string alternateEmail, out List <string> errorMessages)
 {
     return(SendDocument(null, signatureDocumentTemplate, appliesToPerson, assignedToPerson, documentName, alternateEmail, out errorMessages));
 }