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