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