3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <glib/gprintf.h>
22 #include "utility/sync_util.h"
23 #include "data-adapter/agent_handler_manager.h"
24 #include "data-adapter/agent_handler_manager_internal.h"
25 #include "data-adapter/agent.h"
27 #include "data-adapter/interface_item.h"
28 #include "data-adapter/changelog.h"
31 #define EXPORT_API __attribute__ ((visibility("default")))
34 #ifndef SYNC_AGENT_LOG
36 #define LOG_TAG "AF_DACI"
39 EXPORT_API sync_agent_da_return_e sync_agent_create_item(sync_agent_da_item_s ** sync_agent_item)
43 *sync_agent_item = (sync_agent_da_item_s *) calloc(1, sizeof(sync_agent_da_item_s));
44 if (*sync_agent_item == NULL) {
45 _DEBUG_ERROR("calloc failed !!");
46 return SYNC_AGENT_DA_ERRORS;
51 return SYNC_AGENT_DA_SUCCESS;
54 EXPORT_API sync_agent_da_return_e sync_agent_add_item(sync_agent_da_item_s * sync_agent_item, char **item_id, int use_changelog)
58 retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
60 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
62 // item_id = (char **) calloc(count, sizeof(char *));
63 // if (item_id == NULL) {
64 // _DEBUG_ERROR("calloc failed !!");
67 // for (i; i < count; i++) {
68 // if (use_changelog == 1) {
69 // ret = sync_agent_changelog_add_item_new(sync_agent_item, item_id);
71 // ret = sync_agent_add_item_new(sync_agent_item, count, item_id);
74 // if (ret != SYNC_AGENT_DA_SUCCESS) {
75 // _DEBUG_ERROR("add item failed !!");
80 if (use_changelog == 1) {
81 ret = sync_agent_changelog_add_item_new(sync_agent_item, item_id);
82 if (ret != SYNC_AGENT_DA_SUCCESS) {
83 _DEBUG_ERROR("da_changelog_add_item_internal() failed !!");
86 ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
87 if (ret != SYNC_AGENT_DA_SUCCESS) {
88 _DEBUG_ERROR("da_add_item_internal() failed !!");
97 EXPORT_API sync_agent_da_return_e sync_agent_add_bulk_item(sync_agent_da_item_s * sync_agent_item, char **item_id, int use_changelog)
101 retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
103 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
105 if (use_changelog == 1) {
106 ret = sync_agent_changelog_add_item_new(sync_agent_item, item_id);
107 if (ret != SYNC_AGENT_DA_SUCCESS) {
108 _DEBUG_ERROR("da_changelog_add_item_internal() failed !!");
111 ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
112 if (ret != SYNC_AGENT_DA_SUCCESS) {
113 _DEBUG_ERROR("da_add_item_internal() failed !!");
121 EXPORT_API sync_agent_da_return_e sync_agent_update_item(char *item_id, int use_changelog)
125 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
127 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
128 sync_agent_da_item_s *get_item = NULL;
130 if (use_changelog == 1) {
131 ret = sync_agent_create_item(&get_item);
132 if (ret != SYNC_AGENT_DA_SUCCESS) {
133 _DEBUG_ERROR("sync_agent_create_item() failed !!");
137 ret = sync_agent_get_item(item_id, &get_item);
138 if (ret != SYNC_AGENT_DA_SUCCESS) {
139 _DEBUG_ERROR("sync_agent_get_item() failed !!");
140 int ret_temp = sync_agent_free_item(get_item);
141 if (ret_temp != SYNC_AGENT_DA_SUCCESS) {
142 _DEBUG_ERROR("sync_agent_free_item() failed !!");
147 ret = sync_agent_changelog_update_item_new(get_item->account_id, item_id);
148 if (ret != SYNC_AGENT_DA_SUCCESS) {
149 _DEBUG_ERROR("sync_agent_changelog_update_item_new() failed !!");
152 ret = sync_agent_free_item(get_item);
153 if (ret != SYNC_AGENT_DA_SUCCESS) {
154 _DEBUG_ERROR("sync_agent_free_item() failed !!");
157 ret = SYNC_AGENT_DA_SUCCESS;
165 EXPORT_API sync_agent_da_return_e sync_agent_delete_item(char *item_id, int use_changelog)
169 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
171 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
172 sync_agent_da_item_s *get_item = NULL;
174 if (use_changelog == 1) {
175 ret = sync_agent_create_item(&get_item);
176 if (ret != SYNC_AGENT_DA_SUCCESS) {
177 _DEBUG_ERROR("sync_agent_create_item() failed, ret[%d]", ret);
181 ret = sync_agent_get_item_account_id(item_id, &get_item);
182 if (ret != SYNC_AGENT_DA_SUCCESS) {
183 _DEBUG_ERROR("sync_agent_get_item_account_id() failed, ret[%d]", ret);
184 int ret_temp = sync_agent_free_item(get_item);
185 if (ret_temp != SYNC_AGENT_DA_SUCCESS) {
186 _DEBUG_ERROR("sync_agent_get_item_account_id() failed, ret[%d]", ret);
191 ret = sync_agent_changelog_delete_item_new(get_item->account_id, item_id);
192 if (ret != SYNC_AGENT_DA_SUCCESS) {
193 _DEBUG_ERROR("da_changelog_delete_item_internal() failed, ret[%d]", ret);
196 ret = sync_agent_free_item(get_item);
197 if (ret != SYNC_AGENT_DA_SUCCESS) {
198 _DEBUG_ERROR("sync_agent_free_item() failed, ret[%d]", ret);
201 ret = da_delete_item_by_item_id_internal(item_id);
202 if (ret != SYNC_AGENT_DA_SUCCESS) {
203 _DEBUG_ERROR("da_delete_item_by_item_id_internal() failed, ret[%d]", ret);
212 EXPORT_API sync_agent_da_return_e sync_agent_query_delete_item(sync_agent_da_delete_item_query_s * query)
216 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_delete_item_query_s is NULL !!");
218 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
220 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
221 if (daci_handler == NULL) {
222 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
223 return SYNC_AGENT_DA_ERR_DB_HANDLER_MGR;
226 switch (query->option) {
227 case SYNC_AGENT_DA_DELETE_ITEM_OPTION_ITEM_TYPE_ID:
228 ret = da_delete_item_by_item_type_id_new(daci_handler, query->account_id, query->item_type_id);
229 if (ret != SYNC_AGENT_DA_SUCCESS) {
230 _DEBUG_ERROR("da_delete_item_by_item_type_id() failed !!");
242 EXPORT_API sync_agent_da_return_e sync_agent_get_item(char *item_id, sync_agent_da_item_s ** sync_agent_item)
246 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
248 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
250 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
251 if (daci_handler == NULL) {
252 _DEBUG_ERROR("[%s] no handler for key");
256 ret = da_get_item_new(daci_handler, item_id, sync_agent_item);
257 if (ret != SYNC_AGENT_DA_SUCCESS) {
258 _DEBUG_ERROR("da_get_item() faied !!");
266 EXPORT_API sync_agent_da_return_e sync_agent_get_item_data_store_id(char *item_id, sync_agent_da_item_s ** sync_agent_item)
270 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
272 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
274 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
275 if (daci_handler == NULL) {
276 _DEBUG_ERROR("[%s] no handler for key");
280 ret = da_get_item_data_store_id_new(daci_handler, item_id, sync_agent_item);
281 if (ret != SYNC_AGENT_DA_SUCCESS) {
282 _DEBUG_ERROR("da_get_item_data_store_id_new() faied, ret[%d]", ret);
289 EXPORT_API sync_agent_da_return_e sync_agent_get_item_account_id(char *item_id, sync_agent_da_item_s ** sync_agent_item)
293 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
295 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
297 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
298 if (daci_handler == NULL) {
299 _DEBUG_ERROR("[%s] no handler for key");
303 ret = da_get_item_account_id_new(daci_handler, item_id, sync_agent_item);
304 if (ret != SYNC_AGENT_DA_SUCCESS) {
305 _DEBUG_ERROR("da_get_item_account_id_new() faied, ret[%d]", ret);
312 EXPORT_API sync_agent_da_return_e sync_agent_get_item_list(int account_id, sync_agent_da_get_item_list_query_s * query, GList ** list)
316 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_list_query_s is NULL !!");
318 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
320 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
321 if (daci_handler == NULL) {
322 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
326 switch (query->option) {
327 case SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID:
328 ret = da_get_item_by_account_id_new(daci_handler, account_id, query->item_type_id, list);
329 if (ret != SYNC_AGENT_DA_SUCCESS) {
330 _DEBUG_ERROR("da_get_item_by_account_id() failed !!");
333 case SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING:
334 retvm_if(query->item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
336 ret = da_get_item_mapping_service_id_new(daci_handler, account_id, query->item_id, list);
337 if (ret != SYNC_AGENT_DA_SUCCESS) {
338 _DEBUG_ERROR("da_get_item_mapping_service_id() failed !!");
350 EXPORT_API sync_agent_da_return_e sync_agent_get_item_id(sync_agent_da_get_item_id_query_s * query, char **item_id)
354 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_id_query_s is NULL !!");
355 retvm_if(query->service_id == NULL, SYNC_AGENT_DA_ERRORS, "service id is NULL !!");
357 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
359 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
360 if (daci_handler == NULL) {
361 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
365 ret = da_get_item_item_id_new(daci_handler, query->account_id, query->service_id, query->item_type_id, item_id);
366 if (ret != SYNC_AGENT_DA_SUCCESS) {
367 _DEBUG_ERROR("da_get_item_item_id_new() failed !!");
375 EXPORT_API sync_agent_da_return_e sync_agent_get_item_id_list(sync_agent_da_get_item_id_list_query_s * query, GList ** list)
379 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_id_list_query_s is NULL !!");
381 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
383 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
384 if (daci_handler == NULL) {
385 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
389 switch (query->option) {
390 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_COLUMN_NAME:
391 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
393 ret = da_get_item_id_list_new(daci_handler, query->folder_id, query->column_name, list);
394 if (ret != SYNC_AGENT_DA_SUCCESS) {
395 _DEBUG_ERROR("da_get_item_id_list_new() failed !!");
398 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID:
399 ret = da_get_item_id_list_by_operation_id_new(daci_handler, query->account_id, query->item_type_id, query->operation_id, list);
400 if (ret != SYNC_AGENT_DA_SUCCESS) {
401 _DEBUG_ERROR("da_get_item_id_list_by_operation_id_new() failed !!");
404 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_FOLDER_ID:
405 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
407 ret = da_get_item_id_list_by_operation_id_and_folder_id_new(daci_handler, query->account_id, query->folder_id, query->item_type_id, query->operation_id, list);
408 if (ret != SYNC_AGENT_DA_SUCCESS) {
409 _DEBUG_ERROR("da_get_item_id_list_by_operation_id_and_folder_id_new() failed !!");
412 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_ITEM_TYPE_ID:
413 ret = da_get_item_info_by_item_type_id_and_operation_id_new(daci_handler, query->account_id, query->item_type_id, query->operation_id, list);
414 if (ret != SYNC_AGENT_DA_SUCCESS) {
415 _DEBUG_ERROR("da_get_item_info_by_item_type_id_and_operation_id_new() failed !!");
418 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_ITEM_TYPE_ID:
419 ret = da_get_item_id_list_by_datastore_id_new(daci_handler, query->item_type_id, list);
420 if (ret != SYNC_AGENT_DA_SUCCESS) {
421 _DEBUG_ERROR("da_get_item_id_list_by_datastore_id_new() failed !!");
424 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_ITEM_TYPE_ID_N_ACCOUNT_ID:
425 ret = da_get_item_id_list_by_datastore_id_with_account_id_new(daci_handler, query->account_id, query->item_type_id, list);
426 if (ret != SYNC_AGENT_DA_SUCCESS) {
427 _DEBUG_ERROR("da_get_item_id_list_by_datastore_id_with_account_id_new() failed !!");
430 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_FOLDER_ID_N_ACCOUNT_ID:
431 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
433 ret = da_get_item_id_list_by_folder_id_and_account_id(daci_handler, query->account_id, query->folder_id, list);
434 if (ret != SYNC_AGENT_DA_SUCCESS) {
435 _DEBUG_ERROR("da_get_item_id_list_by_folder_id_and_account_id() failed !!");
447 EXPORT_API sync_agent_da_return_e sync_agent_free_item(sync_agent_da_item_s * sync_agent_item)
451 if (sync_agent_item != NULL) {
452 if (sync_agent_item->item_id != NULL)
453 free(sync_agent_item->item_id);
455 if (sync_agent_item->folder_id != NULL)
456 free(sync_agent_item->folder_id);
458 if (sync_agent_item->service_id != NULL)
459 free(sync_agent_item->service_id);
461 if (sync_agent_item->access_name != NULL)
462 free(sync_agent_item->access_name);
464 free(sync_agent_item);
466 _DEBUG_ERROR("sync_agent_item is NULL !!");
467 return SYNC_AGENT_DA_ERRORS;
472 return SYNC_AGENT_DA_SUCCESS;
475 EXPORT_API sync_agent_da_return_e sync_agent_free_item_list(GList * list)
479 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
483 sync_agent_da_item_s *iter_data;
485 for (iter = list; iter != NULL;) {
487 iter_data = (sync_agent_da_item_s *) (iter->data);
489 iter = g_list_next(iter);
490 list = g_list_remove(list, iter_data);
492 ret = sync_agent_free_item(iter_data);
493 if (ret != SYNC_AGENT_DA_SUCCESS) {
494 _DEBUG_ERROR("sync_agent_free_item() failed !!");
501 _DEBUG_ERROR("list is NULL !!");
502 return SYNC_AGENT_DA_ERRORS;
510 EXPORT_API sync_agent_da_return_e sync_agent_free_item_id(char *item_id)
514 if (item_id != NULL) {
517 _DEBUG_ERROR("item id is NULL !!");
518 return SYNC_AGENT_DA_ERRORS;;
523 return SYNC_AGENT_DA_SUCCESS;
526 EXPORT_API sync_agent_da_return_e sync_agent_free_item_id_list(GList * list)
530 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
536 for (iter = list; iter != NULL;) {
538 iter_data = (char *)(iter->data);
540 iter = g_list_next(iter);
541 list = g_list_remove(list, iter_data);
543 ret = sync_agent_free_item_id(iter_data);
544 if (ret != SYNC_AGENT_DA_SUCCESS) {
545 _DEBUG_ERROR("sync_agent_free_item_id() failed !!");
552 _DEBUG_INFO("list is NULL !!");
560 EXPORT_API sync_agent_da_return_e sync_agent_get_item_count(int data_store_id, int *item_count)
564 retvm_if(item_count == NULL, SYNC_AGENT_DA_ERRORS, "*item_count is NULL !!");
566 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
568 GList *account_info_list = NULL;
570 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
571 if (daci_handler == NULL) {
572 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
573 ret = SYNC_AGENT_DA_ERRORS;
577 sync_agent_fw_account_query_s account_query;
578 account_query.query = ACCOUNT_QUERY_BY_SERVICE_TYPE;
579 account_query.service_type = data_store_id;
580 sync_agent_acc_error_e acc_err = sync_agent_query_fw_account(&account_query, &account_info_list);
581 if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
582 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
583 ret = SYNC_AGENT_DA_ERRORS;
587 GList *account_iter = NULL;
588 for (account_iter = account_info_list; account_iter != NULL; account_iter = g_list_next(account_iter)) {
589 sync_agent_fw_account_s *fw_account = (sync_agent_fw_account_s *)account_iter->data;
590 int fw_account_id = fw_account->account_id;
593 ret = da_get_item_count_by_datastore_id(daci_handler, data_store_id, fw_account_id, &temp_count);
594 if (ret != SYNC_AGENT_DA_SUCCESS) {
595 _DEBUG_ERROR("da_get_item_count_by_datastore_id() failed !!");
597 total_count += temp_count;
601 *item_count = total_count;
602 _DEBUG_INFO("item_count = %d", *item_count);
604 if(account_info_list != NULL) {
605 sync_agent_free_fw_account_list(account_info_list);
606 account_info_list = NULL;