/// <summary>
        /// Bind the data grid to the list of file formats in the system.
        /// </summary>
        private void BindGrid()
        {
            var fileFormatService = new ImageCashLetterFileFormatService(new RockContext());

            var types = fileFormatService.Queryable("EntityType")
                        .OrderBy(f => f.Name)
                        .ThenBy(f => f.Id)
                        .ToList();

            gFileFormat.EntityTypeId = EntityTypeCache.Read <ImageCashLetterFileFormat>().Id;
            gFileFormat.DataSource   = types;
            gFileFormat.DataBind();
        }
        /// <summary>
        /// Handles the Click event of the lbSave 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 lbSave_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            ImageCashLetterFileFormat fileFormat = null;

            var fileFormatService = new ImageCashLetterFileFormatService(rockContext);

            if (FileFormatId != 0)
            {
                fileFormat = fileFormatService.Get(FileFormatId);
            }

            if (fileFormat == null)
            {
                fileFormat = new ImageCashLetterFileFormat();
                fileFormatService.Add(fileFormat);
            }

            if (fileFormat != null)
            {
                var completedProjectIds = new List <int>();

                fileFormat.Name             = tbName.Text;
                fileFormat.Description      = tbDescription.Text;
                fileFormat.IsActive         = cbIsActive.Checked;
                fileFormat.FileNameTemplate = tbFileNameTemplate.Text;
                fileFormat.EntityTypeId     = cpFileFormatType.SelectedEntityTypeId;

                //
                // Store the attribute values.
                //
                fileFormat.LoadAttributes(rockContext);
                Rock.Attribute.Helper.GetEditValues(phEditAttributes, fileFormat);

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

                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();
                    fileFormat.SaveAttributeValues(rockContext);
                });

                NavigateToParentPage();
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
            {
                var fileFormatService = new ImageCashLetterFileFormatService(new RockContext());
                var fileFormats       = fileFormatService.Queryable().Where(f => f.IsActive == true);

                ddlFileFormat.Items.Clear();
                ddlFileFormat.Items.Add(new ListItem());
                foreach (var fileFormat in fileFormats)
                {
                    ddlFileFormat.Items.Add(new ListItem(fileFormat.Name, fileFormat.Id.ToString()));
                }
            }
        }
        /// <summary>
        /// Shows the edit panel.
        /// </summary>
        /// <param name="fileFormatId">The file format identifier.</param>
        public void ShowDetail(int fileFormatId)
        {
            var rockContext = new RockContext();
            ImageCashLetterFileFormat fileFormat = null;
            bool editAllowed = IsUserAuthorized(Authorization.EDIT);

            if (fileFormatId != 0)
            {
                fileFormat  = new ImageCashLetterFileFormatService(rockContext).Get(fileFormatId);
                editAllowed = editAllowed || fileFormat.IsAuthorized(Authorization.EDIT, CurrentPerson);
                pdAuditDetails.SetEntity(fileFormat, ResolveRockUrl("~"));
            }

            if (fileFormat == null)
            {
                fileFormat = new ImageCashLetterFileFormat {
                    Id = 0
                };
                fileFormat.FileNameTemplate = "{{ 'Now' | Date:'yyyyMMddHHmm.x937' }}";
                // hide the panel drawer that show created and last modified dates
                pdAuditDetails.Visible = false;
            }

            FileFormatId = fileFormat.Id;

            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if (!editAllowed || !IsUserAuthorized(Authorization.EDIT))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed("File Format");
            }

            if (readOnly)
            {
                ShowReadonlyDetails(fileFormat);
            }
            else
            {
                ShowEditDetails(fileFormat);
            }
        }
        /// <summary>
        /// Handles the Delete event of the 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 gFileFormat_Delete(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            var rockContext       = new RockContext();
            var fileFormatService = new ImageCashLetterFileFormatService(rockContext);
            var fileFormat        = fileFormatService.Get(e.RowKeyId);

            if (fileFormat != null)
            {
                int fileFormatId = fileFormat.Id;

                if (!fileFormat.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson))
                {
                    mdGridWarning.Show("Sorry, you're not authorized to delete this file format.", ModalAlertType.Alert);
                    return;
                }

                fileFormatService.Delete(fileFormat);
                rockContext.SaveChanges();
            }

            BindGrid();
        }
        protected void lbExport_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var           batchId    = tbBatchId.Text.AsInteger();
                var           batches    = new FinancialBatchService(rockContext).Queryable().Where(b => b.Id == batchId).ToList();
                var           fileFormat = new ImageCashLetterFileFormatService(rockContext).Get(ddlFileFormat.SelectedValue.AsInteger());
                var           component  = FileFormatTypeContainer.GetComponent(fileFormat.EntityType.Name);
                List <string> errorMessages;

                fileFormat.LoadAttributes(rockContext);

                var mergeFields = new Dictionary <string, object>
                {
                    { "FileFormat", fileFormat }
                };
                var filename = fileFormat.FileNameTemplate.ResolveMergeFields(mergeFields);

                var stream = component.ExportBatches(fileFormat, batches, out errorMessages);

                var binaryFileService     = new BinaryFileService(rockContext);
                var binaryFileTypeService = new BinaryFileTypeService(rockContext);
                var binaryFile            = new BinaryFile
                {
                    BinaryFileTypeId = binaryFileTypeService.Get(Rock.SystemGuid.BinaryFiletype.DEFAULT.AsGuid()).Id,
                    IsTemporary      = true,
                    FileName         = filename,
                    MimeType         = "octet/stream",
                    ContentStream    = stream
                };

                binaryFileService.Add(binaryFile);
                rockContext.SaveChanges();

                pnlSuccess.Visible     = true;
                hlDownload.NavigateUrl = ResolveUrl(string.Format("~/GetFile.ashx?Id={0}&attachment=True", binaryFile.Id));
            }
        }