2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSContactOrganization.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactOrganization class
25 #include "JSContactOrganization.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <JSWebAPIErrorFactory.h>
29 #include "ContactConverter.h"
33 #define ORGANIZATION_CLASS_NAME "ContactOrganization"
35 #define CONTACT_ATTR_NAME "name"
36 #define CONTACT_ATTR_DEPARTMENT "department"
37 #define CONTACT_ATTR_TITLE "title"
38 #define CONTACT_ATTR_ROLE "role"
39 #define CONTACT_ATTR_LOGO_URI "logoURI"
41 #define CONTACT_ATTR_ASSISTANT_NAME "assistant"
42 #define CONTACT_ATTR_LOCATION "location"
43 #define CONTACT_ATTR_DESCRIPTION "description"
44 #define CONTACT_ATTR_PHONETIC_NAME "phoneticName"
45 #define CONTACT_ATTR_TYPE "type"
46 #define CONTACT_ATTR_LABEL "label"
51 using namespace DeviceAPI::Common;
52 using namespace WrtDeviceApis::Commons;
53 using namespace WrtDeviceApis::CommonsJavaScript;
55 JSClassDefinition JSContactOrganization::m_classInfo =
58 kJSClassAttributeNone,
59 ORGANIZATION_CLASS_NAME,
68 NULL, //DeleteProperty,
69 NULL, //getPropertyNames,
70 NULL, //CallAsFunction,
71 constructor, //CallAsConstructor,
72 hasInstance, //HasInstance,
73 NULL, //ConvertToType,
76 JSStaticValue JSContactOrganization::m_property[] = {
77 { CONTACT_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
78 { CONTACT_ATTR_DEPARTMENT, getDepartment, setDepartment, kJSPropertyAttributeNone },
79 { CONTACT_ATTR_TITLE, getTitle, setTitle, kJSPropertyAttributeNone },
80 { CONTACT_ATTR_ROLE, getRole, setRole, kJSPropertyAttributeNone },
81 { CONTACT_ATTR_LOGO_URI, getLogoURI, setLogoURI, kJSPropertyAttributeNone },
82 { CONTACT_ATTR_ASSISTANT_NAME, getAssistant, setAssistant, kJSPropertyAttributeNone },
83 { CONTACT_ATTR_LOCATION, getLocation, setLocation, kJSPropertyAttributeNone },
84 { CONTACT_ATTR_DESCRIPTION, getDescription, setDescription, kJSPropertyAttributeNone },
85 { CONTACT_ATTR_PHONETIC_NAME, getPhoneticName, setPhoneticName, kJSPropertyAttributeNone },
86 { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
87 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
91 JSStaticFunction JSContactOrganization::m_functions[] =
96 JSClassRef JSContactOrganization::m_classRef = JSClassCreate(&m_classInfo);
98 JSClassRef DLL_EXPORT JSContactOrganization::getClassRef() {
100 m_classRef = JSClassCreate(&m_classInfo);
105 bool JSContactOrganization::isObjectOfClass(JSContextRef context, JSValueRef value)
107 return JSValueIsObjectOfClass(context, value, getClassRef());
110 ContactOrganizationPtr JSContactOrganization::getContactOrganization(JSContextRef context, JSValueRef value)
112 if (!isObjectOfClass(context, value)) {
113 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
115 JSObjectRef object = JSValueToObject(context, value, NULL);
117 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
119 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
121 Throw(WrtDeviceApis::Commons::NullPointerException);
123 return priv->getObject();
126 void JSContactOrganization::Initialize(JSContextRef context, JSObjectRef object)
128 if (!JSObjectGetPrivate(object))
130 ContactOrganizationPtr organization(new ContactOrganization());
131 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, ContactOrganizationPtr(organization));
132 if (!JSObjectSetPrivate(object, priv)) {
138 void JSContactOrganization::Finalize(JSObjectRef object)
140 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
146 JSObjectRef JSContactOrganization::createJSObject(JSContextRef context, ContactOrganizationPtr contactOrganization)
148 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, contactOrganization);
149 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
150 if (NULL == jsObjectRef) {
151 LoggerE("object creation error");
157 ContactOrganizationPtr JSContactOrganization::getPrivData(JSObjectRef object)
159 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
161 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163 ContactOrganizationPtr result = priv->getObject();
165 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
170 JSObjectRef JSContactOrganization::constructor(JSContextRef context,
171 JSObjectRef constructor,
172 size_t argumentCount,
173 const JSValueRef arguments[],
174 JSValueRef* exception)
176 bool js1stParamIsObject = false;
178 JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
180 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
182 JSContextRef gContext = priv->getContext();
184 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
186 if (argumentCount >= 1)
188 if (JSValueIsObject(gContext, arguments[0]))
189 js1stParamIsObject = true;
191 if (!js1stParamIsObject &&
192 !JSValueIsNull(gContext, arguments[0]) &&
193 !JSValueIsUndefined(gContext, arguments[0]))
194 ThrowMsg(InvalidArgumentException, "1st argument must be a 'object'");
197 } Catch(Exception ) {
198 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
199 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'object'");
203 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
205 ContactOrganizationPtr contactOrganization(NULL);
207 if(js1stParamIsObject)
208 contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
210 contactOrganization = ContactOrganizationPtr(new ContactOrganization());
213 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
214 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
218 JSObjectRef jsobject;
221 jsobject = createJSObject(gContext, contactOrganization);
223 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
224 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
231 bool JSContactOrganization::hasInstance(JSContextRef context,
232 JSObjectRef constructor,
233 JSValueRef possibleInstance,
234 JSValueRef* exception)
236 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
239 JSValueRef JSContactOrganization::getName(JSContextRef context,
241 JSStringRef propertyName,
242 JSValueRef* exception)
246 ContactConverterFactory::ConverterType converter =
247 ContactConverterFactory::getConverter(context);
248 ContactOrganizationPtr organization = getPrivData(object);
249 if(!organization->getNameIsSet())
250 return JSValueMakeNull(context);
252 return converter->toJSValueRef(organization->getName());
254 Catch(WrtDeviceApis::Commons::Exception)
256 LoggerW("trying to get incorrect value");
258 return JSValueMakeUndefined(context);
261 bool JSContactOrganization::setName(JSContextRef context,
263 JSStringRef propertyName,
265 JSValueRef* exception)
269 ContactOrganizationPtr organization = getPrivData(object);
270 ContactConverterFactory::ConverterType converter =
271 ContactConverterFactory::getConverter(context);
272 BasicValidator validator =
273 BasicValidatorFactory::getValidator(context, exception);
274 if(validator->isNullOrUndefined(value))
275 organization->unsetName();
277 organization->setName(converter->toString(value));
279 Catch(WrtDeviceApis::Commons::Exception)
281 LoggerW("trying to set incorrect value");
282 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
288 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
290 JSStringRef propertyName,
291 JSValueRef* exception)
295 ContactConverterFactory::ConverterType converter =
296 ContactConverterFactory::getConverter(context);
297 ContactOrganizationPtr organization = getPrivData(object);
298 if(!organization->getDepartmentIsSet())
299 return JSValueMakeNull(context);
301 return converter->toJSValueRef(organization->getDepartment());
303 Catch(WrtDeviceApis::Commons::Exception)
305 LoggerW("trying to get incorrect value");
307 return JSValueMakeUndefined(context);
310 bool JSContactOrganization::setDepartment(JSContextRef context,
312 JSStringRef propertyName,
314 JSValueRef* exception)
318 ContactOrganizationPtr organization = getPrivData(object);
319 ContactConverterFactory::ConverterType converter =
320 ContactConverterFactory::getConverter(context);
321 BasicValidator validator =
322 BasicValidatorFactory::getValidator(context, exception);
323 if(validator->isNullOrUndefined(value))
324 organization->unsetDepartment();
326 organization->setDepartment(converter->toString(value));
328 Catch(WrtDeviceApis::Commons::Exception)
330 LoggerW("trying to set incorrect value");
331 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
336 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
338 JSStringRef propertyName,
339 JSValueRef* exception)
343 ContactConverterFactory::ConverterType converter =
344 ContactConverterFactory::getConverter(context);
345 ContactOrganizationPtr organization = getPrivData(object);
346 if(!organization->getTitleIsSet())
347 return JSValueMakeNull(context);
349 return converter->toJSValueRef(organization->getTitle());
351 Catch(WrtDeviceApis::Commons::Exception)
353 LoggerW("trying to get incorrect value");
355 return JSValueMakeUndefined(context);
358 bool JSContactOrganization::setTitle(JSContextRef context,
360 JSStringRef propertyName,
362 JSValueRef* exception)
366 ContactOrganizationPtr organization = getPrivData(object);
367 ContactConverterFactory::ConverterType converter =
368 ContactConverterFactory::getConverter(context);
369 BasicValidator validator =
370 BasicValidatorFactory::getValidator(context, exception);
371 if(validator->isNullOrUndefined(value))
372 organization->unsetTitle();
374 organization->setTitle(converter->toString(value));
376 Catch(WrtDeviceApis::Commons::Exception)
378 LoggerW("trying to set incorrect value");
379 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
384 JSValueRef JSContactOrganization::getRole(JSContextRef context,
386 JSStringRef propertyName,
387 JSValueRef* exception)
391 ContactConverterFactory::ConverterType converter =
392 ContactConverterFactory::getConverter(context);
393 ContactOrganizationPtr organization = getPrivData(object);
394 if(!organization->getRoleIsSet())
395 return JSValueMakeNull(context);
397 return converter->toJSValueRef(organization->getRole());
399 Catch(WrtDeviceApis::Commons::Exception)
401 LoggerW("trying to get incorrect value");
403 return JSValueMakeUndefined(context);
406 bool JSContactOrganization::setRole(JSContextRef context,
408 JSStringRef propertyName,
410 JSValueRef* exception)
414 ContactOrganizationPtr organization = getPrivData(object);
415 ContactConverterFactory::ConverterType converter =
416 ContactConverterFactory::getConverter(context);
417 BasicValidator validator =
418 BasicValidatorFactory::getValidator(context, exception);
419 if(validator->isNullOrUndefined(value))
420 organization->unsetRole();
422 organization->setRole(converter->toString(value));
424 Catch(WrtDeviceApis::Commons::Exception)
426 LoggerW("trying to set incorrect value");
427 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
432 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
434 JSStringRef propertyName,
435 JSValueRef* exception)
439 ContactConverterFactory::ConverterType converter =
440 ContactConverterFactory::getConverter(context);
441 ContactOrganizationPtr organization = getPrivData(object);
442 if(!organization->getLogoURIIsSet())
443 return JSValueMakeNull(context);
445 return converter->toJSValueRef(organization->getLogoURI());
447 Catch(WrtDeviceApis::Commons::Exception)
449 LoggerW("trying to get incorrect value");
451 return JSValueMakeUndefined(context);
454 bool JSContactOrganization::setLogoURI(JSContextRef context,
456 JSStringRef propertyName,
458 JSValueRef* exception)
462 ContactOrganizationPtr organization = getPrivData(object);
463 ContactConverterFactory::ConverterType converter =
464 ContactConverterFactory::getConverter(context);
465 BasicValidator validator =
466 BasicValidatorFactory::getValidator(context, exception);
467 if(validator->isNullOrUndefined(value))
468 organization->unsetLogoURI();
470 organization->setLogoURI(converter->toString(value));
472 Catch(WrtDeviceApis::Commons::Exception)
474 LoggerW("trying to set incorrect value");
475 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
480 JSValueRef JSContactOrganization::getAssistant(JSContextRef context,
482 JSStringRef propertyName,
483 JSValueRef* exception)
487 ContactConverterFactory::ConverterType converter =
488 ContactConverterFactory::getConverter(context);
489 ContactOrganizationPtr organization = getPrivData(object);
490 if(!organization->getAssistantIsSet())
491 return JSValueMakeNull(context);
493 return converter->toJSValueRef(organization->getAssistant());
495 Catch(WrtDeviceApis::Commons::Exception)
497 LoggerW("trying to get incorrect value");
499 return JSValueMakeUndefined(context);
502 bool JSContactOrganization::setAssistant(JSContextRef context,
504 JSStringRef propertyName,
506 JSValueRef* exception)
510 ContactOrganizationPtr organization = getPrivData(object);
511 ContactConverterFactory::ConverterType converter =
512 ContactConverterFactory::getConverter(context);
513 BasicValidator validator =
514 BasicValidatorFactory::getValidator(context, exception);
515 if(validator->isNullOrUndefined(value))
516 organization->unsetAssistant();
518 organization->setAssistant(converter->toString(value));
520 Catch(WrtDeviceApis::Commons::Exception)
522 LoggerW("trying to set incorrect value");
523 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
528 JSValueRef JSContactOrganization::getLocation(JSContextRef context,
530 JSStringRef propertyName,
531 JSValueRef* exception)
535 ContactConverterFactory::ConverterType converter =
536 ContactConverterFactory::getConverter(context);
537 ContactOrganizationPtr organization = getPrivData(object);
538 return converter->toJSValueRef(organization->getLocation());
540 Catch(WrtDeviceApis::Commons::Exception)
542 LoggerW("trying to get incorrect value");
544 return JSValueMakeUndefined(context);
547 bool JSContactOrganization::setLocation(JSContextRef context,
549 JSStringRef propertyName,
551 JSValueRef* exception)
555 ContactOrganizationPtr organization = getPrivData(object);
556 ContactConverterFactory::ConverterType converter =
557 ContactConverterFactory::getConverter(context);
558 BasicValidator validator =
559 BasicValidatorFactory::getValidator(context, exception);
560 if(validator->isNullOrUndefined(value))
561 organization->unsetLocation();
563 organization->setLocation(converter->toString(value));
565 Catch(WrtDeviceApis::Commons::Exception)
567 LoggerW("trying to set incorrect value");
568 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
573 JSValueRef JSContactOrganization::getDescription(JSContextRef context,
575 JSStringRef propertyName,
576 JSValueRef* exception)
580 ContactConverterFactory::ConverterType converter =
581 ContactConverterFactory::getConverter(context);
582 ContactOrganizationPtr organization = getPrivData(object);
583 if(!organization->getDescriptionIsSet())
584 return JSValueMakeNull(context);
586 return converter->toJSValueRef(organization->getDescription());
588 Catch(WrtDeviceApis::Commons::Exception)
590 LoggerW("trying to get incorrect value");
592 return JSValueMakeUndefined(context);
595 bool JSContactOrganization::setDescription(JSContextRef context,
597 JSStringRef propertyName,
599 JSValueRef* exception)
603 ContactOrganizationPtr organization = getPrivData(object);
604 ContactConverterFactory::ConverterType converter =
605 ContactConverterFactory::getConverter(context);
606 BasicValidator validator =
607 BasicValidatorFactory::getValidator(context, exception);
608 if(validator->isNullOrUndefined(value))
609 organization->unsetDescription();
611 organization->setDescription(converter->toString(value));
613 Catch(WrtDeviceApis::Commons::Exception)
615 LoggerW("trying to set incorrect value");
616 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
621 JSValueRef JSContactOrganization::getPhoneticName(JSContextRef context,
623 JSStringRef propertyName,
624 JSValueRef* exception)
628 ContactConverterFactory::ConverterType converter =
629 ContactConverterFactory::getConverter(context);
630 ContactOrganizationPtr organization = getPrivData(object);
631 if(!organization->getPhoneticNameIsSet())
632 return JSValueMakeNull(context);
634 return converter->toJSValueRef(organization->getPhoneticName());
636 Catch(WrtDeviceApis::Commons::Exception)
638 LoggerW("trying to get incorrect value");
640 return JSValueMakeUndefined(context);
643 bool JSContactOrganization::setPhoneticName(JSContextRef context,
645 JSStringRef propertyName,
647 JSValueRef* exception)
651 ContactOrganizationPtr organization = getPrivData(object);
652 ContactConverterFactory::ConverterType converter =
653 ContactConverterFactory::getConverter(context);
654 BasicValidator validator =
655 BasicValidatorFactory::getValidator(context, exception);
656 if(validator->isNullOrUndefined(value))
657 organization->unsetPhoneticName();
659 organization->setPhoneticName(converter->toString(value));
661 Catch(WrtDeviceApis::Commons::Exception)
663 LoggerW("trying to set incorrect value");
664 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
669 JSValueRef JSContactOrganization::getType(JSContextRef context,
671 JSStringRef propertyName,
672 JSValueRef* exception)
676 ContactConverterFactory::ConverterType converter =
677 ContactConverterFactory::getConverter(context);
678 ContactOrganizationPtr organization = getPrivData(object);
679 if(!organization->getTypeIsSet())
680 return JSValueMakeNull(context);
682 return converter->toJSValueRef(organization->getType());
684 Catch(WrtDeviceApis::Commons::Exception)
686 LoggerW("trying to get incorrect value");
688 return JSValueMakeUndefined(context);
691 bool JSContactOrganization::setType(JSContextRef context,
693 JSStringRef propertyName,
695 JSValueRef* exception)
699 ContactOrganizationPtr organization = getPrivData(object);
700 ContactConverterFactory::ConverterType converter =
701 ContactConverterFactory::getConverter(context);
702 BasicValidator validator =
703 BasicValidatorFactory::getValidator(context, exception);
704 if(validator->isNullOrUndefined(value))
705 organization->unsetType();
707 organization->setType(converter->toContactOrganizationType(value));
709 Catch(WrtDeviceApis::Commons::Exception)
711 LoggerW("trying to set incorrect value");
712 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
717 JSValueRef JSContactOrganization::getLabel(JSContextRef context,
719 JSStringRef propertyName,
720 JSValueRef* exception)
724 ContactConverterFactory::ConverterType converter =
725 ContactConverterFactory::getConverter(context);
726 ContactOrganizationPtr organization = getPrivData(object);
727 return converter->toJSValueRef(organization->getLabel());
729 Catch(WrtDeviceApis::Commons::Exception)
731 LoggerW("trying to get incorrect value");
733 return JSValueMakeUndefined(context);
736 bool JSContactOrganization::setLabel(JSContextRef context,
738 JSStringRef propertyName,
740 JSValueRef* exception)
744 ContactOrganizationPtr organization = getPrivData(object);
745 ContactConverterFactory::ConverterType converter =
746 ContactConverterFactory::getConverter(context);
747 BasicValidator validator =
748 BasicValidatorFactory::getValidator(context, exception);
749 if(validator->isNullOrUndefined(value))
750 organization->unsetLabel();
752 organization->setLabel(converter->toString(value));
754 Catch(WrtDeviceApis::Commons::Exception)
756 LoggerW("trying to set incorrect value");
757 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");