[Contact] Refactoring API structures
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactAnniversary.cpp
index 653aa63..5d4931d 100755 (executable)
@@ -25,7 +25,6 @@
 #include <CommonsJavaScript/Validator.h>
 #include <Tizen/Common/JSTizenExceptionFactory.h>
 #include <Tizen/Common/JSTizenException.h>
-//#include <Tizen/Common/JSGlobalContextFactory.h>
 #include "ContactConverter.h"
 #include "JSContactAnniversary.h"
 
@@ -156,7 +155,9 @@ JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
 {
        LogDebug("entered");
 
-//     AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
+       bool js2ndParamIsString = false;
+
+//     AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
 //     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
        JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
@@ -165,25 +166,23 @@ JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
        }
        JSContextRef gContext = priv->getContext();
 
-//     JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
-
        BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
        Try {
-               if (argumentCount < 1 || argumentCount > 2)
-                       ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
+               if (argumentCount < 1)
+                       ThrowMsg(InvalidArgumentException, "1st argument must be a 'Date object'");
 
                if (!JSValueIsObject(gContext, arguments[0]))
-                       ThrowMsg(InvalidArgumentException, "1st argument is not object.");
+                       ThrowMsg(InvalidArgumentException, "1st argument must be a 'Date object'");
 
                if (argumentCount >= 2)
                {
-                       if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
-                               ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
+                       if (!JSValueIsUndefined(context, arguments[1]) && !JSValueIsNull(context, arguments[1]))
+                               js2ndParamIsString = true;
                }
 
        } Catch(Exception ) {
                LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
-               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Date object'");
                return NULL;
        }
 
@@ -191,25 +190,19 @@ JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
 
        std::tm date;
        std::string label;
-       bool labelIsSet = false;
 
        Try {
                date = converter->toDateTm(arguments[0]);
        } Catch(Exception) {
                LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
-               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Date object'");
                return NULL;
        }
 
        Try {
-               if(argumentCount >= 2)
-               {
-                       if(!JSValueIsNull(gContext, arguments[1]))
-                       {
-                               label = converter->toString(arguments[1]);
-                               labelIsSet = true;
-                       }
-               }
+               if (js2ndParamIsString)
+                       label = converter->toString(arguments[1]);
+
        } Catch(Exception) {
                LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
@@ -220,7 +213,7 @@ JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
        contactAnniversary->setDate(date);
 
        // NOTE: Currently not support label field
-       //if(labelIsSet)
+       //if(js2ndParamIsString)
        //      contactAnniversary->setLabel(label);
 
        JSObjectRef jsobject;
@@ -315,11 +308,15 @@ bool JSContactAnniversary::setLabel(JSContextRef context,
 {
        Try
        {
-               // NOTE: Currently not support this field
-               //ContactAnniversaryPtr anniversary = getPrivData(object);
-               //ContactConverterFactory::ConverterType converter =
-               //              ContactConverterFactory::getConverter(context);
-               //anniversary->setLabel(converter->toString(value));
+               ContactAnniversaryPtr anniversary = getPrivData(object);
+               ContactConverterFactory::ConverterType converter =
+                               ContactConverterFactory::getConverter(context);
+               BasicValidator validator =
+                               BasicValidatorFactory::getValidator(context, exception);
+               if(validator->isNullOrUndefined(value))
+                       anniversary->unsetLabel();
+               else
+                       anniversary->setLabel(converter->toString(value));
                return true;
        }
        Catch(WrtDeviceApis::Commons::Exception)