/* * 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 { /// /// Provides the commands to manage tags in the database. /// /// /// 4 public class TagCommand : MediaCommand { /// /// Initializes a new instance of the class with the specified . /// /// The that the commands run on. /// is null. /// has already been disposed. /// 4 public TagCommand(MediaDatabase database) : base(database) { } /// /// Retrieves the number of tags. /// /// The number of tags. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// 4 public int Count() { return Count(arguments: null); } /// /// Retrieves the number of tags with the . /// /// The criteria to use to filter. This value can be null. /// The number of tags filtered. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// 4 public int Count(CountArguments arguments) { ValidateDatabase(); return CommandHelper.Count(Interop.Tag.GetTagCount, arguments); } /// /// Deletes a tag from the database. /// /// http://tizen.org/privilege/content.write /// The tag ID to delete. /// true if the matched record was found and deleted, otherwise false. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// The caller has no required privilege. /// 4 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; } /// /// Inserts a tag into the database with the specified name. /// /// http://tizen.org/privilege/content.write /// The name of tag. /// The instance that contains the record information inserted. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// The caller has no required privilege. /// 4 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); } } /// /// Updates a tag with the specified name. /// /// http://tizen.org/privilege/content.write /// The tag ID to update. /// The new name. /// true if the matched record was found and updated, otherwise false. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// is less than or equal to zero. /// The caller has no required privilege. /// 4 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); } } /// /// Retrieves the tag with the specified ID. /// /// The tag ID to select. /// The instance if the matched record was found in the database, otherwise null. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// 4 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); } } } /// /// Retrieves all the tags. /// /// The containing the results. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// 4 public MediaDataReader Select() { return Select(arguments: null); } /// /// Retrieves the tags with the . /// /// The criteria to use to filter. This value can be null. /// The containing the results. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// 4 public MediaDataReader Select(SelectArguments arguments) { ValidateDatabase(); return CommandHelper.Select(arguments, Interop.Tag.ForeachTagFromDb, Tag.FromHandle); } /// /// Retrieves the number of media info of the tag. /// /// The tag ID. /// The number of the media information. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// 4 public int CountMedia(int tagId) { return CountMedia(tagId, null); } /// /// Retrieves the number of the media information of the tag with the . /// /// The tag ID to query with. /// The criteria to use to filter. This value can be null. /// The number of the media information. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// 4 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); } /// /// Retrieves the media information of the tag. /// /// The tag ID. /// The containing the results. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// 4 public MediaDataReader SelectMedia(int tagId) { return SelectMedia(tagId, null); } /// /// Retrieves the media information of the tag with the . /// /// The tag ID. /// The criteria to use to filter. This value can be null. /// The containing the results. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is less than or equal to zero. /// 4 public MediaDataReader 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 mediaIds, Func 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); } } } /// /// Adds the media to a tag. /// /// The tag ID that the media will be added to. /// The media ID to add to the tag. /// true if the matched record was found and updated, otherwise false. /// The invalid media ID will be ignored. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// is a zero-length string, contains only white space. /// is less than or equal to zero. /// 4 public bool AddMedia(int tagId, string mediaId) { ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId)); return AddMedia(tagId, new string[] { mediaId }); } /// /// Adds the media set to a tag. /// /// The tag ID that the media will be added to. /// The media ID to add to the tag. /// true if the matched record was found and updated, otherwise false. /// The invalid media IDs will be ignored. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// /// has no element.
/// -or-
/// contains null value.
/// -or-
/// contains a zero-length string, contains only white space. ///
/// is less than or equal to zero. /// 4 public bool AddMedia(int tagId, IEnumerable mediaIds) { return UpdateMember(tagId, mediaIds, Interop.Tag.AddMedia); } /// /// Removes the media from a tag. /// /// The tag ID. /// The media ID to remove from the tag. /// true if the matched record was found and updated, otherwise false. /// The invalid media ID will be ignored. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// is a zero-length string, contains only white space. /// is less than or equal to zero. /// 4 public bool RemoveMedia(int tagId, string mediaId) { ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId)); return RemoveMedia(tagId, new string[] { mediaId }); } /// /// Removes the media set from a tag. /// /// The tag ID. /// The collection of media ID to remove from the tag. /// true if the matched record was found and updated, otherwise false. /// The invalid IDs will be ignored. /// The is disconnected. /// The has already been disposed. /// An error occurred while executing the command. /// is null. /// /// has no element.
/// -or-
/// contains null value.
/// -or-
/// contains a zero-length string or white space. ///
/// is less than or equal to zero. /// 4 public bool RemoveMedia(int tagId, IEnumerable mediaIds) { return UpdateMember(tagId, mediaIds, Interop.Tag.RemoveMedia); } } }