[Internal: merge sync-agent]
[platform/core/system/sync-agent.git] / src / framework / data-adapter / changelog.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <glib.h>
19 #include <pthread.h>
20
21 #include "utility/sync_util.h"
22 #include "utility/fw_alloc.h"
23
24 #include "account/manager.h"
25 #include "account/util_internal.h"
26
27 #include "data-adapter/agent_handler_manager.h"
28 #include "data-adapter/agent_handler_manager_internal.h"
29 #include "data-adapter/common.h"
30 #include "data-adapter/luid.h"
31
32 #include "data-adapter/changelog.h"
33 #include "data-adapter/changelog_internal.h"
34
35 #include "data-adapter/interface_changelog.h"
36 #include "data-adapter/agent.h"
37
38 #ifndef EXPORT_API
39 #define EXPORT_API __attribute__ ((visibility("default")))
40 #endif
41
42 #ifndef SYNC_AGENT_LOG
43 #undef LOG_TAG
44 #define LOG_TAG "AF_DACI"
45 #endif
46
47 static pthread_mutex_t change_lock;
48
49 int da_handle_add_item(int service_account_id, int index, char *service_item_id, char *parent_service_item_id, int item_type, char *service_folder_id, int folder_type)
50 {
51         _EXTERN_FUNC_ENTER;
52
53         retvm_if(service_item_id == NULL, 0, "service item id is NULL !!");
54
55         /*
56          * TODO : 1. Need to Null check parameter
57          * 2. converting type : itemType, folderType
58          * 3. get fw id : accountID, folderID
59          * 4. check whether the item is included
60          * 5. generate item luid
61          * 6. add item & change log
62          */
63         if (pthread_mutex_lock(&change_lock)) {
64                 _DEBUG_INFO("pthread_mutex_lock error\n");
65                 return 0;
66         }
67
68         _DEBUG_INFO("Index : %d", index);
69         _DEBUG_INFO("service_accountID : %d", service_account_id);
70         _DEBUG_INFO("service_itemID : %s", service_item_id);
71         _DEBUG_INFO("itemType : %d", item_type);
72         _DEBUG_INFO("service_folderID : %s", service_folder_id);
73         _DEBUG_INFO("folderType : %d", folder_type);
74         _DEBUG_INFO("parent_service_item_id : %s", parent_service_item_id);
75
76         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
77         sync_agent_da_return_e open_ret = SYNC_AGENT_DA_SUCCESS;
78         char *fw_folder_id = NULL;
79         char *fw_item_id = NULL;
80         char **fw_item_id_list = NULL;
81         sync_agent_da_item_s item;
82         sync_agent_da_item_changelog_s item_changelog;
83
84         open_ret = sync_agent_open_agent();
85         _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
86
87         /*open test */
88         sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
89         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
90                 _DEBUG_ERROR("sync_agent_begin_transaction fail");
91         }
92
93         /*
94          * if folderID value is NULL ==> folderID set NULL
95          */
96         if ((service_folder_id != NULL) && !strcmp(service_folder_id, ""))
97                 service_folder_id = NULL;
98
99         /*
100          * get fw account id
101          */
102         int fw_account_id = acc_get_fw_account_id(item_type, service_account_id, index);
103         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
104
105         if (fw_account_id == -1) {
106                 _DEBUG_ERROR("fw account id does not exist");
107                 goto DACI_FINISH;
108         }
109
110         /*
111          * get fw folder id
112          */
113         fw_folder_id = da_get_folder_id_by_service_id_wrapper(fw_account_id, item_type, service_folder_id, folder_type);
114         if (fw_folder_id == NULL) {
115                 _DEBUG_ERROR("fw folder id does not exist");
116                 goto DACI_FINISH;
117         }
118
119         /*
120          * check whether the item is included
121          */
122         fw_item_id = da_get_item_id_internal(fw_account_id, service_item_id, item_type);
123         if (fw_item_id != NULL) {
124                 _DEBUG_ERROR("service_item_id already exists : %s", service_item_id);
125                 goto DACI_FINISH;
126         }
127
128         /*
129          * generate item luid
130          */
131         fw_item_id_list = sync_agent_generate_item_luid(1, 1);
132         if (fw_item_id_list == NULL) {
133                 _DEBUG_ERROR("Failed to Generate Item LUID");
134                 goto DACI_FINISH;
135         }
136
137         fw_item_id = fw_item_id_list[0];
138         if (fw_item_id == NULL) {
139                 _DEBUG_ERROR("Failed to Generate Item LUID");
140                 goto DACI_FINISH;
141         }
142
143         // add item
144         item.account_id = fw_account_id;
145         item.item_id = fw_item_id;
146         item.folder_id = fw_folder_id;
147         item.data_store_id = item_type;
148         item.service_id = service_item_id;
149         item.parent_service_id = parent_service_item_id;
150         item.access_name = "DACI_ChangeLog";
151
152         ret = da_add_item_internal(&item, 1);
153         if (ret != SYNC_AGENT_DA_SUCCESS) {
154                 _DEBUG_ERROR("Failed to Add Item : %d", ret);
155                 goto DACI_FINISH;
156         }
157
158         // add change log (only for normal item not calendar exdate item)
159         if (parent_service_item_id == NULL) {
160                 item_changelog.item_id = fw_item_id;
161                 item_changelog.operation_id = 301;
162                 item_changelog.status = "SYNC_REQUIRED";
163                 item_changelog.access_name = "DACI_ChangeLog";
164
165                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
166                 if (ret != SYNC_AGENT_DA_SUCCESS) {
167                         _DEBUG_ERROR("Failed to Add Item changelog : %d", ret);
168                         goto DACI_FINISH;
169                 }
170         }
171
172  DACI_FINISH:
173         /*open test */
174         daci_res = sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
175         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
176                 _DEBUG_ERROR("sync_agent_end_transaction fail");
177         }
178
179         open_ret = sync_agent_close_agent();
180         _DEBUG_INFO("Done Close Agent DB : %d", open_ret);
181
182         if (fw_folder_id != NULL) {
183                 free(fw_folder_id);
184                 fw_folder_id = NULL;
185         }
186         if (fw_item_id != NULL) {
187                 free(fw_item_id);
188                 fw_item_id = NULL;
189         }
190         if (fw_item_id_list != NULL) {
191                 free(fw_item_id_list);
192                 fw_item_id_list = NULL;
193         }
194         if (pthread_mutex_unlock(&change_lock)) {
195                 _DEBUG_ERROR("pthread_mutex_unlock error\n");
196                 return 0;
197         }
198
199         _EXTERN_FUNC_EXIT;
200
201         return ret;
202 }
203
204 int da_handle_del_item(int service_account_id, int index, char *service_item_id, int item_type)
205 {
206         _EXTERN_FUNC_ENTER;
207
208         retvm_if(service_item_id == NULL, 0, "service item id is NULL !!");
209
210         /*
211          * TODO : 1. Need to Null check parameter
212          * 2. converting type : itemType
213          * 3. get fw id : accountID
214          * 4. check whether the item is included
215          * 5. check whether the change log is included
216          * 6. add/update/delete item change log
217          */
218         _DEBUG_INFO("service_accountID : %d", service_account_id);
219         _DEBUG_INFO("index : %d", index);
220         _DEBUG_INFO("service_itemID : %s", service_item_id);
221         _DEBUG_INFO("itemType : %d", item_type);
222
223         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
224         sync_agent_da_return_e open_ret = SYNC_AGENT_DA_SUCCESS;
225
226         char *fw_item_id = NULL;
227         sync_agent_da_item_changelog_s item_changelog;
228         item_changelog.status = NULL;
229         item_changelog.access_name = NULL;
230
231         open_ret = sync_agent_open_agent();
232         _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
233
234         /*open test */
235         sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
236         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
237                 _DEBUG_ERROR("sync_agent_begin_transaction fail");
238         }
239
240         /*
241          * get fw account id
242          */
243         int fw_account_id = acc_get_fw_account_id(item_type, service_account_id, index);
244         if (fw_account_id == -1) {
245                 _DEBUG_ERROR("fw account id does not exist");
246                 goto DACI_FINISH;
247         }
248
249         /*
250          * check whether the item is included
251          */
252         fw_item_id = da_get_item_id_internal(fw_account_id, service_item_id, item_type);
253         if (fw_item_id != NULL) {
254                 _DEBUG_INFO("Item Already Exists : %s", fw_item_id);
255         } else {
256                 _DEBUG_ERROR("Item is not existed!!");
257                 goto DACI_FINISH;
258         }
259
260         /* check whether the change log is included */
261         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
262                 item_changelog.item_id = fw_item_id;
263                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
264
265                 switch (item_changelog.operation_id) {
266                 case 301:       /* ADD */
267                         {
268                                 _DEBUG_INFO("Item Changelog Already Exists : ADD Operation");
269                                 ret = da_delete_item_by_item_id_internal(fw_item_id);
270                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
271                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
272                                         goto DACI_FINISH;
273                                 }
274
275                                 ret = da_delete_item_changelog_by_item_id_internal(fw_account_id, fw_item_id);
276                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
277                                         _DEBUG_ERROR("Failed to Delete Item Change Log : %d", ret);
278                                         goto DACI_FINISH;
279                                 }
280                         }
281                         break;
282                 case 302:       /* UPDATE */
283                         {
284                                 _DEBUG_INFO("Item Changelog Already Exists : UPDATE Operation");
285                                 item_changelog.operation_id = 303;
286                                 ret = da_set_item_changelog_operation_id_wrapper(fw_account_id, &item_changelog);
287                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
288                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
289                                         goto DACI_FINISH;
290                                 }
291                         }
292                         break;
293                 case 303:       /* DELETE */
294                         {
295                                 _DEBUG_INFO("Item Changelog Already Exists : DELETE Operation");
296                         }
297                         break;
298                 }
299
300         } else {
301                 /*
302                  * add change log
303                  */
304                 item_changelog.item_id = fw_item_id;
305                 item_changelog.operation_id = 303;
306                 item_changelog.status = strdup("SYNC_REQUIRED");
307                 item_changelog.access_name = strdup("DACI_ChangeLog");
308
309                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
310                 if (ret != SYNC_AGENT_DA_SUCCESS) {
311                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
312                         goto DACI_FINISH;
313                 }
314         }
315
316  DACI_FINISH:
317         /*open test */
318         daci_res = sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
319         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
320                 _DEBUG_ERROR("sync_agent_end_transaction fail");
321         }
322         open_ret = sync_agent_close_agent();
323         _DEBUG_INFO("Done Close Agent DB : %d", open_ret);
324
325         if (fw_item_id != NULL) {
326                 free(fw_item_id);
327                 fw_item_id = NULL;
328         }
329         if (item_changelog.status != NULL) {
330                 free(item_changelog.status);
331                 item_changelog.status = NULL;
332         }
333         if (item_changelog.access_name != NULL) {
334                 free(item_changelog.access_name);
335                 item_changelog.access_name = NULL;
336         }
337
338         _EXTERN_FUNC_EXIT;
339
340         return ret;
341 }
342
343 int da_handle_update_item(int service_account_id, int index, char *service_item_id, char *parent_service_item_id, int item_type)
344 {
345         _EXTERN_FUNC_ENTER;
346
347         retvm_if(service_item_id == NULL, 0, "service item id is NULL !!");
348
349         /*
350          * TODO : 1. Need to Null check parameter
351          * 2. converting type : itemType
352          * 3. get fw id : accountID
353          * 4. check whether the item is included
354          * 5. check whether the change log is included
355          * 6. add item change log
356          */
357
358         _DEBUG_INFO("service_accountID : %d", service_account_id);
359         _DEBUG_INFO("service_itemID : %s", service_item_id);
360         _DEBUG_INFO("itemType : %d", item_type);
361         _DEBUG_INFO("parent_service_item_id : %s", parent_service_item_id);
362
363         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
364         sync_agent_da_return_e open_ret = SYNC_AGENT_DA_SUCCESS;
365
366         char *fw_item_id = NULL;
367         sync_agent_da_item_changelog_s item_changelog;
368         item_changelog.status = NULL;
369         item_changelog.access_name = NULL;
370
371         open_ret = sync_agent_open_agent();
372         _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
373
374         /*open test */
375         sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
376         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
377                 _DEBUG_ERROR("sync_agent_begin_transaction fail");
378         }
379
380         /*
381          * get fw account id
382          */
383         int fw_account_id = acc_get_fw_account_id(item_type, service_account_id, index);
384         if (fw_account_id == -1) {
385                 _DEBUG_ERROR("fw account id does not exist");
386                 goto DACI_FINISH;
387         }
388
389         /*
390          * check whether the item is included
391          */
392         fw_item_id = da_get_item_id_internal(fw_account_id, service_item_id, item_type);
393         if (fw_item_id != NULL) {
394                 _DEBUG_INFO("Item Already Exists : %s", fw_item_id);
395         } else {
396                 _DEBUG_ERROR("Item is Not Existed!!");
397                 goto DACI_FINISH;
398         }
399
400         /*
401          * check whether the change log is included
402          */
403         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
404                 item_changelog.item_id = fw_item_id;
405                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
406
407                 if (item_changelog.operation_id == 303) {       /* DELETE */
408                         _DEBUG_ERROR("Already Deleted Item : %s", fw_item_id);
409                         goto DACI_FINISH;
410                 }
411         } else {
412                 /*
413                  * add change log
414                  */
415                 item_changelog.item_id = fw_item_id;
416                 item_changelog.operation_id = 302;
417                 item_changelog.status = strdup("SYNC_REQUIRED");
418                 item_changelog.access_name = strdup("DACI_ChangeLog");
419
420                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
421                 if (ret != SYNC_AGENT_DA_SUCCESS) {
422                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
423                         goto DACI_FINISH;
424                 }
425         }
426
427         // update parent_id (only for Calendar exdate item)
428         if (parent_service_item_id != NULL) {
429                 ret = da_update_item_internal(fw_item_id, parent_service_item_id);
430                 if (ret != SYNC_AGENT_DA_SUCCESS) {
431                         _DEBUG_ERROR("da_update_item_internal failed");
432                         goto DACI_FINISH;
433                 }
434         }
435
436  DACI_FINISH:
437         _DEBUG_INFO("return_part");
438         daci_res = sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
439         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
440                 _DEBUG_ERROR("sync_agent_end_transaction fail");
441         }
442         open_ret = sync_agent_close_agent();
443         _DEBUG_INFO("Done Close Agent DB : %d", open_ret);
444
445         if (fw_item_id != NULL) {
446                 free(fw_item_id);
447                 fw_item_id = NULL;
448         }
449         if (item_changelog.status != NULL) {
450                 free(item_changelog.status);
451                 item_changelog.status = NULL;
452         }
453         if (item_changelog.access_name != NULL) {
454                 free(item_changelog.access_name);
455                 item_changelog.access_name = NULL;
456         }
457
458         _EXTERN_FUNC_EXIT;
459         return ret;
460 }
461
462 int da_changelog_add_item_internal(sync_agent_da_item_s *item)
463 {
464         _EXTERN_FUNC_ENTER;
465
466         retvm_if(item == NULL, 0, "fw item id is NULL !!");
467
468         /*
469          * TODO : 1. Need to Null check parameter
470          * 2. converting type : itemType
471          * 3. check whether the item is included
472          * 4. generate item luid
473          * 5. add item & change log
474          */
475         _DEBUG_INFO("item_id : %s", item->item_id);
476         _DEBUG_INFO("data_store_id : %d", item->data_store_id);
477         _DEBUG_INFO("account_id : %d", item->account_id);
478         _DEBUG_INFO("folder_id : %s", item->folder_id);
479         _DEBUG_INFO("service_id : %s", item->service_id);
480         _DEBUG_INFO("parent_service_id : %s", item->parent_service_id);
481
482         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
483         sync_agent_da_item_changelog_s item_changelog;
484
485         // add item
486         item->access_name = "DACI_ChangeLog";
487         ret = da_add_item_internal(item, 1);
488         if (ret != SYNC_AGENT_DA_SUCCESS) {
489                 _DEBUG_ERROR("add item failed return : %d", ret);
490                 goto DACI_FINISH;
491         }
492
493         // add change log
494         item_changelog.item_id = item->item_id;
495         item_changelog.operation_id = 301;
496         item_changelog.status = "SYNC_REQUIRED";
497         item_changelog.access_name = "DACI_ChangeLog";
498
499         ret = da_add_item_changelog_wrapper(item->account_id, &item_changelog, 1);
500         if (ret != SYNC_AGENT_DA_SUCCESS) {
501                 _DEBUG_ERROR("add item changelog failed return : %d", ret);
502                 goto DACI_FINISH;
503         }
504
505  DACI_FINISH:
506         _EXTERN_FUNC_EXIT;
507         return ret;
508 }
509
510 EXPORT_API sync_agent_da_return_e sync_agent_changelog_add_item_new(sync_agent_da_item_s * sync_agent_item, char **item_id)
511 {
512         _EXTERN_FUNC_ENTER;
513
514         retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
515
516         /*
517          * TODO : 1. Need to Null check parameter
518          * 2. converting type : itemType
519          * 3. check whether the item is included
520          * 4. generate item luid
521          * 5. add item & change log
522          */
523         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
524         sync_agent_da_item_changelog_s item_changelog;
525
526         // 1. add item
527         ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
528         if (ret != SYNC_AGENT_DA_SUCCESS) {
529                 _DEBUG_ERROR("add item failed return : %d", ret);
530                 goto DACI_FINISH;
531         }
532
533         // 2. add change log
534         item_changelog.item_id = *item_id;
535         item_changelog.operation_id = 301;
536         item_changelog.status = "SYNC_REQUIRED";
537         item_changelog.access_name = "DACI_ChangeLog";
538
539         ret = da_add_item_changelog_wrapper(sync_agent_item->account_id, &item_changelog, 1);
540         if (ret != SYNC_AGENT_DA_SUCCESS) {
541                 _DEBUG_ERROR("add item changelog failed return : %d", ret);
542                 goto DACI_FINISH;
543         }
544
545  DACI_FINISH:
546         _EXTERN_FUNC_EXIT;
547         return ret;
548 }
549
550 int da_changelog_delete_item_internal(int fw_account_id, char *fw_item_id, int item_type)
551 {
552         _EXTERN_FUNC_ENTER;
553
554         retvm_if(fw_item_id == NULL, 0, "fw item id is NULL !!");
555
556         /*
557          * TODO : 1. Need to Null check parameter
558          * 2. check whether the item is included
559          * 3. check whether the change log is included
560          * 4. add/update/delete item change log
561          */
562         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
563         _DEBUG_INFO("fw_item_id : %s", fw_item_id);
564         _DEBUG_INFO("itemType : %d", item_type);
565
566         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
567
568         sync_agent_da_item_changelog_s item_changelog;
569         item_changelog.status = NULL;
570         item_changelog.access_name = NULL;
571
572         /*
573          * check whether the item is included
574          */
575         ret = da_is_exist_item_wrapper(fw_item_id);
576         if (ret == 1) {
577                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
578         } else {
579                 _DEBUG_ERROR("Item is not existed!!");
580                 goto DACI_FINISH;
581         }
582
583         /*
584          * check whether the change log is included
585          */
586         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
587                 item_changelog.item_id = fw_item_id;
588                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
589
590                 switch (item_changelog.operation_id) {
591                 case 301:       /* ADD */
592                         {
593                                 _DEBUG_INFO("Item changelog already exists : ADD Operation");
594                                 ret = da_delete_item_by_item_id_internal(fw_item_id);
595                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
596                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
597                                         goto DACI_FINISH;
598                                 }
599
600                                 ret = da_delete_item_changelog_by_item_id_internal(fw_account_id, fw_item_id);
601                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
602                                         _DEBUG_ERROR("Failed to Delete Item Change Log : %d", ret);
603                                         goto DACI_FINISH;
604                                 }
605                         }
606                         break;
607                 case 302:       /* UPDATE */
608                         {
609                                 _DEBUG_INFO("Item Changelog Already Exists : UPDATE Operation");
610                                 item_changelog.operation_id = 303;
611                                 ret = da_set_item_changelog_operation_id_wrapper(fw_account_id, &item_changelog);
612                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
613                                         _DEBUG_ERROR("Failed to Set Item ChangeLog Operation : %d", ret);
614                                         goto DACI_FINISH;
615                                 }
616                         }
617                         break;
618                 case 303:       /* DELETE */
619                         {
620                                 _DEBUG_INFO("Item Changelog Already Exists : DELETE Operation");
621                         }
622                         break;
623                 }
624         } else {
625                 /*
626                  * add change log
627                  */
628                 item_changelog.item_id = fw_item_id;
629                 item_changelog.operation_id = 303;
630                 item_changelog.status = strdup("SYNC_REQUIRED");
631                 item_changelog.access_name = strdup("DACI_ChangeLog");
632
633                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
634                 if (ret != SYNC_AGENT_DA_SUCCESS) {
635                         _DEBUG_ERROR("[da_changelog_delete_item_internal] add item change log failed return : %d", ret);
636                         goto DACI_FINISH;
637                 }
638         }
639
640  DACI_FINISH:
641         if (item_changelog.status != NULL) {
642                 free(item_changelog.status);
643                 item_changelog.status = NULL;
644         }
645         if (item_changelog.access_name != NULL) {
646                 free(item_changelog.access_name);
647                 item_changelog.access_name = NULL;
648         }
649
650         _EXTERN_FUNC_EXIT;
651
652         return ret;
653 }
654
655 EXPORT_API sync_agent_da_return_e sync_agent_changelog_delete_item_new(int fw_account_id, char *fw_item_id)
656 {
657         _EXTERN_FUNC_ENTER;
658
659         retvm_if(fw_item_id == NULL, SYNC_AGENT_DA_ERRORS, "fw item id is NULL !!");
660
661         /*
662          * TODO : 1. Need to Null check parameter
663          * 2. check whether the item is included
664          * 3. check whether the change log is included
665          * 4. add/update/delete item change log
666          */
667         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
668
669         sync_agent_da_item_changelog_s item_changelog;
670         item_changelog.status = NULL;
671         item_changelog.access_name = NULL;
672
673         /*
674          * check whether the item is included
675          */
676         ret = da_is_exist_item_wrapper(fw_item_id);
677         if (ret == 1) {
678                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
679 //              ret = sync_agent_delete_item_by_item_id(fw_item_id);
680 //              if (ret != SYNC_AGENT_DA_SUCCESS) {
681 //                      _DEBUG_ERROR("sync_agent_delete_item() failed !! : %d", ret);
682 //                      goto DACI_FINISH;
683 //              } else {
684 //                      _DEBUG_INFO("sync_agent_delete_item() success !!");
685 //              }
686         } else {
687                 _DEBUG_ERROR("Item is not existed!!");
688                 goto DACI_FINISH;
689         }
690
691         /*
692          * check whether the change log is included
693          */
694         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
695                 item_changelog.item_id = fw_item_id;
696                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
697
698                 switch (item_changelog.operation_id) {
699                 case 301:       /* ADD */
700                         {
701                                 _DEBUG_INFO("Item changelog already exists : ADD Operation");
702                                 ret = da_delete_item_by_item_id_internal(fw_item_id);
703                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
704                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
705                                         goto DACI_FINISH;
706                                 }
707
708                                 ret = da_delete_item_changelog_by_item_id_internal(fw_account_id, fw_item_id);
709                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
710                                         _DEBUG_ERROR("Failed to Delete Item Change Log : %d", ret);
711                                         goto DACI_FINISH;
712                                 }
713                         }
714                         break;
715                 case 302:       /* UPDATE */
716                         {
717                                 _DEBUG_INFO("Item Changelog Already Exists : UPDATE Operation");
718                                 item_changelog.operation_id = 303;
719                                 ret = da_set_item_changelog_operation_id_wrapper(fw_account_id, &item_changelog);
720                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
721                                         _DEBUG_ERROR("Failed to Set Item ChangeLog Operation : %d", ret);
722                                         goto DACI_FINISH;
723                                 }
724                         }
725                         break;
726                 case 303:       /* DELETE */
727                         {
728                                 _DEBUG_INFO("Item Changelog Already Exists : DELETE Operation");
729                         }
730                         break;
731                 }
732         } else {
733                 /*
734                  * add change log
735                  */
736                 item_changelog.item_id = fw_item_id;
737                 item_changelog.operation_id = 303;
738                 item_changelog.status = strdup("SYNC_REQUIRED");
739                 item_changelog.access_name = strdup("DACI_ChangeLog");
740
741                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
742                 if (ret != SYNC_AGENT_DA_SUCCESS) {
743                         _DEBUG_ERROR("[da_changelog_delete_item_internal] add item change log failed return : %d", ret);
744                         goto DACI_FINISH;
745                 }
746         }
747
748  DACI_FINISH:
749         if (item_changelog.status != NULL) {
750                 free(item_changelog.status);
751                 item_changelog.status = NULL;
752         }
753         if (item_changelog.access_name != NULL) {
754                 free(item_changelog.access_name);
755                 item_changelog.access_name = NULL;
756         }
757
758         _EXTERN_FUNC_EXIT;
759
760         return ret;
761 }
762
763 int da_changelog_update_item_internal(int fw_account_id, char *fw_item_id, int item_type)
764 {
765         _EXTERN_FUNC_ENTER;
766
767         retvm_if(fw_item_id == NULL, 0, "fw item id is NULL !!");
768
769         /*
770          * TODO : 1. Need to Null check parameter
771          * 2. converting type : itemType
772          * 3. check whether the item is included
773          * 4. check whether the change log is included
774          * 5. add item change log
775          */
776
777         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
778         _DEBUG_INFO("fw_item_id : %s", fw_item_id);
779         _DEBUG_INFO("itemType : %d", item_type);
780
781         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
782
783         sync_agent_da_item_changelog_s item_changelog;
784         item_changelog.status = NULL;
785         item_changelog.access_name = NULL;
786
787         /*
788          * check whether the item is included
789          */
790         ret = da_is_exist_item_wrapper(fw_item_id);
791         if (ret == 1) {
792                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
793         } else {
794                 _DEBUG_ERROR("Item is not existed!!");
795                 goto DACI_FINISH;
796         }
797
798         /* check whether the change log is included */
799         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
800                 item_changelog.item_id = fw_item_id;
801                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
802
803                 if (item_changelog.operation_id == 303) {       /* DELETE */
804                         _DEBUG_ERROR("already deleted item");
805                         goto DACI_FINISH;
806                 }
807
808         } else {
809                 /*
810                  * add change log
811                  */
812                 item_changelog.item_id = fw_item_id;
813                 item_changelog.operation_id = 302;
814                 item_changelog.status = strdup("SYNC_REQUIRED");
815                 item_changelog.access_name = strdup("DACI_ChangeLog");
816
817                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
818                 if (ret != SYNC_AGENT_DA_SUCCESS) {
819                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
820                         goto DACI_FINISH;
821                 }
822         }
823
824  DACI_FINISH:
825         if (item_changelog.status != NULL) {
826                 free(item_changelog.status);
827                 item_changelog.status = NULL;
828         }
829         if (item_changelog.access_name != NULL) {
830                 free(item_changelog.access_name);
831                 item_changelog.access_name = NULL;
832         }
833
834         _EXTERN_FUNC_EXIT;
835
836         return ret;
837 }
838
839 EXPORT_API sync_agent_da_return_e sync_agent_changelog_update_item_new(int fw_account_id, char *fw_item_id)
840 {
841         _EXTERN_FUNC_ENTER;
842
843         retvm_if(fw_item_id == NULL, SYNC_AGENT_DA_ERRORS, "fw item id is NULL !!");
844
845         /*
846          * TODO : 1. Need to Null check parameter
847          * 2. converting type : itemType
848          * 3. check whether the item is included
849          * 4. check whether the change log is included
850          * 5. add item change log
851          */
852         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
853
854         sync_agent_da_item_changelog_s item_changelog;
855         item_changelog.status = NULL;
856         item_changelog.access_name = NULL;
857
858         /*
859          * check whether the item is included
860          */
861         ret = da_is_exist_item_wrapper(fw_item_id);
862         if (ret == 1) {
863                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
864         } else {
865                 _DEBUG_ERROR("Item is not existed!!");
866                 goto DACI_FINISH;
867         }
868
869         /* check whether the change log is included */
870         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
871                 item_changelog.item_id = fw_item_id;
872                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
873
874                 if (item_changelog.operation_id == 303) {       /* DELETE */
875                         _DEBUG_ERROR("already deleted item");
876                         goto DACI_FINISH;
877                 }
878
879         } else {
880                 /*
881                  * add change log
882                  */
883                 item_changelog.item_id = fw_item_id;
884                 item_changelog.operation_id = 302;
885                 item_changelog.status = strdup("SYNC_REQUIRED");
886                 item_changelog.access_name = strdup("DACI_ChangeLog");
887
888                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
889                 if (ret != SYNC_AGENT_DA_SUCCESS) {
890                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
891                         goto DACI_FINISH;
892                 }
893         }
894
895  DACI_FINISH:
896         if (item_changelog.status != NULL) {
897                 free(item_changelog.status);
898                 item_changelog.status = NULL;
899         }
900         if (item_changelog.access_name != NULL) {
901                 free(item_changelog.access_name);
902                 item_changelog.access_name = NULL;
903         }
904
905         _EXTERN_FUNC_EXIT;
906
907         return ret;
908 }
909
910 int da_handle_del_child_item(char *parent_service_id, char *child_service_id)
911 {
912         _EXTERN_FUNC_ENTER;
913         retvm_if(parent_service_id == NULL, 0, "parent_service_id is NULL!!");
914
915         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
916         // If child_service_id is null, it means that all child item should be deleted.
917         if (child_service_id) {
918                 _DEBUG_INFO("parent id = %s, child id = %s", parent_service_id, child_service_id);
919                 ret = da_delete_child_item_internal(parent_service_id, child_service_id);
920                 goto_if(ret != SYNC_AGENT_DA_SUCCESS, "da_delete_child_item_internal() failed");
921         }
922         else {
923                 sync_agent_da_return_e open_ret = sync_agent_open_agent();
924                 _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
925
926                 sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
927                 if (daci_res != SYNC_AGENT_DA_SUCCESS) { _DEBUG_ERROR("sync_agent_begin_transaction fail"); }
928
929                 ret = da_delete_all_child_item_internal(parent_service_id);
930                 if (ret != SYNC_AGENT_DA_SUCCESS) {     _DEBUG_ERROR("da_delete_all_child_item_internal() failed"); }
931
932                 daci_res = sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
933                 if (daci_res != SYNC_AGENT_DA_SUCCESS) { _DEBUG_ERROR("sync_agent_end_transaction fail"); }
934
935                 open_ret = sync_agent_close_agent();
936                 _DEBUG_INFO("Done Close Agent DB : %d", open_ret);
937         }
938
939 return_part:
940         _EXTERN_FUNC_EXIT;
941         return ret;
942 }