486 lines
19 KiB
C#
486 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
|
|
using C4IT.API.Contracts;
|
|
using C4IT.Logging;
|
|
|
|
using C4IT_CustomerPanel.libs;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using static C4IT.Logging.cLogManager;
|
|
|
|
namespace C4IT_CustomerPanel.UserControls
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for Announcements.xaml
|
|
/// </summary>
|
|
public partial class Announcements : UserControl
|
|
{
|
|
public readonly StackPanel _adHocannouncementHead = new StackPanel();
|
|
public Dictionary<Guid, AnnouncementListItem> announcementCollection = new Dictionary<Guid, AnnouncementListItem>();
|
|
public Dictionary<Guid, AnnouncementListItem> adhocAnnouncementCollection = new Dictionary<Guid, AnnouncementListItem>();
|
|
public List<Guid> unreadAnnouncements = new List<Guid>();
|
|
public List<Guid> announcementIDs = new List<Guid>();
|
|
|
|
private MainWindow mainWindow;
|
|
|
|
private const string ApiAnnouncementsBaseUrl = "m42Services/api/c4it/customerpanel/announcements";
|
|
private const string ApiAnnouncementsQueryParam = "?type=";
|
|
|
|
public Announcements()
|
|
{
|
|
mainWindow = MainWindow.MainInstance;
|
|
InitializeComponent();
|
|
}
|
|
|
|
public void InitializeContracts()
|
|
{
|
|
try
|
|
{
|
|
announcementIDs = ConfigClass.LoadFromJson<List<Guid>>("LastAnnouncementIDs", "run");
|
|
}
|
|
catch { }
|
|
try
|
|
{
|
|
unreadAnnouncements = ConfigClass.LoadFromJson<List<Guid>>("UnreadAnnouncements", "run");
|
|
}
|
|
catch { }
|
|
|
|
if (announcementIDs == null)
|
|
announcementIDs = new List<Guid>();
|
|
|
|
|
|
if (unreadAnnouncements == null)
|
|
unreadAnnouncements = new List<Guid>();
|
|
}
|
|
|
|
internal void ReadAll_PreviewMouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
foreach (KeyValuePair<Guid, AnnouncementListItem> item in announcementCollection)
|
|
{
|
|
item.Value.IsUnread = false;
|
|
}
|
|
foreach (KeyValuePair<Guid, AnnouncementListItem> item in adhocAnnouncementCollection)
|
|
{
|
|
item.Value.IsUnread = false;
|
|
}
|
|
CheckAllAnnouncementsRead(null, null);
|
|
}
|
|
|
|
public void ProcessNewAnnouncements(List<Announcement> _announcements, announcementType type)
|
|
{
|
|
try
|
|
{
|
|
if (type == announcementType.Adhoc)
|
|
{
|
|
foreach (var pair in adhocAnnouncementCollection)
|
|
{
|
|
adhocAnnouncementCollection[pair.Key]._PrepareForDelete = true;
|
|
}
|
|
}
|
|
if (type == announcementType.Regular)
|
|
{
|
|
foreach (var pair in announcementCollection)
|
|
{
|
|
announcementCollection[pair.Key]._PrepareForDelete = true;
|
|
}
|
|
}
|
|
if (_announcements != null && _announcements.Count > 0)
|
|
{
|
|
var newItemsCount = 0;
|
|
|
|
foreach (Announcement an in _announcements)
|
|
{
|
|
Guid currentEOID = an._objectID;
|
|
|
|
if (!currentEOID.Equals(Guid.Empty))
|
|
{
|
|
AnnouncementListItem tmpItem;
|
|
if (type == announcementType.Adhoc)
|
|
{
|
|
|
|
if (adhocAnnouncementCollection.TryGetValue(currentEOID, out tmpItem))
|
|
{
|
|
tmpItem.AnnouncementItem = an;
|
|
tmpItem._PrepareForDelete = false;
|
|
adhocAnnouncementCollection[currentEOID] = tmpItem;
|
|
continue;
|
|
}
|
|
}
|
|
else if (type == announcementType.Regular)
|
|
{
|
|
if (announcementCollection.TryGetValue(currentEOID, out tmpItem))
|
|
{
|
|
tmpItem._announcementItem = an;
|
|
tmpItem._PrepareForDelete = false;
|
|
announcementCollection[currentEOID] = tmpItem;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
var newItem = new AnnouncementListItem()
|
|
{
|
|
Margin = new Thickness(2, 10, 2, 0),
|
|
_PrepareForDelete = false,
|
|
_announcementItem = an
|
|
};
|
|
|
|
if (unreadAnnouncements.Contains(currentEOID) | announcementIDs.Count == 0)
|
|
{
|
|
newItem.IsUnread = true;
|
|
}
|
|
|
|
if (!announcementIDs.Contains(currentEOID))
|
|
{
|
|
announcementIDs.Add(currentEOID);
|
|
if (!unreadAnnouncements.Contains(currentEOID))
|
|
{
|
|
unreadAnnouncements.Add(currentEOID);
|
|
}
|
|
newItem.IsUnread = true;
|
|
|
|
var suj = an._subject;
|
|
if (!string.IsNullOrEmpty(suj))
|
|
mainWindow.NewContentSignalsInfo[enumMainFunctions.Announcement] = suj;
|
|
|
|
newItemsCount++;
|
|
}
|
|
|
|
newItem.imgOpen.PreviewMouseDown += new MouseButtonEventHandler(OnOpenAnnClicked);
|
|
newItem.signal.PreviewMouseDown += CheckAllAnnouncementsRead;
|
|
switch (type)
|
|
{
|
|
case announcementType.Regular:
|
|
announcementCollection.Add(currentEOID, newItem);
|
|
break;
|
|
case announcementType.Adhoc:
|
|
adhocAnnouncementCollection.Add(currentEOID, newItem);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (newItemsCount > 1 || (newItemsCount == 1 && !mainWindow.NewContentSignalsInfo.ContainsKey(enumMainFunctions.Announcement)))
|
|
mainWindow.NewContentSignalsInfo[enumMainFunctions.Announcement] = Properties.Resources.NewAnnouncementsMessage;
|
|
|
|
ConfigClass.SaveAsJson("LastAnnouncementIDs", announcementIDs, "run");
|
|
ConfigClass.SaveAsJson("UnreadAnnouncements", unreadAnnouncements, "run");
|
|
|
|
NoAnnouncement.Visibility = Visibility.Hidden;
|
|
CanvasAnnouncements.Visibility = Visibility.Visible;
|
|
}
|
|
else
|
|
{
|
|
if (type == announcementType.Adhoc) adhocAnnouncementCollection.Clear();
|
|
if (type == announcementType.Regular) announcementCollection.Clear();
|
|
}
|
|
|
|
ConfigClass.SaveAsJson("UnreadAnnouncements", unreadAnnouncements, "run");
|
|
announcementCollection = announcementCollection.Where(i => i.Value._PrepareForDelete == false).ToDictionary(i => i.Key, i => i.Value);
|
|
adhocAnnouncementCollection = adhocAnnouncementCollection.Where(i => i.Value._PrepareForDelete == false).ToDictionary(i => i.Key, i => i.Value);
|
|
//var combinedCollection = announcementCollection
|
|
// .Concat(adhocAnnouncementCollection)
|
|
// .ToDictionary(i => i.Key, i => i.Value)
|
|
// .ToList();
|
|
|
|
//announcementIDs = combinedCollection.Select(x => x.Key).Distinct().ToList();
|
|
FillAnnouncementGrid();
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
LogException(E);
|
|
}
|
|
}
|
|
|
|
public void FillAnnouncementGrid()
|
|
{
|
|
try
|
|
{
|
|
Boolean setNewContent = false;
|
|
|
|
// Zusammenführen der Ankündigungen AdHoc und Regular
|
|
Dictionary<Guid, AnnouncementListItem> combinedAnnouncements = new Dictionary<Guid, AnnouncementListItem>();
|
|
foreach (var item in announcementCollection)
|
|
{
|
|
combinedAnnouncements[item.Key] = item.Value;
|
|
}
|
|
foreach (var item in adhocAnnouncementCollection)
|
|
{
|
|
combinedAnnouncements[item.Key] = item.Value;
|
|
}
|
|
|
|
foreach (var announcement in combinedAnnouncements.Values)
|
|
{
|
|
var effectiveDate = announcement.VisibleFrom.HasValue && announcement.VisibleFrom > announcement.CreatedDate
|
|
? announcement.VisibleFrom.Value
|
|
: announcement.CreatedDate;
|
|
announcement.CreatedDate = effectiveDate;
|
|
}
|
|
var sortedAnnouncementsList = combinedAnnouncements.Values
|
|
.OrderByDescending(a => a.Priority >= 3)
|
|
.ThenByDescending(a => a.IsUnread)
|
|
.ThenByDescending(a => a.CreatedDate)
|
|
.ToList();
|
|
|
|
var sortedAnnouncements = sortedAnnouncementsList
|
|
.ToDictionary(announcement => announcement.getEOID(), announcement => announcement);
|
|
|
|
_adHocannouncementHead.Children.Clear();
|
|
List<Guid> toRemoveAdHoc = new List<Guid>();
|
|
foreach (var item in sortedAnnouncements)
|
|
{
|
|
if (announcementIDs.Contains(item.Value.getEOID()))
|
|
{
|
|
if (item.Value.IsUnread)
|
|
{
|
|
setNewContent = true;
|
|
}
|
|
_adHocannouncementHead.Children.Add(item.Value);
|
|
}
|
|
else
|
|
{
|
|
toRemoveAdHoc.Add(item.Key);
|
|
}
|
|
}
|
|
foreach (var rem in toRemoveAdHoc)
|
|
{
|
|
adhocAnnouncementCollection.Remove(rem);
|
|
}
|
|
|
|
if (_adHocannouncementHead.Children.Count > 0)
|
|
{
|
|
CanvasAnnouncements.Visibility = Visibility.Visible;
|
|
CanvasAnnouncements.Content = _adHocannouncementHead;
|
|
}
|
|
else
|
|
{
|
|
CanvasAnnouncements.Visibility = Visibility.Collapsed;
|
|
}
|
|
|
|
if (setNewContent)
|
|
{
|
|
lock (mainWindow.NewContentSignals)
|
|
mainWindow.NewContentSignals[enumMainFunctions.Announcement] = true;
|
|
}
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
|
|
cLogManager.LogException(exp);
|
|
}
|
|
}
|
|
|
|
public void CheckAllAnnouncementsRead(object sender, MouseEventArgs e)
|
|
{
|
|
var methodInfo = MethodBase.GetCurrentMethod();
|
|
LogMethodBegin(methodInfo);
|
|
|
|
if (e != null)
|
|
e.Handled = true;
|
|
|
|
try
|
|
{
|
|
if (sender == null)
|
|
{
|
|
unreadAnnouncements.Clear();
|
|
lock (mainWindow.NewContentSignals)
|
|
{
|
|
mainWindow.NewContentSignals[enumMainFunctions.Announcement] = false;
|
|
mainWindow._isRedDotActive = false;
|
|
}
|
|
mainWindow.UpdateNewContentSignals();
|
|
}
|
|
else
|
|
{
|
|
Boolean allRead = true;
|
|
AnnouncementListItem annItem = null;
|
|
|
|
if (sender is Image)
|
|
{
|
|
Image item = (Image)sender;
|
|
annItem = (AnnouncementListItem)item.DataContext;
|
|
}
|
|
else if (sender is AnnouncementListItem)
|
|
{
|
|
annItem = (AnnouncementListItem)sender;
|
|
}
|
|
if (annItem != null)
|
|
{
|
|
annItem.IsUnread = false;
|
|
if (annItem.IsAdhoc)
|
|
{
|
|
AnnouncementListItem tempItem;
|
|
if (announcementCollection.TryGetValue(annItem._announcementItem._objectID, out tempItem))
|
|
{
|
|
tempItem.IsUnread = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AnnouncementListItem tempItem;
|
|
if (adhocAnnouncementCollection.TryGetValue(annItem._announcementItem._objectID, out tempItem))
|
|
{
|
|
tempItem.IsUnread = false;
|
|
}
|
|
}
|
|
foreach (KeyValuePair<Guid, AnnouncementListItem> ann in announcementCollection)
|
|
{
|
|
if (ann.Value.IsUnread)
|
|
{
|
|
allRead = false;
|
|
break;
|
|
}
|
|
}
|
|
foreach (KeyValuePair<Guid, AnnouncementListItem> ann in adhocAnnouncementCollection)
|
|
{
|
|
if (ann.Value.IsUnread)
|
|
{
|
|
allRead = false;
|
|
break;
|
|
}
|
|
}
|
|
if (allRead)
|
|
{
|
|
unreadAnnouncements.Clear();
|
|
lock (mainWindow.NewContentSignals)
|
|
{
|
|
mainWindow.NewContentSignals[enumMainFunctions.Announcement] = false;
|
|
mainWindow._isRedDotActive = false;
|
|
}
|
|
mainWindow.UpdateNewContentSignals();
|
|
}
|
|
else
|
|
{
|
|
unreadAnnouncements.Remove(annItem.getEOID());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogEntry($"Ausnahme in {methodInfo.Name}: {ex.Message}", LogLevels.Debug);
|
|
LogException(ex);
|
|
}
|
|
finally
|
|
{
|
|
LogMethodEnd(methodInfo);
|
|
}
|
|
|
|
ConfigClass.SaveAsJson("UnreadAnnouncements", unreadAnnouncements, "run");
|
|
|
|
}
|
|
|
|
public async Task<bool> LoadAnnouncementsAsync(announcementType type)
|
|
{
|
|
string strRes = null;
|
|
var RetVal = true;
|
|
|
|
try
|
|
{
|
|
string reqUrl = mainWindow.ConfigSettings.usingGeneralAPIToken
|
|
? $"{ApiAnnouncementsBaseUrl}/{mainWindow.ConfigSettings.userInfo.Id}/{ApiAnnouncementsQueryParam}{type}"
|
|
: $"{ApiAnnouncementsBaseUrl}/{ApiAnnouncementsQueryParam}{type}";
|
|
|
|
LogEntry($"Requesting Announcements url: {reqUrl}", LogLevels.Debug);
|
|
|
|
using (var _http = mainWindow.ConfigSettings.m42WebClient.GetHttp())
|
|
{
|
|
strRes = await _http.GetApiJsonAsync(reqUrl, "Getting Announcementsinfo");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(strRes))
|
|
{
|
|
strRes = "[]";
|
|
RetVal = false;
|
|
return RetVal;
|
|
}
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
RetVal = false;
|
|
|
|
LogException(E);
|
|
}
|
|
|
|
List<Announcement> _Data = null;
|
|
try
|
|
{
|
|
_Data = JsonConvert.DeserializeObject<List<Announcement>>(strRes);
|
|
this.Dispatcher.Invoke(() =>
|
|
{
|
|
ProcessNewAnnouncements(_Data, type);
|
|
});
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
RetVal = false;
|
|
_Data = null;
|
|
LogException(E);
|
|
}
|
|
|
|
return RetVal;
|
|
}
|
|
|
|
public void OnOpenAnnClicked(object sender, EventArgs e)
|
|
{
|
|
Image img = (Image)sender;
|
|
AnnouncementListItem Item = (AnnouncementListItem)img.DataContext;
|
|
if (!Item.getEOID().Equals(Guid.Empty))
|
|
{
|
|
try
|
|
{
|
|
Process.Start(mainWindow.ConfigSettings.GetMatrixServer(true) +
|
|
"/wm/app-SelfServicePortal/notSet/preview-object/SVMAnnouncementType/" +
|
|
Item.getEOID().ToString() +
|
|
"/0/?view-options={'embedded':true}");
|
|
CheckAllAnnouncementsRead(Item, null);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//not yet implemented
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OpenAnnClicked(object sender, EventArgs e)
|
|
{
|
|
AnnouncementListItem announcementListItem = (AnnouncementListItem)sender;
|
|
AnnouncementListItem item = (AnnouncementListItem)announcementListItem.DataContext;
|
|
if (!item.getEOID().Equals(Guid.Empty))
|
|
{
|
|
try
|
|
{
|
|
Process.Start(mainWindow.ConfigSettings.GetMatrixServer(true) +
|
|
"/wm/app-SelfServicePortal/notSet/preview-object/SVMAnnouncementType/" +
|
|
item.getEOID().ToString() +
|
|
"/0/?view-options={'embedded':true}");
|
|
CheckAllAnnouncementsRead(item, null);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//not yet implemented
|
|
}
|
|
}
|
|
}
|
|
|
|
public Image ReadAllButton => buttonReadAllAnnouncements;
|
|
}
|
|
}
|