Update change log and spec for wrt-plugins-tizen_0.4.11
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactObjectA2PConverter.cpp
1 //
2 // Tizen Web Device API
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 /*
19  * @file        ContactObjectA2PConverter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Converter (ContactPtr -> contacts_record_h)
23  */
24
25 #include "ContactObjectA2PConverter.h"
26
27 #include <Commons/Exception.h>
28 #include "ContactUtility.h"
29
30 namespace DeviceAPI {
31 namespace Contact {
32
33 using namespace WrtDeviceApis::Commons;
34 using namespace std;
35
36 static const char *EMPTY_STRING = "";
37
38 ContactObjectA2PConverter::ContactObjectA2PConverter(const ContactPtr &abstractContact,
39                 bool forScratch) :
40                 m_abstractContact(abstractContact),
41                 m_platformContact(NULL),
42                 m_forScratch(forScratch),
43                 m_convertFinished(false),
44                 m_platformContactOwnership(false)
45 {
46 }
47
48 ContactObjectA2PConverter::ContactObjectA2PConverter(const ContactPtr &abstractContact,
49                 bool forScratch,
50                 contacts_record_h platformContact) :
51                 m_abstractContact(abstractContact),
52                 m_platformContact(platformContact),
53                 m_forScratch(forScratch),
54                 m_convertFinished(false),
55                 m_platformContactOwnership(false)
56 {
57 }
58
59 ContactObjectA2PConverter::~ContactObjectA2PConverter()
60 {
61         if(m_platformContactOwnership && m_platformContact != NULL)
62         {
63                 contacts_record_destroy(m_platformContact, true);
64                 m_platformContact = NULL;
65                 m_platformContactOwnership = false;
66         }
67 }
68
69 contacts_record_h ContactObjectA2PConverter::getPlatformContact()
70 {
71         LogDebug("enter");
72
73         int errorCode = 0;
74
75         if(m_abstractContact == NULL)
76         {
77                 LogError("Abstract contact object did not set");
78                 ThrowMsg(InvalidArgumentException, "Abstract contact object did not set");
79         }
80
81         if(m_platformContact == NULL)
82         {
83                 if(m_abstractContact->getIdIsSet() && (m_forScratch == false))
84                 {
85                         int id = ContactUtility::strToInt(m_abstractContact->getId());
86                         LogDebug("Load platform object id : " << id);
87                         errorCode = contacts_db_get_record(_contacts_contact._uri, id, &m_platformContact);
88                         if(errorCode != CONTACTS_ERROR_NONE)
89                                 ThrowMsg(UnknownException, "error on contacts_db_get_record (errorCode:" << errorCode << ")");
90                 }
91                 else
92                 {
93                         LogDebug("New platform object has been created");
94                         errorCode = contacts_record_create(_contacts_contact._uri, &m_platformContact);
95                         if(errorCode != CONTACTS_ERROR_NONE)
96                                 ThrowMsg(UnknownException, "error on contacts_record_create (errorCode:" << errorCode << ")");
97                 }
98
99                 m_platformContactOwnership = true;
100         }
101
102         LogDebug("for scratch : " << m_forScratch);
103
104         Try
105         {
106                 if(m_convertFinished == false)
107                         convertToPlatformObject();
108         }
109         Catch(Exception)
110         {
111                 LogError("Error while converting - " << _rethrown_exception.GetMessage());
112                 ThrowMsg(PlatformException, "Fail to convert abstract object to platform object.");
113         }
114
115         return m_platformContact;
116 }
117
118 void ContactObjectA2PConverter::convertToPlatformObject()
119 {
120         importBaseInfoValue();
121         importNameValue();
122         importCompanyList();
123         importNoteList();
124         importNumberList();
125         importEmailList();
126         importGrouprelList();
127         importEventList();
128         importPostalList();
129         importWebAddrList();
130         importNicknameList();
131
132         m_convertFinished = true;
133 }
134
135 void ContactObjectA2PConverter::importBaseInfoValue()
136 {
137         int errorCode = 0;
138         contacts_record_h child_record = NULL;
139
140         const char *newValueStr = NULL;
141         char *oldValueStr = NULL;
142         string abstractValueStr;
143         string realPath;
144
145         // Contact.photoURI
146         if(!m_abstractContact->getPhotoURIIsSet() || m_abstractContact->getPhotoURI().empty())
147         {
148                 errorCode = contacts_record_create(_contacts_image._uri, &child_record);
149                 if(errorCode != CONTACTS_ERROR_NONE)
150                         ThrowMsg(UnknownException, "creating image value A (errorCode:" << errorCode << ")");
151                 contacts_record_add_child_record(m_platformContact, _contacts_contact.image, child_record);
152         }
153         else
154         {
155                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.image, 0, &child_record);
156                 if(errorCode != CONTACTS_ERROR_NONE || child_record == NULL)
157                 {
158                         LogDebug("Fail to get contacts_record_get_child_record_at_p : " << errorCode);
159                         errorCode = contacts_record_create(_contacts_image._uri, &child_record);
160                         if(errorCode != CONTACTS_ERROR_NONE)
161                                 ThrowMsg(UnknownException, "creating name value B (errorCode:" << errorCode << ")");
162                 }
163
164                 contacts_record_get_str_p(child_record, _contacts_image.path, &oldValueStr);
165                 realPath = ContactUtility::convertUriToPath(m_abstractContact->getPhotoURI());
166                 m_abstractContact->setPhotoURIRealPath(realPath);
167                 abstractValueStr = m_abstractContact->getPhotoURIRealPath();
168                 if(oldValueStr != NULL &&
169                                 (!m_abstractContact->getPhotoURIIsSet() || m_abstractContact->getPhotoURI().empty()))
170                 {
171                         newValueStr = EMPTY_STRING;
172                 }
173                 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
174                 {
175                         newValueStr = abstractValueStr.c_str();
176                 }
177
178                 if(newValueStr != NULL)
179                 {
180                         LogDebug("image : " << newValueStr);
181                         errorCode = contacts_record_set_str(child_record, _contacts_image.path, newValueStr);
182                         if(errorCode != CONTACTS_ERROR_NONE)
183                         {
184                                 ThrowMsg(PlatformException, "importing photoURI E (err:" <<
185                                                 errorCode << ", str:" << abstractValueStr << ")");
186                         }
187                 }
188                 contacts_record_add_child_record(m_platformContact, _contacts_contact.image, child_record);
189         }
190
191         // Contact.ringtoneURI
192         newValueStr = NULL;
193         oldValueStr = NULL;
194         contacts_record_get_str_p(m_platformContact, _contacts_contact.ringtone_path, &oldValueStr);
195         realPath = ContactUtility::convertUriToPath(m_abstractContact->getRingtoneURI());
196         m_abstractContact->setRingtoneURIRealPath(realPath);
197         abstractValueStr = m_abstractContact->getRingtoneURIRealPath();
198         if(oldValueStr != NULL &&
199                         (!m_abstractContact->getRingtoneURIIsSet() || m_abstractContact->getRingtoneURI().empty()))
200         {
201                 newValueStr = EMPTY_STRING;
202         }
203         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
204         {
205                 newValueStr = abstractValueStr.c_str();
206         }
207
208         if(newValueStr != NULL)
209         {
210                 errorCode = contacts_record_set_str(m_platformContact, _contacts_contact.ringtone_path, newValueStr);
211                 if(errorCode != CONTACTS_ERROR_NONE)
212                 {
213                         ThrowMsg(PlatformException, "importing ringtoneURI E (err:" <<
214                                         errorCode << ", str:" << abstractValueStr << ")");
215                 }
216         }
217 }
218
219 void ContactObjectA2PConverter::importNameValue()
220 {
221         int errorCode = 0;
222         contacts_record_h child_record = NULL;
223
224         ContactNamePtr contactName = m_abstractContact->getName();
225         if(contactName == NULL ||
226                         (( !contactName->getFirstNameIsSet() || contactName->getFirstName().empty() )
227                         && ( !contactName->getLastNameIsSet() || contactName->getLastName().empty() )
228                         && ( !contactName->getMiddleNameIsSet() || contactName->getMiddleName().empty() )
229                         && ( !contactName->getPhoneticFirstNameIsSet() || contactName->getPhoneticFirstName().empty() )
230                         && ( !contactName->getPhoneticLastNameIsSet() || contactName->getPhoneticLastName().empty() )
231                         && ( !contactName->getSuffixIsSet() || contactName->getSuffix().empty() )
232                         && ( !contactName->getPrefixIsSet() || contactName->getPrefix().empty() ) ) )
233         {
234                 errorCode = contacts_record_create(_contacts_name._uri, &child_record);
235                 if(errorCode != CONTACTS_ERROR_NONE)
236                         ThrowMsg(UnknownException, "creating name value A (errorCode:" << errorCode << ")");
237                 contacts_record_add_child_record(m_platformContact, _contacts_contact.name, child_record);
238                 return;
239         }
240
241         errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.name, 0, &child_record);
242         if(errorCode != CONTACTS_ERROR_NONE || child_record == NULL)
243         {
244                 LogDebug("Fail to get contacts_record_get_child_record_at_p : " << errorCode);
245                 errorCode = contacts_record_create(_contacts_name._uri, &child_record);
246                 if(errorCode != CONTACTS_ERROR_NONE)
247                         ThrowMsg(UnknownException, "creating name value B (errorCode:" << errorCode << ")");
248
249                 importNameValueToNewValue(child_record, contactName);
250         }
251         else
252         {
253                 importNameValueToExistingValue(child_record, contactName);
254         }
255
256         contacts_record_add_child_record(m_platformContact, _contacts_contact.name, child_record);
257 }
258
259 void ContactObjectA2PConverter::importNameValueToExistingValue(contacts_record_h child_record, ContactNamePtr &contactName)
260 {
261         int errorCode = 0;
262
263         const char *newValueStr = NULL;
264         char *oldValueStr = NULL;
265         string abstractValueStr;
266
267         // ContactName.firstName
268         contacts_record_get_str_p(child_record, _contacts_name.first, &oldValueStr);
269         abstractValueStr = contactName->getFirstName();
270         if(oldValueStr != NULL && (!contactName->getFirstNameIsSet() || abstractValueStr.empty()) )
271         {
272                 newValueStr = EMPTY_STRING;
273         }
274         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
275         {
276                 newValueStr = abstractValueStr.c_str();
277         }
278
279         if(newValueStr != NULL)
280         {
281                 errorCode = contacts_record_set_str(child_record, _contacts_name.first, newValueStr);
282                 if(errorCode != CONTACTS_ERROR_NONE)
283                 {
284                         contacts_record_destroy(child_record, true);
285                         ThrowMsg(PlatformException, "importing firstName E (err:" <<
286                                         errorCode << ", str:" << newValueStr << ")");
287                 }
288         }
289
290         // ContactName.lastName
291         newValueStr = NULL;
292         oldValueStr = NULL;
293         contacts_record_get_str_p(child_record, _contacts_name.last, &oldValueStr);
294         abstractValueStr = contactName->getLastName();
295         if(oldValueStr != NULL && (!contactName->getLastNameIsSet() || abstractValueStr.empty()) )
296         {
297                 newValueStr = EMPTY_STRING;
298         }
299         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
300         {
301                 newValueStr = abstractValueStr.c_str();
302         }
303
304         if(newValueStr != NULL)
305         {
306                 errorCode = contacts_record_set_str(child_record, _contacts_name.last, newValueStr);
307                 if(errorCode != CONTACTS_ERROR_NONE)
308                 {
309                         contacts_record_destroy(child_record, true);
310                         ThrowMsg(PlatformException, "importing lastName E (err:" <<
311                                         errorCode << ", str:" << newValueStr << ")");
312                 }
313         }
314
315         // ContactName.middleName
316         newValueStr = NULL;
317         oldValueStr = NULL;
318         contacts_record_get_str_p(child_record, _contacts_name.addition, &oldValueStr);
319         abstractValueStr = contactName->getMiddleName();
320         if(oldValueStr != NULL && (!contactName->getMiddleNameIsSet() || abstractValueStr.empty()) )
321         {
322                 newValueStr = EMPTY_STRING;
323         }
324         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
325         {
326                 newValueStr = abstractValueStr.c_str();
327         }
328
329         if(newValueStr != NULL)
330         {
331                 errorCode = contacts_record_set_str(child_record, _contacts_name.addition, newValueStr);
332                 if(errorCode != CONTACTS_ERROR_NONE)
333                 {
334                         contacts_record_destroy(child_record, true);
335                         ThrowMsg(PlatformException, "importing middleName E (err:" <<
336                                         errorCode << ", str:" << newValueStr << ")");
337                 }
338         }
339
340         // ContactName.prefix
341         newValueStr = NULL;
342         oldValueStr = NULL;
343         contacts_record_get_str_p(child_record, _contacts_name.prefix, &oldValueStr);
344         abstractValueStr = contactName->getPrefix();
345         if(oldValueStr != NULL && (!contactName->getPrefixIsSet() || abstractValueStr.empty()) )
346         {
347                 newValueStr = EMPTY_STRING;
348         }
349         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
350         {
351                 newValueStr = abstractValueStr.c_str();
352         }
353
354         if(newValueStr != NULL)
355         {
356                 errorCode = contacts_record_set_str(child_record, _contacts_name.prefix, newValueStr);
357                 if(errorCode != CONTACTS_ERROR_NONE)
358                 {
359                         contacts_record_destroy(child_record, true);
360                         ThrowMsg(PlatformException, "importing prefix E (err:" <<
361                                         errorCode << ", str:" << newValueStr << ")");
362                 }
363         }
364
365         // ContactName.suffix
366         newValueStr = NULL;
367         oldValueStr = NULL;
368         contacts_record_get_str_p(child_record, _contacts_name.suffix, &oldValueStr);
369         abstractValueStr = contactName->getSuffix();
370         if(oldValueStr != NULL && (!contactName->getSuffixIsSet() || abstractValueStr.empty()) )
371         {
372                 newValueStr = EMPTY_STRING;
373         }
374         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
375         {
376                 newValueStr = abstractValueStr.c_str();
377         }
378
379         if(newValueStr != NULL)
380         {
381                 errorCode = contacts_record_set_str(child_record, _contacts_name.suffix, newValueStr);
382                 if(errorCode != CONTACTS_ERROR_NONE)
383                 {
384                         contacts_record_destroy(child_record, true);
385                         ThrowMsg(PlatformException, "importing suffix E (err:" <<
386                                         errorCode << ", str:" << newValueStr << ")");
387                 }
388         }
389
390         // ContactName.phoneticFirstName
391         newValueStr = NULL;
392         oldValueStr = NULL;
393         contacts_record_get_str_p(child_record, _contacts_name.phonetic_first, &oldValueStr);
394         abstractValueStr = contactName->getPhoneticFirstName();
395         if(oldValueStr != NULL && (!contactName->getPhoneticFirstNameIsSet() || abstractValueStr.empty()) )
396         {
397                 newValueStr = EMPTY_STRING;
398         }
399         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
400         {
401                 newValueStr = abstractValueStr.c_str();
402         }
403
404         if(newValueStr != NULL)
405         {
406                 errorCode = contacts_record_set_str(child_record, _contacts_name.phonetic_first, newValueStr);
407                 if(errorCode != CONTACTS_ERROR_NONE)
408                 {
409                         contacts_record_destroy(child_record, true);
410                         ThrowMsg(PlatformException, "importing phoneticFirstName E (err:" <<
411                                         errorCode << ", str:" << newValueStr << ")");
412                 }
413         }
414
415         // ContactName.phoneticLastName
416         newValueStr = NULL;
417         oldValueStr = NULL;
418         contacts_record_get_str_p(child_record, _contacts_name.phonetic_last, &oldValueStr);
419         abstractValueStr = contactName->getPhoneticLastName();
420         if(oldValueStr != NULL && (!contactName->getPhoneticLastNameIsSet() || abstractValueStr.empty()) )
421         {
422                 newValueStr = EMPTY_STRING;
423         }
424         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
425         {
426                 newValueStr = abstractValueStr.c_str();
427         }
428
429         if(newValueStr != NULL)
430         {
431                 errorCode = contacts_record_set_str(child_record, _contacts_name.phonetic_last, newValueStr);
432                 if(errorCode != CONTACTS_ERROR_NONE)
433                 {
434                         contacts_record_destroy(child_record, true);
435                         ThrowMsg(PlatformException, "importing phoneticLastName E (err:" <<
436                                         errorCode << ", str:" << newValueStr << ")");
437                 }
438         }
439 }
440
441 void ContactObjectA2PConverter::importNameValueToNewValue(contacts_record_h child_record, ContactNamePtr &contactName)
442 {
443         int errorCode = 0;
444
445         // ContactName.firstName
446         if(contactName->getFirstNameIsSet())
447         {
448                 string firstName = contactName->getFirstName();
449                 if(firstName != EMPTY_STRING)
450                 {
451                         errorCode = contacts_record_set_str(child_record, _contacts_name.first, firstName.c_str());
452                         if(errorCode != CONTACTS_ERROR_NONE)
453                         {
454                                 contacts_record_destroy(child_record, true);
455                                 ThrowMsg(PlatformException, "importing firstName N (err:" <<
456                                                 errorCode << ", str:" << firstName << ")");
457                         }
458                 }
459         }
460
461         // ContactName.lastName
462         if(contactName->getLastNameIsSet())
463         {
464                 string lastName = contactName->getLastName();
465                 if(lastName != EMPTY_STRING)
466                 {
467                         errorCode = contacts_record_set_str(child_record, _contacts_name.last, lastName.c_str());
468                         if(errorCode != CONTACTS_ERROR_NONE)
469                         {
470                                 contacts_record_destroy(child_record, true);
471                                 ThrowMsg(PlatformException, "importing lastName N (err:" <<
472                                                 errorCode << ", str:" << lastName << ")");
473                         }
474                 }
475         }
476
477         // ContactName.middleName
478         if(contactName->getMiddleNameIsSet())
479         {
480                 string middleName = contactName->getMiddleName();
481                 if(middleName != EMPTY_STRING)
482                 {
483                         errorCode = contacts_record_set_str(child_record, _contacts_name.addition, middleName.c_str());
484                         if(errorCode != CONTACTS_ERROR_NONE)
485                         {
486                                 contacts_record_destroy(child_record, true);
487                                 ThrowMsg(PlatformException, "importing middleName N (err:" <<
488                                                 errorCode << ", str:" << middleName << ")");
489                         }
490                 }
491         }
492
493         // ContactName.prefix
494         if(contactName->getPrefixIsSet())
495         {
496                 string prefix = contactName->getPrefix();
497                 if(prefix != EMPTY_STRING)
498                 {
499                         errorCode = contacts_record_set_str(child_record, _contacts_name.prefix, prefix.c_str());
500                         if(errorCode != CONTACTS_ERROR_NONE)
501                         {
502                                 contacts_record_destroy(child_record, true);
503                                 ThrowMsg(PlatformException, "importing prefix N (err:" <<
504                                                 errorCode << ", str:" << prefix << ")");
505                         }
506                 }
507         }
508
509         // ContactName.suffix
510         if(contactName->getSuffixIsSet())
511         {
512                 string suffix = contactName->getSuffix();
513                 if(suffix != EMPTY_STRING)
514                 {
515                         errorCode = contacts_record_set_str(child_record, _contacts_name.suffix, suffix.c_str());
516                         if(errorCode != CONTACTS_ERROR_NONE)
517                         {
518                                 contacts_record_destroy(child_record, true);
519                                 ThrowMsg(PlatformException, "importing suffix N (err:" <<
520                                                 errorCode << ", str:" << suffix << ")");
521                         }
522                 }
523         }
524
525         // ContactName.phoneticFirstName
526         if(contactName->getPhoneticFirstNameIsSet())
527         {
528                 string phoneticFirstName = contactName->getPhoneticFirstName();
529                 if(phoneticFirstName != EMPTY_STRING)
530                 {
531                         errorCode = contacts_record_set_str(child_record, _contacts_name.phonetic_first, phoneticFirstName.c_str());
532                         if(errorCode != CONTACTS_ERROR_NONE)
533                         {
534                                 contacts_record_destroy(child_record, true);
535                                 ThrowMsg(PlatformException, "importing phoneticFirstName N (err:" <<
536                                                 errorCode << ", str:" << phoneticFirstName << ")");
537                         }
538                 }
539         }
540
541         // ContactName.phoneticLastName
542         if(contactName->getPhoneticLastNameIsSet())
543         {
544                 string phoneticLastName = contactName->getPhoneticLastName();
545                 if(phoneticLastName != EMPTY_STRING)
546                 {
547                         errorCode = contacts_record_set_str(child_record, _contacts_name.phonetic_last, phoneticLastName.c_str());
548                         if(errorCode != CONTACTS_ERROR_NONE)
549                         {
550                                 contacts_record_destroy(child_record, true);
551                                 ThrowMsg(PlatformException, "importing phoneticLastName N (err:" <<
552                                                 errorCode << ", str:" << phoneticLastName << ")");
553                         }
554                 }
555         }
556 }
557
558 void ContactObjectA2PConverter::importCompanyList()
559 {
560         int errorCode = 0;
561         contacts_record_h child_record = NULL;
562         unsigned int record_count = 0;
563
564         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.company, &record_count);
565         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
566         {
567                 ThrowMsg(PlatformException, "getting company count (err:" << errorCode << ")");
568         }
569
570         for(int i=record_count-1; i>=0; i--)
571         {
572                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.company, i, &child_record);
573                 if(errorCode != CONTACTS_ERROR_NONE)
574                         ThrowMsg(UnknownException, "getting company value A (errorCode:" << errorCode << ")");
575
576                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.company, child_record);
577                 if(errorCode != CONTACTS_ERROR_NONE)
578                         ThrowMsg(UnknownException, "removing company value A (errorCode:" << errorCode << ")");
579
580                 child_record = NULL;
581         }
582
583         ContactOrganizationArrayPtr organizations = m_abstractContact->getOrganizations();
584         if(organizations->size() == 0)
585                 return;
586
587         ContactOrganizationArray::iterator organizationsIter = organizations->begin();
588         for(; organizationsIter != organizations->end(); organizationsIter++)
589         {
590                 ContactOrganizationPtr organization = *organizationsIter;
591
592                 child_record = NULL;
593
594                 errorCode = contacts_record_create(_contacts_company._uri, &child_record);
595                 if(errorCode != CONTACTS_ERROR_NONE)
596                 {
597                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_company._uri)");
598                         continue;
599                 }
600
601                 // ContactOrganization.name
602                 if(organization->getNameIsSet())
603                 {
604                         string name = organization->getName();
605                         if(!name.empty())
606                         {
607                                 errorCode = contacts_record_set_str(child_record, _contacts_company.name, name.c_str());
608                                 if(errorCode != CONTACTS_ERROR_NONE)
609                                 {
610                                         LogWarning("importing company name N (err:" << errorCode << ", str:" << name << ")");
611                                         contacts_record_destroy(child_record, true);
612                                         continue;
613                                 }
614                         }
615                 }
616
617                 // ContactOrganization.department
618                 if(organization->getDepartmentIsSet())
619                 {
620                         string department = organization->getDepartment();
621                         if(!department.empty())
622                         {
623                                 errorCode = contacts_record_set_str(child_record, _contacts_company.department, department.c_str());
624                                 if(errorCode != CONTACTS_ERROR_NONE)
625                                 {
626                                         LogWarning("importing department N (err:" << errorCode << ", str:" << department << ")");
627                                         contacts_record_destroy(child_record, true);
628                                         continue;
629                                 }
630                         }
631                 }
632
633                 // ContactOrganization.title
634                 if(organization->getTitleIsSet())
635                 {
636                         string title = organization->getTitle();
637                         if(!title.empty())
638                         {
639                                 errorCode = contacts_record_set_str(child_record, _contacts_company.job_title, title.c_str());
640                                 if(errorCode != CONTACTS_ERROR_NONE)
641                                 {
642                                         LogWarning("importing title N (err:" << errorCode << ", str:" << title << ")");
643                                         contacts_record_destroy(child_record, true);
644                                         continue;
645                                 }
646                         }
647                 }
648
649                 // ContactOrganization.role
650                 if(organization->getRoleIsSet())
651                 {
652                         string role = organization->getRole();
653                         if(!role.empty())
654                         {
655                                 errorCode = contacts_record_set_str(child_record, _contacts_company.role, role.c_str());
656                                 if(errorCode != CONTACTS_ERROR_NONE)
657                                 {
658                                         LogWarning("importing role N (err:" << errorCode << ", str:" << role << ")");
659                                         contacts_record_destroy(child_record, true);
660                                         continue;
661                                 }
662                         }
663                 }
664
665                 // ContactOrganization.logoURI
666                 if(organization->getLogoURIIsSet())
667                 {
668                         string logoURI = organization->getLogoURI();
669                         if(!logoURI.empty())
670                         {
671                                 logoURI = ContactUtility::convertUriToPath(logoURI);
672                                 errorCode = contacts_record_set_str(child_record, _contacts_company.logo, logoURI.c_str());
673                                 if(errorCode != CONTACTS_ERROR_NONE)
674                                 {
675                                         LogWarning("importing logoURI N (err:" << errorCode << ", str:" << logoURI << ")");
676                                         contacts_record_destroy(child_record, true);
677                                         continue;
678                                 }
679                         }
680                 }
681
682                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.company, child_record);
683                 if(errorCode != CONTACTS_ERROR_NONE)
684                 {
685                         LogWarning("Fail to set company value to child_record");
686                         contacts_record_destroy(child_record, true);
687                         continue;
688                 }
689         }
690 }
691
692 void ContactObjectA2PConverter::importNoteList()
693 {
694         int errorCode = 0;
695         contacts_record_h child_record = NULL;
696         unsigned int record_count = 0;
697
698         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.note, &record_count);
699         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
700         {
701                 ThrowMsg(PlatformException, "getting note count (err:" << errorCode << ")");
702         }
703
704         for(int i=record_count-1; i>=0; i--)
705         {
706                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.note, i, &child_record);
707                 if(errorCode != CONTACTS_ERROR_NONE)
708                         ThrowMsg(UnknownException, "getting note value A (errorCode:" << errorCode << ")");
709
710                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.note, child_record);
711                 if(errorCode != CONTACTS_ERROR_NONE)
712                         ThrowMsg(UnknownException, "removing note value A (errorCode:" << errorCode << ")");
713
714                 child_record = NULL;
715         }
716
717         StringArrayPtr notes = m_abstractContact->getNotes();
718         if(notes->size() == 0)
719                 return;
720
721         StringArray::iterator notesIter = notes->begin();
722         for(; notesIter != notes->end(); notesIter++)
723         {
724                 string note = *notesIter;
725
726                 child_record = NULL;
727
728                 if(note.empty())
729                 {
730                         continue;
731                 }
732
733                 errorCode = contacts_record_create(_contacts_note._uri, &child_record);
734                 if(errorCode != CONTACTS_ERROR_NONE)
735                 {
736                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_note._uri)");
737                         continue;
738                 }
739
740                 errorCode = contacts_record_set_str(child_record, _contacts_note.note, note.c_str());
741                 if(errorCode != CONTACTS_ERROR_NONE)
742                 {
743                         LogWarning("importing note N (err:" << errorCode << ", str:" << note << ")");
744                         contacts_record_destroy(child_record, true);
745                         continue;
746                 }
747
748                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.note, child_record);
749                 if(errorCode != CONTACTS_ERROR_NONE)
750                 {
751                         LogWarning("Fail to set note value to child_record");
752                         contacts_record_destroy(child_record, true);
753                         continue;
754                 }
755         }
756 }
757
758 void ContactObjectA2PConverter::importNumberList()
759 {
760         int errorCode = 0;
761         contacts_record_h child_record = NULL;
762         unsigned int record_count = 0;
763
764         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.number, &record_count);
765         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
766         {
767                 ThrowMsg(PlatformException, "getting number count (err:" << errorCode << ")");
768         }
769
770         for(int i=record_count-1; i>=0; i--)
771         {
772                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.number, i, &child_record);
773                 if(errorCode != CONTACTS_ERROR_NONE)
774                         ThrowMsg(UnknownException, "getting number value A (errorCode:" << errorCode << ")");
775
776                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.number, child_record);
777                 if(errorCode != CONTACTS_ERROR_NONE)
778                         ThrowMsg(UnknownException, "removing number value A (errorCode:" << errorCode << ")");
779
780                 child_record = NULL;
781         }
782
783         ContactPhoneNumberArrayPtr phoneNumbers = m_abstractContact->getPhoneNumbers();
784         if(phoneNumbers->size() == 0)
785                 return;
786
787         ContactPhoneNumberArray::iterator phoneNumbersIter = phoneNumbers->begin();
788         for(; phoneNumbersIter != phoneNumbers->end(); phoneNumbersIter++)
789         {
790                 ContactPhoneNumberPtr phoneNumber = *phoneNumbersIter;
791                 ContactPhoneNumberTypeArrayPtr types = phoneNumber->getTypes();
792
793                 if(!phoneNumber->getNumberIsSet() || phoneNumber->getNumber().empty())
794                 {
795                         LogDebug("Number was not set.");
796                         continue;
797                 }
798
799                 string number = phoneNumber->getNumber();
800
801                 bool ctsBoolDefault = phoneNumber->getIsDefault();
802
803                 int ctsIntType = 0;
804                 //char *ctsStrCustomType = NULL;
805
806                 ContactPhoneNumberTypeArray::iterator typesIter = types->begin();
807                 for(; typesIter != types->end(); typesIter++)
808                 {
809                         ContactPhoneNumberType type = *typesIter;
810                         if(type == CONTACT_PHONE_NUMBER_TYPE_HOME)
811                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_HOME;
812                         else if(type == CONTACT_PHONE_NUMBER_TYPE_WORK)
813                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_WORK;
814                         else if(type == CONTACT_PHONE_NUMBER_TYPE_VOICE)
815                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_VOICE;
816                         else if(type == CONTACT_PHONE_NUMBER_TYPE_FAX)
817                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_FAX;
818                         else if(type == CONTACT_PHONE_NUMBER_TYPE_MSG)
819                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_MSG;
820                         else if(type == CONTACT_PHONE_NUMBER_TYPE_CELL)
821                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_CELL;
822                         else if(type == CONTACT_PHONE_NUMBER_TYPE_PAGER)
823                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_PAGER;
824                         else if(type == CONTACT_PHONE_NUMBER_TYPE_BBS)
825                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_BBS;
826                         else if(type == CONTACT_PHONE_NUMBER_TYPE_MODEM)
827                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_MODEM;
828                         else if(type == CONTACT_PHONE_NUMBER_TYPE_CAR)
829                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_CAR;
830                         else if(type == CONTACT_PHONE_NUMBER_TYPE_ISDN)
831                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_ISDN;
832                         else if(type == CONTACT_PHONE_NUMBER_TYPE_VIDEO)
833                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_VIDEO;
834                         else if(type == CONTACT_PHONE_NUMBER_TYPE_PCS)
835                                 ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_PCS;
836                         else
837                         {
838                                 // TODO Will be added after type changed to string
839                                 // ctsIntType = ctsIntType | CONTACTS_NUMBER_TYPE_CUSTOM;
840                                 // ctsStrCustomType = ...;
841                         }
842                 }
843
844                 child_record = NULL;
845
846                 errorCode = contacts_record_create(_contacts_number._uri, &child_record);
847                 if(errorCode != CONTACTS_ERROR_NONE)
848                 {
849                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_number._uri)");
850                         continue;
851                 }
852
853                 errorCode = contacts_record_set_str(child_record, _contacts_number.number, number.c_str());
854                 if(errorCode != CONTACTS_ERROR_NONE)
855                 {
856                         LogWarning("Fail to set number value to child_record number : " << number);
857                         contacts_record_destroy(child_record, true);
858                         continue;
859                 }
860
861                 errorCode = contacts_record_set_bool(child_record, _contacts_number.is_default, ctsBoolDefault);
862                 if(errorCode != CONTACTS_ERROR_NONE)
863                 {
864                         LogWarning("Fail to set number value to child_record default : " << ctsBoolDefault);
865                         contacts_record_destroy(child_record, true);
866                         continue;
867                 }
868
869                 errorCode = contacts_record_set_int(child_record, _contacts_number.type, ctsIntType);
870                 if(errorCode != CONTACTS_ERROR_NONE)
871                 {
872                         LogWarning("Fail to set number value to child_record type : " << ctsIntType);
873                         contacts_record_destroy(child_record, true);
874                         continue;
875                 }
876
877                 //if(ctsStrCustomType != NULL)
878                 //{
879                 //      errorCode = contacts_record_set_str(child_record, _contacts_number.label, ctsStrCustomType);
880                 //      if(errorCode != CONTACTS_ERROR_NONE)
881                 //      {
882                 //              LogWarning("Fail to set number value to child_record custom type : " << ctsStrCustomType);
883                 //              contacts_record_destroy(child_record, true);
884                 //              continue;
885                 //      }
886                 //}
887
888                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.number, child_record);
889                 if(errorCode != CONTACTS_ERROR_NONE)
890                 {
891                         LogWarning("Fail to set number value to child_record");
892                         contacts_record_destroy(child_record, true);
893                         continue;
894                 }
895         }
896 }
897
898 void ContactObjectA2PConverter::importEmailList()
899 {
900         int errorCode = 0;
901         contacts_record_h child_record = NULL;
902         unsigned int record_count = 0;
903
904         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.email, &record_count);
905         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
906         {
907                 ThrowMsg(PlatformException, "getting email count (err:" << errorCode << ")");
908         }
909
910         for(int i=record_count-1; i>=0; i--)
911         {
912                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.email, i, &child_record);
913                 if(errorCode != CONTACTS_ERROR_NONE)
914                         ThrowMsg(UnknownException, "getting email value A (errorCode:" << errorCode << ")");
915
916                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.email, child_record);
917                 if(errorCode != CONTACTS_ERROR_NONE)
918                         ThrowMsg(UnknownException, "removing email value A (errorCode:" << errorCode << ")");
919
920                 child_record = NULL;
921         }
922
923         ContactEmailAddressArrayPtr emails = m_abstractContact->getEmails();
924         if(emails->size() == 0)
925                 return;
926
927         ContactEmailAddressArray::iterator emailsIter = emails->begin();
928         for(; emailsIter != emails->end(); emailsIter++)
929         {
930                 ContactEmailAddressPtr email = *emailsIter;
931                 ContactEmailAddressTypeArrayPtr types = email->getTypes();
932
933                 if(!email->getEmailIsSet() || email->getEmail().empty())
934                 {
935                         LogDebug("email address was not set.");
936                         continue;
937                 }
938
939                 string emailAddress = email->getEmail();
940
941                 bool ctsBoolDefault = email->getIsDefault();
942
943                 int ctsIntType = 0;
944                 //char *ctsStrCustomType = NULL;
945
946                 ContactEmailAddressTypeArray::iterator typesIter = types->begin();
947                 for(; typesIter != types->end(); typesIter++)
948                 {
949                         ContactEmailAddressType type = *typesIter;
950                         if(type == CONTACT_EMAIL_TYPE_HOME)
951                                 ctsIntType = ctsIntType | CONTACTS_EMAIL_TYPE_HOME;
952                         else if(type == CONTACT_EMAIL_TYPE_WORK)
953                                 ctsIntType = ctsIntType | CONTACTS_EMAIL_TYPE_WORK;
954                         //else
955                         //{
956                         //      //TODO Will be added after type changed to string
957                         //      //ctsIntType = ctsIntType | CONTACTS_EMAIL_TYPE_CUSTOM;
958                         //      // ctsStrCustomType = ...;
959                         //}
960                 }
961
962                 child_record = NULL;
963
964                 errorCode = contacts_record_create(_contacts_email._uri, &child_record);
965                 if(errorCode != CONTACTS_ERROR_NONE)
966                 {
967                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_email._uri)");
968                         continue;
969                 }
970
971                 errorCode = contacts_record_set_str(child_record, _contacts_email.email, emailAddress.c_str());
972                 if(errorCode != CONTACTS_ERROR_NONE)
973                 {
974                         LogWarning("Fail to set email value to child_record email : " << emailAddress);
975                         contacts_record_destroy(child_record, true);
976                         continue;
977                 }
978
979                 errorCode = contacts_record_set_bool(child_record, _contacts_email.is_default, ctsBoolDefault);
980                 if(errorCode != CONTACTS_ERROR_NONE)
981                 {
982                         LogWarning("Fail to set email value to child_record default : " << ctsBoolDefault);
983                         contacts_record_destroy(child_record, true);
984                         continue;
985                 }
986
987                 errorCode = contacts_record_set_int(child_record, _contacts_email.type, ctsIntType);
988                 if(errorCode != CONTACTS_ERROR_NONE)
989                 {
990                         LogWarning("Fail to set email value to child_record type : " << ctsIntType);
991                         contacts_record_destroy(child_record, true);
992                         continue;
993                 }
994
995                 //if(ctsStrCustomType != NULL)
996                 //{
997                 //      errorCode = contacts_record_set_str(child_record, _contacts_email.label, ctsStrCustomType);
998                 //      if(errorCode != CONTACTS_ERROR_NONE)
999                 //      {
1000                 //              LogWarning("Fail to set email value to child_record custom type : " << ctsStrCustomType);
1001                 //              contacts_record_destroy(child_record, true);
1002                 //              continue;
1003                 //      }
1004                 //}
1005
1006                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.email, child_record);
1007                 if(errorCode != CONTACTS_ERROR_NONE)
1008                 {
1009                         LogWarning("Fail to set email value to child_record");
1010                         contacts_record_destroy(child_record, true);
1011                         continue;
1012                 }
1013         }
1014 }
1015
1016 void ContactObjectA2PConverter::importGrouprelList()
1017 {
1018         int errorCode = 0;
1019         contacts_record_h child_record = NULL;
1020         unsigned int record_count = 0;
1021
1022         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.group_relation, &record_count);
1023         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
1024         {
1025                 ThrowMsg(PlatformException, "getting note count (err:" << errorCode << ")");
1026         }
1027
1028         for(int i=record_count-1; i>=0; i--)
1029         {
1030                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.group_relation, i, &child_record);
1031                 if(errorCode != CONTACTS_ERROR_NONE)
1032                         ThrowMsg(UnknownException, "getting note value A (errorCode:" << errorCode << ")");
1033
1034                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.group_relation, child_record);
1035                 if(errorCode != CONTACTS_ERROR_NONE)
1036                         ThrowMsg(UnknownException, "removing note value A (errorCode:" << errorCode << ")");
1037
1038                 child_record = NULL;
1039         }
1040
1041         StringArrayPtr groupIds = m_abstractContact->getGroupIds();
1042         if(groupIds->size() == 0)
1043                 return;
1044
1045         StringArray::iterator groupIdsIter = groupIds->begin();
1046         for(; groupIdsIter != groupIds->end(); groupIdsIter++)
1047         {
1048                 string groupId = *groupIdsIter;
1049
1050                 child_record = NULL;
1051
1052                 if(!ContactUtility::checkStrIsUInt(groupId))
1053                 {
1054                         LogWarning("groupId (" << groupId << ") is wrong.");
1055                         continue;
1056                 }
1057
1058                 int groupIdInt = ContactUtility::strToInt(groupId);
1059
1060                 errorCode = contacts_record_create(_contacts_group_relation._uri, &child_record);
1061                 if(errorCode != CONTACTS_ERROR_NONE)
1062                 {
1063                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_group_relation._uri)");
1064                         continue;
1065                 }
1066
1067                 errorCode = contacts_record_set_int(child_record, _contacts_group_relation.group_id, groupIdInt);
1068                 if(errorCode != CONTACTS_ERROR_NONE)
1069                 {
1070                         LogWarning("importing group id N (err:" << errorCode << ", str:" << groupIdInt << ")");
1071                         contacts_record_destroy(child_record, true);
1072                         continue;
1073                 }
1074
1075                 int contactId = ContactUtility::strToInt(m_abstractContact->getId());
1076                 errorCode = contacts_record_set_int(child_record, _contacts_group_relation.contact_id, contactId);
1077                 if(errorCode != CONTACTS_ERROR_NONE)
1078                 {
1079                         LogWarning("importing group contact_id N (err:" << errorCode << ", str:" << contactId << ")");
1080                         contacts_record_destroy(child_record, true);
1081                         continue;
1082                 }
1083
1084                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.group_relation, child_record);
1085                 if(errorCode != CONTACTS_ERROR_NONE)
1086                 {
1087                         LogWarning("Fail to set group value to child_record");
1088                         contacts_record_destroy(child_record, true);
1089                         continue;
1090                 }
1091         }
1092 }
1093
1094 void ContactObjectA2PConverter::importEventList()
1095 {
1096         int errorCode = 0;
1097         contacts_record_h child_record = NULL;
1098         unsigned int record_count = 0;
1099
1100         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.event, &record_count);
1101         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
1102         {
1103                 ThrowMsg(PlatformException, "getting event count (err:" << errorCode << ")");
1104         }
1105
1106         for(int i=record_count-1; i>=0; i--)
1107         {
1108                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.event, i, &child_record);
1109                 if(errorCode != CONTACTS_ERROR_NONE)
1110                         ThrowMsg(UnknownException, "getting event value A (errorCode:" << errorCode << ")");
1111
1112                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.event, child_record);
1113                 if(errorCode != CONTACTS_ERROR_NONE)
1114                         ThrowMsg(UnknownException, "removing event value A (errorCode:" << errorCode << ")");
1115
1116                 child_record = NULL;
1117         }
1118
1119         if(!m_abstractContact->getBirthdayIsSet() && m_abstractContact->getAnniversariesNum() == 0)
1120                 return;
1121
1122         importEventListBirthday();
1123
1124         importEventListAnniversary();
1125 }
1126
1127 void ContactObjectA2PConverter::importEventListBirthday()
1128 {
1129         int errorCode = 0;
1130
1131         if(!m_abstractContact->getBirthdayIsSet())
1132                 return;
1133
1134         tm birthdayTm = m_abstractContact->getBirthday();
1135
1136         if (birthdayTm.tm_year == 0 && birthdayTm.tm_mon == 0 && birthdayTm.tm_mday == 0)
1137                 return;
1138
1139         int birthday = ContactUtility::toDateDbInt(birthdayTm);
1140
1141         contacts_record_h child_record = NULL;
1142
1143         errorCode = contacts_record_create(_contacts_event._uri, &child_record);
1144         if(errorCode != CONTACTS_ERROR_NONE)
1145         {
1146                 LogWarning("Fail to create child_record with contacts_record_create(_contacts_event._uri)");
1147                 return;
1148         }
1149
1150         errorCode = contacts_record_set_int(child_record, _contacts_event.type, CONTACTS_EVENT_TYPE_BIRTH);
1151         if(errorCode != CONTACTS_ERROR_NONE)
1152         {
1153                 LogWarning("Fail to set event value to child_record type : " << CONTACTS_EVENT_TYPE_BIRTH);
1154                 contacts_record_destroy(child_record, true);
1155                 return;
1156         }
1157
1158         errorCode = contacts_record_set_int(child_record, _contacts_event.date, birthday);
1159         if(errorCode != CONTACTS_ERROR_NONE)
1160         {
1161                 LogWarning("Fail to set event value to child_record date : " << birthday);
1162                 contacts_record_destroy(child_record, true);
1163                 return;
1164         }
1165
1166         errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.event, child_record);
1167         if(errorCode != CONTACTS_ERROR_NONE)
1168         {
1169                 LogWarning("Fail to set event value to child_record");
1170                 contacts_record_destroy(child_record, true);
1171                 return;
1172         }
1173 }
1174
1175 void ContactObjectA2PConverter::importEventListAnniversary()
1176 {
1177         int errorCode = 0;
1178         contacts_record_h child_record = NULL;
1179
1180         if(m_abstractContact->getAnniversariesNum() == 0)
1181                 return;
1182
1183         ContactAnniversaryArrayPtr anniversaries = m_abstractContact->getAnniversaries();
1184
1185         ContactAnniversaryArray::iterator anniversariesIter = anniversaries->begin();
1186         for(; anniversariesIter != anniversaries->end(); anniversariesIter++)
1187         {
1188                 ContactAnniversaryPtr anniversary = *anniversariesIter;
1189
1190                 if(!anniversary->getDateIsSet())
1191                         continue;
1192
1193                 tm dateTm = anniversary->getDate();
1194
1195                 if (dateTm.tm_year == 0 && dateTm.tm_mon == 0 && dateTm.tm_mday == 0)
1196                         continue;
1197
1198                 int date = ContactUtility::toDateDbInt(dateTm);
1199
1200                 child_record = NULL;
1201
1202                 errorCode = contacts_record_create(_contacts_event._uri, &child_record);
1203                 if(errorCode != CONTACTS_ERROR_NONE)
1204                 {
1205                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_event._uri)");
1206                         continue;
1207                 }
1208
1209                 errorCode = contacts_record_set_int(child_record, _contacts_event.type, CONTACTS_EVENT_TYPE_ANNIVERSARY);
1210                 if(errorCode != CONTACTS_ERROR_NONE)
1211                 {
1212                         LogWarning("Fail to set event value to child_record type : CONTACTS_EVENT_TYPE_ANNIVERSARY");
1213                         contacts_record_destroy(child_record, true);
1214                         continue;
1215                 }
1216
1217                 errorCode = contacts_record_set_int(child_record, _contacts_event.date, date);
1218                 if(errorCode != CONTACTS_ERROR_NONE)
1219                 {
1220                         LogWarning("Fail to set event value to child_record date : " << date);
1221                         contacts_record_destroy(child_record, true);
1222                         continue;
1223                 }
1224
1225                 if(anniversary->getLabelIsSet())
1226                 {
1227                         string label = anniversary->getLabel();
1228                         errorCode = contacts_record_set_str(child_record, _contacts_event.label, label.c_str());
1229                         if(errorCode != CONTACTS_ERROR_NONE)
1230                         {
1231                                 LogWarning("Fail to set label value to child_record label : " << label);
1232                                 contacts_record_destroy(child_record, true);
1233                                 continue;
1234                         }
1235
1236                 }
1237
1238                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.event, child_record);
1239                 if(errorCode != CONTACTS_ERROR_NONE)
1240                 {
1241                         LogWarning("Fail to set event value to child_record");
1242                         contacts_record_destroy(child_record, true);
1243                         continue;
1244                 }
1245         }
1246 }
1247
1248 void ContactObjectA2PConverter::importPostalList()
1249 {
1250         int errorCode = 0;
1251         contacts_record_h child_record = NULL;
1252         unsigned int record_count = 0;
1253
1254         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.address, &record_count);
1255         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
1256         {
1257                 ThrowMsg(PlatformException, "getting address count (err:" << errorCode << ")");
1258         }
1259
1260         for(int i=record_count-1; i>=0; i--)
1261         {
1262                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.address, i, &child_record);
1263                 if(errorCode != CONTACTS_ERROR_NONE)
1264                         ThrowMsg(UnknownException, "getting address value A (errorCode:" << errorCode << ")");
1265
1266                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.address, child_record);
1267                 if(errorCode != CONTACTS_ERROR_NONE)
1268                         ThrowMsg(UnknownException, "removing address value A (errorCode:" << errorCode << ")");
1269
1270                 child_record = NULL;
1271         }
1272
1273         if(m_abstractContact->getAddressesNum() == 0)
1274                 return;
1275
1276         ContactAddressArrayPtr addresses = m_abstractContact->getAddresses();
1277
1278         ContactAddressArray::iterator addressesIter = addresses->begin();
1279         for(; addressesIter != addresses->end(); addressesIter++)
1280         {
1281                 ContactAddressPtr address = *addressesIter;
1282                 ContactAddressTypeArrayPtr types = address->getTypes();
1283
1284                 if(( !address->getCountryIsSet() || address->getCountry().empty() ) &&
1285                                 ( !address->getRegionIsSet() || address->getRegion().empty() ) &&
1286                                 ( !address->getCityIsSet() || address->getCity().empty() ) &&
1287                                 ( !address->getStreetAddressIsSet() || address->getStreetAddress().empty() ) &&
1288                                 ( !address->getAdditionalInformationIsSet() || address->getAdditionalInformation().empty() ) &&
1289                                 ( !address->getPostalCodeIsSet() || address->getPostalCode().empty() ) )
1290                 {
1291                         LogDebug("No information for address was not set.");
1292                         continue;
1293                 }
1294
1295                 bool ctsBoolDefault = address->getIsDefault();
1296
1297                 int ctsIntType = 0;
1298                 //char *ctsStrCustomType = NULL;
1299
1300                 ContactAddressTypeArray::iterator typesIter = types->begin();
1301                 for(; typesIter != types->end(); typesIter++)
1302                 {
1303                         ContactAddressType type = *typesIter;
1304                         if(type == CONTACT_ADDRESS_TYPE_HOME)
1305                                 ctsIntType = ctsIntType | CONTACTS_ADDRESS_TYPE_HOME;
1306                         else if(type == CONTACT_ADDRESS_TYPE_WORK)
1307                                 ctsIntType = ctsIntType | CONTACTS_ADDRESS_TYPE_WORK;
1308                         //else
1309                         //{
1310                         //      // TODO Will be added after type changed to string
1311                         //      // ctsIntType = ctsIntType | CTS_POSTAL_TYPE_CUSTOM;
1312                         //      // ctsStrCustomType = ...;
1313                         //}
1314                 }
1315
1316                 child_record = NULL;
1317
1318                 errorCode = contacts_record_create(_contacts_address._uri, &child_record);
1319                 if(errorCode != CONTACTS_ERROR_NONE)
1320                 {
1321                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_address._uri)");
1322                         continue;
1323                 }
1324
1325                 if(address->getCountryIsSet())
1326                 {
1327                         string country = address->getCountry();
1328                         errorCode = contacts_record_set_str(child_record, _contacts_address.country, country.c_str());
1329                         if(errorCode != CONTACTS_ERROR_NONE)
1330                         {
1331                                 LogWarning("Fail to set country value to child_record str : " << country);
1332                                 contacts_record_destroy(child_record, true);
1333                                 continue;
1334                         }
1335                 }
1336
1337                 if(address->getRegionIsSet())
1338                 {
1339                         string region = address->getRegion();
1340                         errorCode = contacts_record_set_str(child_record, _contacts_address.region, region.c_str());
1341                         if(errorCode != CONTACTS_ERROR_NONE)
1342                         {
1343                                 LogWarning("Fail to set region value to child_record str : " << region);
1344                                 contacts_record_destroy(child_record, true);
1345                                 continue;
1346                         }
1347                 }
1348
1349                 if(address->getCityIsSet())
1350                 {
1351                         string city = address->getCity();
1352                         errorCode = contacts_record_set_str(child_record, _contacts_address.locality, city.c_str());
1353                         if(errorCode != CONTACTS_ERROR_NONE)
1354                         {
1355                                 LogWarning("Fail to set locality value to child_record str : " << city);
1356                                 contacts_record_destroy(child_record, true);
1357                                 continue;
1358                         }
1359                 }
1360
1361                 if(address->getStreetAddressIsSet())
1362                 {
1363                         string streetAddress = address->getStreetAddress();
1364                         errorCode = contacts_record_set_str(child_record, _contacts_address.street, streetAddress.c_str());
1365                         if(errorCode != CONTACTS_ERROR_NONE)
1366                         {
1367                                 LogWarning("Fail to set street value to child_record str : " << streetAddress);
1368                                 contacts_record_destroy(child_record, true);
1369                                 continue;
1370                         }
1371                 }
1372
1373                 if(address->getAdditionalInformationIsSet())
1374                 {
1375                         string additionalInformation = address->getAdditionalInformation();
1376                         errorCode = contacts_record_set_str(child_record, _contacts_address.extended, additionalInformation.c_str());
1377                         if(errorCode != CONTACTS_ERROR_NONE)
1378                         {
1379                                 LogWarning("Fail to set extended value to child_record str : " << additionalInformation);
1380                                 contacts_record_destroy(child_record, true);
1381                                 continue;
1382                         }
1383                 }
1384
1385                 if(address->getPostalCodeIsSet())
1386                 {
1387                         string postalCode = address->getPostalCode();
1388                         errorCode = contacts_record_set_str(child_record, _contacts_address.postal_code, postalCode.c_str());
1389                         if(errorCode != CONTACTS_ERROR_NONE)
1390                         {
1391                                 LogWarning("Fail to set postal_code value to child_record str : " << postalCode);
1392                                 contacts_record_destroy(child_record, true);
1393                                 continue;
1394                         }
1395                 }
1396
1397                 errorCode = contacts_record_set_bool(child_record, _contacts_address.is_default, ctsBoolDefault);
1398                 if(errorCode != CONTACTS_ERROR_NONE)
1399                 {
1400                         LogWarning("Fail to set favorite value to child_record default : " << ctsBoolDefault);
1401                         contacts_record_destroy(child_record, true);
1402                         continue;
1403                 }
1404
1405                 errorCode = contacts_record_set_int(child_record, _contacts_address.type, ctsIntType);
1406                 if(errorCode != CONTACTS_ERROR_NONE)
1407                 {
1408                         LogWarning("Fail to set default value to child_record type : " << ctsIntType);
1409                         contacts_record_destroy(child_record, true);
1410                         continue;
1411                 }
1412
1413                 //if(ctsStrCustomType != NULL)
1414                 //{
1415                 //      errorCode = contacts_record_set_str(child_record, _contacts_address.label, ctsStrCustomType);
1416                 //      if(errorCode != CONTACTS_ERROR_NONE)
1417                 //      {
1418                 //              LogWarning("Fail to set custom value to child_record type : " << ctsStrCustomType);
1419                 //              contacts_record_destroy(child_record, true);
1420                 //              continue;
1421                 //      }
1422                 //}
1423
1424                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.address, child_record);
1425                 if(errorCode != CONTACTS_ERROR_NONE)
1426                 {
1427                         LogWarning("Fail to set address value to child_record");
1428                         contacts_record_destroy(child_record, true);
1429                         continue;
1430                 }
1431         }
1432 }
1433
1434 void ContactObjectA2PConverter::importWebAddrList()
1435 {
1436         int errorCode = 0;
1437         contacts_record_h child_record = NULL;
1438         unsigned int record_count = 0;
1439
1440         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.url, &record_count);
1441         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
1442         {
1443                 ThrowMsg(PlatformException, "getting url count (err:" << errorCode << ")");
1444         }
1445
1446         for(int i=record_count-1; i>=0; i--)
1447         {
1448                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.url, i, &child_record);
1449                 if(errorCode != CONTACTS_ERROR_NONE)
1450                         ThrowMsg(UnknownException, "getting url value A (errorCode:" << errorCode << ")");
1451
1452                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.url, child_record);
1453                 if(errorCode != CONTACTS_ERROR_NONE)
1454                         ThrowMsg(UnknownException, "removing url value A (errorCode:" << errorCode << ")");
1455
1456                 child_record = NULL;
1457         }
1458
1459         ContactWebSiteArrayPtr webSites = m_abstractContact->getUrls();
1460         if(webSites->size() == 0)
1461                 return;
1462
1463         ContactWebSiteArray::iterator webSiteIter = webSites->begin();
1464         for(; webSiteIter != webSites->end(); webSiteIter++)
1465         {
1466                 ContactWebSitePtr webSite = *webSiteIter;
1467                 ContactWebSiteType type = webSite->getType();
1468
1469                 if(!webSite->getUrlIsSet() || webSite->getUrl().empty())
1470                 {
1471                         LogDebug("url was not set.");
1472                         continue;
1473                 }
1474
1475                 string url = webSite->getUrl();
1476
1477                 int ctsIntType = 0;
1478 //              char *ctsStrCustomType = NULL;
1479
1480                 if(type == WEBSITE_TYPE_HOMEPAGE)
1481                         ctsIntType = CONTACTS_URL_TYPE_HOME;
1482                 else if(type == WEBSITE_TYPE_BLOG)
1483                         ctsIntType = CONTACTS_URL_TYPE_WORK;
1484                 else
1485                 {
1486                         // TODO Will be added after type changed to string
1487                         // ctsIntType = CONTACTS_URL_TYPE_CUSTOM;
1488                         // ctsStrCustomType = ...;
1489                 }
1490
1491                 child_record = NULL;
1492
1493                 errorCode = contacts_record_create(_contacts_url._uri, &child_record);
1494                 if(errorCode != CONTACTS_ERROR_NONE)
1495                 {
1496                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_url._uri)");
1497                         continue;
1498                 }
1499
1500                 errorCode = contacts_record_set_str(child_record, _contacts_url.url, url.c_str());
1501                 if(errorCode != CONTACTS_ERROR_NONE)
1502                 {
1503                         LogWarning("Fail to set url value to child_record str : " << url);
1504                         contacts_record_destroy(child_record, true);
1505                         continue;
1506                 }
1507
1508                 errorCode = contacts_record_set_int(child_record, _contacts_url.type, ctsIntType);
1509                 if(errorCode != CONTACTS_ERROR_NONE)
1510                 {
1511                         LogWarning("Fail to set default value to child_record type : " << ctsIntType);
1512                         contacts_record_destroy(child_record, true);
1513                         continue;
1514                 }
1515 /*
1516                 if(ctsStrCustomType != NULL)
1517                 {
1518                         errorCode = contacts_record_set_str(child_record, _contacts_url.label, ctsStrCustomType);
1519                         if(errorCode != CONTACTS_ERROR_NONE)
1520                         {
1521                                 LogWarning("Fail to set custom value to child_record type : " << ctsStrCustomType);
1522                                 contacts_record_destroy(child_record, true);
1523                                 continue;
1524                         }
1525                 }
1526 */
1527                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.url, child_record);
1528                 if(errorCode != CONTACTS_ERROR_NONE)
1529                 {
1530                         LogWarning("Fail to set url value to child_record");
1531                         contacts_record_destroy(child_record, true);
1532                         continue;
1533                 }
1534         }
1535 }
1536
1537 void ContactObjectA2PConverter::importNicknameList()
1538 {
1539         int errorCode = 0;
1540         contacts_record_h child_record = NULL;
1541         unsigned int record_count = 0;
1542
1543         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.nickname, &record_count);
1544         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
1545         {
1546                 ThrowMsg(PlatformException, "getting nickname count (err:" << errorCode << ")");
1547         }
1548
1549         for(int i=record_count-1; i>=0; i--)
1550         {
1551                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.nickname, i, &child_record);
1552                 if(errorCode != CONTACTS_ERROR_NONE)
1553                         ThrowMsg(UnknownException, "getting nickname value A (errorCode:" << errorCode << ")");
1554
1555                 errorCode = contacts_record_remove_child_record(m_platformContact, _contacts_contact.nickname, child_record);
1556                 if(errorCode != CONTACTS_ERROR_NONE)
1557                         ThrowMsg(UnknownException, "removing nickname value A (errorCode:" << errorCode << ")");
1558
1559                 child_record = NULL;
1560         }
1561
1562         ContactNamePtr contactName = m_abstractContact->getName();
1563         if(contactName == NULL)
1564                 return;
1565
1566         if(contactName->getNicknamesNum() == 0)
1567                 return;
1568
1569         StringArrayPtr nicknames = contactName->getNicknames();
1570
1571         StringArray::iterator nicknamesIter = nicknames->begin();
1572         for(; nicknamesIter != nicknames->end(); nicknamesIter++)
1573         {
1574                 string nickname = *nicknamesIter;
1575
1576                 if(nickname.empty())
1577                 {
1578                         LogDebug("nickname was not set.");
1579                         continue;
1580                 }
1581
1582                 child_record = NULL;
1583
1584                 errorCode = contacts_record_create(_contacts_nickname._uri, &child_record);
1585                 if(errorCode != CONTACTS_ERROR_NONE)
1586                 {
1587                         LogWarning("Fail to create child_record with contacts_record_create(_contacts_nickname._uri)");
1588                         continue;
1589                 }
1590
1591                 errorCode = contacts_record_set_str(child_record, _contacts_nickname.name, nickname.c_str());
1592                 if(errorCode != CONTACTS_ERROR_NONE)
1593                 {
1594                         LogWarning("Fail to set nickname value to ctsValue str : " << nickname);
1595                         contacts_record_destroy(child_record, true);
1596                         continue;
1597                 }
1598
1599                 errorCode = contacts_record_add_child_record(m_platformContact, _contacts_contact.nickname, child_record);
1600                 if(errorCode != CONTACTS_ERROR_NONE)
1601                 {
1602                         LogWarning("Fail to set nickname value to child_record");
1603                         contacts_record_destroy(child_record, true);
1604                         continue;
1605                 }
1606         }
1607 }
1608
1609 } // Contact
1610 } // DeviceAPI