aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/ViewModels/ResultsViewVM.cs
blob: dfad0017daa0c79a2d072ab6f7808811df30c151 (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.CSV;
using Tango.FSE.Common;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Procedures.CSV;
using Tango.FSE.Procedures.Dialogs;

namespace Tango.FSE.Procedures.ViewModels
{
    public class ResultsViewVM : FSEViewModel
    {
        private List<Result> _results;
        public List<Result> Results
        {
            get { return _results; }
            set { _results = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand<Result> DisplayResultGridCommand { get; set; }
        public RelayCommand<Result> ExportResultGridCommand { get; set; }

        public ResultsViewVM()
        {
            TangoIOC.Default.Inject(this);

            Results = new List<Result>();
            DisplayResultGridCommand = new RelayCommand<Result>(DisplayResultGrid);
            ExportResultGridCommand = new RelayCommand<Result>(ExportResultGrid);
        }

        private void DisplayResultGrid(Result result)
        {
            if (result != null)
            {
                ResultGridViewVM vm = new ResultGridViewVM(result);
                NotificationProvider.ShowDialog(vm);
            }
        }

        private async void ExportResultGrid(Result result)
        {
            if (result != null && result.IsValueArray && (result.Value as IList).Count > 0)
            {
                var r = await StorageProvider.SaveFile("Export procedure result", "CSV Files|*.csv", result.Name + ".csv", ".csv");
                if (r.Confirmed)
                {
                    using (NotificationProvider.PushTaskItem("Exporting csv file..."))
                    {
                        try
                        {
                            await Task.Factory.StartNew(() =>
                            {
                                List<Object> values = (result.Value as IEnumerable).Cast<Object>().ToList();

                                var model = values.First();

                                if (model.GetType().IsValueTypeOrString())
                                {
                                    CsvFile<CsvRow> csvFile = new CsvFile<CsvRow>(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = new List<String>() { "Values" } });

                                    foreach (var value in values)
                                    {
                                        csvFile.Append(new CsvRow()
                                        {
                                            V0 = value
                                        });
                                    }

                                    csvFile.Dispose();
                                }
                                else
                                {
                                    List<CsvColumn> columns = new List<CsvColumn>();

                                    //prop columns
                                    foreach (var prop in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                                    {
                                        var att = prop.GetCustomAttribute<DescriptionAttribute>();
                                        columns.Add(new CsvColumn()
                                        {
                                            Name = prop.Name,
                                            Description = att != null ? att.Description : prop.Name,
                                            IsProperty = true,
                                            PropertyInfo = prop
                                        });
                                    }

                                    //field columns
                                    foreach (var field in model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
                                    {
                                        var att = field.GetCustomAttribute<DescriptionAttribute>();
                                        columns.Add(new CsvColumn()
                                        {
                                            Name = field.Name,
                                            Description = att != null ? att.Description : field.Name,
                                            FieldInfo = field
                                        });
                                    }

                                    CsvFile<CsvRow> csvFile = new CsvFile<CsvRow>(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = columns.Select(x => x.Description).ToList() });

                                    List<PropertyInfo> rowProps = typeof(CsvRow).GetProperties().ToList();

                                    foreach (var value in values)
                                    {
                                        CsvRow row = new CsvRow();

                                        for (int i = 0; i < columns.Count; i++)
                                        {
                                            var column = columns[i];
                                            rowProps[i].SetValue(row, column.GetValue(value));
                                        }

                                        csvFile.Append(row);
                                    }

                                    csvFile.Dispose();
                                }
                            });

                            NotificationProvider.PushSnackbarItem(MessageType.Success, "CSV file exported", true, "Procedure result was exported to file.\nTap to browse the file location.", TimeSpan.FromSeconds(5), null, () => 
                            {
                                StorageProvider.ShowInExplorer(r.SelectedItem);
                            });
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, "Error exporting csv file.");
                            await NotificationProvider.ShowError($"Error occurred while exporting the csv file.\n{ex.FlattenMessage()}");
                        }
                    }
                }
            }
        }
    }
}