-
Notifications
You must be signed in to change notification settings - Fork 137
/
CrudTab.cs
145 lines (127 loc) · 4.52 KB
/
CrudTab.cs
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.Linq;
using System.Windows.Forms;
using SolarWinds.InformationService.Contract2;
using SwqlStudio.Metadata;
using SwqlStudio.Utils;
namespace SwqlStudio
{
public partial class CrudTab : TabTemplate, IConnectionTab
{
private const string SwisUri = "SwisUri";
private readonly CrudOperation _operation;
private Entity _entity;
public CrudTab(CrudOperation operation)
{
_operation = operation;
InitializeComponent();
DpiHelper.FixRowHeight(propertiesDataGridView);
}
public Entity Entity
{
get => _entity;
set
{
_entity = value;
BindProperties();
CommitButton.Text = _operation.ToString();
}
}
public event EventHandler CloseItself;
private void BindProperties()
{
crudPropertyBindingSource.Clear();
bool createProperties = true;
switch (_operation)
{
case CrudOperation.Create:
break;
case CrudOperation.Delete:
createProperties = false;
goto case CrudOperation.Update;
case CrudOperation.Update:
crudPropertyBindingSource.Add(new CrudProperty(new Property
{
IsKey = true,
Name = SwisUri,
Type = typeof(string).Name
}, false));
break;
default:
throw new ArgumentOutOfRangeException();
}
// for delete, uri is enough
if (createProperties)
{
foreach (var property in _entity.Properties
.OrderByDescending(p => p.IsKey)
.ThenBy(p => p.IsNavigable)
.ThenBy(p => p.IsInherited)
.ThenBy(p => p.Name))
{
crudPropertyBindingSource.Add(new CrudProperty(property));
}
}
}
private void CommitButton_Click(object sender, EventArgs e)
{
if (ConnectionInfo == null)
return;
var propertyBag = CreatePropertyBag();
try
{
var message = "";
switch (_operation)
{
case CrudOperation.Create:
var rv = ConnectionInfo.Proxy.Create(_entity.FullName, propertyBag);
message = $"Created: {rv}";
break;
case CrudOperation.Update:
ConnectionInfo.Proxy.Update(((CrudProperty)crudPropertyBindingSource[0]).Value, propertyBag);
message = "OK";
break;
case CrudOperation.Delete:
ConnectionInfo.Proxy.Delete(((CrudProperty)crudPropertyBindingSource[0]).Value);
message = "OK";
break;
default:
throw new ArgumentOutOfRangeException();
}
MessageBox.Show(message, "SwqlStudio", MessageBoxButtons.OK, MessageBoxIcon.Information);
OnCloseItself();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to {_operation}: {ex.Message}",
"SwqlStudio",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private PropertyBag CreatePropertyBag()
{
var rv = new PropertyBag();
foreach (CrudProperty property in crudPropertyBindingSource)
{
if (property.UseInPropertyBag && !string.IsNullOrEmpty(property.Value))
{
rv[property.Name] = property.Value;
}
}
return rv;
}
protected virtual void OnCloseItself()
{
CloseItself?.Invoke(this, EventArgs.Empty);
}
private void propertiesDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
int imageIndex = (int)propertiesDataGridView[e.ColumnIndex, e.RowIndex].Value;
e.Value = propertiesImageList.Images[imageIndex];
}
}
}
}