Пример #1
0
        protected void ddlAddEditDocumentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbDocumentName.Text.IsNotNullOrWhiteSpace() || ddlAddEditDocumentType.SelectedIndex == 0)
            {
                // If there is already a name or nothing is selected then do do anything.
                fuUploader.Visible           = false;
                nbSelectDocumentType.Visible = true;
                return;
            }

            // Get the selected DocumentType from cache and update the BinaryFileTypeGuid in the FileUploader
            var documentTypeCache = DocumentTypeCache.Get(ddlAddEditDocumentType.SelectedValueAsInt() ?? 0);

            fuUploader.BinaryFileTypeGuid = new BinaryFileTypeService(new RockContext()).GetGuid(documentTypeCache.BinaryFileTypeId).Value;

            fuUploader.Visible           = true;
            nbSelectDocumentType.Visible = false;

            string template = documentTypeCache.DefaultDocumentNameTemplate;

            if (template.IsNotNullOrWhiteSpace())
            {
                // If there is a template then apply it
                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                tbDocumentName.Text = template.ResolveMergeFields(mergeFields);
            }
        }
        private List<DocumentData> GetBinaryFiles()
        {
            Guid binaryFileTypeGuid = binaryFileType != null ? binaryFileType.Guid : Guid.NewGuid();
            RockContext context = new RockContext();
            var binaryFileService = new BinaryFileService( context );
            var attributeService = new AttributeService( context );
            var attributeValueService = new AttributeValueService( context );
            var personAliasService = new PersonAliasService( context );
            var personService = new PersonService( context );
            var documentService = new DocumentService( context );

            // If the document type is not set
            if ( string.IsNullOrWhiteSpace(GetAttributeValue( "DocumentType" ) ) )
            {
                return null;
            }

            int documentTypeId = DocumentTypeCache.Get( GetAttributeValue( "DocumentType" ).AsGuid() ).Id;

            var documentQuery = documentService.Queryable( "BinaryFile" ).Where( d => d.DocumentTypeId == documentTypeId );

            // Add any query filters here
            string name = fBinaryFile.GetUserPreference( "File Name" );
            if ( !string.IsNullOrWhiteSpace( name ) )
            {
                documentQuery = documentQuery.Where( d => d.BinaryFile.FileName.Contains( name ) );
            }

            var personQuery = personService.Queryable( true );

            // Filter for a specific Person
            if ( ppPerson.SelectedValue.HasValue && ppPerson.SelectedValue.Value > 0 ) {
                string givingId = personService.Queryable().Where(a => a.Id == ppPerson.SelectedValue ).Select(p => p.GivingId ).FirstOrDefault();
                personQuery = personQuery.Where( p => p.GivingId == givingId );
            }

            var documents = documentQuery
                    .Join( personQuery,
                        obj => obj.EntityId,
                        p => p.Id,
                        ( obj, p ) => new { Document = obj, Person = p } )
                    .GroupBy(obj => obj.Document.BinaryFile.Id);


            List<DocumentData> list = documents.Select(d => new DocumentData() { BinaryFile = d.FirstOrDefault().Document.BinaryFile, People = d.Select( p => p.Person ).ToList() }).ToList();


            List<Guid?> dataviews = GetAttributeValue( "PrintAndMail" ).SplitDelimitedValues().AsGuidOrNullList();
            if ( list.Count() > 0 )
            {
                if ( dataviews != null && dataviews.Count > 0 )
                {
                    var dataViewService = new DataViewService( context );
                    foreach ( var dataviewguid in dataviews )
                    {
                        List<string> errorMessages = new List<string>();
                        list.FirstOrDefault().MailPersonIds.AddRange( dataViewService.Get( dataviewguid.Value ).GetQuery( null, null, out errorMessages ).OfType<Rock.Model.Person>().Select( p => p.Id ).ToList() );
                    }
                }
            }

            // Apply the Statement Delivery Preference filter
            if ( cbDeliveryPreference.SelectedValue == "Electronic" )
            {
                list = list.Where( l => !l.MailPersonIds.Intersect( l.People.Select( p => p.Id ) ).Any() ).ToList();
            }
            else if ( cbDeliveryPreference.SelectedValue == "Print & Mail" )
            {
                list = list.Where( l => l.MailPersonIds.Intersect( l.People.Select( p => p.Id ) ).Any() ).ToList();
            }


            var sortProperty = gBinaryFile.SortProperty;
            // Sort by Person Name
            if ( sortProperty != null && sortProperty.Property == "PersonNames" )
            {
                if ( sortProperty.Direction == SortDirection.Ascending )
                {
                    list = list.OrderBy( l => l.People.FirstOrDefault().LastName ).ToList();
                }
                else
                {
                    list = list.OrderByDescending( l => l.People.FirstOrDefault().LastName ).ToList();
                }
            }
            // Sort by Person Name
            else if ( sortProperty != null && sortProperty.Property == "GivingId" )
            {
                if ( sortProperty.Direction == SortDirection.Ascending )
                {
                    list = list.OrderBy( l => l.People.FirstOrDefault().GivingId ).ToList();
                }
                else
                {
                    list = list.OrderByDescending( l => l.People.FirstOrDefault().GivingId ).ToList();
                }
            }
            // Sort by Delivery Preference
            else if ( sortProperty != null && sortProperty.Property == "StatementDelivery" )
            {
                if ( sortProperty.Direction == SortDirection.Ascending )
                {
                    list = list.OrderBy( l => l.StatementDelivery ).ToList();
                }
                else
                {
                    list = list.OrderByDescending( l => l.StatementDelivery ).ToList();
                }
            }
            // Sort by LastModified
            else if ( sortProperty != null && sortProperty.Property == "ModifiedDateTime" )
            {
                if ( sortProperty.Direction == SortDirection.Ascending )
                {
                    list = list.OrderBy( l => l.BinaryFile.ModifiedDateTime ).ToList();
                }
                else
                {
                    list = list.OrderByDescending( l => l.BinaryFile.ModifiedDateTime ).ToList();
                }
            }
            // Sort by Giving Id
            else
            {
                if ( sortProperty == null || sortProperty.Direction == SortDirection.Ascending )
                {
                    list = list.OrderBy( d => d.BinaryFile.FileName ).ToList();
                }
                else
                {
                    list = list.OrderByDescending( d => d.BinaryFile.FileName ).ToList();

                }
            }

            return list;
        }
Пример #3
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // Get the entity type
            EntityTypeCache entityType     = null;
            var             entityTypeGuid = GetAttributeValue(action, AttributeKey.EntityType).AsGuidOrNull();

            if (entityTypeGuid.HasValue)
            {
                entityType = EntityTypeCache.Get(entityTypeGuid.Value);
            }

            if (entityType == null)
            {
                var message = $"Entity Type could not be found for selected value ('{entityTypeGuid.ToString()}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var mergeFields  = GetMergeFields(action);
            var _rockContext = new RockContext();

            // Get the entity
            var     entityTypeService  = new EntityTypeService(_rockContext);
            IEntity entityObject       = null;
            var     entityIdGuidString = GetAttributeValue(action, AttributeKey.EntityIdOrGuid, true).ResolveMergeFields(mergeFields).Trim();
            var     entityId           = entityIdGuidString.AsIntegerOrNull();

            if (entityId.HasValue)
            {
                entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value);
            }
            else
            {
                var entityGuid = entityIdGuidString.AsGuidOrNull();
                if (entityGuid.HasValue)
                {
                    entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value);
                }
            }

            if (entityObject == null)
            {
                var value = GetActionAttributeValue(action, AttributeKey.EntityIdOrGuid);
                entityObject = action.GetEntityFromAttributeValue(value, rockContext);
            }

            if (entityObject == null)
            {
                var message = $"Entity could not be found for selected value ('{entityIdGuidString}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var attributeFilteredDocumentType = GetAttributeValue(action, AttributeKey.DocumentType).Split(',').Select(int.Parse).FirstOrDefault();
            var documentType = DocumentTypeCache.Get(attributeFilteredDocumentType);

            if (documentType == null)
            {
                var message = $"Document Type could not be found for selected value ('{attributeFilteredDocumentType}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityType.Id, true);

            if (!documentypesForContextEntityType.Any(d => attributeFilteredDocumentType == d.Id))
            {
                var message = "The Document Type does not match the selected entity type.";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var isDocumentTypeValid = IsDocumentTypeValidForEntity(entityObject, documentType);

            if (!isDocumentTypeValid)
            {
                var message = "The Document Type selected is not valid for the entity.";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var binaryFile = new BinaryFileService(rockContext).Get(GetAttributeValue(action, AttributeKey.DocumentAttribute, true).AsGuid());

            if (binaryFile == null)
            {
                action.AddLogEntry("The document to add to the entity was not found.", true);

                // returning true here to allow the action to run 'successfully' without a document.
                // This allows the action to be easily used when the document is optional without a bunch of action filter tests.
                return(true);
            }

            var documentName = GetAttributeValue(action, AttributeKey.DocumentName).ResolveMergeFields(mergeFields);

            if (documentName.IsNullOrWhiteSpace())
            {
                documentName = Path.GetFileNameWithoutExtension(binaryFile.FileName);
            }

            var document = new Document();

            document.Name           = documentName;
            document.Description    = GetAttributeValue(action, AttributeKey.DocumentDescription).ResolveMergeFields(mergeFields);
            document.EntityId       = entityObject.Id;
            document.DocumentTypeId = documentType.Id;
            document.SetBinaryFile(binaryFile.Id, rockContext);

            if (!document.IsValidDocument(rockContext, out string errorMessage))
            {
                errorMessages.Add(errorMessage);
                action.AddLogEntry(errorMessage, true);
                return(false);
            }

            var documentService = new DocumentService(rockContext);

            documentService.Add(document);
            rockContext.SaveChanges();

            action.AddLogEntry("Added document to the Entity.");
            return(true);
        }
Пример #4
0
 /// <summary>
 /// Gets the cache object associated with this Entity
 /// </summary>
 /// <returns></returns>
 public IEntityCache GetCacheObject()
 {
     return DocumentTypeCache.Get( this.Id );
 }