/
TagCommand.cs
490 lines (434 loc) · 23.3 KB
/
TagCommand.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace Tizen.Content.MediaContent
{
/// <summary>
/// Provides the commands to manage tags in the database.
/// </summary>
/// <seealso cref="Tag"/>
/// <since_tizen> 4 </since_tizen>
public class TagCommand : MediaCommand
{
/// <summary>
/// Initializes a new instance of the <see cref="TagCommand"/> class with the specified <see cref="MediaDatabase"/>.
/// </summary>
/// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
/// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
/// <since_tizen> 4 </since_tizen>
public TagCommand(MediaDatabase database) : base(database)
{
}
/// <summary>
/// Retrieves the number of tags.
/// </summary>
/// <returns>The number of tags.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <since_tizen> 4 </since_tizen>
public int Count()
{
return Count(arguments: null);
}
/// <summary>
/// Retrieves the number of tags with the <see cref="CountArguments"/>.
/// </summary>
/// <param name="arguments">The criteria to use to filter. This value can be null.</param>
/// <returns>The number of tags filtered.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <since_tizen> 4 </since_tizen>
public int Count(CountArguments arguments)
{
ValidateDatabase();
return CommandHelper.Count(Interop.Tag.GetTagCount, arguments);
}
/// <summary>
/// Deletes a tag from the database.
/// </summary>
/// <privilege>http://tizen.org/privilege/content.write</privilege>
/// <param name="tagId">The tag ID to delete.</param>
/// <returns>true if the matched record was found and deleted, otherwise false.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <since_tizen> 4 </since_tizen>
public bool Delete(int tagId)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
if (CommandHelper.Count(Interop.Tag.GetTagCount, $"{TagColumns.Id}={tagId}") == 0)
{
return false;
}
CommandHelper.Delete(Interop.Tag.Delete, tagId);
return true;
}
/// <summary>
/// Inserts a tag into the database with the specified name.
/// </summary>
/// <privilege>http://tizen.org/privilege/content.write</privilege>
/// <param name="name">The name of tag.</param>
/// <returns>The <see cref="Tag"/> instance that contains the record information inserted.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <since_tizen> 4 </since_tizen>
public Tag Insert(string name)
{
ValidateDatabase();
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length == 0)
{
throw new ArgumentException("Tag name can't be an empty string.");
}
Interop.Tag.Insert(name, out var handle).ThrowIfError("Failed to insert a new tag");
try
{
return new Tag(handle);
}
finally
{
Interop.Tag.Destroy(handle);
}
}
/// <summary>
/// Updates a tag with the specified name.
/// </summary>
/// <privilege>http://tizen.org/privilege/content.write</privilege>
/// <param name="tagId">The tag ID to update.</param>
/// <param name="name">The new name.</param>
/// <returns>true if the matched record was found and updated, otherwise false.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <since_tizen> 4 </since_tizen>
public bool UpdateName(int tagId, string name)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (CommandHelper.Count(Interop.Tag.GetTagCount, $"{TagColumns.Id}={tagId}") == 0)
{
return false;
}
Interop.Tag.Create(out var handle).ThrowIfError("Failed to update");
try
{
Interop.Tag.SetName(handle, name).ThrowIfError("Failed to update");
Interop.Tag.Update(tagId, handle).ThrowIfError("Failed to update");
return true;
}
finally
{
Interop.Tag.Destroy(handle);
}
}
/// <summary>
/// Retrieves the tag with the specified ID.
/// </summary>
/// <param name="tagId">The tag ID to select.</param>
/// <returns>The <see cref="Tag"/> instance if the matched record was found in the database, otherwise null.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public Tag Select(int tagId)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
IntPtr handle = IntPtr.Zero;
try
{
Interop.Tag.GetTagFromDb(tagId, out handle).ThrowIfError("Failed to query");
return new Tag(handle);
}
catch (ArgumentException)
{
// Native FW returns ArgumentException when there's no matched record.
return null;
}
finally
{
if (handle != IntPtr.Zero)
{
Interop.Tag.Destroy(handle);
}
}
}
/// <summary>
/// Retrieves all the tags.
/// </summary>
/// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <since_tizen> 4 </since_tizen>
public MediaDataReader<Tag> Select()
{
return Select(arguments: null);
}
/// <summary>
/// Retrieves the tags with the <see cref="SelectArguments"/>.
/// </summary>
/// <param name="arguments">The criteria to use to filter. This value can be null.</param>
/// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <since_tizen> 4 </since_tizen>
public MediaDataReader<Tag> Select(SelectArguments arguments)
{
ValidateDatabase();
return CommandHelper.Select(arguments, Interop.Tag.ForeachTagFromDb, Tag.FromHandle);
}
/// <summary>
/// Retrieves the number of media info of the tag.
/// </summary>
/// <param name="tagId">The tag ID.</param>
/// <returns>The number of the media information.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public int CountMedia(int tagId)
{
return CountMedia(tagId, null);
}
/// <summary>
/// Retrieves the number of the media information of the tag with the <see cref="CountArguments"/>.
/// </summary>
/// <param name="tagId">The tag ID to query with.</param>
/// <param name="arguments">The criteria to use to filter. This value can be null.</param>
/// <returns>The number of the media information.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public int CountMedia(int tagId, CountArguments arguments)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
return CommandHelper.Count(Interop.Tag.GetMediaCount, tagId, arguments);
}
/// <summary>
/// Retrieves the media information of the tag.
/// </summary>
/// <param name="tagId">The tag ID.</param>
/// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public MediaDataReader<MediaInfo> SelectMedia(int tagId)
{
return SelectMedia(tagId, null);
}
/// <summary>
/// Retrieves the media information of the tag with the <see cref="SelectArguments"/>.
/// </summary>
/// <param name="tagId">The tag ID.</param>
/// <param name="filter">The criteria to use to filter. This value can be null.</param>
/// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public MediaDataReader<MediaInfo> SelectMedia(int tagId, SelectArguments filter)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
return CommandHelper.SelectMedia(Interop.Tag.ForeachMediaFromDb, tagId, filter);
}
private bool UpdateMember(int tagId, IEnumerable<string> mediaIds,
Func<IntPtr, string, MediaContentError> func)
{
ValidateDatabase();
if (tagId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(tagId), tagId,
"Tag id can't be less than or equal to zero.");
}
if (mediaIds == null)
{
throw new ArgumentNullException(nameof(mediaIds));
}
if (mediaIds.Count() == 0)
{
throw new ArgumentException("mediaIds has no element.", nameof(mediaIds));
}
if (CommandHelper.Count(Interop.Tag.GetTagCount, $"{TagColumns.Id}={tagId}") == 0)
{
return false;
}
IntPtr handle = IntPtr.Zero;
Interop.Tag.Create(out handle).ThrowIfError("Failed to initialize update member operation");
try
{
foreach (var mediaId in mediaIds)
{
if (mediaId == null)
{
throw new ArgumentException("Media id should not be null.", nameof(mediaIds));
}
if (string.IsNullOrWhiteSpace(mediaId))
{
throw new ArgumentException("Media id should not be a zero-length string.", nameof(mediaId));
}
func(handle, mediaId).ThrowIfError("Failed to update member");
}
Interop.Tag.Update(tagId, handle).ThrowIfError("Failed to run member update");
return true;
}
finally
{
if (handle != IntPtr.Zero)
{
Interop.Tag.Destroy(handle);
}
}
}
/// <summary>
/// Adds the media to a tag.
/// </summary>
/// <param name="tagId">The tag ID that the media will be added to.</param>
/// <param name="mediaId">The media ID to add to the tag.</param>
/// <returns>true if the matched record was found and updated, otherwise false.</returns>
/// <remarks>The invalid media ID will be ignored.</remarks>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="mediaId"/> is a zero-length string, contains only white space.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public bool AddMedia(int tagId, string mediaId)
{
ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId));
return AddMedia(tagId, new string[] { mediaId });
}
/// <summary>
/// Adds the media set to a tag.
/// </summary>
/// <param name="tagId">The tag ID that the media will be added to.</param>
/// <param name="mediaIds">The media ID to add to the tag.</param>
/// <returns>true if the matched record was found and updated, otherwise false.</returns>
/// <remarks>The invalid media IDs will be ignored.</remarks>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="mediaIds"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="mediaIds"/> has no element.<br/>
/// -or-<br/>
/// <paramref name="mediaIds"/> contains null value.<br/>
/// -or-<br/>
/// <paramref name="mediaIds"/> contains a zero-length string, contains only white space.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public bool AddMedia(int tagId, IEnumerable<string> mediaIds)
{
return UpdateMember(tagId, mediaIds, Interop.Tag.AddMedia);
}
/// <summary>
/// Removes the media from a tag.
/// </summary>
/// <param name="tagId">The tag ID.</param>
/// <param name="mediaId">The media ID to remove from the tag.</param>
/// <returns>true if the matched record was found and updated, otherwise false.</returns>
/// <remarks>The invalid media ID will be ignored.</remarks>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="mediaId"/> is a zero-length string, contains only white space.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public bool RemoveMedia(int tagId, string mediaId)
{
ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId));
return RemoveMedia(tagId, new string[] { mediaId });
}
/// <summary>
/// Removes the media set from a tag.
/// </summary>
/// <param name="tagId">The tag ID.</param>
/// <param name="mediaIds">The collection of media ID to remove from the tag.</param>
/// <returns>true if the matched record was found and updated, otherwise false.</returns>
/// <remarks>The invalid IDs will be ignored.</remarks>
/// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
/// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
/// <exception cref="ArgumentNullException"><paramref name="mediaIds"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="mediaIds"/> has no element.<br/>
/// -or-<br/>
/// <paramref name="mediaIds"/> contains null value.<br/>
/// -or-<br/>
/// <paramref name="mediaIds"/> contains a zero-length string or white space.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="tagId"/> is less than or equal to zero.</exception>
/// <since_tizen> 4 </since_tizen>
public bool RemoveMedia(int tagId, IEnumerable<string> mediaIds)
{
return UpdateMember(tagId, mediaIds, Interop.Tag.RemoveMedia);
}
}
}