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