Release 4.0.0-preview1-00172
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / FaceInfoCommand.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace Tizen.Content.MediaContent
20 {
21     /// <summary>
22     /// Provides commands to manage face information in the database.
23     /// </summary>
24     /// <seealso cref="Album"/>
25     public class FaceInfoCommand : MediaCommand
26     {
27         /// <summary>
28         /// Initializes a new instance of the <see cref="FaceInfoCommand"/> class with the specified <see cref="MediaDatabase"/>.
29         /// </summary>
30         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
31         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
32         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
33         public FaceInfoCommand(MediaDatabase database) : base(database)
34         {
35         }
36
37         /// <summary>
38         /// Deletes the face information from the database.
39         /// </summary>
40         /// <privilege>http://tizen.org/privilege/content.write</privilege>
41         /// <param name="faceInfoId">The face information ID to delete.</param>
42         /// <returns>true if the matched record was found and deleted, otherwise false.</returns>
43         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
44         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
45         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
46         /// <exception cref="ArgumentNullException"><paramref name="faceInfoId"/> is null.</exception>
47         /// <exception cref="ArgumentException"><paramref name="faceInfoId"/> is a zero-length string, contains only white space.</exception>
48         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
49         public bool Delete(string faceInfoId)
50         {
51             ValidateDatabase();
52
53             ValidationUtil.ValidateNotNullOrEmpty(faceInfoId, nameof(faceInfoId));
54
55             var reader = Select(new SelectArguments { FilterExpression = $"{FaceInfoColumns.Id}='{faceInfoId}'" });
56
57             if (reader.Read() == false)
58             {
59                 return false;
60             }
61
62             CommandHelper.Delete(Interop.Face.DeleteFromDb, faceInfoId);
63             return true;
64         }
65
66         /// <summary>
67         /// Retrieves the face information.
68         /// </summary>
69         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
70         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
71         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
72         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
73         public MediaDataReader<FaceInfo> Select()
74         {
75             return Select(null);
76         }
77
78         /// <summary>
79         /// Retrieves the face information with the <see cref="SelectArguments"/>.
80         /// </summary>
81         /// <param name="filter">The criteria to use to filter. This value can be null.</param>
82         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
83         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
84         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
85         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
86         public MediaDataReader<FaceInfo> Select(SelectArguments filter)
87         {
88             ValidateDatabase();
89
90             return CommandHelper.Select(filter, Interop.Face.ForeachFromDb, FaceInfo.FromHandle);
91         }
92
93         /// <summary>
94         /// Updates a tag with the specified tag.
95         /// </summary>
96         /// <privilege>http://tizen.org/privilege/content.write</privilege>
97         /// <param name="faceInfoId">The face information ID to update.</param>
98         /// <param name="tag">The tag value for update.</param>
99         /// <returns>true if the matched record was found and updated, otherwise false.</returns>
100         /// <remarks>Only values set in the <see cref="PlaylistUpdateValues"/> are updated.</remarks>
101         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
102         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
103         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
104         /// <exception cref="ArgumentNullException"><paramref name="faceInfoId"/> is null.</exception>
105         /// <exception cref="ArgumentException"><paramref name="faceInfoId"/> is a zero-length string, contains only white space.</exception>
106         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
107         public bool UpdateTag(string faceInfoId, string tag)
108         {
109             ValidateDatabase();
110
111             ValidationUtil.ValidateNotNullOrEmpty(faceInfoId, nameof(faceInfoId));
112
113             if (tag == null)
114             {
115                 throw new ArgumentNullException(nameof(tag));
116             }
117
118             var handle = CommandHelper.SelectScalar(Interop.Face.ForeachFromDb, $"{FaceInfoColumns.Id}='{faceInfoId}'",
119                 Interop.Face.Clone);
120
121             if (handle == IntPtr.Zero)
122             {
123                 return false;
124             }
125
126             try
127             {
128                 Interop.Face.SetTag(handle, tag).ThrowIfError("Failed to update(setting tag)");
129
130                 Interop.Face.Update(handle).ThrowIfError("Failed to update(executing query)");
131                 return true;
132             }
133             finally
134             {
135                 Interop.Face.Destroy(handle);
136             }
137         }
138     }
139 }