123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Core.Postable;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- namespace PRSDesktop;
- public static class PostUtils
- {
- private static readonly Inflector.Inflector inflector = new(new CultureInfo("en"));
- public static void PostEntities<T>(IDataModel<T> model, Action refresh, Action? configurePost = null)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- try
- {
- var result = PosterUtils.Process(model);
- if(result is null)
- {
- MessageWindow.ShowMessage($"Processing failed", "Processing failed");
- refresh();
- }
- else
- {
- var failedMessages = new List<string>();
- var successCount = 0;
- foreach(var entity in result.PostedEntities)
- {
- if(entity.PostedStatus == PostedStatus.PostFailed)
- {
- failedMessages.Add(entity.PostedNote);
- }
- else
- {
- successCount++;
- }
- }
- if(successCount == 0)
- {
- MessageWindow.ShowMessage($"Processing failed:\n - {string.Join("\n - ", failedMessages)}", "Processing failed.");
- }
- else if(failedMessages.Count == 0)
- {
- MessageWindow.ShowMessage($"Processing successful; {successCount} items processed", "Processing successful.");
- }
- else
- {
- MessageWindow.ShowMessage($"{successCount} items succeeded, but {failedMessages.Count} failed:\n - {string.Join("\n - ", failedMessages)}", "Partial success");
- }
- refresh();
- }
- }
- catch (EmptyPostException)
- {
- MessageWindow.ShowMessage($"Please select at least one {typeof(T).Name}.", "Select items");
- }
- catch (PostFailedMessageException e)
- {
- MessageWindow.ShowMessage(e.Message, "Post failed");
- }
- catch (RepostedException)
- {
- MessageWindow.ShowMessage("At least one of the items you selected has already been processed. Processing cancelled.", "Already processed");
- }
- catch (PostCancelledException)
- {
- MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
- }
- catch(MissingSettingException e)
- {
- if (configurePost is not null && Security.CanConfigurePost<T>())
- {
- if (MessageWindow.ShowYesNo($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
- "Configure Processing?"))
- {
- if (e.SettingsType.IsAssignableTo(typeof(IGlobalPosterSettings)))
- {
- PostableSettingsGrid.ConfigureGlobalPosterSettings(e.SettingsType);
- }
- else
- {
- PostableSettingsGrid.ConfigurePosterSettings<T>(e.SettingsType);
- }
- }
- else
- {
- MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
- }
- }
- else
- {
- MessageWindow.ShowMessage($"'{e.Setting}' has not been set-up for {inflector.Pluralize(typeof(T).Name)}", "Unconfigured");
- }
- }
- catch (MissingSettingsException)
- {
- if (configurePost is not null && Security.CanConfigurePost<T>())
- {
- if (MessageWindow.ShowYesNo($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
- "Configure Processing?"))
- {
- configurePost();
- }
- else
- {
- MessageWindow.ShowMessage("Processing cancelled.", "Cancelled");
- }
- }
- else
- {
- MessageWindow.ShowMessage($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!", "Unconfigured");
- }
- }
- catch (Exception e)
- {
- MessageWindow.ShowError("Processing failed.", e);
- refresh();
- }
- }
- public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, Action? configurePost = null)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- var postSettings = PosterUtils.LoadPostableSettings<T>();
- if (Security.CanPost<T>() && !postSettings.PosterType.IsNullOrWhiteSpace())
- {
- Bitmap? image = null;
- if (postSettings.Thumbnail.ID != Guid.Empty)
- {
- var icon = new Client<Document>()
- .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
- if (icon?.Data?.Any() == true)
- image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
- }
- host.CreatePanelAction(new PanelAction
- {
- Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
- Image = image ?? PRSDesktop.Resources.edit,
- OnExecute = action =>
- {
- PostEntities(
- model(),
- refresh,
- configurePost);
- }
- });
- if (postSettings.ShowClearButton)
- {
- host.CreatePanelAction(new PanelAction
- {
- Caption = "Clear Posted Flag",
- Image = image ?? PRSDesktop.Resources.refresh,
- OnExecute = action =>
- {
- var dataModel = model();
- foreach(var (key, table) in dataModel.ModelTables)
- {
- table.IsDefault = false;
- }
- dataModel.SetColumns<T>(Columns.Required<T>().Add(x => x.PostedStatus).Add(x => x.PostedReference).Add(x => x.PostedNote).Add(x => x.Posted));
- dataModel.SetIsDefault<T>(true);
- dataModel.LoadModel();
- var items = dataModel.GetTable<T>().ToArray<T>();
- foreach(var item in items)
- {
- item.PostedStatus = PostedStatus.NeverPosted;
- item.PostedReference = "";
- item.PostedNote = "";
- item.Posted = DateTime.MinValue;
- }
- Client.Save(items, "Cleared posted flag");
- refresh();
- }
- });
- }
- }
- if (configurePost is not null && Security.CanConfigurePost<T>())
- {
- host.CreateSetupAction(new PanelAction
- {
- Caption = $"Configure {CoreUtils.Neatify(typeof(T).Name)} Processing",
- OnExecute = action =>
- {
- configurePost();
- }
- });
- }
- }
- public static void ConfigurePost<T>()
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- var postSettings = PosterUtils.LoadPostableSettings<T>();
- var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
- if (grid.EditItems(new PostableSettings[] { postSettings }))
- {
- PosterUtils.SavePostableSettings<T>(postSettings);
- }
- }
- public static void CreateToolbarButtons<T>(IPanelHost host, Func<IDataModel<T>> model, Action refresh, bool allowConfig)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- CreateToolbarButtons(host, model, refresh, allowConfig ? ConfigurePost<T> : null);
- }
- #region PostColumn
- private static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
- private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
- private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
- public static void AddPostColumn<T>(DynamicGrid<T> grid)
- where T : Entity, IPostable, IRemotable, IPersistent, new()
- {
- grid.HiddenColumns.Add(x => x.PostedStatus);
- grid.HiddenColumns.Add(x => x.PostedNote);
-
- grid.ActionColumns.Add(new DynamicImageColumn(
- row =>
- {
- if (row is null)
- return post;
- return row.Get<T, PostedStatus>(x => x.PostedStatus) switch
- {
- PostedStatus.PostFailed => warning,
- PostedStatus.Posted => tick,
- PostedStatus.RequiresRepost => refresh,
- PostedStatus.NeverPosted or _ => null,
- };
- },
- null)
- {
- ToolTip = (column, row) =>
- {
- if (row is null)
- {
- return column.TextToolTip($"{CoreUtils.Neatify(typeof(T).Name)} Processed Status");
- }
- return column.TextToolTip(row.Get<T, PostedStatus>(x => x.PostedStatus) switch
- {
- PostedStatus.PostFailed => "Post failed: " + row.Get<T, string>(x => x.PostedNote),
- PostedStatus.RequiresRepost => "Repost required: " + row.Get<T, string>(x => x.PostedNote),
- PostedStatus.Posted => "Processed",
- PostedStatus.NeverPosted or _ => "Not posted yet",
- });
- }
- });
- }
- #endregion
- }
|