e0dcb9c2411d826fe37fbd1cf23281b837846d87
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSPerson.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        JSPerson.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSPerson class
23  */
24
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <SecurityExceptions.h>
28 #include "ContactFactory.h"
29 #include "plugin_config.h"
30 #include "ContactConverter.h"
31 #include "JSPerson.h"
32 #include <ArgumentValidator.h>
33 #include <JSWebAPIErrorFactory.h>
34 #include <TimeTracer.h>
35 #include <Logger.h>
36 #include <Export.h>
37
38 #define FILTER_CLASS_NAME               "Person"
39
40 #define CONTACT_ATTR_ID                 "id"
41 #define CONTACT_ATTR_DISPLAY_NAME       "displayName"
42 #define CONTACT_ATTR_CONTACT_COUNT      "contactCount"
43 #define CONTACT_ATTR_HAS_PHONE_NUMBER   "hasPhoneNumber"
44 #define CONTACT_ATTR_HAS_EMAIL          "hasEmail"
45 #define CONTACT_ATTR_IS_FAVORITE        "isFavorite"
46 #define CONTACT_ATTR_PHOTO_URI              "photoURI"
47 #define CONTACT_ATTR_RINGTONE_URI       "ringtoneURI"
48 #define CONTACT_ATTR_DISPLAY_CONTACT_ID "displayContactId"
49 #define CONTACT_FUNC_LINK               "link"
50 #define CONTACT_FUNC_UNLINK             "unlink"
51
52 namespace DeviceAPI {
53 namespace Contact {
54
55 using namespace DeviceAPI::Common;
56 using namespace WrtDeviceApis::Commons;
57 using namespace WrtDeviceApis::CommonsJavaScript;
58
59 JSClassDefinition JSPerson::m_classInfo =
60 {
61         0,
62         kJSClassAttributeNone,
63         FILTER_CLASS_NAME,
64         NULL,
65         m_property,
66         m_functions,
67         Initialize,
68         Finalize,
69         NULL, //hasProperty,
70         NULL, //GetProperty,
71         NULL, //SetProperty,
72         NULL, //DeleteProperty,
73         NULL, //getPropertyNames,
74         NULL, //CallAsFunction,
75         NULL, //CallAsConstructor,
76         hasInstance, //HasInstance,
77         NULL, //ConvertToType,
78 };
79
80 JSStaticValue JSPerson::m_property[] = {
81         { CONTACT_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
82         { CONTACT_ATTR_DISPLAY_NAME, getDisplayName, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
83         { CONTACT_ATTR_CONTACT_COUNT, getContactCount, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
84         { CONTACT_ATTR_HAS_PHONE_NUMBER, getHasPhoneNumber, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
85         { CONTACT_ATTR_HAS_EMAIL, getHasEmail, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
86         { CONTACT_ATTR_IS_FAVORITE, getIsFavorite, setIsFavorite, kJSPropertyAttributeNone },
87         { CONTACT_ATTR_PHOTO_URI, getPhotoURI, setPhotoURI, kJSPropertyAttributeNone },
88         { CONTACT_ATTR_RINGTONE_URI, getRingtoneURI, setRingtoneURI, kJSPropertyAttributeNone },
89         { CONTACT_ATTR_DISPLAY_CONTACT_ID, getDisplayContactId, setDisplayContactId, kJSPropertyAttributeNone },
90         { 0, 0, 0, 0 }
91 };
92
93 JSStaticFunction JSPerson::m_functions[] =
94 {
95         { CONTACT_FUNC_LINK, link, kJSPropertyAttributeNone },
96         { CONTACT_FUNC_UNLINK, unlink, kJSPropertyAttributeNone },
97         { 0, 0, 0 }
98 };
99
100 JSClassRef JSPerson::m_classRef = JSClassCreate(&m_classInfo);
101
102 JSClassRef DLL_EXPORT JSPerson::getClassRef() {
103         if (!m_classRef) {
104                 m_classRef = JSClassCreate(&m_classInfo);
105         }
106         return m_classRef;
107 }
108
109 bool JSPerson::isObjectOfClass(JSContextRef context, JSValueRef value)
110 {
111         return JSValueIsObjectOfClass(context, value, getClassRef());
112 }
113
114 PersonPtr JSPerson::getPerson(JSContextRef context, JSValueRef value)
115 {
116         if (!isObjectOfClass(context, value)) {
117                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
118         }
119         JSObjectRef object = JSValueToObject(context, value, NULL);
120         if (!object) {
121                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
122         }
123         JSPersonPriv *priv = static_cast<JSPersonPriv*>(JSObjectGetPrivate(object));
124         if (!priv) {
125                 Throw(WrtDeviceApis::Commons::NullPointerException);
126         }
127         return priv->getObject();
128 }
129
130 void JSPerson::Initialize(JSContextRef context, JSObjectRef object)
131 {
132         if (!JSObjectGetPrivate(object))
133         {
134                 PersonPtr person = ContactFactory::getInstance().createPerson();
135                 JSPersonPriv *priv = new JSPersonPriv(context, PersonPtr(person));
136                 if (!JSObjectSetPrivate(object, priv)) {
137                         delete priv;
138                 }
139         }
140 }
141
142 void JSPerson::Finalize(JSObjectRef object)
143 {
144         JSPersonPriv *priv = static_cast<JSPersonPriv*>(JSObjectGetPrivate(object));
145
146         if (priv != NULL)
147                 delete (priv);
148 }
149
150 JSObjectRef JSPerson::createJSObject(JSContextRef context, PersonPtr person)
151 {
152         JSPersonPriv *priv = new JSPersonPriv(context, person);
153         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
154         if (NULL == jsObjectRef) {
155                 LoggerE("object creation error");
156                 return NULL;
157         }
158         return jsObjectRef;
159 }
160
161 PersonPtr JSPerson::getPrivData(JSObjectRef object)
162 {
163         JSPersonPriv *priv = static_cast<JSPersonPriv*>(JSObjectGetPrivate(object));
164         if (!priv) {
165                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
166         }
167         PersonPtr result = priv->getObject();
168         if (!result) {
169                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null.");
170         }
171         return result;
172 }
173
174 bool JSPerson::hasInstance(JSContextRef context,
175                 JSObjectRef constructor,
176                 JSValueRef possibleInstance,
177                 JSValueRef* exception)
178 {
179         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
180 }
181
182 JSValueRef JSPerson::getId(JSContextRef context,
183                 JSObjectRef object,
184                 JSStringRef propertyName,
185                 JSValueRef* exception)
186 {
187         Try
188         {
189                 ContactConverterFactory::ConverterType converter =
190                                 ContactConverterFactory::getConverter(context);
191                 PersonPtr person = getPrivData(object);
192                 if(!person->getIdIsSet())
193                         return JSValueMakeNull(context);
194                 else
195                         return converter->toJSValueRef(person->getId());
196         }
197         Catch(WrtDeviceApis::Commons::Exception)
198         {
199                 LoggerW("trying to get incorrect value");
200         }
201         return JSValueMakeUndefined(context);
202 }
203
204 JSValueRef JSPerson::getDisplayName(JSContextRef context,
205                 JSObjectRef object,
206                 JSStringRef propertyName,
207                 JSValueRef* exception)
208 {
209         Try
210         {
211                 ContactConverterFactory::ConverterType converter =
212                                 ContactConverterFactory::getConverter(context);
213                 PersonPtr person = getPrivData(object);
214                 if(!person->getIdIsSet())
215                         return JSValueMakeNull(context);
216                 else
217                         return converter->toJSValueRef(person->getDisplayName());
218         }
219         Catch(WrtDeviceApis::Commons::Exception)
220         {
221                 LoggerW("trying to get incorrect value");
222         }
223         return JSValueMakeUndefined(context);
224 }
225
226 JSValueRef JSPerson::getContactCount(JSContextRef context,
227                 JSObjectRef object,
228                 JSStringRef propertyName,
229                 JSValueRef* exception)
230 {
231         Try
232         {
233                 ContactConverterFactory::ConverterType converter =
234                                 ContactConverterFactory::getConverter(context);
235                 PersonPtr person = getPrivData(object);
236                 if(!person->getIdIsSet())
237                         return JSValueMakeNull(context);
238                 else
239                         return converter->toJSValueRefLong(person->getContactCount());
240         }
241         Catch(WrtDeviceApis::Commons::Exception)
242         {
243                 LoggerW("trying to get incorrect value");
244         }
245         return JSValueMakeUndefined(context);
246 }
247
248 JSValueRef JSPerson::getHasPhoneNumber(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyName,
251                 JSValueRef* exception)
252 {
253         Try
254         {
255                 ContactConverterFactory::ConverterType converter =
256                                 ContactConverterFactory::getConverter(context);
257                 PersonPtr person = getPrivData(object);
258                 if(!person->getIdIsSet())
259                         return JSValueMakeNull(context);
260                 else
261                         return converter->toJSValueRef(person->getHasPhoneNumber());
262         }
263         Catch(WrtDeviceApis::Commons::Exception)
264         {
265                 LoggerW("trying to get incorrect value");
266         }
267         return JSValueMakeUndefined(context);
268 }
269
270 JSValueRef JSPerson::getHasEmail(JSContextRef context,
271                 JSObjectRef object,
272                 JSStringRef propertyName,
273                 JSValueRef* exception)
274 {
275         Try
276         {
277                 ContactConverterFactory::ConverterType converter =
278                                 ContactConverterFactory::getConverter(context);
279                 PersonPtr person = getPrivData(object);
280                 if(!person->getIdIsSet())
281                         return JSValueMakeNull(context);
282                 else
283                         return converter->toJSValueRef(person->getHasEmail());
284         }
285         Catch(WrtDeviceApis::Commons::Exception)
286         {
287                 LoggerW("trying to get incorrect value");
288         }
289
290         return JSValueMakeUndefined(context);
291 }
292
293 JSValueRef JSPerson::getIsFavorite(JSContextRef context,
294                 JSObjectRef object,
295                 JSStringRef propertyName,
296                 JSValueRef* exception)
297 {
298         Try
299         {
300                 ContactConverterFactory::ConverterType converter =
301                                 ContactConverterFactory::getConverter(context);
302                 PersonPtr person = getPrivData(object);
303                 return converter->toJSValueRef(person->getIsFavorite());
304         }
305         Catch(WrtDeviceApis::Commons::Exception)
306         {
307                 LoggerW("trying to get incorrect value");
308         }
309
310         return JSValueMakeUndefined(context);
311 }
312
313 bool JSPerson::setIsFavorite(JSContextRef context,
314                 JSObjectRef object,
315                 JSStringRef propertyName,
316                 JSValueRef value,
317                 JSValueRef* exception)
318 {
319         Try
320         {
321                 PersonPtr person = getPrivData(object);
322                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
323                 person->setIsFavorite(converter->toBool(value));
324         }
325         Catch(WrtDeviceApis::Commons::Exception)
326         {
327                 LoggerW("trying to set incorrect value");
328                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
329         }
330
331         return true;
332 }
333
334 JSValueRef JSPerson::getPhotoURI(JSContextRef context,
335                 JSObjectRef object,
336                 JSStringRef propertyName,
337                 JSValueRef* exception)
338 {
339         Try
340         {
341                 ContactConverterFactory::ConverterType converter =
342                                 ContactConverterFactory::getConverter(context);
343                 PersonPtr person = getPrivData(object);
344                 if(!person->getPhotoURIIsSet())
345                         return JSValueMakeNull(context);
346                 else
347                         return converter->toJSValueRef(person->getPhotoURI());
348         }
349         Catch(WrtDeviceApis::Commons::Exception)
350         {
351                 LoggerW("trying to get incorrect value");
352         }
353
354         return JSValueMakeUndefined(context);
355 }
356
357 bool JSPerson::setPhotoURI(JSContextRef context,
358                 JSObjectRef object,
359                 JSStringRef propertyName,
360                 JSValueRef value,
361                 JSValueRef* exception)
362 {
363         Try
364         {
365                 PersonPtr person = getPrivData(object);
366                 ContactConverterFactory::ConverterType converter =
367                                 ContactConverterFactory::getConverter(context);
368                 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
369                 if(validator->isNullOrUndefined(value))
370                         person->unsetPhotoURI();
371                 else
372                         person->setPhotoURI(converter->toString(value));
373         }
374         Catch(WrtDeviceApis::Commons::Exception)
375         {
376                 LoggerW("trying to set incorrect value");
377                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
378         }
379
380         return true;
381 }
382
383 JSValueRef JSPerson::getRingtoneURI(JSContextRef context,
384                 JSObjectRef object,
385                 JSStringRef propertyName,
386                 JSValueRef* exception)
387 {
388         Try
389         {
390                 ContactConverterFactory::ConverterType converter =
391                                 ContactConverterFactory::getConverter(context);
392                 PersonPtr person = getPrivData(object);
393                 if(!person->getRingtoneURIIsSet())
394                         return JSValueMakeNull(context);
395                 else
396                         return converter->toJSValueRef(person->getRingtoneURI());
397         }
398         Catch(WrtDeviceApis::Commons::Exception)
399         {
400                 LoggerW("trying to get incorrect value");
401         }
402
403         return JSValueMakeUndefined(context);
404 }
405
406 bool JSPerson::setRingtoneURI(JSContextRef context,
407                 JSObjectRef object,
408                 JSStringRef propertyName,
409                 JSValueRef value,
410                 JSValueRef* exception)
411 {
412         Try
413         {
414                 PersonPtr person = getPrivData(object);
415                 ContactConverterFactory::ConverterType converter =
416                                 ContactConverterFactory::getConverter(context);
417                 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
418                 if(validator->isNullOrUndefined(value))
419                         person->unsetRingtoneURI();
420                 else
421                         person->setRingtoneURI(converter->toString(value));
422         }
423         Catch(WrtDeviceApis::Commons::Exception)
424         {
425                 LoggerW("trying to set incorrect value");
426                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
427         }
428
429         return true;
430 }
431
432 JSValueRef JSPerson::getDisplayContactId(JSContextRef context,
433                 JSObjectRef object,
434                 JSStringRef propertyName,
435                 JSValueRef* exception)
436 {
437         Try
438         {
439                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
440                 PersonPtr person = getPrivData(object);
441                 if(!person->getDisplayContactIdIsSet())
442                         return JSValueMakeNull(context);
443                 else
444                         return converter->toJSValueRef(person->getDisplayContactId());
445         }
446         Catch(WrtDeviceApis::Commons::Exception)
447         {
448                 LoggerW("trying to get incorrect value");
449         }
450
451         return JSValueMakeUndefined(context);
452 }
453
454 bool JSPerson::setDisplayContactId(JSContextRef context,
455                 JSObjectRef object,
456                 JSStringRef propertyName,
457                 JSValueRef value,
458                 JSValueRef* exception)
459 {
460         Try
461         {
462                 PersonPtr person = getPrivData(object);
463                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
464                 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
465                 if(validator->isNullOrUndefined(value))
466                         person->unsetDisplayContactId();
467                 else
468                         person->setDisplayContactId(converter->toString(value));
469         }
470         Catch(WrtDeviceApis::Commons::Exception)
471         {
472                 LoggerW("trying to set incorrect value");
473                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
474         }
475
476         return true;
477 }
478
479 JSValueRef JSPerson::link(JSContextRef context,
480                 JSObjectRef object,
481                 JSObjectRef thisObject,
482                 size_t argumentCount,
483                 const JSValueRef arguments[],
484                 JSValueRef* exception)
485 {
486         LoggerD("entered");
487         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
488         PersonPtr person(NULL);
489
490         Try
491         {
492                 person = getPrivData(thisObject);
493                 if (person == NULL)
494                         ThrowMsg(InvalidArgumentException, "No private object.");
495         }
496         Catch(Exception)
497         {
498                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
499                 return JSWebAPIErrorFactory::postException(context, exception,
500                                 JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
501         }
502
503         AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_PERSON_LINK);
504         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
505
506         ArgumentValidator validator(context, argumentCount, arguments);
507         std::string personId;
508         try     {
509                 personId = validator.toString(0, false);
510         } catch (const TypeMismatchException& err ) {
511                 return JSWebAPIErrorFactory::postException(context, exception, err);
512         } catch(const BasePlatformException& err) {
513                 return JSWebAPIErrorFactory::postException(context, exception, err);
514         } catch(const ConversionException& err) {
515                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
516         } catch(const NullPointerException& err) {
517                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
518         }
519
520         EventPersonLinkPtr dplEvent(new EventPersonLink());
521
522         dplEvent->setPersonId(personId);
523     dplEvent->setForSynchronousCall();
524
525         Try
526         {
527                 person->link(dplEvent);
528         }
529         Catch(ConversionException)
530         {
531                 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
532                 return JSWebAPIErrorFactory::postException(context, exception,
533                                 JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be 'PersonId'");
534         }
535         Catch(Exception)
536         {
537                 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
538                 return JSWebAPIErrorFactory::postException(context, exception,
539                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
540         }
541
542         if (!dplEvent->getResult())
543         {
544                 std::stringstream oss;
545                 switch (dplEvent->getExceptionCode())
546                 {
547                 case ExceptionCodes::NotFoundException:
548                         oss << "Person id (" << personId << ") is not exists.";
549                         return JSWebAPIErrorFactory::postException(context, exception,
550                                         JSWebAPIErrorFactory::INVALID_VALUES_ERROR, oss.str());
551                         break;
552                 case ExceptionCodes::InvalidArgumentException:
553                         oss << "Person id (" << personId << ") is wrong.";
554                         return JSWebAPIErrorFactory::postException(context, exception,
555                                         JSWebAPIErrorFactory::INVALID_VALUES_ERROR, oss.str());
556                         break;
557                 case ExceptionCodes::PlatformException:
558                         return JSWebAPIErrorFactory::postException(context, exception,
559                                         JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
560                         break;
561                 default:
562                         return JSWebAPIErrorFactory::postException(context, exception,
563                                         JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
564                         break;
565                 }
566         }
567         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
568         return JSValueMakeUndefined(context);
569 }
570
571 JSValueRef JSPerson::unlink(JSContextRef context,
572                 JSObjectRef object,
573                 JSObjectRef thisObject,
574                 size_t argumentCount,
575                 const JSValueRef arguments[],
576                 JSValueRef* exception)
577 {
578         LoggerD("entered");
579         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
580         PersonPtr person(NULL);
581
582         Try
583         {
584                 person = getPrivData(thisObject);
585                 if (person == NULL)
586                         ThrowMsg(InvalidArgumentException, "No private object.");
587         }
588         Catch(Exception)
589         {
590                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
591                 return JSWebAPIErrorFactory::postException(context, exception,
592                                 JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
593         }
594
595         AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_PERSON_UNLINK);
596         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
597
598         ArgumentValidator validator(context, argumentCount, arguments);
599         std::string contactId;
600         try     {
601                 contactId = validator.toString(0, false);
602         } catch (const TypeMismatchException& err ) {
603                 return JSWebAPIErrorFactory::postException(context, exception, err);
604         } catch(const BasePlatformException& err) {
605                 return JSWebAPIErrorFactory::postException(context, exception, err);
606         } catch(const ConversionException& err) {
607                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
608         } catch(const NullPointerException& err) {
609                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
610         }
611
612         EventPersonUnlinkPtr dplEvent(new EventPersonUnlink());
613
614         dplEvent->setContactId(contactId);
615     dplEvent->setForSynchronousCall();
616
617         Try
618         {
619                 person->unlink(dplEvent);
620         }
621         Catch(ConversionException)
622         {
623                 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
624                 return JSWebAPIErrorFactory::postException(context, exception,
625                                 JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be 'ContactId'");
626         }
627         Catch(Exception)
628         {
629                 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
630                 return JSWebAPIErrorFactory::postException(context, exception,
631                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
632         }
633
634         if (!dplEvent->getResult())
635         {
636                 std::stringstream oss;
637                 switch (dplEvent->getExceptionCode())
638                 {
639                 case ExceptionCodes::InvalidArgumentException:
640                         oss << "No Contact id is wrong : '" << contactId << "'";
641                         return JSWebAPIErrorFactory::postException(context, exception,
642                                         JSWebAPIErrorFactory::INVALID_VALUES_ERROR, oss.str());
643                         break;
644                 case ExceptionCodes::NotFoundException:
645                         oss << "No Contact id '" << contactId << "'";
646                         return JSWebAPIErrorFactory::postException(context, exception,
647                                         JSWebAPIErrorFactory::INVALID_VALUES_ERROR, oss.str());
648                         break;
649                 case ExceptionCodes::OutOfRangeException:
650                         oss << "Contact (id:'" << contactId << "') is not a member of person";
651                         return JSWebAPIErrorFactory::postException(context, exception,
652                                         JSWebAPIErrorFactory::INVALID_VALUES_ERROR, oss.str());
653                         break;
654                 case ExceptionCodes::PlatformException:
655                         return JSWebAPIErrorFactory::postException(context, exception,
656                                         JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
657                         break;
658                 default:
659                         return JSWebAPIErrorFactory::postException(context, exception,
660                                         JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
661                         break;
662                 }
663         }
664
665         PersonPtr unlinkedPerson = dplEvent->getPerson();
666         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
667
668         JSValueRef result;
669         Try
670         {
671                 result = converter->toJSValueRef(unlinkedPerson);
672         }
673         Catch(Exception)
674         {
675                 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
676                 return JSWebAPIErrorFactory::postException(context, exception,
677                                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
678         }
679         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
680         return result;
681 }
682
683 } // Contact
684 } // DeviceAPI