This repository was archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathResourceCollection.cs
More file actions
239 lines (205 loc) · 7.51 KB
/
ResourceCollection.cs
File metadata and controls
239 lines (205 loc) · 7.51 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Sage.SData.Client.Atom;
using Sage.SData.Client.Core;
using Sage.SData.Client.Extensions;
using Sage.SData.Client.Framework;
namespace SDataClientApp
{
public partial class ResourceCollection : BaseControl
{
private SDataResourceCollectionRequest _sdataResourceCollectionRequest;
private AtomFeed _feed;
private AtomFeedReader _reader;
public ResourceCollection()
{
InitializeComponent();
}
public override void Refresh()
{
tbCollectionURL.Text = new SDataUri(Service.Url).AppendPath(tbCollectionResourceKind.Text).ToString();
}
private void tbCollectionResourceKind_TextChanged(object sender, EventArgs e)
{
tbCollectionURL.Text = new SDataUri(Service.Url).AppendPath(tbCollectionResourceKind.Text).ToString();
}
private void btnCollectionRead_Click(object sender, EventArgs e)
{
collectionPayloadGrid.SelectedObject = null;
UpdateCollection();
}
private void btnReaderRead_Click(object sender, EventArgs e)
{
try
{
_sdataResourceCollectionRequest = new SDataResourceCollectionRequest(Service)
{
ResourceKind = tbCollectionResourceKind.Text,
StartIndex = (int) numStartIndex.Value,
Count = (int) numCount.Value
};
_feed = null;
_reader = _sdataResourceCollectionRequest.ExecuteReader();
tbReaderCount.Text = _reader.Count.ToString();
UpdateReaderGrid();
}
catch (SDataClientException ex)
{
MessageBox.Show(ex.Message);
}
}
private void atomEntryGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (_feed == null)
{
return;
}
var index = atomEntryGrid.SelectedRows[0].Index;
// get the atomentry for the row selected from the feed
var entry = _feed.Entries.Skip(index).First();
// show it in the grid
collectionPayloadGrid.SelectedObject = entry.GetSDataPayload();
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (_reader != null)
{
_reader.MoveFirst();
UpdateReaderGrid();
}
else
{
NavigateCollection("first");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (_reader != null)
{
_reader.MovePrevious();
UpdateReaderGrid();
}
else
{
NavigateCollection("previous");
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (_reader != null)
{
if (_reader.MoveNext())
{
UpdateReaderGrid();
}
else
{
MessageBox.Show("No entries available");
}
}
else
{
NavigateCollection("next");
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (_reader != null)
{
if (_reader.MoveLast())
{
UpdateReaderGrid();
}
else
{
MessageBox.Show("No entries available");
}
}
else
{
NavigateCollection("last");
}
}
private void NavigateCollection(string relation)
{
var link = _feed.Links.First(item => item.Relation == relation);
var query = link.Uri.Query;
var pos1 = query.IndexOf("startIndex=") + 11;
var pos2 = query.IndexOf("&", pos1);
if (pos2 < 0)
{
pos2 = query.Length;
}
numStartIndex.Value = int.Parse(query.Substring(pos1, pos2 - pos1));
UpdateCollection();
}
private void UpdateCollection()
{
try
{
_sdataResourceCollectionRequest = new SDataResourceCollectionRequest(Service)
{
ResourceKind = tbCollectionResourceKind.Text,
StartIndex = (int) numStartIndex.Value,
Count = (int) numCount.Value
};
_feed = _sdataResourceCollectionRequest.Read();
_reader = null;
var lookup = _feed.Links.ToLookup(link => link.Relation);
btnFirst.Enabled = lookup["first"].Any();
btnPrevious.Enabled = lookup["previous"].Any();
btnNext.Enabled = lookup["next"].Any();
btnLast.Enabled = lookup["last"].Any();
var table = new DataTable();
table.Columns.Add("Author");
table.Columns.Add("Id");
table.Columns.Add("Title");
// iterate through the list of entries in the feed
foreach (var atomentry in _feed.Entries)
{
var dr = table.NewRow();
dr[0] = atomentry.Authors.Select(author => author.Name).FirstOrDefault();
dr[1] = atomentry.Id.Uri.AbsoluteUri;
dr[2] = atomentry.Title.Content;
table.Rows.Add(dr);
}
// show it in the grid
atomEntryGrid.DataSource = table;
atomEntryGrid.Refresh();
atomEntryGrid.AutoResizeColumns();
if (atomEntryGrid.SelectedRows.Count != 0)
{
atomEntryGrid_CellContentClick(null, null);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateReaderGrid()
{
var table = new DataTable();
table.Columns.Add("Author");
table.Columns.Add("Id");
table.Columns.Add("Title");
var dr = table.NewRow();
var atomentry = _reader.Current;
dr[0] = atomentry.Authors.Select(author => author.Name).FirstOrDefault();
dr[1] = atomentry.Id.Uri.AbsoluteUri;
dr[2] = atomentry.Title.Content;
table.Rows.Add(dr);
// show it in the grid
atomEntryGrid.DataSource = table;
atomEntryGrid.Refresh();
var payload = atomentry.GetSDataPayload();
// show it in the grid
collectionPayloadGrid.SelectedObject = payload;
tbCurrentItem.Text = _reader.CurrentIndex.ToString();
btnPrevious.Enabled = _reader.CurrentIndex > 1;
btnNext.Enabled = _reader.CurrentIndex < _reader.Count;
}
}
}