Skip to content

Commit e05625f

Browse files
Improved ProtocolExtension and its usage
1 parent edb85c9 commit e05625f

File tree

5 files changed

+310
-44
lines changed

5 files changed

+310
-44
lines changed

QAction_1/ProtocolExtensions.cs

+290-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Skyline.Protocol.Extensions
1+
namespace Skyline.Protocol.Extension
22
{
33
using System;
44
using System.Collections.Generic;
@@ -8,53 +8,327 @@ namespace Skyline.Protocol.Extensions
88

99
public static class ProtocolExtension
1010
{
11-
public static void SetParams(this SLProtocol protocol, Dictionary<int, object> setParamsData)
11+
/// <summary>
12+
/// Removes the rows with the specified primary keys from the specified table.
13+
/// </summary>
14+
/// <param name="protocol">Link with SLProtocol process.</param>
15+
/// <param name="tablePid">The ID of the table parameter.</param>
16+
/// <param name="keysToDelete">The primary keys of the rows to remove.</param>
17+
/// <exception cref="ArgumentNullException"><paramref name="keysToDelete"/> is <see langword="null"/>.</exception>
18+
public static void DeleteRow(this SLProtocol protocol, int tablePid, IEnumerable<object> keysToDelete)
1219
{
13-
if (!setParamsData.Any())
20+
// Sanity checks
21+
if (keysToDelete == null)
22+
throw new ArgumentNullException(nameof(keysToDelete));
23+
24+
var keysToDeleteArray = keysToDelete.ToArray();
25+
26+
if (keysToDeleteArray.Length == 0)
1427
{
15-
// No param to update
28+
// No rows to delete
1629
return;
1730
}
1831

19-
protocol.SetParameters(setParamsData.Keys.ToArray(), setParamsData.Values.ToArray());
32+
// Build delete row object
33+
string[] deleteRowKeys = new string[keysToDeleteArray.Length];
34+
for (int i = 0; i < deleteRowKeys.Length; i++)
35+
{
36+
deleteRowKeys[i] = (string)keysToDeleteArray[i];
37+
}
38+
39+
// Delete rows
40+
protocol.NotifyProtocol(156, tablePid, deleteRowKeys);
2041
}
2142

22-
public static void SetColumns(this SLProtocol protocol, Dictionary<object, List<object>> setColumnsData, DateTime dateTime = default)
43+
/// <summary>
44+
/// Removes the rows with the specified primary keys from the specified table.
45+
/// </summary>
46+
/// <param name="protocol">Link with SLProtocol process.</param>
47+
/// <param name="tablePid">The ID of the table parameter.</param>
48+
/// <param name="keysToDelete">The primary keys of the rows to remove.</param>
49+
/// <exception cref="ArgumentNullException"><paramref name="keysToDelete"/> is <see langword="null"/>.</exception>
50+
public static void DeleteRow(this SLProtocol protocol, int tablePid, IEnumerable<string> keysToDelete)
2351
{
24-
// Requires Main 10.0.0 [CU?] or Feature 9.6.6 [CU?] (see RN 23815)
25-
int rowCount = setColumnsData.ElementAt(0).Value.Count;
52+
// Sanity checks
53+
if (keysToDelete == null)
54+
throw new ArgumentNullException(nameof(keysToDelete));
55+
56+
var deleteRowKeys = keysToDelete.ToArray();
57+
58+
if (deleteRowKeys.Length == 0)
59+
{
60+
// No rows to delete
61+
return;
62+
}
63+
64+
// Delete rows
65+
protocol.NotifyProtocol(156, tablePid, deleteRowKeys);
66+
}
67+
68+
/// <summary>
69+
/// Retrieves a cell from a table with the specified <paramref name="tablePid"/>, <paramref name="rowPK"/> and the <paramref name="columnIdx"/>.
70+
/// </summary>
71+
/// <param name="protocol">Link with SLProtocol process.</param>
72+
/// <param name="tablePid">The ID of the table parameter.</param>
73+
/// <param name="rowPK">The primary key of the row.</param>
74+
/// <param name="columnIdx">The 0-based position of the column, corresponding to the idx as defined in protocol.xml file.</param>
75+
/// <returns>The value of the cell.</returns>
76+
/// <exception cref="ArgumentNullException"><paramref name="rowPK"/> is <see langword="null"/>.</exception>
77+
/// <remarks>
78+
/// <list type="bullet">
79+
/// <para>The tablePid can be retrieved with the static Parameter class.</para>
80+
/// <para>The columnIdx can be retrieved with the static Parameter.[table].Idx class.</para>
81+
/// <para>returns <see langword="null"/> for uninitialized cells.</para>
82+
/// </list>
83+
/// </remarks>
84+
public static object GetCell(this SLProtocol protocol, int tablePid, string rowPK, int columnIdx)
85+
{
86+
if (rowPK == null)
87+
throw new ArgumentNullException(nameof(rowPK));
88+
89+
return protocol.NotifyProtocol(122, new object[] { tablePid, rowPK, columnIdx + 1 }, null);
90+
}
91+
92+
/// <summary>
93+
/// Retrieves the values of the column with the specified <paramref name="tablePid"/> and <paramref name="columnIdx"/>.
94+
/// </summary>
95+
/// <param name="protocol">Link with SLProtocol process.</param>
96+
/// <param name="tablePid">The ID of the table parameter.</param>
97+
/// <param name="columnIdx">The 0-based position of the column, corresponding to the idx as defined in protocol.xml file.</param>
98+
/// <returns>The values of the retrieved column.</returns>
99+
public static object[] GetColumn(this SLProtocol protocol, int tablePid, uint columnIdx)
100+
{
101+
var columns = protocol.GetColumns(tablePid, new uint[] { columnIdx });
102+
return (object[])columns[0];
103+
}
104+
105+
/// <summary>
106+
/// Retrieves the values of the columns with the specified <paramref name="tablePid"/> and <paramref name="columnsIdx"/>.
107+
/// </summary>
108+
/// <param name="protocol">Link with SLProtocol process.</param>
109+
/// <param name="tablePid">The ID of the table parameter.</param>
110+
/// <param name="columnsIdx">The 0-based positions of the columns, corresponding to the idx as defined in protocol.xml file.</param>
111+
/// <returns>The values of the retrieved columns.</returns>
112+
/// <exception cref="ArgumentNullException"><paramref name="columnsIdx"/> is <see langword="null"/>.</exception>
113+
public static object[] GetColumns(this SLProtocol protocol, int tablePid, IEnumerable<uint> columnsIdx)
114+
{
115+
// Sanity checks
116+
if (columnsIdx == null)
117+
throw new ArgumentNullException(nameof(columnsIdx));
118+
119+
var columnsIdxArray = columnsIdx.ToArray();
120+
121+
if (columnsIdxArray.Length == 0)
122+
{
123+
return new object[] { };
124+
}
125+
126+
return (object[])protocol.NotifyProtocol(321, tablePid, columnsIdxArray);
127+
}
128+
129+
/// <summary>
130+
/// Sets the value of a cell in a table, identified by the primary key of the row and column position, with the specified value.
131+
/// Use <see langword="null"/> as <paramref name="value"/> to clear the cell.
132+
/// The <paramref name="tablePid"/> can be retrieved with the static Parameter class.
133+
/// The <paramref name="columnIdx"/> can be retrieved with the static Parameter.[table].Idx class.
134+
/// </summary>
135+
/// <param name="protocol">Link with SLProtocol process.</param>
136+
/// <param name="tablePid">The ID of the table parameter.</param>
137+
/// <param name="rowPK">The primary key of the row.</param>
138+
/// <param name="columnIdx">The 0-based column position.</param>
139+
/// <param name="value">The new value. Use <see langword="null"/> to clear the cell.</param>
140+
/// <param name="dateTime">The time stamp for the new values (in case of historySets).</param>
141+
/// <returns>Whether the cell value has changed. <see langword="true"/> indicates change; otherwise, <see langword="false"/>.</returns>
142+
/// <remarks>The primary key can never be updated.</remarks>
143+
/// <exception cref="ArgumentNullException"><paramref name="rowPK"/> is <see langword="null"/>.</exception>
144+
public static bool SetCell(this SLProtocol protocol, int tablePid, string rowPK, int columnIdx, object value, DateTime? dateTime = null)
145+
{
146+
if (rowPK == null)
147+
throw new ArgumentNullException(nameof(rowPK));
148+
149+
if (dateTime == null)
150+
{
151+
return protocol.SetParameterIndexByKey(tablePid, rowPK, columnIdx + 1, value);
152+
}
153+
else
154+
{
155+
return protocol.SetParameterIndexByKey(tablePid, rowPK, columnIdx + 1, value, dateTime.Value);
156+
}
157+
}
158+
159+
/// <summary>
160+
/// Sets the specified columns.
161+
/// </summary>
162+
/// <param name="protocol">Link with SLProtocol process.</param>
163+
/// <param name="columnsPid">The column parameter ID of the columns to update. First item should contain the table PID. Primary key column PID should never be provided.</param>
164+
/// <param name="columnsValues">The column values for each column to update. First item should contain the primary keys as <see cref="string" />.</param>
165+
/// <param name="dateTime">The time stamp for the new values (in case of historySets).</param>
166+
/// <exception cref="ArgumentNullException"><paramref name="columnsPid"/> or <paramref name="columnsValues"/> is <see langword="null"/>.</exception>
167+
public static void SetColumns(this SLProtocol protocol, IList<int> columnsPid, IList<IEnumerable<object>> columnsValues, DateTime? dateTime = null)
168+
{
169+
// Sanity checks
170+
if (columnsPid == null)
171+
throw new ArgumentNullException(nameof(columnsPid));
172+
173+
if (columnsValues == null)
174+
throw new ArgumentNullException(nameof(columnsValues));
26175

27-
if (rowCount <= 0)
176+
if (columnsPid.Count != columnsValues.Count)
177+
throw new ArgumentException($"Length of {nameof(columnsPid)} '{columnsPid.Count}' != length of {nameof(columnsValues)} '{columnsValues.Count}'.");
178+
179+
// Prepare data
180+
int columnsCount = columnsPid.Count;
181+
182+
object[] columnsPidArray = new object[columnsCount + 1];
183+
object[] columnsValuesArray = new object[columnsCount];
184+
185+
for (int i = 0; i < columnsCount; i++)
186+
{
187+
columnsPidArray[i] = columnsPid[i];
188+
columnsValuesArray[i] = columnsValues[i].ToArray();
189+
}
190+
191+
// Options (Clear & Leave, history sets)
192+
object[] setColumnOptions = dateTime == null ? new object[] { true } : new object[] { true, dateTime.Value };
193+
columnsPidArray[columnsCount] = setColumnOptions;
194+
195+
// Set columns
196+
protocol.NotifyProtocol(220, columnsPidArray, columnsValuesArray);
197+
}
198+
199+
///// <summary>
200+
///// Sets the specified columns.
201+
///// </summary>
202+
///// <param name="protocol">Link with SLProtocol process.</param>
203+
///// <param name="columnsPid">The column parameter ID of the columns to update. First item should contain the table PID. Primary key column PID should never be provided.</param>
204+
///// <param name="columnsValues">The column values for each column to update. First item should contain the primary keys as <see cref="string" />.</param>
205+
///// <param name="dateTime">The time stamp for the new values (in case of historySets).</param>
206+
///// <exception cref="ArgumentNullException"><paramref name="columnsPid"/> or <paramref name="columnsValues"/> is <see langword="null"/>.</exception>
207+
//public static void SetColumns(this SLProtocol protocol, IEnumerable<int> columnsPid, IEnumerable<IEnumerable<object>> columnsValues, DateTime? dateTime = null)
208+
//{
209+
// // Sanity checks
210+
// if (columnsPid == null)
211+
// throw new ArgumentNullException(nameof(columnsPid));
212+
213+
// if (columnsValues == null)
214+
// throw new ArgumentNullException(nameof(columnsValues));
215+
216+
// int columnsPidCount = columnsPid.Count();
217+
218+
// if (columnsPidCount != columnsValues.Count())
219+
// throw new ArgumentException($"Length of {nameof(columnsPid)} '{columnsPidCount}' != length of {nameof(columnsValues)} '{columnsValues.Count()}'.");
220+
221+
// // Prepare data
222+
// object[] columnsPidArray = new object[columnsPidCount + 1];
223+
// object[] columnsValuesArray = new object[columnsPidCount];
224+
225+
// int columnPos = 0;
226+
// foreach (var columnPid in columnsPid)
227+
// {
228+
// columnsPidArray[columnPos++] = columnPid;
229+
// }
230+
231+
// columnPos = 0;
232+
// foreach (var columnValue in columnsValues)
233+
// {
234+
// columnsValuesArray[columnPos++] = columnValue.ToArray();
235+
// }
236+
237+
// // Options (Clear & Leave, history sets)
238+
// object[] setColumnOptions = dateTime == null ? new object[] { true } : new object[] { true, dateTime.Value };
239+
// columnsPidArray[columnsPidCount] = setColumnOptions;
240+
241+
// // Set columns
242+
// protocol.NotifyProtocol(220, columnsPidArray, columnsValuesArray);
243+
//}
244+
245+
/// <summary>
246+
/// Sets the specified columns (Requires Main 10.0.0 [CU?] or Feature 9.6.6 [CU?] (see RN 23815)).
247+
/// </summary>
248+
/// <param name="protocol">Link with SLProtocol process.</param>
249+
/// <param name="setColumnsData">The new column values per column PID. The first dictionary item should contain table PID as key and primary keys as value.</param>
250+
/// <param name="dateTime">The time stamp for the new values (in case of historySets).</param>
251+
/// <exception cref="ArgumentNullException"><paramref name="setColumnsData"/> is <see langword="null"/>.</exception>
252+
public static void SetColumns(this SLProtocol protocol, IDictionary<int, List<object>> setColumnsData, DateTime? dateTime = null)
253+
{
254+
// Sanity checks
255+
if (setColumnsData == null)
256+
throw new ArgumentNullException(nameof(setColumnsData));
257+
258+
if (setColumnsData.Count == 0)
259+
return;
260+
261+
int rowCount = setColumnsData.ElementAt(0).Value.Count;
262+
if (rowCount == 0)
28263
{
29264
// No rows to update
30265
return;
31266
}
32267

268+
// Prepare data
33269
object[] setColumnPids = new object[setColumnsData.Count + 1];
34-
object[] setColumnOptions = dateTime == default ? new object[] { true } : new object[] { true, dateTime };
35-
36270
object[] setColumnValues = new object[setColumnsData.Count];
37271

38-
for (int i = 0; i < setColumnValues.Length; i++)
272+
int columnPos = 0;
273+
foreach (var setColumnData in setColumnsData)
39274
{
40275
// Sanity checks
41-
if (setColumnsData.ElementAt(i).Value.Count != rowCount)
276+
if (setColumnData.Value.Count != rowCount)
42277
{
43278
protocol.Log(
44-
$"QA{protocol.QActionID}|SetColumns|SetColumns on table '{setColumnsData.Keys.ToArray()[0]}' failed. Not all columns contain the same number of rows.",
279+
$"QA{protocol.QActionID}|SetColumns|SetColumns on table '{setColumnsData.Keys.ToArray()[0]}' failed. " +
280+
$"Not all columns contain the same number of rows.",
45281
LogType.Error,
46282
LogLevel.NoLogging);
47283

48284
return;
49285
}
50286

51287
// Build set columns objects
52-
setColumnPids[i] = setColumnsData.ElementAt(i).Key;
53-
setColumnValues[i] = setColumnsData.ElementAt(i).Value.ToArray();
288+
setColumnPids[columnPos] = setColumnData.Key;
289+
setColumnValues[columnPos] = setColumnData.Value.ToArray();
290+
291+
columnPos++;
54292
}
55293

294+
// Options (Clear & Leave, history sets)
295+
object[] setColumnOptions = dateTime == null ? new object[] { true } : new object[] { true, dateTime.Value };
56296
setColumnPids[setColumnPids.Length - 1] = setColumnOptions;
297+
298+
// Set columns
57299
protocol.NotifyProtocol(220, setColumnPids, setColumnValues);
58300
}
301+
302+
/// <summary>
303+
/// Sets the specified parameters to the specified values.
304+
/// </summary>
305+
/// <param name="protocol">Link with SLProtocol process.</param>
306+
/// <param name="paramsToSet">The IDs of the parameters to set with their value to set.</param>
307+
/// <param name="dateTime">The time stamp for the new values (in case of historySets).</param>
308+
/// <exception cref="ArgumentNullException"><paramref name="paramsToSet"/> is <see langword="null"/>.</exception>
309+
public static void SetParams(this SLProtocol protocol, IDictionary<int, object> paramsToSet, DateTime? dateTime = null)
310+
{
311+
// Sanity checks
312+
if (paramsToSet == null)
313+
throw new ArgumentNullException(nameof(paramsToSet));
314+
315+
if (paramsToSet.Count == 0)
316+
return;
317+
318+
if (dateTime == null)
319+
{
320+
protocol.SetParameters(paramsToSet.Keys.ToArray(), paramsToSet.Values.ToArray());
321+
}
322+
else
323+
{
324+
DateTime[] historySetDates = new DateTime[paramsToSet.Count];
325+
for (int i = 0; i < historySetDates.Length; i++)
326+
{
327+
historySetDates[i] = dateTime.Value;
328+
}
329+
330+
protocol.SetParameters(paramsToSet.Keys.ToArray(), paramsToSet.Values.ToArray(), historySetDates);
331+
}
332+
}
59333
}
60334
}

0 commit comments

Comments
 (0)