aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultViewVM.cs
blob: 68a886f99f947bebbb9d2229952cc19a9372c015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Data.Entity;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.ActionLogs;
using Tango.BL.DTO;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.BL.ValueObjects;
using Tango.Core.Commands;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.ThreadExtensions.Models;
using Tango.SharedUI;

namespace Tango.MachineStudio.ThreadExtensions.ViewModels
{
    public class TestResultViewVM : ViewModel
    {
        private INotificationProvider _notification;
        private IActionLogManager _actionLogManager;

        #region Properties

        private string _threadName;
        /// <summary>
        /// Gets or sets the name of the thread. Using in print
        /// </summary>
        /// <value>
        /// The name of the thread.
        /// </value>
        public string ThreadName
        {
            get { return _threadName; }
            set
            {
                _threadName = value;
                RaisePropertyChangedAuto();
            }
        }


        private bool _isSelected;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; RaisePropertyChangedAuto(); }
        }

        private RmlExtensionTestResult _testResult;

        public RmlExtensionTestResult TestResult
        {
            get { return _testResult; }
            set { _testResult = value;
                RaisePropertyChangedAuto();
                RaisePropertyChanged(nameof(TestResultsFiles));
            }
        }

        public List<RmlExtensionTestResultsFile> TestResultsFiles
        {
            get
            {
                return TestResult.RmlExtensionTestResultsFiles.ToList();
            }
        }
        
        public RelayCommand<RmlExtensionTestResultsFile> DeleteCommand { get; set; }
        public RelayCommand<RmlExtensionTestResultsFile> DownLoadFileCommand { get; set; }
        public RelayCommand UploadCommand { get; set; }
        public RelayCommand DownLoadAllCommand { get; set; }
        #endregion


        public TestResultViewVM(INotificationProvider notification, IActionLogManager actionLogManager)
        {
            _notification = notification;
            _actionLogManager = actionLogManager;
            
            UploadCommand = new RelayCommand(UploadFiles);
            DownLoadFileCommand = new RelayCommand<RmlExtensionTestResultsFile>(DownLoadFile);
            DeleteCommand = new RelayCommand<RmlExtensionTestResultsFile>(DeleteFile);
            DownLoadAllCommand = new RelayCommand(DownLoadAllFiles);
        }

        #region TestResultsFiles
        
        private async void UploadFiles(object obj)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select data file";
            dlg.Filter = "CSV Files|*.csv";
            dlg.Multiselect = true;
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    var files = dlg.FileNames.ToList();

                    using (ObservablesContext db = ObservablesContext.CreateDefault())
                    {
                        var testResult = await db.RmlExtensionTestResults.Where(x => x.Guid == TestResult.Guid).Include(t1 => t1.RmlExtensionTestResultsFiles).FirstOrDefaultAsync();
                        foreach (var strpath in files)
                        {
                            var testResultfile = new RmlExtensionTestResultsFile();
                            testResultfile.FileName = Path.GetFileName(strpath);
                            //temporary!!!
                            testResultfile.FilePath = strpath;
                            
                           // TestResult.RmlExtensionTestResultsFiles.Add(testResultfile);
                            testResult.RmlExtensionTestResultsFiles.Add(testResultfile);
                        }
                        if (testResult != null)
                        {
                            await db.SaveChangesAsync();
                        }
                        TestResult.RmlExtensionTestResultsFiles = testResult.RmlExtensionTestResultsFiles;///?????
                        RaisePropertyChanged(nameof(TestResultsFiles));
                    }
                    _notification.ShowInfo("File successfully loaded.");
                }
                catch (Exception ex)
                {
                    _notification.ShowError($"An error occurred while trying to import the file.\n{ex.FlattenMessage()}");
                }
            }
           
        }
        
        private void DownLoadFile(RmlExtensionTestResultsFile file)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Save the csv file";
            dlg.Filter = "CSV Files|*.csv";
            dlg.DefaultExt = ".csv";
            dlg.FileName = file.FileName;
            if (dlg.ShowDialog().Value)
            {
                ///
            }
        }

        private void DownLoadAllFiles()
        {
            CommonOpenFileDialog dlg = new CommonOpenFileDialog();
            dlg.Title = "Select folder.";
            dlg.IsFolderPicker = true;
            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
            {
                var filesPath = TestResult.RmlExtensionTestResultsFiles.Select( x=>x.FilePath).ToList();
                /////
            }
        }

        private async void DeleteFile(RmlExtensionTestResultsFile file)
        {
            if (_notification.ShowQuestion("Are you sure you want to delete the selected file?"))
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    var deletefile = db.RmlExtensionTestResultsFiles.FirstOrDefault(x => x.Guid == file.Guid);
                    if(deletefile != null)
                    {
                        db.RmlExtensionTestResultsFiles.Remove(deletefile);
                        await db.SaveChangesAsync();
                    }
                }
                TestResult.RmlExtensionTestResultsFiles.Remove(file);
                RaisePropertyChanged(nameof(TestResultsFiles));
            }
        }
        
        #endregion

    }
}