using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using DTS.Common.Classes.Groups; using DTS.Common.Interface.Groups; using DTS.Common.Strings; using GroupImport.Resources; namespace GroupImport { /// /// Interaction logic for GroupImportOptionsView.xaml /// public partial class GroupImportPreviewView : IGroupImportPreviewView { public GroupImportPreviewView() { InitializeComponent(); } /// /// handles when tb group name changes /// in this case the UI needs to be notified and the validity of the group name may change /// /// /// private void GroupName_Changed(object sender, System.Windows.Controls.TextChangedEventArgs e) { var tb = (TextBox)sender; var group = (GroupGRPImportGroup)tb.DataContext; group.GroupName = tb.Text; foreach (var ch in group.Channels) { ch.GroupNameInvalidate(); } var vm = (GroupImportViewModel)DataContext; vm.CheckGroupName(); } /// /// handles when tb group tag changes /// in this case the UI needs to be notified and the validity of the group tag may change /// /// /// private void GroupTags_Changed(object sender, System.Windows.Controls.TextChangedEventArgs e) { var tb = (TextBox)sender; var group = (GroupGRPImportGroup)tb.DataContext; group.GroupTags = tb.Text; } /// /// handles included checkbox being checked for a group /// in this case the channels for the group should be added to the UI /// /// /// private void IncludedChecked(object sender, System.Windows.RoutedEventArgs e) { var vm = (GroupImportViewModel)DataContext; vm.InvalidateChannels(); } /// /// handles the included checkbox being unchecked for a group /// in this case the channels for the group should be removed from the UI /// /// /// private void IncludedUnchecked(object sender, System.Windows.RoutedEventArgs e) { var vm = (GroupImportViewModel)DataContext; vm.InvalidateChannels(); } /// /// checks whether it is okay to proceed to import, /// checks the validity of the selected channels and groups /// /// whether or not the user has Administrator privileges /// any fatal errors that would prevent importing /// any non fatal errors that the user needs to be warned about (but that don't prevent importing) /// true if it is okay to proceed to import public bool Validate(bool userIsAdmin, out List errors, out List warnings) { errors = new List(); warnings = new List(); var bValid = true; var vm = (GroupImportViewModel)DataContext; var bAnyValidChannels = false; if (vm.Groups.Any()) { foreach (var g in vm.Groups) { if (!g.Included) { continue; } if (!string.IsNullOrWhiteSpace(g.GroupTags) && !userIsAdmin) { foreach (var userTag in g.ImportingUserTags.Split()) { if (g.GroupTags.Split().Contains(userTag)) continue; var sb = new StringBuilder(); sb.Append(string.Format(StringResources.PreviewGroups_WontSaveWithIncompatibleTags, userTag, g.GroupTags)); sb.Append($"\r\n{StringResources.UserTags}"); sb.Append(g.ImportingUserTags.Length == 0 ? StringResources.None : g.ImportingUserTags); //The static group in question will always have at least 1 Tag, otherwise there would be no problem sb.Append($"\r\n{StringResources.GroupTags}"); sb.Append(g.GroupTags); errors.Add(sb.ToString()); return false; } } if (g.GroupNameHasError) { var msg = string.Format(StringResources.Preview_InvalidName, g.GroupName); if (!errors.Contains(msg)) { errors.Add(msg); } return false; } if (null != g.GroupErrors && g.GroupErrors.Any()) { foreach (var error in g.GroupErrors) { if (!errors.Contains(error.ExtraInfo)) { errors.Add(error.ExtraInfo); } } return false; } if (vm.CheckGroupExists(g.GroupName) && !g.Overwrite) { var msg = string.Format(StringResources.Preview_InvalidName, g.GroupName); if (!errors.Contains(msg)) { errors.Add(msg); } return false; } bValid = false; foreach (var ch in g.Channels) { if (null != ch.Error) { switch (ch.Error.ErrorCode) { case GroupGRPImportError.Errors.FileEmpty: case GroupGRPImportError.Errors.InvalidISOCodeInput: bAnyValidChannels = true; break; case GroupGRPImportError.Errors.InvalidFullScaleInput: warnings.Add(string.Format(StringResources.Preview_InvalidFullScaleInput, g.GroupName, ch.DisplayName)); bAnyValidChannels = true; break; case GroupGRPImportError.Errors.InvalidInvertInput: warnings.Add(string.Format(StringResources.Preview_InvalidSensorInput, g.GroupName, ch.DisplayName)); bAnyValidChannels = true; break; case GroupGRPImportError.Errors.InvalidSensorInput: var msg = string.Format(StringResources.Preview_InvalidRow, g.GroupName); if (!warnings.Contains(msg)) { warnings.Add(msg); } break; case GroupGRPImportError.Errors.SensorNotFound: warnings.Add(string.Format(StringResources.Preview_InvalidSensor, g.GroupName, ch.DisplayName)); break; default: throw new ArgumentOutOfRangeException(); } } else { bValid = true; bAnyValidChannels = true; } } } if (!bAnyValidChannels) { errors.Add(StringResources.Preview_NoGroupsToImport); } return bValid; } errors.Add(StringResources.Preview_NoGroupsToImport); return false; } } }