Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactConverter.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        ContactConverter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "ContactConverter.h"
26 #include <algorithm>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <CommonsJavaScript/JSUtils.h>
30 #include <Commons/RegexUtils.h>
31 #include "ContactFactory.h"
32 #include "AddressBookController.h"
33 #include "JSAddressBook.h"
34 #include "JSContact.h"
35 #include "JSPerson.h"
36 #include "JSContactGroup.h"
37 #include "JSContactRef.h"
38 #include "JSContactName.h"
39 #include "JSContactAnniversary.h"
40 #include "JSContactAddress.h"
41 #include "JSContactManager.h"
42 #include "JSContactPhoneNumber.h"
43 #include "JSContactEmailAddress.h"
44 #include "JSContactOrganization.h"
45 #include "JSContactWebSite.h"
46 #include <Logger.h>
47
48 #define CONTACT_ATTRIBUTE_ACCOUNT_ID "accountId"
49 #define CONTACT_ATTRIBUTE_ADDITIONAL_INFORMATION "additionalInformation"
50 #define CONTACT_ATTRIBUTE_ADDRESSES "addresses"
51 #define CONTACT_ATTRIBUTE_ANNIVERSARIES "anniversaries"
52 #define CONTACT_ATTRIBUTE_BIRTHDAY "birthday"
53 #define CONTACT_ATTRIBUTE_CITY "city"
54 #define CONTACT_ATTRIBUTE_CONTACT_ID "contactId"
55 #define CONTACT_ATTRIBUTE_CONTACT_URI "contactURI"
56 #define CONTACT_ATTRIBUTE_COUNTRY "country"
57 #define CONTACT_ATTRIBUTE_COUNTY "county"
58 #define CONTACT_ATTRIBUTE_DATE "date"
59 #define CONTACT_ATTRIBUTE_DEPARTMENT "department"
60 #define CONTACT_ATTRIBUTE_DISPLAY_NAME "displayName"
61 #define CONTACT_ATTRIBUTE_EMAIL "email"
62 #define CONTACT_ATTRIBUTE_EMAILS "emails"
63 #define CONTACT_ATTRIBUTE_FIRST_NAME "firstName"
64 #define CONTACT_ATTRIBUTE_GROUP_IDS "groupIds"
65 #define CONTACT_ATTRIBUTE_ID "id"
66 #define CONTACT_ATTRIBUTE_IS_DEFAULT "isDefault"
67 #define CONTACT_ATTRIBUTE_IS_FAVORITE "isFavorite"
68 #define CONTACT_ATTRIBUTE_LABEL "label"
69 #define CONTACT_ATTRIBUTE_LAST_NAME "lastName"
70 #define CONTACT_ATTRIBUTE_LAST_UPDATED "lastUpdated"
71 #define CONTACT_ATTRIBUTE_LOGO_URI "logoURI"
72 #define CONTACT_ATTRIBUTE_MIDDLE_NAME "middleName"
73 #define CONTACT_ATTRIBUTE_NAME "name"
74 #define CONTACT_ATTRIBUTE_NICKNAMES "nicknames"
75 #define CONTACT_ATTRIBUTE_NOTES "notes"
76 #define CONTACT_ATTRIBUTE_NUMBER "number"
77 #define CONTACT_ATTRIBUTE_OFFICE "office"
78 #define CONTACT_ATTRIBUTE_ORGANIZATIONS "organizations"
79 #define CONTACT_ATTRIBUTE_PHONE_NUMBERS "phoneNumbers"
80 #define CONTACT_ATTRIBUTE_PHONETIC_FIRST_NAME "phoneticFirstName"
81 #define CONTACT_ATTRIBUTE_PHONETIC_MIDDLE_NAME "phoneticMiddleName"
82 #define CONTACT_ATTRIBUTE_PHONETIC_LAST_NAME "phoneticLastName"
83 #define CONTACT_ATTRIBUTE_PHOTO_URI  "photoURI"
84 #define CONTACT_ATTRIBUTE_POSITION "position"
85 #define CONTACT_ATTRIBUTE_POSTAL_CODE "postalCode"
86 #define CONTACT_ATTRIBUTE_PREFIX "prefix"
87 #define CONTACT_ATTRIBUTE_READ_ONLY "readOnly"
88 #define CONTACT_ATTRIBUTE_REGION "region"
89 #define CONTACT_ATTRIBUTE_RINGTONE_URI "ringtoneURI"
90 #define CONTACT_ATTRIBUTE_ROLE "role"
91 #define CONTACT_ATTRIBUTE_SUFFIX "suffix"
92 #define CONTACT_ATTRIBUTE_STREET_ADDRESS "streetAddress"
93 #define CONTACT_ATTRIBUTE_TITLE "title"
94 #define CONTACT_ATTRIBUTE_TYPE "type"
95 #define CONTACT_ATTRIBUTE_TYPES "types"
96 #define CONTACT_ATTRIBUTE_LABEL "label"
97 #define CONTACT_ATTRIBUTE_URL "url"
98 #define CONTACT_ATTRIBUTE_URLS "urls"
99
100 #define CONTACT_ATTRIBUTE_ASSISTANT_NAME "assistant"
101 #define CONTACT_ATTRIBUTE_LOCATION "location"
102 #define CONTACT_ATTRIBUTE_DESCRIPTION "description"
103 #define CONTACT_ATTRIBUTE_PHONETIC_NAME "phoneticName"
104
105 #define STR_CONTACT_EMPTY_STRING ""
106
107 #define STR_CONTACT_EMAIL_TYPE_WORK "WORK"
108 #define STR_CONTACT_EMAIL_TYPE_PREF "PREF"
109 #define STR_CONTACT_EMAIL_TYPE_HOME "HOME"
110 #define STR_CONTACT_EMAIL_TYPE_MOBILE "MOBILE"
111
112 #define STR_CONTACT_PHONE_NUMBER_TYPE_WORK "WORK"
113 #define STR_CONTACT_PHONE_NUMBER_TYPE_PREF "PREF"
114 #define STR_CONTACT_PHONE_NUMBER_TYPE_HOME "HOME"
115 #define STR_CONTACT_PHONE_NUMBER_TYPE_VOICE "VOICE"
116 #define STR_CONTACT_PHONE_NUMBER_TYPE_FAX "FAX"
117 #define STR_CONTACT_PHONE_NUMBER_TYPE_MSG "MSG"
118 #define STR_CONTACT_PHONE_NUMBER_TYPE_CELL "CELL"
119 #define STR_CONTACT_PHONE_NUMBER_TYPE_PAGER "PAGER"
120 #define STR_CONTACT_PHONE_NUMBER_TYPE_BBS "BBS"
121 #define STR_CONTACT_PHONE_NUMBER_TYPE_MODEM "MODEM"
122 #define STR_CONTACT_PHONE_NUMBER_TYPE_CAR "CAR"
123 #define STR_CONTACT_PHONE_NUMBER_TYPE_ISDN "ISDN"
124 #define STR_CONTACT_PHONE_NUMBER_TYPE_VIDEO "VIDEO"
125 #define STR_CONTACT_PHONE_NUMBER_TYPE_PCS "PCS"
126 #define STR_CONTACT_PHONE_NUMBER_TYPE_ASSISTANT "ASSISTANT"
127
128 #define STR_CONTACT_ADDRESS_TYPE_WORK "WORK"
129 #define STR_CONTACT_ADDRESS_TYPE_PREF "PREF"
130 #define STR_CONTACT_ADDRESS_TYPE_HOME "HOME"
131 #define STR_CONTACT_ADDRESS_TYPE_DOM "DOM"
132 #define STR_CONTACT_ADDRESS_TYPE_INTL "INTL"
133 #define STR_CONTACT_ADDRESS_TYPE_POSTAL "POSTAL"
134 #define STR_CONTACT_ADDRESS_TYPE_PARCEL "PARCEL"
135
136 #define STR_WEBSITE_TYPE_HOMEPAGE "HOMEPAGE"
137 #define STR_WEBSITE_TYPE_BLOG "BLOG"
138
139 #define STR_ORGANIZATION_TYPE_WORK "WORK"
140 #define STR_ORGANIZATION_TYPE_OTHER "OTHER"
141
142 namespace DeviceAPI {
143 namespace Contact {
144
145 using namespace WrtDeviceApis::Commons;
146 using namespace WrtDeviceApis::CommonsJavaScript;
147
148 std::vector<std::string> ContactConverter::m_allowedContactInit;
149 std::vector<std::string> ContactConverter::m_allowedContactNameInit;
150 std::vector<std::string> ContactConverter::m_allowedContactOrganizationInit;
151 std::vector<std::string> ContactConverter::m_allowedContactAddressInit;
152
153 ContactConverter::ContactConverter(JSContextRef context) : Converter(context)
154 {
155         static bool init = initializeAllowedProperties();
156         (void) init;
157 }
158
159 ContactConverter::~ContactConverter()
160 {
161 }
162
163 JSValueRef ContactConverter::toJSValueRef(const AddressBookPtr &arg)
164 {
165         // It may not used
166         AddressBookController *priv = new AddressBookController(m_context, arg);
167         return JSObjectMake(m_context, JSAddressBook::getClassRef(), priv);
168 }
169
170 AddressBookPtr ContactConverter::toAddressBook(const JSValueRef &jsValue)
171 {
172         if(!JSAddressBook::isObjectOfClass(m_context, jsValue))
173                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
174
175         return JSAddressBook::getAddressBook(m_context, jsValue);
176 }
177
178 JSValueRef ContactConverter::toJSValueRef(const AddressBookArrayPtr &arg)
179 {
180         int size = arg->size();
181
182         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
183         if (!resultObject)
184                 ThrowMsg(ConversionException, "Can not create array object.");
185
186         for(int i = 0; i < size; i++)
187         {
188                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
189                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
190                         ThrowMsg(ConversionException, "Can not fill address book array.");
191         }
192
193         return static_cast<JSValueRef>(resultObject);
194 }
195
196 AddressBookArrayPtr ContactConverter::toAddressBookArray(const JSValueRef &jsValue)
197 {
198         if(!JSIsArrayValue(m_context, jsValue))
199                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "AddressBookArray is not array.");
200
201         AddressBookArrayPtr result(new AddressBookArray());
202
203         JSObjectRef jsObject = toJSObjectRef(jsValue);
204     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
205         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
206         result->push_back(toAddressBook(element));
207     }
208     return result;
209 }
210
211 JSValueRef ContactConverter::toJSValueRef(const ContactPtr &arg)
212 {
213         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContact::getClassRef(), arg);
214 }
215
216 ContactPtr ContactConverter::toContact(const JSValueRef &jsValue)
217 {
218         if(!JSContact::isObjectOfClass(m_context, jsValue))
219         {
220                 LoggerE("Not a Contact object");
221                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
222         }
223
224         ContactPtr tempContact = JSContact::getContact(m_context, jsValue);
225
226         const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NAME));
227         const ScopedJSStringRef addressesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ADDRESSES));
228         const ScopedJSStringRef groupIdsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_GROUP_IDS));
229         const ScopedJSStringRef notesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NOTES));
230         const ScopedJSStringRef phoneNumbersStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONE_NUMBERS));
231         const ScopedJSStringRef emailsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_EMAILS));
232         const ScopedJSStringRef anniversariesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ANNIVERSARIES));
233         const ScopedJSStringRef organizationsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ORGANIZATIONS));
234         const ScopedJSStringRef urlsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_URLS));
235
236         JSObjectRef jsObject = toJSObjectRef(jsValue);
237         JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
238         JSValueRef addressesData = JSObjectGetProperty(m_context, jsObject, addressesStr.get(), NULL);
239         JSValueRef phoneNumbersData = JSObjectGetProperty(m_context, jsObject, phoneNumbersStr.get(), NULL);
240         JSValueRef emailsData = JSObjectGetProperty(m_context, jsObject, emailsStr.get(), NULL);
241         JSValueRef groupIdsData = JSObjectGetProperty(m_context, jsObject, groupIdsStr.get(), NULL);
242         JSValueRef notesData = JSObjectGetProperty(m_context, jsObject, notesStr.get(), NULL);
243         JSValueRef anniversariesData = JSObjectGetProperty(m_context, jsObject, anniversariesStr.get(), NULL);
244         JSValueRef organizationsData = JSObjectGetProperty(m_context, jsObject, organizationsStr.get(), NULL);
245         JSValueRef urlsData = JSObjectGetProperty(m_context, jsObject, urlsStr.get(), NULL);
246
247         ContactNamePtr name;
248         ContactAddressArrayPtr addresses;
249         ContactPhoneNumberArrayPtr phoneNumbers;
250         ContactEmailAddressArrayPtr emails;
251         StringArrayPtr groupIds;
252         StringArrayPtr notes;
253         ContactAnniversaryArrayPtr anniversaries;
254         ContactOrganizationArrayPtr organizations;
255         ContactWebSiteArrayPtr urls;
256
257         Validator validator(m_context);
258
259         if (!validator.isNullOrUndefined(nameData)) {
260                 name = toContactName(nameData);
261                 tempContact->setName(name);
262         }
263
264         if (!validator.isNullOrUndefined(addressesData)) {
265                 addresses = toContactAddressArray(addressesData);
266                 tempContact->setAddresses(addresses);
267         }
268
269         if (!validator.isNullOrUndefined(phoneNumbersData)) {
270                 phoneNumbers = toContactPhoneNumberArray(phoneNumbersData);
271                 tempContact->setPhoneNumbers(phoneNumbers);
272         }
273
274         if (!validator.isNullOrUndefined(emailsData)) {
275                 emails = toContactEmailAddressArray(emailsData);
276                 tempContact->setEmails(emails);
277         }
278
279         if (!validator.isNullOrUndefined(groupIdsData)) {
280                 groupIds = toStringArray(groupIdsData);
281                 tempContact->setGroupIds(groupIds);
282         }
283
284         if (!validator.isNullOrUndefined(notesData)) {
285                 notes = toStringArray(notesData);
286                 tempContact->setNotes(notes);
287         }
288
289         if (!validator.isNullOrUndefined(anniversariesData)) {
290                 anniversaries = toContactAnniversaryArray(anniversariesData);
291                 tempContact->setAnniversaries(anniversaries);
292         }
293
294         if (!validator.isNullOrUndefined(organizationsData)) {
295                 organizations = toContactOrganizationArray(organizationsData);
296                 tempContact->setOrganizations(organizations);
297         }
298
299         if (!validator.isNullOrUndefined(urlsData)) {
300                 urls = toContactWebSiteArray(urlsData);
301                 tempContact->setUrls(urls);
302         }
303
304         return tempContact;
305
306 //      return JSContact::getContact(m_context, jsValue);
307 }
308
309 ContactPtr ContactConverter::toContactFromInit(const JSValueRef &jsValue)
310 {
311         // from ContactInit to Contact
312         Validator validator(m_context);
313         if (validator.isNullOrUndefined(jsValue)) {
314                 LoggerE("ContactInit object is null");
315                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
316         }
317
318         if (!JSValueIsObject(m_context, jsValue) || JSIsArrayValue(m_context, jsValue)) {
319                 LoggerE("ContactInit object is not object");
320                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
321         }
322
323 //      if (!validator.checkArrayKeys(m_allowedContactInit, jsValue)) {
324 //              LoggerE("invalid properties in ContactInit object");
325 //              ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
326 //      }
327
328         const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NAME));
329         const ScopedJSStringRef addressesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ADDRESSES));
330         const ScopedJSStringRef photoURIStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHOTO_URI));
331         const ScopedJSStringRef phoneNumbersStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONE_NUMBERS));
332         const ScopedJSStringRef emailsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_EMAILS));
333         const ScopedJSStringRef birthdayStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_BIRTHDAY));
334         const ScopedJSStringRef anniversariesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ANNIVERSARIES));
335         const ScopedJSStringRef organizationsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ORGANIZATIONS));
336         const ScopedJSStringRef notesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NOTES));
337         const ScopedJSStringRef urlsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_URLS));
338         const ScopedJSStringRef isFavoriteStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_IS_FAVORITE));
339         const ScopedJSStringRef ringtoneURIStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_RINGTONE_URI));
340         const ScopedJSStringRef groupIdsStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_GROUP_IDS));
341
342         JSObjectRef jsObject = toJSObjectRef(jsValue);
343
344         JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
345         JSValueRef addressesData = JSObjectGetProperty(m_context, jsObject, addressesStr.get(), NULL);
346         JSValueRef photoURIData = JSObjectGetProperty(m_context, jsObject, photoURIStr.get(), NULL);
347         JSValueRef phoneNumbersData = JSObjectGetProperty(m_context, jsObject, phoneNumbersStr.get(), NULL);
348         JSValueRef emailsData = JSObjectGetProperty(m_context, jsObject, emailsStr.get(), NULL);
349         JSValueRef birthdayData = JSObjectGetProperty(m_context, jsObject, birthdayStr.get(), NULL);
350         JSValueRef anniversariesData = JSObjectGetProperty(m_context, jsObject, anniversariesStr.get(), NULL);
351         JSValueRef organizationsData = JSObjectGetProperty(m_context, jsObject, organizationsStr.get(), NULL);
352         JSValueRef notesData = JSObjectGetProperty(m_context, jsObject, notesStr.get(), NULL);
353         JSValueRef urlsData = JSObjectGetProperty(m_context, jsObject, urlsStr.get(), NULL);
354         JSValueRef ringtoneURIData = JSObjectGetProperty(m_context, jsObject, ringtoneURIStr.get(), NULL);
355         JSValueRef groupIdsData = JSObjectGetProperty(m_context, jsObject, groupIdsStr.get(), NULL);
356
357         ContactNamePtr name;
358         ContactAddressArrayPtr addresses;
359         std::string photoURI;
360         ContactPhoneNumberArrayPtr phoneNumbers;
361         ContactEmailAddressArrayPtr emails;
362         tm birthday;
363         ContactAnniversaryArrayPtr anniversaries;
364         ContactOrganizationArrayPtr organizations;
365         StringArrayPtr notes;
366         ContactWebSiteArrayPtr urls;
367         std::string ringtoneURI;
368         StringArrayPtr groupIds;
369
370         ContactPtr result = ContactFactory::getInstance().createContact();
371         if (!result) {
372                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Cannot create Contact object");
373         }
374
375         if (!validator.isNullOrUndefined(nameData)) {
376                 name = toContactName(nameData);
377                 result->setName(name);
378         }
379
380         if (!validator.isNullOrUndefined(addressesData)) {
381                 addresses = toContactAddressArray(addressesData);
382                 result->setAddresses(addresses);
383         }
384
385         if (!validator.isNullOrUndefined(photoURIData)) {
386                 if(!JSValueIsString(m_context, photoURIData))
387                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "photoURI is not string");
388                 photoURI = toString(photoURIData);
389                 result->setPhotoURI(photoURI);
390         }
391
392         if (!validator.isNullOrUndefined(phoneNumbersData)) {
393                 phoneNumbers = toContactPhoneNumberArray(phoneNumbersData);
394                 result->setPhoneNumbers(phoneNumbers);
395         }
396
397         if (!validator.isNullOrUndefined(emailsData)) {
398                 emails = toContactEmailAddressArray(emailsData);
399                 result->setEmails(emails);
400         }
401
402         if (!validator.isNullOrUndefined(birthdayData)) {
403                 if(!validator.isDate(birthdayData))
404                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "invalid birthday");
405                 birthday = toDateTm(birthdayData);
406                 result->setBirthday(birthday);
407         }
408
409         if (!validator.isNullOrUndefined(anniversariesData)) {
410                 anniversaries = toContactAnniversaryArray(anniversariesData);
411                 result->setAnniversaries(anniversaries);
412         }
413
414         if (!validator.isNullOrUndefined(organizationsData)) {
415                 organizations = toContactOrganizationArray(organizationsData);
416                 result->setOrganizations(organizations);
417         }
418
419         if (!validator.isNullOrUndefined(notesData)) {
420                 notes = toStringArray(notesData);
421                 result->setNotes(notes);
422         }
423
424         if (!validator.isNullOrUndefined(urlsData)) {
425                 urls = toContactWebSiteArray(urlsData);
426                 result->setUrls(urls);
427         }
428
429         if (!validator.isNullOrUndefined(ringtoneURIData)) {
430                 if(!JSValueIsString(m_context, ringtoneURIData))
431                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "invalid ringtoneURI");
432                 ringtoneURI = toString(ringtoneURIData);
433                 result->setRingtoneURI(ringtoneURI);
434         }
435
436         if (!validator.isNullOrUndefined(groupIdsData)) {
437                 groupIds = toStringArray(groupIdsData);
438                 result->setGroupIds(groupIds);
439         }
440
441         return result;
442 }
443
444 JSValueRef ContactConverter::toJSValueRef(const ContactArrayPtr &arg)
445 {
446         int size = arg->size();
447
448         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
449         if (!resultObject)
450                 ThrowMsg(ConversionException, "Can not create array object.");
451
452         for(int i = 0; i < size; i++)
453         {
454                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
455                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
456                         ThrowMsg(ConversionException, "Can not fill contact array.");
457         }
458
459         return static_cast<JSValueRef>(resultObject);
460 }
461
462 ContactArrayPtr ContactConverter::toContactArray(const JSValueRef &jsValue)
463 {
464         if(!JSIsArrayValue(m_context, jsValue))
465                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactArray is not array.");
466
467         ContactArrayPtr result = ContactArrayPtr(new ContactArray());
468         bool duplicate = false;
469
470         JSObjectRef jsObject = toJSObjectRef(jsValue);
471     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
472         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
473
474                 ContactPtr tmpContactObj = toContact(element);
475                 duplicate = false;
476                 for(unsigned int j=0; j<result->size(); j++){
477                         if(result->at(j) == tmpContactObj)
478                                 duplicate = true;
479                 }
480                 if(!duplicate)
481                 result->push_back(tmpContactObj);
482     }
483     return result;
484 }
485
486 JSValueRef ContactConverter::toJSValueRef(const PersonPtr &arg)
487 {
488         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSPerson::getClassRef(), arg);
489 }
490
491 PersonPtr ContactConverter::toPerson(const JSValueRef &jsValue)
492 {
493         if(!JSPerson::isObjectOfClass(m_context, jsValue))
494         {
495                 LoggerE("Not a Person object");
496                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
497         }
498
499         return JSPerson::getPerson(m_context, jsValue);
500 }
501
502 JSValueRef ContactConverter::toJSValueRef(const PersonArrayPtr &arg)
503 {
504         int size = arg->size();
505
506         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
507         if (!resultObject)
508                 ThrowMsg(ConversionException, "Can not create array object.");
509
510         for(int i = 0; i < size; i++)
511         {
512                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
513                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
514                         ThrowMsg(ConversionException, "Can not add object to array.");
515         }
516
517         return static_cast<JSValueRef>(resultObject);
518 }
519
520 PersonArrayPtr ContactConverter::toPersonArray(const JSValueRef &jsValue)
521 {
522         if(!JSIsArrayValue(m_context, jsValue))
523                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "PersonArray is not array.");
524
525         PersonArrayPtr result(new PersonArray());
526
527         JSObjectRef jsObject = toJSObjectRef(jsValue);
528     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
529         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
530         result->push_back(toPerson(element));
531     }
532     return result;
533 }
534
535 JSValueRef ContactConverter::toJSValueRef(const ContactGroupPtr &arg)
536 {
537         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactGroup::getClassRef(), arg);
538 }
539
540 ContactGroupPtr ContactConverter::toContactGroup(const JSValueRef &jsValue)
541 {
542         if(!JSContactGroup::isObjectOfClass(m_context, jsValue))
543         {
544                 LoggerE("Not a ContactGroup object");
545                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
546         }
547
548         return JSContactGroup::getContactGroup(m_context, jsValue);
549 }
550
551 JSValueRef ContactConverter::toJSValueRef(const ContactGroupArrayPtr &arg)
552 {
553         int size = arg->size();
554
555         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
556         if (!resultObject)
557                 ThrowMsg(ConversionException, "Can not create array object.");
558
559         for(int i = 0; i < size; i++)
560         {
561                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
562                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
563                         ThrowMsg(ConversionException, "Can not add object to array.");
564         }
565
566         return static_cast<JSValueRef>(resultObject);
567 }
568
569 ContactGroupArrayPtr ContactConverter::toContactGroupArray(const JSValueRef &jsValue)
570 {
571         if(!JSIsArrayValue(m_context, jsValue))
572                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactGroupArray is not array.");
573
574         ContactGroupArrayPtr result(new ContactGroupArray());
575
576         JSObjectRef jsObject = toJSObjectRef(jsValue);
577     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
578         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
579         result->push_back(toContactGroup(element));
580     }
581     return result;
582 }
583
584 JSValueRef ContactConverter::toJSValueRef(const ContactRefPtr &arg)
585 {
586         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactRef::getClassRef(), arg);
587 }
588
589 ContactRefPtr ContactConverter::toContactRef(const JSValueRef &jsValue)
590 {
591         if(!JSContactRef::isObjectOfClass(m_context, jsValue))
592         {
593                 LoggerE("Not a ContactRef object");
594                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
595         }
596
597         return JSContactRef::getContactRef(m_context, jsValue);
598 }
599
600 JSValueRef ContactConverter::toJSValueRef(const ContactNamePtr &arg)
601 {
602         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactName::getClassRef(), arg);
603 }
604
605 ContactNamePtr ContactConverter::toContactName(const JSValueRef &jsValue)
606 {
607         if(!JSContactName::isObjectOfClass(m_context, jsValue))
608         {
609                 LoggerE("Not a ContactName object");
610                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
611         }
612
613         return JSContactName::getContactName(m_context, jsValue);
614 }
615
616 ContactNamePtr ContactConverter::toContactNameFromInit(const JSValueRef &jsValue)
617 {
618         // from ContactNameInit to ContactName
619         Validator validator(m_context);
620         if (validator.isNullOrUndefined(jsValue)) {
621                 LoggerE("ContactNameInit object is null");
622                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
623         }
624
625         if (!JSValueIsObject(m_context, jsValue) || JSIsArrayValue(m_context, jsValue)) {
626                 LoggerE("ContactNameInit object is not object");
627                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
628         }
629
630 //      if (!validator.checkArrayKeys(m_allowedContactNameInit, jsValue)) {
631 //              LoggerE("invalid properties in ContactNameInit object");
632 //              ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
633 //      }
634
635         const ScopedJSStringRef prefixStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PREFIX));
636         const ScopedJSStringRef suffixStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_SUFFIX));
637         const ScopedJSStringRef firstNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_FIRST_NAME));
638         const ScopedJSStringRef middleNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_MIDDLE_NAME));
639         const ScopedJSStringRef lastNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_LAST_NAME));
640         const ScopedJSStringRef nicknamesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NICKNAMES));
641         const ScopedJSStringRef phoneticFirstNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONETIC_FIRST_NAME));
642         const ScopedJSStringRef phoneticMiddleNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONETIC_MIDDLE_NAME));
643         const ScopedJSStringRef phoneticLastNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONETIC_LAST_NAME));
644
645         JSObjectRef jsObject = toJSObjectRef(jsValue);
646
647         JSValueRef prefixData = JSObjectGetProperty(m_context, jsObject, prefixStr.get(), NULL);
648         JSValueRef suffixData = JSObjectGetProperty(m_context, jsObject, suffixStr.get(), NULL);
649         JSValueRef firstNameData = JSObjectGetProperty(m_context, jsObject, firstNameStr.get(), NULL);
650         JSValueRef middleNameData = JSObjectGetProperty(m_context, jsObject, middleNameStr.get(), NULL);
651         JSValueRef lastNameData = JSObjectGetProperty(m_context, jsObject, lastNameStr.get(), NULL);
652         JSValueRef nicknamesData = JSObjectGetProperty(m_context, jsObject, nicknamesStr.get(), NULL);
653         JSValueRef phoneticFirstNameData = JSObjectGetProperty(m_context, jsObject, phoneticFirstNameStr.get(), NULL);
654         JSValueRef phoneticMiddleNameData = JSObjectGetProperty(m_context, jsObject, phoneticMiddleNameStr.get(), NULL);
655         JSValueRef phoneticLastNameData = JSObjectGetProperty(m_context, jsObject, phoneticLastNameStr.get(), NULL);
656
657         std::string prefix;
658         std::string suffix;
659         std::string firstName;
660         std::string middleName;
661         std::string lastName;
662         StringArrayPtr nicknames;
663         std::string phoneticFirstName;
664         std::string phoneticMiddleName;
665         std::string phoneticLastName;
666
667         ContactNamePtr result = ContactNamePtr(new ContactName());
668         if (!result) {
669                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Cannot create ContactName object");
670         }
671
672         if (!validator.isNullOrUndefined(prefixData)) {
673                 if(!JSValueIsString(m_context, prefixData))
674                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "prefix is not string");
675                 prefix = toString(prefixData);
676                 result->setPrefix(prefix);
677         }
678
679         if (!validator.isNullOrUndefined(suffixData)) {
680                 if(!JSValueIsString(m_context, suffixData))
681                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "suffix is not string");
682                 suffix = toString(suffixData);
683                 result->setSuffix(suffix);
684         }
685
686         if (!validator.isNullOrUndefined(firstNameData)) {
687                 if(!JSValueIsString(m_context, firstNameData))
688                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "firstName is not string");
689                 firstName = toString(firstNameData);
690                 result->setFirstName(firstName);
691         }
692
693         if (!validator.isNullOrUndefined(middleNameData)) {
694                 if(!JSValueIsString(m_context, middleNameData))
695                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "middleName is not string");
696                 middleName = toString(middleNameData);
697                 result->setMiddleName(middleName);
698         }
699
700         if (!validator.isNullOrUndefined(lastNameData)) {
701                 if(!JSValueIsString(m_context, lastNameData))
702                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "lastName is not string");
703                 lastName = toString(lastNameData);
704                 result->setLastName(lastName);
705         }
706
707         if (!validator.isNullOrUndefined(nicknamesData)) {
708                 nicknames = toStringArray(nicknamesData);
709                 result->setNicknames(nicknames);
710         }
711
712         if (!validator.isNullOrUndefined(phoneticFirstNameData)) {
713                 if(!JSValueIsString(m_context, phoneticFirstNameData))
714                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "phoneticFirstName is not string");
715                 phoneticFirstName = toString(phoneticFirstNameData);
716                 result->setPhoneticFirstName(phoneticFirstName);
717         }
718
719         if (!validator.isNullOrUndefined(phoneticMiddleNameData)) {
720                 if(!JSValueIsString(m_context, phoneticMiddleNameData))
721                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "phoneticMiddleName is not string");
722                 phoneticMiddleName = toString(phoneticMiddleNameData);
723                 result->setPhoneticMiddleName(phoneticMiddleName);
724         }
725
726         if (!validator.isNullOrUndefined(phoneticLastNameData)) {
727                 if(!JSValueIsString(m_context, phoneticLastNameData))
728                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "phoneticLastName is not string");
729                 phoneticLastName = toString(phoneticLastNameData);
730                 result->setPhoneticLastName(phoneticLastName);
731         }
732
733         return result;
734 }
735
736 JSValueRef ContactConverter::toJSValueRef(const ContactOrganizationPtr &arg)
737 {
738         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactOrganization::getClassRef(), arg);
739 }
740
741 ContactOrganizationPtr ContactConverter::toContactOrganization(const JSValueRef &jsValue)
742 {
743         if(!JSContactOrganization::isObjectOfClass(m_context, jsValue))
744         {
745                 LoggerE("Not a ContactOrganization object");
746                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
747         }
748
749         return JSContactOrganization::getContactOrganization(m_context, jsValue);
750 }
751
752 ContactOrganizationPtr ContactConverter::toContactOrganizationFromInit(const JSValueRef &jsValue)
753 {
754         // from ContactOrganizationInit to ContactOrganization
755         Validator validator(m_context);
756         if (validator.isNullOrUndefined(jsValue)) {
757                 LoggerE("ContactOrganizationInit object is null");
758                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
759         }
760
761         if (!JSValueIsObject(m_context, jsValue) || JSIsArrayValue(m_context, jsValue)) {
762                 LoggerE("ContactOrganizationInit object is not object");
763                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
764         }
765
766 //      if (!validator.checkArrayKeys(m_allowedContactOrganizationInit, jsValue)) {
767 //              LoggerE("invalid properties in ContactOrganizationInit object");
768 //              ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
769 //      }
770
771         // NOTE: Currently not support office and logoURI fields
772         const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_NAME));
773         const ScopedJSStringRef departmentStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_DEPARTMENT));
774         const ScopedJSStringRef titleStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_TITLE));
775         const ScopedJSStringRef roleStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ROLE));
776         const ScopedJSStringRef logoURIStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_LOGO_URI));
777         const ScopedJSStringRef assistantStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ASSISTANT_NAME));
778         const ScopedJSStringRef locationStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_LOCATION));
779         const ScopedJSStringRef descriptionStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_DESCRIPTION));
780         const ScopedJSStringRef phoneticNameStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_PHONETIC_NAME));
781         const ScopedJSStringRef typeStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_TYPE));
782         const ScopedJSStringRef labelStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_LABEL));
783
784         JSObjectRef jsObject = toJSObjectRef(jsValue);
785
786         JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
787         JSValueRef departmentData = JSObjectGetProperty(m_context, jsObject, departmentStr.get(), NULL);
788         JSValueRef titleData = JSObjectGetProperty(m_context, jsObject, titleStr.get(), NULL);
789         JSValueRef roleData = JSObjectGetProperty(m_context, jsObject, roleStr.get(), NULL);
790         JSValueRef logoURIData = JSObjectGetProperty(m_context, jsObject, logoURIStr.get(), NULL);
791         JSValueRef assistantData = JSObjectGetProperty(m_context, jsObject, assistantStr.get(), NULL);
792         JSValueRef locationData = JSObjectGetProperty(m_context, jsObject, locationStr.get(), NULL);
793         JSValueRef descriptionData = JSObjectGetProperty(m_context, jsObject, descriptionStr.get(), NULL);
794         JSValueRef phoneticNameData = JSObjectGetProperty(m_context, jsObject, phoneticNameStr.get(), NULL);
795         JSValueRef typeData = JSObjectGetProperty(m_context, jsObject, typeStr.get(), NULL);
796         JSValueRef labelData = JSObjectGetProperty(m_context, jsObject, labelStr.get(), NULL);
797
798         std::string name;
799         std::string department;
800         std::string title;
801         std::string role;
802         std::string logoURI;
803         std::string assistant;
804         std::string location;
805         std::string description;
806         std::string phoneticName;
807         ContactOrganizationType type;
808         std::string label;
809
810         ContactOrganizationPtr result(new ContactOrganization());
811         if (!result) {
812                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Cannot create ContactOrganization object");
813         }
814
815         if (!validator.isNullOrUndefined(nameData)) {
816                 if(!JSValueIsString(m_context, nameData))
817                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "name is not string");
818                 name = toString(nameData);
819                 result->setName(name);
820         }
821
822         if (!validator.isNullOrUndefined(departmentData)) {
823                 if(!JSValueIsString(m_context, departmentData))
824                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "department is not string");
825                 department = toString(departmentData);
826                 result->setDepartment(department);
827         }
828
829         if (!validator.isNullOrUndefined(titleData)) {
830                 if(!JSValueIsString(m_context, titleData))
831                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "title is not string");
832                 title = toString(titleData);
833                 result->setTitle(title);
834         }
835
836         if (!validator.isNullOrUndefined(roleData)) {
837                 if(!JSValueIsString(m_context, roleData))
838                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "role is not string");
839                 role = toString(roleData);
840                 result->setRole(role);
841         }
842
843         if (!validator.isNullOrUndefined(logoURIData)) {
844                 if(!JSValueIsString(m_context, logoURIData))
845                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "logoURI is not string");
846                 logoURI = toString(logoURIData);
847                 result->setLogoURI(logoURI);
848         }
849
850         if (!validator.isNullOrUndefined(assistantData)) {
851                 if(!JSValueIsString(m_context, assistantData))
852                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "assistant is not string");
853                 assistant = toString(assistantData);
854                 result->setAssistant(assistant);
855         }
856
857         if (!validator.isNullOrUndefined(locationData)) {
858                 if(!JSValueIsString(m_context, locationData))
859                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "location is not string");
860                 location = toString(locationData);
861                 result->setLocation(location);
862         }
863
864         if (!validator.isNullOrUndefined(descriptionData)) {
865                 if(!JSValueIsString(m_context, descriptionData))
866                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "description is not string");
867                 description = toString(descriptionData);
868                 result->setDescription(description);
869         }
870
871         if (!validator.isNullOrUndefined(phoneticNameData)) {
872                 if(!JSValueIsString(m_context, phoneticNameData))
873                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "phoneticName is not string");
874                 phoneticName = toString(phoneticNameData);
875                 result->setPhoneticName(phoneticName);
876         }
877
878         if (!validator.isNullOrUndefined(typeData)) {
879                 if(!JSValueIsString(m_context, typeData))
880                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "type is not string");
881                 type = toContactOrganizationType(typeData);
882                 result->setType(type);
883         }
884
885         if (!validator.isNullOrUndefined(labelData)) {
886                 if(!JSValueIsString(m_context, labelData))
887                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "label is not string");
888                 label = toString(labelData);
889                 result->setLabel(label);
890         }
891
892         return result;
893 }
894
895 JSValueRef ContactConverter::toJSValueRef(const ContactOrganizationArrayPtr &arg)
896 {
897         int size = arg->size();
898
899         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
900         if (!resultObject)
901                 ThrowMsg(ConversionException, "Can not create array object.");
902
903         for(int i = 0; i < size; i++)
904         {
905                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
906                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
907                         ThrowMsg(ConversionException, "Can not add object to array.");
908         }
909
910         return static_cast<JSValueRef>(resultObject);
911 }
912
913 ContactOrganizationArrayPtr ContactConverter::toContactOrganizationArray(const JSValueRef &jsValue)
914 {
915         if(!JSIsArrayValue(m_context, jsValue))
916                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactOrganizationArray is not array.");
917
918         ContactOrganizationArrayPtr result(new ContactOrganizationArray());
919
920         JSObjectRef jsObject = toJSObjectRef(jsValue);
921     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
922         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
923         result->push_back(toContactOrganization(element));
924     }
925     return result;
926 }
927
928 JSValueRef ContactConverter::toJSValueRef(const ContactWebSitePtr &arg)
929 {
930         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactWebSite::getClassRef(), arg);
931 }
932
933 ContactWebSitePtr ContactConverter::toContactWebSite(const JSValueRef &jsValue)
934 {
935         if(!JSContactWebSite::isObjectOfClass(m_context, jsValue))
936         {
937                 LoggerE("Not a ContactWebSite object");
938                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
939         }
940
941         return JSContactWebSite::getContactWebSite(m_context, jsValue);
942 }
943
944 JSValueRef ContactConverter::toJSValueRef(const ContactWebSiteArrayPtr &arg)
945 {
946         int size = arg->size();
947
948         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
949         if (!resultObject)
950                 ThrowMsg(ConversionException, "Can not create array object.");
951
952         for(int i = 0; i < size; i++)
953         {
954                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
955                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
956                         ThrowMsg(ConversionException, "Can not add object to array.");
957         }
958
959         return static_cast<JSValueRef>(resultObject);
960 }
961
962 ContactWebSiteArrayPtr ContactConverter::toContactWebSiteArray(const JSValueRef &jsValue)
963 {
964         if(!JSIsArrayValue(m_context, jsValue))
965                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactWebSiteArray is not array.");
966
967         ContactWebSiteArrayPtr result(new ContactWebSiteArray());
968
969         JSObjectRef jsObject = toJSObjectRef(jsValue);
970     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
971         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
972         result->push_back(toContactWebSite(element));
973     }
974     return result;
975 }
976
977 JSValueRef ContactConverter::toJSValueRef(const ContactAnniversaryPtr &arg)
978 {
979         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactAnniversary::getClassRef(), arg);
980 }
981
982 ContactAnniversaryPtr ContactConverter::toContactAnniversary(const JSValueRef &jsValue)
983 {
984         if(!JSContactAnniversary::isObjectOfClass(m_context, jsValue))
985         {
986                 LoggerE("Not a ContactAnniversary object");
987                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
988         }
989
990         return JSContactAnniversary::getContactAnniversary(m_context, jsValue);
991 }
992
993 JSValueRef ContactConverter::toJSValueRef(const ContactAnniversaryArrayPtr &arg)
994 {
995         int size = arg->size();
996
997         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
998         if (!resultObject)
999                 ThrowMsg(ConversionException, "Can not create array object.");
1000
1001         for(int i = 0; i < size; i++)
1002         {
1003                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1004                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1005                         ThrowMsg(ConversionException, "Can not add object to array.");
1006         }
1007
1008         return static_cast<JSValueRef>(resultObject);
1009 }
1010
1011 ContactAnniversaryArrayPtr ContactConverter::toContactAnniversaryArray(const JSValueRef &jsValue)
1012 {
1013         if(!JSIsArrayValue(m_context, jsValue))
1014                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactAnniversaryArray is not array.");
1015
1016         ContactAnniversaryArrayPtr result(new ContactAnniversaryArray());
1017
1018         JSObjectRef jsObject = toJSObjectRef(jsValue);
1019     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1020         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1021         result->push_back(toContactAnniversary(element));
1022     }
1023     return result;
1024 }
1025
1026 JSValueRef ContactConverter::toJSValueRef(const ContactAddressPtr &arg)
1027 {
1028         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactAddress::getClassRef(), arg);
1029 }
1030
1031 ContactAddressPtr ContactConverter::toContactAddress(const JSValueRef &jsValue)
1032 {
1033         if(!JSContactAddress::isObjectOfClass(m_context, jsValue))
1034         {
1035                 LoggerE("Not a ContactAddress object");
1036                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1037         }
1038
1039         return JSContactAddress::getContactAddress(m_context, jsValue);
1040 }
1041
1042 ContactAddressPtr ContactConverter::toContactAddressFromInit(const JSValueRef &jsValue)
1043 {
1044         // from ContactAddressInit to ContactAddress
1045         Validator validator(m_context);
1046         if (validator.isNullOrUndefined(jsValue)) {
1047                 LoggerE("ContactAddressInit object is null");
1048                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1049         }
1050
1051         if (!JSValueIsObject(m_context, jsValue) || JSIsArrayValue(m_context, jsValue)) {
1052                 LoggerE("ContactAddressInit object is not object");
1053                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1054         }
1055
1056 //      if (!validator.checkArrayKeys(m_allowedContactAddressInit, jsValue)) {
1057 //              LoggerE("invalid properties in ContactAddressInit object");
1058 //              ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1059 //      }
1060
1061         const ScopedJSStringRef countryStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_COUNTRY));
1062         const ScopedJSStringRef regionStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_REGION));
1063         const ScopedJSStringRef cityStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_CITY));
1064         const ScopedJSStringRef streetAddressStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_STREET_ADDRESS));
1065         const ScopedJSStringRef additionalInformationStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_ADDITIONAL_INFORMATION));
1066         const ScopedJSStringRef postalCodeStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_POSTAL_CODE));
1067         const ScopedJSStringRef isDefaultStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_IS_DEFAULT));
1068         const ScopedJSStringRef typesStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_TYPES));
1069         const ScopedJSStringRef labelStr(JSStringCreateWithUTF8CString(CONTACT_ATTRIBUTE_LABEL));
1070
1071         JSObjectRef jsObject = toJSObjectRef(jsValue);
1072
1073         JSValueRef countryData = JSObjectGetProperty(m_context, jsObject, countryStr.get(), NULL);
1074         JSValueRef regionData = JSObjectGetProperty(m_context, jsObject, regionStr.get(), NULL);
1075         JSValueRef cityData = JSObjectGetProperty(m_context, jsObject, cityStr.get(), NULL);
1076         JSValueRef streetAddressData = JSObjectGetProperty(m_context, jsObject, streetAddressStr.get(), NULL);
1077         JSValueRef additionalInformationData = JSObjectGetProperty(m_context, jsObject, additionalInformationStr.get(), NULL);
1078         JSValueRef postalCodeData = JSObjectGetProperty(m_context, jsObject, postalCodeStr.get(), NULL);
1079         JSValueRef isDefaultData = JSObjectGetProperty(m_context, jsObject, isDefaultStr.get(), NULL);
1080         JSValueRef typesData = JSObjectGetProperty(m_context, jsObject, typesStr.get(), NULL);
1081         JSValueRef labelData = JSObjectGetProperty(m_context, jsObject, labelStr.get(), NULL);
1082
1083         std::string country;
1084         std::string region;
1085         std::string city;
1086         std::string streetAddress;
1087         std::string additionalInformation;
1088         std::string postalCode;
1089         bool isDefault;
1090         ContactAddressTypeArrayPtr types;
1091         std::string label;
1092
1093         ContactAddressPtr result(new ContactAddress());
1094         if (!result) {
1095                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Cannot make ContactAddress");
1096         }
1097
1098         if (!validator.isNullOrUndefined(countryData)) {
1099                 if(!JSValueIsString(m_context, countryData))
1100                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "country is not string");
1101                 country = toString(countryData);
1102                 result->setCountry(country);
1103         }
1104
1105         if (!validator.isNullOrUndefined(regionData)) {
1106                 if(!JSValueIsString(m_context, regionData))
1107                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "region is not string");
1108                 region = toString(regionData);
1109                 result->setRegion(region);
1110         }
1111
1112         if (!validator.isNullOrUndefined(cityData)) {
1113                 if(!JSValueIsString(m_context, cityData))
1114                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "city is not string");
1115                 city = toString(cityData);
1116                 result->setCity(city);
1117         }
1118
1119         if (!validator.isNullOrUndefined(streetAddressData)) {
1120                 if(!JSValueIsString(m_context, streetAddressData))
1121                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "streetAddress is not string");
1122                 streetAddress = toString(streetAddressData);
1123                 result->setStreetAddress(streetAddress);
1124         }
1125
1126         if (!validator.isNullOrUndefined(additionalInformationData)) {
1127                 if(!JSValueIsString(m_context, additionalInformationData))
1128                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "additionalInformation is not string");
1129                 additionalInformation = toString(additionalInformationData);
1130                 result->setAdditionalInformation(additionalInformation);
1131         }
1132
1133         if (!validator.isNullOrUndefined(postalCodeData)) {
1134                 if(!JSValueIsString(m_context, postalCodeData))
1135                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "postalCode is not string");
1136                 postalCode = toString(postalCodeData);
1137                 result->setPostalCode(postalCode);
1138         }
1139
1140         if (!validator.isNullOrUndefined(isDefaultData)) {
1141                 isDefault = toBool(isDefaultData);
1142                 result->setIsDefault(isDefault);
1143         }
1144
1145         if (!validator.isNullOrUndefined(typesData)) {
1146                 types = toContactAddressTypeArray(typesData);
1147                 result->setTypes(types);
1148         }else{
1149                 ContactAddressTypeArrayPtr typeArray(new ContactAddressTypeArray());
1150                 typeArray->push_back(CONTACT_ADDRESS_TYPE_HOME);
1151                 result->setTypes(typeArray);
1152         }
1153
1154         if (!validator.isNullOrUndefined(labelData)) {
1155                 if(!JSValueIsString(m_context, labelData))
1156                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "labelData is not string");
1157                 label = toString(labelData);
1158                 result->setLabel(label);
1159         }
1160
1161         return result;
1162 }
1163
1164 JSValueRef ContactConverter::toJSValueRef(const ContactAddressArrayPtr &arg)
1165 {
1166         int size = arg->size();
1167
1168         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1169         if (!resultObject)
1170                 ThrowMsg(ConversionException, "Can not create array object.");
1171
1172         for(int i = 0; i < size; i++)
1173         {
1174                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1175                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1176                         ThrowMsg(ConversionException, "Can not add object to array.");
1177         }
1178
1179         return static_cast<JSValueRef>(resultObject);
1180 }
1181
1182 ContactAddressArrayPtr ContactConverter::toContactAddressArray(const JSValueRef &jsValue)
1183 {
1184         if(!JSIsArrayValue(m_context, jsValue))
1185                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactAddressArray is not array.");
1186
1187         ContactAddressArrayPtr result(new ContactAddressArray());
1188
1189         JSObjectRef jsObject = toJSObjectRef(jsValue);
1190     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1191         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1192         result->push_back(toContactAddress(element));
1193     }
1194     return result;
1195 }
1196
1197 JSValueRef ContactConverter::toJSValueRef(const ContactPhoneNumberPtr &arg)
1198 {
1199         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactPhoneNumber::getClassRef(), arg);
1200 }
1201
1202 ContactPhoneNumberPtr ContactConverter::toContactPhoneNumber(const JSValueRef &jsValue)
1203 {
1204         if(!JSContactPhoneNumber::isObjectOfClass(m_context, jsValue))
1205         {
1206                 LoggerE("Not a ContactPhoneNumber object");
1207                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1208         }
1209
1210         return JSContactPhoneNumber::getContactPhoneNumber(m_context, jsValue);
1211 }
1212
1213 JSValueRef ContactConverter::toJSValueRef(const ContactPhoneNumberArrayPtr &arg)
1214 {
1215         int size = arg->size();
1216
1217         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1218         if (!resultObject)
1219                 ThrowMsg(ConversionException, "Can not create array object.");
1220
1221         for(int i = 0; i < size; i++)
1222         {
1223                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1224                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1225                         ThrowMsg(ConversionException, "Can not add object to array.");
1226         }
1227
1228         return static_cast<JSValueRef>(resultObject);
1229 }
1230
1231 ContactPhoneNumberArrayPtr ContactConverter::toContactPhoneNumberArray(const JSValueRef &jsValue)
1232 {
1233         if(!JSIsArrayValue(m_context, jsValue))
1234                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactPhoneNumberArray is not array.");
1235
1236         ContactPhoneNumberArrayPtr result(new ContactPhoneNumberArray());
1237
1238         JSObjectRef jsObject = toJSObjectRef(jsValue);
1239     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1240         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1241         result->push_back(toContactPhoneNumber(element));
1242     }
1243     return result;
1244 }
1245
1246 JSValueRef ContactConverter::toJSValueRef(const ContactEmailAddressPtr &arg)
1247 {
1248         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSContactEmailAddress::getClassRef(), arg);
1249 }
1250
1251 ContactEmailAddressPtr ContactConverter::toContactEmailAddress(const JSValueRef &jsValue)
1252 {
1253         if(!JSContactEmailAddress::isObjectOfClass(m_context, jsValue))
1254         {
1255                 LoggerE("Not a ContactEmailAddress object");
1256                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
1257         }
1258
1259         return JSContactEmailAddress::getContactEmailAddress(m_context, jsValue);
1260 }
1261
1262 JSValueRef ContactConverter::toJSValueRef(const ContactEmailAddressArrayPtr &arg)
1263 {
1264         int size = arg->size();
1265
1266         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1267         if (!resultObject)
1268                 ThrowMsg(ConversionException, "Can not create array object.");
1269
1270         for(int i = 0; i < size; i++)
1271         {
1272                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1273                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1274                         ThrowMsg(ConversionException, "Can not add object to array.");
1275         }
1276
1277         return static_cast<JSValueRef>(resultObject);
1278 }
1279
1280 ContactEmailAddressArrayPtr ContactConverter::toContactEmailAddressArray(const JSValueRef &jsValue)
1281 {
1282         if(!JSIsArrayValue(m_context, jsValue))
1283                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactEmailAddressArray is not array.");
1284
1285         ContactEmailAddressArrayPtr result(new ContactEmailAddressArray());
1286
1287         JSObjectRef jsObject = toJSObjectRef(jsValue);
1288     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1289         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1290         result->push_back(toContactEmailAddress(element));
1291     }
1292     return result;
1293 }
1294
1295 ContactWebSiteType ContactConverter::toContactWebSiteType(const std::string &arg)
1296 {
1297         std::string argUpper;
1298         std::transform(arg.begin(), arg.end(), std::back_inserter(argUpper), ::toupper);
1299
1300         if (argUpper == STR_WEBSITE_TYPE_HOMEPAGE) {
1301                 return WEBSITE_TYPE_HOMEPAGE;
1302         } else if (argUpper == STR_WEBSITE_TYPE_BLOG) {
1303                 return WEBSITE_TYPE_BLOG;
1304         }
1305
1306         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactWebSiteType : " << arg);
1307         return WEBSITE_TYPE_HOMEPAGE;
1308 }
1309
1310 std::string ContactConverter::toContactWebSiteTypeStr(ContactWebSiteType arg)
1311 {
1312         if (arg == WEBSITE_TYPE_HOMEPAGE) {
1313                 return STR_WEBSITE_TYPE_HOMEPAGE;
1314         } else if (arg == WEBSITE_TYPE_BLOG) {
1315                 return STR_WEBSITE_TYPE_BLOG;
1316         }
1317
1318         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactWebSiteType : " << arg);
1319         return STR_WEBSITE_TYPE_HOMEPAGE;
1320 }
1321
1322 ContactWebSiteType ContactConverter::toContactWebSiteType(const JSValueRef &value)
1323 {
1324         if(!JSValueIsString(m_context, value))
1325                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactWebSiteType is not string.");
1326
1327         return toContactWebSiteType(toString(value));
1328 }
1329
1330 JSValueRef ContactConverter::toJSValueRef(ContactWebSiteType arg)
1331 {
1332         return toJSValueRef(toContactWebSiteTypeStr(arg));
1333 }
1334
1335 ContactOrganizationType ContactConverter::toContactOrganizationType(const std::string &arg)
1336 {
1337         std::string argUpper;
1338         std::transform(arg.begin(), arg.end(), std::back_inserter(argUpper), ::toupper);
1339
1340         if (argUpper == STR_ORGANIZATION_TYPE_WORK) {
1341                 return ORGANIZATION_TYPE_WORK;
1342         } else if (argUpper == STR_ORGANIZATION_TYPE_OTHER) {
1343                 return ORGANIZATION_TYPE_OTHER;
1344         }
1345
1346         return ORGANIZATION_TYPE_WORK;
1347 }
1348
1349 std::string ContactConverter::toContactOrganizationTypeStr(ContactOrganizationType arg)
1350 {
1351         if (arg == ORGANIZATION_TYPE_WORK) {
1352                 return STR_ORGANIZATION_TYPE_WORK;
1353         } else if (arg == ORGANIZATION_TYPE_OTHER) {
1354                 return STR_ORGANIZATION_TYPE_OTHER;
1355         }
1356
1357         return STR_ORGANIZATION_TYPE_WORK;
1358 }
1359
1360 ContactOrganizationType ContactConverter::toContactOrganizationType(const JSValueRef &value)
1361 {
1362         if(!JSValueIsString(m_context, value))
1363                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactWebSiteType is not string.");
1364
1365         return toContactOrganizationType(toString(value));
1366 }
1367
1368 JSValueRef ContactConverter::toJSValueRef(ContactOrganizationType arg)
1369 {
1370         return toJSValueRef(toContactOrganizationTypeStr(arg));
1371 }
1372
1373 ContactAddressType ContactConverter::toContactAddressType(const std::string &arg)
1374 {
1375         std::string argUpper;
1376         std::transform(arg.begin(), arg.end(), std::back_inserter(argUpper), ::toupper);
1377
1378         if (argUpper == STR_CONTACT_ADDRESS_TYPE_WORK) {
1379                 return CONTACT_ADDRESS_TYPE_WORK;
1380         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_PREF) {
1381                 return CONTACT_ADDRESS_TYPE_PREF;
1382         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_HOME) {
1383                 return CONTACT_ADDRESS_TYPE_HOME;
1384         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_DOM) {
1385                 return CONTACT_ADDRESS_TYPE_DOM;
1386         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_INTL) {
1387                 return CONTACT_ADDRESS_TYPE_INTL;
1388         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_POSTAL) {
1389                 return CONTACT_ADDRESS_TYPE_POSTAL;
1390         } else if (argUpper == STR_CONTACT_ADDRESS_TYPE_PARCEL) {
1391                 return CONTACT_ADDRESS_TYPE_PARCEL;
1392         }
1393
1394         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactAddressType : " << arg);
1395         return CONTACT_ADDRESS_TYPE_HOME;
1396 }
1397
1398 std::string ContactConverter::toContactAddressTypeStr(ContactAddressType arg)
1399 {
1400         if (arg == CONTACT_ADDRESS_TYPE_WORK) {
1401                 return STR_CONTACT_ADDRESS_TYPE_WORK;
1402         } else if (arg == CONTACT_ADDRESS_TYPE_PREF) {
1403                 return STR_CONTACT_ADDRESS_TYPE_PREF;
1404         } else if (arg == CONTACT_ADDRESS_TYPE_HOME) {
1405                 return STR_CONTACT_ADDRESS_TYPE_HOME;
1406         } else if (arg == CONTACT_ADDRESS_TYPE_DOM) {
1407                 return STR_CONTACT_ADDRESS_TYPE_DOM;
1408         } else if (arg == CONTACT_ADDRESS_TYPE_INTL) {
1409                 return STR_CONTACT_ADDRESS_TYPE_INTL;
1410         } else if (arg == CONTACT_ADDRESS_TYPE_POSTAL) {
1411                 return STR_CONTACT_ADDRESS_TYPE_POSTAL;
1412         } else if (arg == CONTACT_ADDRESS_TYPE_PARCEL) {
1413                 return STR_CONTACT_ADDRESS_TYPE_PARCEL;
1414         }
1415
1416         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactAddressType : " << arg);
1417         return STR_CONTACT_ADDRESS_TYPE_HOME;
1418 }
1419
1420 ContactAddressType ContactConverter::toContactAddressType(const JSValueRef &value)
1421 {
1422         if(!JSValueIsString(m_context, value))
1423                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactAddressType is not string.");
1424
1425         return toContactAddressType(toString(value));
1426 }
1427
1428 JSValueRef ContactConverter::toJSValueRef(ContactAddressType arg)
1429 {
1430         return toJSValueRef(toContactAddressTypeStr(arg));
1431 }
1432
1433 JSValueRef ContactConverter::toJSValueRef(const ContactAddressTypeArrayPtr &arg)
1434 {
1435         int size = arg->size();
1436
1437         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1438         if (!resultObject)
1439                 ThrowMsg(ConversionException, "Can not create array object.");
1440
1441         for(int i = 0; i < size; i++)
1442         {
1443                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1444                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1445                         ThrowMsg(ConversionException, "Can not add object to array.");
1446         }
1447
1448         if(size == 0){
1449                 JSValueRef jsvalue = toJSValueRef(CONTACT_ADDRESS_TYPE_HOME);
1450                 if (!JSSetArrayElement(m_context, resultObject, 0, jsvalue))
1451                         ThrowMsg(ConversionException, "Can not add object to array.");
1452         }
1453
1454         return static_cast<JSValueRef>(resultObject);
1455 }
1456
1457 ContactAddressTypeArrayPtr ContactConverter::toContactAddressTypeArray(const JSValueRef &jsValue)
1458 {
1459         if(!JSIsArrayValue(m_context, jsValue))
1460                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactAddressTypeArray is not array.");
1461
1462         ContactAddressTypeArrayPtr result(new ContactAddressTypeArray());
1463
1464         JSObjectRef jsObject = toJSObjectRef(jsValue);
1465     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1466         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1467         result->push_back(toContactAddressType(element));
1468     }
1469     return result;
1470 }
1471
1472 ContactPhoneNumberType ContactConverter::toContactPhoneNumberType(const std::string &arg)
1473 {
1474         std::string argUpper;
1475         std::transform(arg.begin(), arg.end(), std::back_inserter(argUpper), ::toupper);
1476
1477         if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_WORK) {
1478                 return CONTACT_PHONE_NUMBER_TYPE_WORK;
1479         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_PREF) {
1480                 return CONTACT_PHONE_NUMBER_TYPE_PREF;
1481         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_HOME) {
1482                 return CONTACT_PHONE_NUMBER_TYPE_HOME;
1483         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_VOICE) {
1484                 return CONTACT_PHONE_NUMBER_TYPE_VOICE;
1485         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_FAX) {
1486                 return CONTACT_PHONE_NUMBER_TYPE_FAX;
1487         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_MSG) {
1488                 return CONTACT_PHONE_NUMBER_TYPE_MSG;
1489         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_CELL) {
1490                 return CONTACT_PHONE_NUMBER_TYPE_CELL;
1491         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_PAGER) {
1492                 return CONTACT_PHONE_NUMBER_TYPE_PAGER;
1493         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_BBS) {
1494                 return CONTACT_PHONE_NUMBER_TYPE_BBS;
1495         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_MODEM) {
1496                 return CONTACT_PHONE_NUMBER_TYPE_MODEM;
1497         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_CAR) {
1498                 return CONTACT_PHONE_NUMBER_TYPE_CAR;
1499         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_ISDN) {
1500                 return CONTACT_PHONE_NUMBER_TYPE_ISDN;
1501         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_VIDEO) {
1502                 return CONTACT_PHONE_NUMBER_TYPE_VIDEO;
1503         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_PCS) {
1504                 return CONTACT_PHONE_NUMBER_TYPE_PCS;
1505         } else if (argUpper == STR_CONTACT_PHONE_NUMBER_TYPE_ASSISTANT) {
1506                 return CONTACT_PHONE_NUMBER_TYPE_ASSISTANT;
1507         }
1508
1509         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactPhoneNumverType : " << arg);
1510         return CONTACT_PHONE_NUMBER_TYPE_VOICE;
1511 }
1512
1513 std::string ContactConverter::toContactPhoneNumberTypeStr(ContactPhoneNumberType arg)
1514 {
1515         if (arg == CONTACT_PHONE_NUMBER_TYPE_WORK) {
1516                 return STR_CONTACT_PHONE_NUMBER_TYPE_WORK;
1517         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_PREF) {
1518                 return STR_CONTACT_PHONE_NUMBER_TYPE_PREF;
1519         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_HOME) {
1520                 return STR_CONTACT_PHONE_NUMBER_TYPE_HOME;
1521         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_VOICE) {
1522                 return STR_CONTACT_PHONE_NUMBER_TYPE_VOICE;
1523         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_FAX) {
1524                 return STR_CONTACT_PHONE_NUMBER_TYPE_FAX;
1525         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_MSG) {
1526                 return STR_CONTACT_PHONE_NUMBER_TYPE_MSG;
1527         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_CELL) {
1528                 return STR_CONTACT_PHONE_NUMBER_TYPE_CELL;
1529         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_PAGER) {
1530                 return STR_CONTACT_PHONE_NUMBER_TYPE_PAGER;
1531         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_BBS) {
1532                 return STR_CONTACT_PHONE_NUMBER_TYPE_BBS;
1533         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_MODEM) {
1534                 return STR_CONTACT_PHONE_NUMBER_TYPE_MODEM;
1535         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_CAR) {
1536                 return STR_CONTACT_PHONE_NUMBER_TYPE_CAR;
1537         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_ISDN) {
1538                 return STR_CONTACT_PHONE_NUMBER_TYPE_ISDN;
1539         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_VIDEO) {
1540                 return STR_CONTACT_PHONE_NUMBER_TYPE_VIDEO;
1541         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_PCS) {
1542                 return STR_CONTACT_PHONE_NUMBER_TYPE_PCS;
1543         } else if (arg == CONTACT_PHONE_NUMBER_TYPE_ASSISTANT) {
1544                 return STR_CONTACT_PHONE_NUMBER_TYPE_ASSISTANT;
1545         }
1546
1547         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactPhoneNumberType : " << arg);
1548         return STR_CONTACT_PHONE_NUMBER_TYPE_VOICE;
1549 }
1550
1551 ContactPhoneNumberType ContactConverter::toContactPhoneNumberType(const JSValueRef &value)
1552 {
1553         if(!JSValueIsString(m_context, value))
1554                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactPhoneNumberType is not string.");
1555
1556         return toContactPhoneNumberType(toString(value));
1557 }
1558
1559 JSValueRef ContactConverter::toJSValueRef(ContactPhoneNumberType arg)
1560 {
1561         return toJSValueRef(toContactPhoneNumberTypeStr(arg));
1562 }
1563
1564 JSValueRef ContactConverter::toJSValueRef(const ContactPhoneNumberTypeArrayPtr &arg)
1565 {
1566         int size = arg->size();
1567
1568         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1569         if (!resultObject)
1570                 ThrowMsg(ConversionException, "Can not create array object.");
1571
1572         for(int i = 0; i < size; i++)
1573         {
1574                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1575                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1576                         ThrowMsg(ConversionException, "Can not add object to array.");
1577         }
1578
1579         return static_cast<JSValueRef>(resultObject);
1580 }
1581
1582 ContactPhoneNumberTypeArrayPtr ContactConverter::toContactPhoneNumberTypeArray(const JSValueRef &jsValue)
1583 {
1584         ContactPhoneNumberTypeArrayPtr result(new ContactPhoneNumberTypeArray());
1585
1586         if(!JSIsArrayValue(m_context, jsValue) || JSValueIsNull(m_context, jsValue))
1587                  return result;
1588
1589         bool duplicate = false;
1590
1591         JSObjectRef jsObject = toJSObjectRef(jsValue);
1592     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1593         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1594         Try {
1595                         ContactPhoneNumberType tempType = toContactPhoneNumberType(element);
1596                         duplicate = false;
1597                         for(unsigned int j=0; j<result->size(); j++){
1598                                 if(result->at(j) == tempType)
1599                                         duplicate = true;
1600                         }
1601                         if(!duplicate)
1602                         result->push_back(toContactPhoneNumberType(element));
1603         } Catch (Exception) {
1604                 // nothing
1605         }
1606     }
1607     return result;
1608 }
1609
1610 ContactEmailAddressType ContactConverter::toContactEmailAddressType(const std::string &arg)
1611 {
1612         std::string argUpper;
1613         std::transform(arg.begin(), arg.end(), std::back_inserter(argUpper), ::toupper);
1614
1615         if (argUpper == STR_CONTACT_EMAIL_TYPE_WORK) {
1616                 return CONTACT_EMAIL_TYPE_WORK;
1617         } else if (argUpper == STR_CONTACT_EMAIL_TYPE_PREF) {
1618                 return CONTACT_EMAIL_TYPE_PREF;
1619         } else if (argUpper == STR_CONTACT_EMAIL_TYPE_HOME) {
1620                 return CONTACT_EMAIL_TYPE_HOME;
1621         } else if (argUpper == STR_CONTACT_EMAIL_TYPE_MOBILE) {
1622                 return CONTACT_EMAIL_TYPE_MOBILE;
1623         }
1624
1625         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactEmailAddressType : " << arg);
1626         return CONTACT_EMAIL_TYPE_WORK;
1627 }
1628
1629 std::string ContactConverter::toContactEmailAddressTypeStr(ContactEmailAddressType arg)
1630 {
1631         if (arg == CONTACT_EMAIL_TYPE_WORK) {
1632                 return STR_CONTACT_EMAIL_TYPE_WORK;
1633         } else if (arg == CONTACT_EMAIL_TYPE_PREF) {
1634                 return STR_CONTACT_EMAIL_TYPE_PREF;
1635         } else if (arg == CONTACT_EMAIL_TYPE_HOME) {
1636                 return STR_CONTACT_EMAIL_TYPE_HOME;
1637         } else if (arg == CONTACT_EMAIL_TYPE_MOBILE) {
1638                 return STR_CONTACT_EMAIL_TYPE_MOBILE;
1639         }
1640
1641         //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Invalid ContactEmailAddressType : " << arg);
1642         return STR_CONTACT_EMAIL_TYPE_WORK;
1643 }
1644
1645 ContactEmailAddressType ContactConverter::toContactEmailAddressType(const JSValueRef &value)
1646 {
1647         if(!JSValueIsString(m_context, value))
1648                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "ContactEmailAddressType is not string.");
1649
1650         return toContactEmailAddressType(toString(value));
1651 }
1652
1653 JSValueRef ContactConverter::toJSValueRef(ContactEmailAddressType arg)
1654 {
1655         return toJSValueRef(toContactEmailAddressTypeStr(arg));
1656 }
1657
1658 JSValueRef ContactConverter::toJSValueRef(const ContactEmailAddressTypeArrayPtr &arg)
1659 {
1660         int size = arg->size();
1661
1662         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1663         if (!resultObject)
1664                 ThrowMsg(ConversionException, "Can not create array object.");
1665
1666         for(int i = 0; i < size; i++)
1667         {
1668                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1669                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1670                         ThrowMsg(ConversionException, "Can not add object to array.");
1671         }
1672
1673         return static_cast<JSValueRef>(resultObject);
1674 }
1675
1676 ContactEmailAddressTypeArrayPtr ContactConverter::toContactEmailAddressTypeArray(const JSValueRef &jsValue)
1677 {
1678         ContactEmailAddressTypeArrayPtr result(new ContactEmailAddressTypeArray());
1679
1680         if(!JSIsArrayValue(m_context, jsValue) || JSValueIsNull(m_context, jsValue))
1681                  return result;
1682
1683         bool duplicate = false;
1684
1685         JSObjectRef jsObject = toJSObjectRef(jsValue);
1686     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1687         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1688         Try {
1689                         ContactEmailAddressType tempType = toContactEmailAddressType(element);
1690                         duplicate = false;
1691                         for(unsigned int j=0; j<result->size(); j++){
1692                                 if(result->at(j) == tempType)
1693                                         duplicate = true;
1694                         }
1695                         if(!duplicate)
1696                         result->push_back(toContactEmailAddressType(element));
1697
1698         } Catch (Exception) {
1699                 // nothing
1700         }
1701     }
1702     return result;
1703 }
1704
1705 StringArrayPtr ContactConverter::toStringArray(const JSValueRef &jsValue)
1706 {
1707         if(!JSIsArrayValue(m_context, jsValue))
1708                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "StringArray is not array.");
1709
1710         StringArrayPtr result = StringArrayPtr(new StringArray());
1711         JSObjectRef jsObject = toJSObjectRef(jsValue);
1712     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
1713         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
1714         result->push_back(toString(element));
1715     }
1716     return result;
1717 }
1718
1719 JSValueRef ContactConverter::toJSValueRef(const StringArrayPtr &arg)
1720 {
1721         int size = arg->size();
1722
1723         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
1724         if (!resultObject)
1725                 ThrowMsg(ConversionException, "Can not create array object.");
1726
1727         for(int i = 0; i < size; i++)
1728         {
1729                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
1730                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
1731                         ThrowMsg(ConversionException, "Can not add object to array.");
1732         }
1733
1734         return static_cast<JSValueRef>(resultObject);
1735 }
1736
1737 bool ContactConverter::initializeAllowedProperties()
1738 {
1739         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_NAME);
1740         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_ACCOUNT_ID);
1741         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_ADDRESSES);
1742         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_PHOTO_URI );
1743         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_PHONE_NUMBERS);
1744         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_EMAILS);
1745         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_BIRTHDAY);
1746         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_ANNIVERSARIES);
1747         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_ORGANIZATIONS);
1748         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_NOTES);
1749         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_URLS);
1750         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_RINGTONE_URI);
1751         m_allowedContactInit.push_back(CONTACT_ATTRIBUTE_GROUP_IDS);
1752
1753         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_PREFIX);
1754         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_FIRST_NAME);
1755         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_MIDDLE_NAME);
1756         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_LAST_NAME);
1757         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_NICKNAMES);
1758         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_PHONETIC_FIRST_NAME);
1759         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_PHONETIC_MIDDLE_NAME);
1760         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_PHONETIC_LAST_NAME);
1761         m_allowedContactNameInit.push_back(CONTACT_ATTRIBUTE_DISPLAY_NAME);
1762
1763         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_NAME);
1764         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_DEPARTMENT);
1765         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_OFFICE);
1766         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_TITLE);
1767         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_ROLE);
1768         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_LOGO_URI);
1769         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_ASSISTANT_NAME);
1770         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_LOCATION);
1771         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_DESCRIPTION);
1772         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_PHONETIC_NAME);
1773         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_LABEL);
1774         m_allowedContactOrganizationInit.push_back(CONTACT_ATTRIBUTE_TYPE);
1775
1776         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_COUNTRY);
1777         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_REGION);
1778         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_CITY);
1779         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_STREET_ADDRESS);
1780         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_ADDITIONAL_INFORMATION);
1781         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_POSTAL_CODE);
1782         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_TYPES);
1783         m_allowedContactAddressInit.push_back(CONTACT_ATTRIBUTE_LABEL);
1784
1785         return true;
1786 }
1787
1788 } // Contact
1789 } // DeviceAPI