Tizen 2.1 base
[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, 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
75         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
76         sync_agent_da_return_e open_ret = SYNC_AGENT_DA_SUCCESS;
77         char *fw_folder_id = NULL;
78         char *fw_item_id = NULL;
79         char **fw_item_id_list = NULL;
80         sync_agent_da_item_s item;
81         sync_agent_da_item_changelog_s item_changelog;
82
83         open_ret = sync_agent_open_agent();
84         _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
85
86         /*open test */
87         sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
88         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
89                 _DEBUG_ERROR("sync_agent_begin_transaction fail");
90         }
91
92         /*
93          * if folderID value is NULL ==> folderID set NULL
94          */
95         if ((service_folder_id != NULL) && !strcmp(service_folder_id, ""))
96                 service_folder_id = NULL;
97
98         /*
99          * get fw account id
100          */
101         int fw_account_id = acc_get_fw_account_id(item_type, service_account_id, index);
102         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
103
104         if (fw_account_id == -1) {
105                 _DEBUG_ERROR("fw account id does not exist");
106                 goto DACI_FINISH;
107         }
108
109         /*
110          * get fw folder id
111          */
112         fw_folder_id = da_get_folder_id_by_service_id_wrapper(fw_account_id, item_type, service_folder_id, folder_type);
113         if (fw_folder_id == NULL) {
114                 _DEBUG_ERROR("fw folder id does not exist");
115                 goto DACI_FINISH;
116         }
117
118         /*
119          * check whether the item is included
120          */
121         fw_item_id = da_get_item_id_internal(fw_account_id, service_item_id, item_type);
122         if (fw_item_id != NULL) {
123                 _DEBUG_ERROR("service_item_id already exists : %s", service_item_id);
124                 goto DACI_FINISH;
125         }
126
127         /*
128          * generate item luid
129          */
130         fw_item_id_list = sync_agent_generate_item_luid(1, 1);
131         if (fw_item_id_list == NULL) {
132                 _DEBUG_ERROR("Failed to Generate Item LUID");
133                 goto DACI_FINISH;
134         }
135
136         fw_item_id = fw_item_id_list[0];
137         if (fw_item_id == NULL) {
138                 _DEBUG_ERROR("Failed to Generate Item LUID");
139                 goto DACI_FINISH;
140         }
141
142         /*
143          * add item
144          */
145         item.account_id = fw_account_id;
146         item.item_id = fw_item_id;
147         item.folder_id = fw_folder_id;
148         item.data_store_id = item_type;
149         item.service_id = 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         /*
159          * add change log
160          */
161         item_changelog.item_id = fw_item_id;
162         item_changelog.operation_id = 301;
163         item_changelog.status = "SYNC_REQUIRED";
164         item_changelog.access_name = "DACI_ChangeLog";
165
166         ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
167         if (ret != SYNC_AGENT_DA_SUCCESS) {
168                 _DEBUG_ERROR("Failed to Add Item changelog : %d", ret);
169                 goto DACI_FINISH;
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, 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
362         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
363         sync_agent_da_return_e open_ret = SYNC_AGENT_DA_SUCCESS;
364
365         char *fw_item_id = NULL;
366         sync_agent_da_item_changelog_s item_changelog;
367         item_changelog.status = NULL;
368         item_changelog.access_name = NULL;
369
370         open_ret = sync_agent_open_agent();
371         _DEBUG_INFO("Done Open Agent DB : %d", open_ret);
372
373         /*open test */
374         sync_agent_da_return_e daci_res = sync_agent_begin_transaction();
375         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
376                 _DEBUG_ERROR("sync_agent_begin_transaction fail");
377         }
378
379         /*
380          * get fw account id
381          */
382         int fw_account_id = acc_get_fw_account_id(item_type, service_account_id, index);
383         if (fw_account_id == -1) {
384                 _DEBUG_ERROR("fw account id does not exist");
385                 goto DACI_FINISH;
386         }
387
388         /*
389          * check whether the item is included
390          */
391         fw_item_id = da_get_item_id_internal(fw_account_id, service_item_id, item_type);
392         if (fw_item_id != NULL) {
393                 _DEBUG_INFO("Item Already Exists : %s", fw_item_id);
394         } else {
395                 _DEBUG_ERROR("Item is Not Existed!!");
396                 goto DACI_FINISH;
397         }
398
399         /*
400          * check whether the change log is included
401          */
402         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
403                 item_changelog.item_id = fw_item_id;
404                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
405
406                 if (item_changelog.operation_id == 303) {       /* DELETE */
407                         _DEBUG_ERROR("Already Deleted Item : %s", fw_item_id);
408                         goto DACI_FINISH;
409                 }
410         } else {
411                 /*
412                  * add change log
413                  */
414                 item_changelog.item_id = fw_item_id;
415                 item_changelog.operation_id = 302;
416                 item_changelog.status = strdup("SYNC_REQUIRED");
417                 item_changelog.access_name = strdup("DACI_ChangeLog");
418
419                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
420                 if (ret != SYNC_AGENT_DA_SUCCESS) {
421                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
422                         goto DACI_FINISH;
423                 }
424         }
425
426  DACI_FINISH:
427         /*open test */
428         daci_res = sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
429         if (daci_res != SYNC_AGENT_DA_SUCCESS) {
430                 _DEBUG_ERROR("sync_agent_end_transaction fail");
431         }
432         open_ret = sync_agent_close_agent();
433         _DEBUG_INFO("Done Close Agent DB : %d", open_ret);
434
435         if (fw_item_id != NULL) {
436                 free(fw_item_id);
437                 fw_item_id = NULL;
438         }
439         if (item_changelog.status != NULL) {
440                 free(item_changelog.status);
441                 item_changelog.status = NULL;
442         }
443         if (item_changelog.access_name != NULL) {
444                 free(item_changelog.access_name);
445                 item_changelog.access_name = NULL;
446         }
447
448         _EXTERN_FUNC_EXIT;
449
450         return ret;
451 }
452
453 int da_changelog_add_item_internal(int fw_account_id, char *fw_item_id, char *service_item_id, int item_type, char *fw_folder_id)
454 {
455         _EXTERN_FUNC_ENTER;
456
457         retvm_if(fw_item_id == NULL, 0, "fw item id is NULL !!");
458         retvm_if(service_item_id == NULL, 0, "service item id is NULL !!");
459         retvm_if(fw_folder_id == NULL, 0, "fw folder id is NULL !!");
460
461         /*
462          * TODO : 1. Need to Null check parameter
463          * 2. converting type : itemType
464          * 3. check whether the item is included
465          * 4. generate item luid
466          * 5. add item & change log
467          */
468         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
469         _DEBUG_INFO("fw_item_id : %s", fw_item_id);
470         _DEBUG_INFO("service_item_id : %s", service_item_id);
471         _DEBUG_INFO("itemType : %d", item_type);
472         _DEBUG_INFO("fw_folder_id : %s", fw_folder_id);
473
474         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
475
476         sync_agent_da_item_s item;
477         sync_agent_da_item_changelog_s item_changelog;
478
479         /*
480          * check whether the item is included
481          */
482
483         /*
484            char *item_id = sync_agent_get_item_id(fw_account_id, service_item_id, itemType);
485            if (item_id != NULL) {
486            _DEBUG_ERROR("Item already exists : %s", item_id);
487            goto DACI_FINISH;
488            }
489          */
490
491         /*
492          * add item
493          */
494         item.account_id = fw_account_id;
495         item.item_id = fw_item_id;
496         item.folder_id = fw_folder_id;
497         item.data_store_id = item_type;
498         item.service_id = service_item_id;
499         item.access_name = "DACI_ChangeLog";
500
501         ret = da_add_item_internal(&item, 1);
502         if (ret != SYNC_AGENT_DA_SUCCESS) {
503                 _DEBUG_ERROR("add item failed return : %d", ret);
504                 goto DACI_FINISH;
505         }
506
507         /*
508          * add change log
509          */
510         item_changelog.item_id = fw_item_id;
511         item_changelog.operation_id = 301;
512         item_changelog.status = "SYNC_REQUIRED";
513         item_changelog.access_name = "DACI_ChangeLog";
514
515         ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
516         if (ret != SYNC_AGENT_DA_SUCCESS) {
517                 _DEBUG_ERROR("add item changelog failed return : %d", ret);
518                 goto DACI_FINISH;
519         }
520
521  DACI_FINISH:
522         /*
523            if (item_id != NULL) {
524            free(item_id);
525            }
526          */
527
528         _EXTERN_FUNC_EXIT;
529
530         return ret;
531 }
532
533 EXPORT_API sync_agent_da_return_e sync_agent_changelog_add_item_new(sync_agent_da_item_s * sync_agent_item, char **item_id)
534 {
535         _EXTERN_FUNC_ENTER;
536
537         retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
538
539         /*
540          * TODO : 1. Need to Null check parameter
541          * 2. converting type : itemType
542          * 3. check whether the item is included
543          * 4. generate item luid
544          * 5. add item & change log
545          */
546         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
547         sync_agent_da_item_changelog_s item_changelog;
548
549         /*
550          * check whether the item is included
551          */
552
553         /*
554            char *item_id = sync_agent_get_item_id(fw_account_id, service_item_id, itemType);
555            if (item_id != NULL) {
556            _DEBUG_ERROR("Item already exists : %s", item_id);
557            goto DACI_FINISH;
558            }
559          */
560
561         /*
562          * add item
563          */
564         ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
565         if (ret != SYNC_AGENT_DA_SUCCESS) {
566                 _DEBUG_ERROR("add item failed return : %d", ret);
567                 goto DACI_FINISH;
568         }
569
570         /*
571          * add change log
572          */
573         item_changelog.item_id = *item_id;
574         item_changelog.operation_id = 301;
575         item_changelog.status = "SYNC_REQUIRED";
576         item_changelog.access_name = "DACI_ChangeLog";
577
578         ret = da_add_item_changelog_wrapper(sync_agent_item->account_id, &item_changelog, 1);
579         if (ret != SYNC_AGENT_DA_SUCCESS) {
580                 _DEBUG_ERROR("add item changelog failed return : %d", ret);
581                 goto DACI_FINISH;
582         }
583
584  DACI_FINISH:
585         /*
586            if (item_id != NULL) {
587            free(item_id);
588            }
589          */
590
591         _EXTERN_FUNC_EXIT;
592
593         return ret;
594 }
595
596 int da_changelog_delete_item_internal(int fw_account_id, char *fw_item_id, int item_type)
597 {
598         _EXTERN_FUNC_ENTER;
599
600         retvm_if(fw_item_id == NULL, 0, "fw item id is NULL !!");
601
602         /*
603          * TODO : 1. Need to Null check parameter
604          * 2. check whether the item is included
605          * 3. check whether the change log is included
606          * 4. add/update/delete item change log
607          */
608         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
609         _DEBUG_INFO("fw_item_id : %s", fw_item_id);
610         _DEBUG_INFO("itemType : %d", item_type);
611
612         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
613
614         sync_agent_da_item_changelog_s item_changelog;
615         item_changelog.status = NULL;
616         item_changelog.access_name = NULL;
617
618         /*
619          * check whether the item is included
620          */
621         ret = da_is_exist_item_wrapper(fw_item_id);
622         if (ret == 1) {
623                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
624         } else {
625                 _DEBUG_ERROR("Item is not existed!!");
626                 goto DACI_FINISH;
627         }
628
629         /*
630          * check whether the change log is included
631          */
632         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
633                 item_changelog.item_id = fw_item_id;
634                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
635
636                 switch (item_changelog.operation_id) {
637                 case 301:       /* ADD */
638                         {
639                                 _DEBUG_INFO("Item changelog already exists : ADD Operation");
640                                 ret = da_delete_item_by_item_id_internal(fw_item_id);
641                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
642                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
643                                         goto DACI_FINISH;
644                                 }
645
646                                 ret = da_delete_item_changelog_by_item_id_internal(fw_account_id, fw_item_id);
647                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
648                                         _DEBUG_ERROR("Failed to Delete Item Change Log : %d", ret);
649                                         goto DACI_FINISH;
650                                 }
651                         }
652                         break;
653                 case 302:       /* UPDATE */
654                         {
655                                 _DEBUG_INFO("Item Changelog Already Exists : UPDATE Operation");
656                                 item_changelog.operation_id = 303;
657                                 ret = da_set_item_changelog_operation_id_wrapper(fw_account_id, &item_changelog);
658                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
659                                         _DEBUG_ERROR("Failed to Set Item ChangeLog Operation : %d", ret);
660                                         goto DACI_FINISH;
661                                 }
662                         }
663                         break;
664                 case 303:       /* DELETE */
665                         {
666                                 _DEBUG_INFO("Item Changelog Already Exists : DELETE Operation");
667                         }
668                         break;
669                 }
670         } else {
671                 /*
672                  * add change log
673                  */
674                 item_changelog.item_id = fw_item_id;
675                 item_changelog.operation_id = 303;
676                 item_changelog.status = strdup("SYNC_REQUIRED");
677                 item_changelog.access_name = strdup("DACI_ChangeLog");
678
679                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
680                 if (ret != SYNC_AGENT_DA_SUCCESS) {
681                         _DEBUG_ERROR("[da_changelog_delete_item_internal] add item change log failed return : %d", ret);
682                         goto DACI_FINISH;
683                 }
684         }
685
686  DACI_FINISH:
687         if (item_changelog.status != NULL) {
688                 free(item_changelog.status);
689                 item_changelog.status = NULL;
690         }
691         if (item_changelog.access_name != NULL) {
692                 free(item_changelog.access_name);
693                 item_changelog.access_name = NULL;
694         }
695
696         _EXTERN_FUNC_EXIT;
697
698         return ret;
699 }
700
701 EXPORT_API sync_agent_da_return_e sync_agent_changelog_delete_item_new(int fw_account_id, char *fw_item_id)
702 {
703         _EXTERN_FUNC_ENTER;
704
705         retvm_if(fw_item_id == NULL, SYNC_AGENT_DA_ERRORS, "fw item id is NULL !!");
706
707         /*
708          * TODO : 1. Need to Null check parameter
709          * 2. check whether the item is included
710          * 3. check whether the change log is included
711          * 4. add/update/delete item change log
712          */
713         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
714
715         sync_agent_da_item_changelog_s item_changelog;
716         item_changelog.status = NULL;
717         item_changelog.access_name = NULL;
718
719         /*
720          * check whether the item is included
721          */
722         ret = da_is_exist_item_wrapper(fw_item_id);
723         if (ret == 1) {
724                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
725 //              ret = sync_agent_delete_item_by_item_id(fw_item_id);
726 //              if (ret != SYNC_AGENT_DA_SUCCESS) {
727 //                      _DEBUG_ERROR("sync_agent_delete_item() failed !! : %d", ret);
728 //                      goto DACI_FINISH;
729 //              } else {
730 //                      _DEBUG_INFO("sync_agent_delete_item() success !!");
731 //              }
732         } else {
733                 _DEBUG_ERROR("Item is not existed!!");
734                 goto DACI_FINISH;
735         }
736
737         /*
738          * check whether the change log is included
739          */
740         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
741                 item_changelog.item_id = fw_item_id;
742                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
743
744                 switch (item_changelog.operation_id) {
745                 case 301:       /* ADD */
746                         {
747                                 _DEBUG_INFO("Item changelog already exists : ADD Operation");
748                                 ret = da_delete_item_by_item_id_internal(fw_item_id);
749                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
750                                         _DEBUG_ERROR("Failed to Delete Item : %d", ret);
751                                         goto DACI_FINISH;
752                                 }
753
754                                 ret = da_delete_item_changelog_by_item_id_internal(fw_account_id, fw_item_id);
755                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
756                                         _DEBUG_ERROR("Failed to Delete Item Change Log : %d", ret);
757                                         goto DACI_FINISH;
758                                 }
759                         }
760                         break;
761                 case 302:       /* UPDATE */
762                         {
763                                 _DEBUG_INFO("Item Changelog Already Exists : UPDATE Operation");
764                                 item_changelog.operation_id = 303;
765                                 ret = da_set_item_changelog_operation_id_wrapper(fw_account_id, &item_changelog);
766                                 if (ret != SYNC_AGENT_DA_SUCCESS) {
767                                         _DEBUG_ERROR("Failed to Set Item ChangeLog Operation : %d", ret);
768                                         goto DACI_FINISH;
769                                 }
770                         }
771                         break;
772                 case 303:       /* DELETE */
773                         {
774                                 _DEBUG_INFO("Item Changelog Already Exists : DELETE Operation");
775                         }
776                         break;
777                 }
778         } else {
779                 /*
780                  * add change log
781                  */
782                 item_changelog.item_id = fw_item_id;
783                 item_changelog.operation_id = 303;
784                 item_changelog.status = strdup("SYNC_REQUIRED");
785                 item_changelog.access_name = strdup("DACI_ChangeLog");
786
787                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
788                 if (ret != SYNC_AGENT_DA_SUCCESS) {
789                         _DEBUG_ERROR("[da_changelog_delete_item_internal] add item change log failed return : %d", ret);
790                         goto DACI_FINISH;
791                 }
792         }
793
794  DACI_FINISH:
795         if (item_changelog.status != NULL) {
796                 free(item_changelog.status);
797                 item_changelog.status = NULL;
798         }
799         if (item_changelog.access_name != NULL) {
800                 free(item_changelog.access_name);
801                 item_changelog.access_name = NULL;
802         }
803
804         _EXTERN_FUNC_EXIT;
805
806         return ret;
807 }
808
809 int da_changelog_update_item_internal(int fw_account_id, char *fw_item_id, int item_type)
810 {
811         _EXTERN_FUNC_ENTER;
812
813         retvm_if(fw_item_id == NULL, 0, "fw item id is NULL !!");
814
815         /*
816          * TODO : 1. Need to Null check parameter
817          * 2. converting type : itemType
818          * 3. check whether the item is included
819          * 4. check whether the change log is included
820          * 5. add item change log
821          */
822
823         _DEBUG_INFO("fw_account_id : %d", fw_account_id);
824         _DEBUG_INFO("fw_item_id : %s", fw_item_id);
825         _DEBUG_INFO("itemType : %d", item_type);
826
827         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
828
829         sync_agent_da_item_changelog_s item_changelog;
830         item_changelog.status = NULL;
831         item_changelog.access_name = NULL;
832
833         /*
834          * check whether the item is included
835          */
836         ret = da_is_exist_item_wrapper(fw_item_id);
837         if (ret == 1) {
838                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
839         } else {
840                 _DEBUG_ERROR("Item is not existed!!");
841                 goto DACI_FINISH;
842         }
843
844         /* check whether the change log is included */
845         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
846                 item_changelog.item_id = fw_item_id;
847                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
848
849                 if (item_changelog.operation_id == 303) {       /* DELETE */
850                         _DEBUG_ERROR("already deleted item");
851                         goto DACI_FINISH;
852                 }
853
854         } else {
855                 /*
856                  * add change log
857                  */
858                 item_changelog.item_id = fw_item_id;
859                 item_changelog.operation_id = 302;
860                 item_changelog.status = strdup("SYNC_REQUIRED");
861                 item_changelog.access_name = strdup("DACI_ChangeLog");
862
863                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
864                 if (ret != SYNC_AGENT_DA_SUCCESS) {
865                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
866                         goto DACI_FINISH;
867                 }
868         }
869
870  DACI_FINISH:
871         if (item_changelog.status != NULL) {
872                 free(item_changelog.status);
873                 item_changelog.status = NULL;
874         }
875         if (item_changelog.access_name != NULL) {
876                 free(item_changelog.access_name);
877                 item_changelog.access_name = NULL;
878         }
879
880         _EXTERN_FUNC_EXIT;
881
882         return ret;
883 }
884
885 EXPORT_API sync_agent_da_return_e sync_agent_changelog_update_item_new(int fw_account_id, char *fw_item_id)
886 {
887         _EXTERN_FUNC_ENTER;
888
889         retvm_if(fw_item_id == NULL, SYNC_AGENT_DA_ERRORS, "fw item id is NULL !!");
890
891         /*
892          * TODO : 1. Need to Null check parameter
893          * 2. converting type : itemType
894          * 3. check whether the item is included
895          * 4. check whether the change log is included
896          * 5. add item change log
897          */
898         sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
899
900         sync_agent_da_item_changelog_s item_changelog;
901         item_changelog.status = NULL;
902         item_changelog.access_name = NULL;
903
904         /*
905          * check whether the item is included
906          */
907         ret = da_is_exist_item_wrapper(fw_item_id);
908         if (ret == 1) {
909                 _DEBUG_INFO("Item already exists : %s", fw_item_id);
910         } else {
911                 _DEBUG_ERROR("Item is not existed!!");
912                 goto DACI_FINISH;
913         }
914
915         /* check whether the change log is included */
916         if (da_is_exist_item_changelog_wrapper(fw_account_id, fw_item_id)) {
917                 item_changelog.item_id = fw_item_id;
918                 sync_agent_get_item_changelog_by_item_id(fw_account_id, &item_changelog);
919
920                 if (item_changelog.operation_id == 303) {       /* DELETE */
921                         _DEBUG_ERROR("already deleted item");
922                         goto DACI_FINISH;
923                 }
924
925         } else {
926                 /*
927                  * add change log
928                  */
929                 item_changelog.item_id = fw_item_id;
930                 item_changelog.operation_id = 302;
931                 item_changelog.status = strdup("SYNC_REQUIRED");
932                 item_changelog.access_name = strdup("DACI_ChangeLog");
933
934                 ret = da_add_item_changelog_wrapper(fw_account_id, &item_changelog, 1);
935                 if (ret != SYNC_AGENT_DA_SUCCESS) {
936                         _DEBUG_ERROR("Failed to Add Item Change Log : %d", ret);
937                         goto DACI_FINISH;
938                 }
939         }
940
941  DACI_FINISH:
942         if (item_changelog.status != NULL) {
943                 free(item_changelog.status);
944                 item_changelog.status = NULL;
945         }
946         if (item_changelog.access_name != NULL) {
947                 free(item_changelog.access_name);
948                 item_changelog.access_name = NULL;
949         }
950
951         _EXTERN_FUNC_EXIT;
952
953         return ret;
954 }