[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_activity_photo.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include "contacts.h"
20 #include "ctsvc_internal.h"
21 #include "ctsvc_db_schema.h"
22 #include "ctsvc_db_sqlite.h"
23 #include "ctsvc_db_utils.h"
24 #include "ctsvc_db_init.h"
25 #include "ctsvc_db_access_control.h"
26 #include "ctsvc_db_plugin_activity_photo_helper.h"
27 #include "ctsvc_record.h"
28 #include "ctsvc_notification.h"
29 #include "ctsvc_db_query.h"
30 #include "ctsvc_list.h"
31
32
33 static int __ctsvc_db_activity_photo_insert_record(contacts_record_h record, int *id)
34 {
35         int ret;
36         int addressbook_id;
37         char query[CTS_SQL_MAX_LEN] = {0};
38         ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
39
40         RETV_IF(NULL == activity_photo->photo_url, CONTACTS_ERROR_INVALID_PARAMETER);
41
42         ret = ctsvc_begin_trans();
43         if (CONTACTS_ERROR_NONE != ret) {
44                 /* LCOV_EXCL_START */
45                 ERR("ctsvc_begin_trans() Fail(%d)", ret);
46                 return ret;
47                 /* LCOV_EXCL_STOP */
48         }
49
50         snprintf(query, sizeof(query),
51                         "SELECT addressbook_id from "CTSVC_DB_VIEW_CONTACT" "
52                         "WHERE contact_id = (SELECT contact_id from "CTS_TABLE_ACTIVITIES" WHERE id = %d)", activity_photo->activity_id);
53         ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
54         if (CONTACTS_ERROR_NONE != ret) {
55                 /* LCOV_EXCL_START */
56                 ctsvc_end_trans(false);
57                 if (CONTACTS_ERROR_NO_DATA == ret) {
58                         ERR("No data : activity_id (%d) is not exist", activity_photo->activity_id);
59                         return CONTACTS_ERROR_INVALID_PARAMETER;
60                 } else {
61                         ERR("ctsvc_query_get_first_int_result() Fail(%d)", ret);
62                         return ret;
63                 }
64                 /* LCOV_EXCL_STOP */
65         }
66
67         if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
68                 /* LCOV_EXCL_START */
69                 ERR("No permission in this addresbook_id(%d)", addressbook_id);
70                 ctsvc_end_trans(false);
71                 return CONTACTS_ERROR_PERMISSION_DENIED;
72                 /* LCOV_EXCL_STOP */
73         }
74
75         ret = ctsvc_db_activity_photo_insert(record, activity_photo->activity_id, id);
76         if (CONTACTS_ERROR_NONE != ret) {
77                 /* LCOV_EXCL_START */
78                 ERR("ctsvc_db_activity_photo_insert() Fail(%d)", ret);
79                 ctsvc_end_trans(false);
80                 return ret;
81                 /* LCOV_EXCL_STOP */
82         }
83
84         ret = ctsvc_end_trans(true);
85         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
86
87         return CONTACTS_ERROR_NONE;
88 }
89
90 static int __ctsvc_db_activity_photo_get_record(int id, contacts_record_h *out_record)
91 {
92         int ret;
93         cts_stmt stmt = NULL;
94         char query[CTS_SQL_MAX_LEN] = {0};
95
96         RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
97         *out_record = NULL;
98
99         snprintf(query, sizeof(query),
100                         "SELECT id, activity_id, photo_url, sort_index "
101                         "FROM "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" "
102                         "WHERE id = %d",
103                         id);
104
105         ret = ctsvc_query_prepare(query, &stmt);
106         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
107
108         ret = ctsvc_stmt_step(stmt);
109         if (1 /*CTS_TRUE*/ != ret) {
110                 /* LCOV_EXCL_START */
111                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
112                 ctsvc_stmt_finalize(stmt);
113                 if (CONTACTS_ERROR_NONE == ret)
114                         return CONTACTS_ERROR_NO_DATA;
115                 else
116                         return ret;
117                 /* LCOV_EXCL_STOP */
118         }
119
120         ctsvc_db_activity_photo_get_value_from_stmt(stmt, out_record);
121
122         ctsvc_stmt_finalize(stmt);
123
124         return CONTACTS_ERROR_NONE;
125 }
126
127 static int __ctsvc_db_activity_photo_update_record(contacts_record_h record)
128 {
129         int ret;
130         int addressbook_id;
131         char query[CTS_SQL_MAX_LEN] = {0};
132         ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
133
134         RETVM_IF(NULL == activity_photo->photo_url, CONTACTS_ERROR_INVALID_PARAMETER, "photo_url is empty");
135
136         ret = ctsvc_begin_trans();
137         if (CONTACTS_ERROR_NONE != ret) {
138                 /* LCOV_EXCL_START */
139                 ERR("ctsvc_begin_trans() Fail(%d)", ret);
140                 return ret;
141                 /* LCOV_EXCL_STOP */
142         }
143
144         snprintf(query, sizeof(query),
145                         "SELECT addressbook_id from "CTSVC_DB_VIEW_CONTACT" "
146                         "WHERE contact_id = (SELECT contact_id from "CTS_TABLE_ACTIVITIES" WHERE id = %d)",
147                         activity_photo->activity_id);
148
149         ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
150         if (CONTACTS_ERROR_NONE != ret) {
151                 /* LCOV_EXCL_START */
152                 ERR("No data : activity_id (%d) is not exist", activity_photo->activity_id);
153                 ctsvc_end_trans(false);
154                 return ret;
155                 /* LCOV_EXCL_STOP */
156         }
157
158         if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
159                 /* LCOV_EXCL_START */
160                 ERR("No permission in this addresbook_id(%d)", addressbook_id);
161                 ctsvc_end_trans(false);
162                 return CONTACTS_ERROR_PERMISSION_DENIED;
163                 /* LCOV_EXCL_STOP */
164         }
165
166         ret = ctsvc_db_activity_photo_update(record);
167         if (CONTACTS_ERROR_NONE != ret) {
168                 /* LCOV_EXCL_START */
169                 ERR("Update record Fail(%d)", ret);
170                 ctsvc_end_trans(false);
171                 return ret;
172                 /* LCOV_EXCL_STOP */
173         }
174
175         ret = ctsvc_end_trans(true);
176         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
177
178         return CONTACTS_ERROR_NONE;
179 }
180
181 static int __ctsvc_db_activity_photo_delete_record(int id)
182 {
183         int ret;
184         char query[CTS_SQL_MAX_LEN] = {0};
185         int addressbook_id;
186
187         ret = ctsvc_begin_trans();
188         if (CONTACTS_ERROR_NONE != ret) {
189                 /* LCOV_EXCL_START */
190                 ERR("ctsvc_begin_trans() Fail(%d)", ret);
191                 return ret;
192                 /* LCOV_EXCL_STOP */
193         }
194
195         snprintf(query, sizeof(query),
196                         "SELECT C.addressbook_id FROM "CTSVC_DB_VIEW_CONTACT" C, "CTSVC_DB_VIEW_ACTIVITY" A, "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" P "
197                         "ON C.contact_id = A.contact_id AND A.id = P.activity_id "
198                         "WHERE P.id = %d", id);
199
200         ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
201         if (CONTACTS_ERROR_NONE != ret) {
202                 /* LCOV_EXCL_START */
203                 ERR("No data : id (%d) is not exist", id);
204                 ctsvc_end_trans(false);
205                 return ret;
206                 /* LCOV_EXCL_STOP */
207         }
208
209         if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
210                 /* LCOV_EXCL_START */
211                 ERR("No permission in this addresbook_id(%d)", addressbook_id);
212                 ctsvc_end_trans(false);
213                 return CONTACTS_ERROR_PERMISSION_DENIED;
214                 /* LCOV_EXCL_STOP */
215         }
216
217         ret = ctsvc_db_activity_photo_delete(id);
218         if (CONTACTS_ERROR_NONE != ret) {
219                 /* LCOV_EXCL_START */
220                 ERR("ctsvc_db_activity_photo_delete() Fail(%d)", ret);
221                 ctsvc_end_trans(false);
222                 return ret;
223                 /* LCOV_EXCL_STOP */
224         }
225
226         ret = ctsvc_end_trans(true);
227         RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
228
229         return CONTACTS_ERROR_NONE;
230 }
231
232 static int __ctsvc_db_activity_photo_get_all_records(int offset, int limit, contacts_list_h *out_list)
233 {
234         int len;
235         int ret;
236         contacts_list_h list;
237         ctsvc_activity_photo_s *activity_photo;
238         cts_stmt stmt = NULL;
239         char query[CTS_SQL_MAX_LEN] = {0};
240
241         len = snprintf(query, sizeof(query),
242                         "SELECT P.id, P.activity_id, P.photo_url, P.sort_index "
243                         "FROM "CTSVC_DB_VIEW_CONTACT" C, "CTSVC_DB_VIEW_ACTIVITY" A, "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" P "
244                         "ON C.contact_id = A.contact_id AND A.id = P.activity_id ");
245
246         if (0 != limit) {
247                 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
248                 if (0 < offset)
249                         len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
250         }
251
252         ret = ctsvc_query_prepare(query, &stmt);
253         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
254
255         contacts_list_create(&list);
256         while ((ret = ctsvc_stmt_step(stmt))) {
257                 if (1 /*CTS_TRUE */ != ret) {
258                         /* LCOV_EXCL_START */
259                         ERR("DB : ctsvc_stmt_step fail(%d)", ret);
260                         ctsvc_stmt_finalize(stmt);
261                         contacts_list_destroy(list, true);
262                         return ret;
263                         /* LCOV_EXCL_STOP */
264                 }
265                 ctsvc_db_activity_photo_get_value_from_stmt(stmt, (contacts_record_h*)&activity_photo);
266                 ctsvc_list_prepend(list, (contacts_record_h)activity_photo);
267         }
268         ctsvc_stmt_finalize(stmt);
269         ctsvc_list_reverse(list);
270
271         *out_list = list;
272         return CONTACTS_ERROR_NONE;
273 }
274
275 static int __ctsvc_db_activity_photo_get_records_with_query(contacts_query_h query, int offset,
276                 int limit, contacts_list_h *out_list)
277 {
278         int ret;
279         int i;
280         int field_count;
281         ctsvc_query_s *s_query;
282         cts_stmt stmt;
283         contacts_list_h list;
284         ctsvc_activity_photo_s *activity_photo;
285
286         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
287         s_query = (ctsvc_query_s*)query;
288
289         ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
290         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
291
292         contacts_list_create(&list);
293         while ((ret = ctsvc_stmt_step(stmt))) {
294                 contacts_record_h record;
295                 if (1 /*CTS_TRUE */ != ret) {
296                         /* LCOV_EXCL_START */
297                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
298                         ctsvc_stmt_finalize(stmt);
299                         contacts_list_destroy(list, true);
300                         return ret;
301                         /* LCOV_EXCL_STOP */
302                 }
303
304                 contacts_record_create(_contacts_activity_photo._uri, &record);
305                 activity_photo = (ctsvc_activity_photo_s*)record;
306                 if (0 == s_query->projection_count) {
307                         field_count = s_query->property_count;
308                 } else {
309                         field_count = s_query->projection_count;
310                         ret = ctsvc_record_set_projection_flags(record, s_query->projection,
311                                         s_query->projection_count, s_query->property_count);
312
313                         if (CONTACTS_ERROR_NONE != ret)
314                                 ASSERT_NOT_REACHED("To set projection is Fail\n");
315                 }
316
317                 for (i = 0; i < field_count; i++) {
318                         char *temp;
319                         int property_id;
320                         if (0 == s_query->projection_count)
321                                 property_id = s_query->properties[i].property_id;
322                         else
323                                 property_id = s_query->projection[i];
324
325                         switch (property_id) {
326                         case CTSVC_PROPERTY_ACTIVITY_PHOTO_ID:
327                                 activity_photo->id = ctsvc_stmt_get_int(stmt, i);
328                                 break;
329                         case CTSVC_PROPERTY_ACTIVITY_PHOTO_ACTIVITY_ID:
330                                 activity_photo->activity_id = ctsvc_stmt_get_int(stmt, i);
331                                 break;
332                         case CTSVC_PROPERTY_ACTIVITY_PHOTO_URL:
333                                 temp = ctsvc_stmt_get_text(stmt, i);
334                                 free(activity_photo->photo_url);
335                                 activity_photo->photo_url = SAFE_STRDUP(temp);
336                                 break;
337                         case CTSVC_PROPERTY_ACTIVITY_PHOTO_SORT_INDEX:
338                                 activity_photo->sort_index = ctsvc_stmt_get_int(stmt, i);
339                                 break;
340                         default:
341                                 break;
342                         }
343                 }
344                 ctsvc_list_prepend(list, record);
345         }
346         ctsvc_stmt_finalize(stmt);
347         ctsvc_list_reverse(list);
348
349         *out_list = list;
350         return CONTACTS_ERROR_NONE;
351 }
352
353 ctsvc_db_plugin_info_s ctsvc_db_plugin_activity_photo = {
354         .is_query_only = false,
355         .insert_record = __ctsvc_db_activity_photo_insert_record,
356         .get_record = __ctsvc_db_activity_photo_get_record,
357         .update_record = __ctsvc_db_activity_photo_update_record,
358         .delete_record = __ctsvc_db_activity_photo_delete_record,
359         .get_all_records = __ctsvc_db_activity_photo_get_all_records,
360         .get_records_with_query = __ctsvc_db_activity_photo_get_records_with_query,
361         .insert_records = NULL,
362         .update_records = NULL,
363         .delete_records = NULL,
364         .get_count = NULL,
365         .get_count_with_query = NULL,
366         .replace_record = NULL,
367         .replace_records = NULL,
368 };
369