2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @file EventFilterConverter.cpp
20 * @author Jihwa Park (jh7979.park@samsung.com)
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/ScopedJSStringRef.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/JSUtils.h>
29 #include <Commons/RegexUtils.h>
30 #include "JSAccount.h"
31 #include "AccountConverter.h"
32 //#include "JSAccountService.h"
33 #include "JSAccountServiceType.h"
34 #include "JSAccountManager.h"
35 #include "JSFeatureArray.h"
36 #include "JSAccountServices.h"
37 #include "JSAccountServiceProvider.h"
39 #define ACCOUNT_SERVICES_NAME "serviceName"
40 #define ACCOUNT_SERVICES_TYPE "serviceType"
42 using namespace TizenApis::Api::Account;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
47 const int DEFAULT_EVENT_INTERVAL = 1;
54 std::vector<std::string> AccountConverter::m_allowedAttributes;
55 std::vector<std::string> AccountConverter::m_allowedCreateProperties;
57 AccountConverter::AccountConverter(JSContextRef context) : Converter(context)
60 if (m_allowedAttributes.size() == 0) {
61 m_allowedAttributes.push_back("id");
62 m_allowedAttributes.push_back("iconPath");
64 if (m_allowedCreateProperties.size() == 0) {
65 m_allowedCreateProperties.push_back("id");
66 m_allowedCreateProperties.push_back("iconPath");
70 AccountConverter::~AccountConverter()
75 JSValueRef AccountConverter::toJSValueRefAccount(const EventAccountPtr& arg)
78 return JSAccount::createJSAccount(m_context, arg);
81 JSValueRef AccountConverter::toJSValueRefAccountServiceType(const AccountServiceTypePropertyPtr& arg)
84 return JSAccountServiceType::createJSObject(m_context, arg);
87 JSValueRef AccountConverter::toJSValueRefAccountServiceProvider(const AccountServiceProviderPropertyPtr& arg)
90 return JSAccountServiceProvider::createJSObject(m_context, arg);
96 JSValueRef AccountConverter::toJSValueRef(
97 const std::vector<EventAccountPtr> &arg)
100 return toJSValueRef_(arg, &AccountConverter::toJSValueRefAccount, this);
103 JSValueRef AccountConverter::toJSValueRef(const EventAccountListPtr &arg){
104 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
106 if (NULL == jsResult) {
107 ThrowMsg(NullPointerException, "Could not create js array object");
110 // for(unsigned int i = 0; i < arg->size(); i++) {
111 // if (!JSSetArrayElement(m_context, jsResult, i, JSAccountServiceType::createJSObject(m_context, arg->at(i)))) {
112 // ThrowMsg(UnknownException, "Could not insert value into js array");
121 JSValueRef AccountConverter::toFunctionOrNull(const JSValueRef& arg)
124 if (Validator(m_context).isCallback(arg)) {
126 } else if (!JSValueIsNull(m_context,
127 arg) && !JSValueIsUndefined(m_context, arg)) {
128 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
133 JSValueRef AccountConverter::toFunction(const JSValueRef& arg)
136 if (Validator(m_context).isCallback(arg)) {
138 } else if (JSValueIsNull(m_context,
139 arg) || JSValueIsUndefined(m_context, arg)) {
140 ThrowMsg(ConversionException, "JS null passed as function.");
142 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
145 AccountServicesPtr AccountConverter::toService(const JSValueRef jsValue)
147 const ScopedJSStringRef serviceNameStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_NAME));
148 const ScopedJSStringRef serviceTypeStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_TYPE));
150 JSObjectRef jsObject = toJSObjectRef(jsValue);
152 JSValueRef serviceNameData = JSObjectGetProperty(m_context, jsObject, serviceNameStr.get(), NULL);
153 JSValueRef serviceTypeData = JSObjectGetProperty(m_context, jsObject, serviceTypeStr.get(), NULL);
155 std::string serviceName;
156 std::string serviceTypeId;
158 AccountServicesPtr result = AccountServicesPtr(new AccountServices());
160 Throw(ConversionException);
163 if (!JSValueIsUndefined(m_context, serviceNameData)) {
164 serviceName = toString(serviceNameData);
165 result->setName(serviceName);
168 if (!JSValueIsUndefined(m_context, serviceTypeData)) {
169 serviceTypeId = toString(serviceTypeData);
170 result->setServiceTypeId(serviceTypeId);
176 AccountServicesArrayPtr AccountConverter::toAccountServiceArray(const JSValueRef jsValue)
178 AccountServicesArrayPtr result(new AccountServicesArray());
180 std::vector<AccountServicesPtr> resultVector;
181 JSObjectRef objArg = toJSObjectRef(jsValue);
182 LogDebug("array length "<<JSGetArrayLength(m_context, objArg));
183 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
184 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
185 resultVector.push_back(toService(element));
188 *result = resultVector;
192 std::vector<std::string> AccountConverter::toTags(const JSValueRef jsValue){
193 std::vector<std::string> tags;
195 JSObjectRef objArg = toJSObjectRef(jsValue);
196 LogDebug("array length:" << JSGetArrayLength(m_context, objArg));
198 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
199 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
200 tags.push_back(toString(element));
207 AccountServiceFilterPropertyPtr AccountConverter::toAccountServiceFilterProperty(const JSValueRef jsValue){
208 AccountServiceFilterPropertyPtr filterPropertyPtr(new AccountServiceFilterProperty());
210 JSValueRef serviceTypeIdRef = JSUtils::getJSProperty(m_context, jsValue, "serviceTypeId");
211 if(serviceTypeIdRef!=NULL){
212 std::string serviceType = toString(serviceTypeIdRef);
213 LogDebug("serviceType:[" << serviceType << "]");
214 filterPropertyPtr->setServiceTypeId(serviceType);
216 AccountLogWarning("serviceTypeIdRef is NULL");
219 JSValueRef tagsRef = JSUtils::getJSProperty(m_context, jsValue, "tags");
221 std::vector<std::string> tags = toTags(tagsRef);
222 LogDebug("tags length:" << tags.size());
223 filterPropertyPtr->setTags(tags);
225 AccountLogWarning("tagsRef is NULL");
228 JSValueRef providerRef = JSUtils::getJSProperty(m_context, jsValue, "provider");
229 if(providerRef != NULL){
230 std::string provider = toString(providerRef);
231 LogDebug("provider:[" << provider << "]");
232 filterPropertyPtr->setProvider(provider);
234 AccountLogWarning("providerRef is NULL");
237 return filterPropertyPtr;
240 void AccountConverter::setProvider(const JSValueRef jsValue)
244 const ScopedJSStringRef providerStr(JSStringCreateWithUTF8CString("providerId"));
245 JSObjectRef arg = toJSObjectRef(jsValue);
246 JSValueRef providerData = JSObjectGetProperty(m_context, arg, providerStr.get(), NULL);
248 if (!JSValueIsUndefined(m_context, providerData)) {
249 m_provider = toString(providerData);
250 LogDebug("m_provider : " << m_provider);
253 m_provider = toString(jsValue);
256 std::string AccountConverter::convertTostring(const JSValueRef jsValue)
260 string = toString(jsValue);
264 EventAccountPtr AccountConverter::toAccount(const std::string& accountServiceProviderId){
266 EventAccountPtr result(new EventAccount());
268 const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
269 const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
271 //TODO set displayName
273 // JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
274 // JSValueRef iconPathData = JSObjectGetProperty(m_context, arg, iconPathStr.get(), NULL);
276 // if (!JSValueIsUndefined(m_context, displayNameData)) {
277 // result->setDisplayName(toString(displayNameData));
280 // if (!JSValueIsUndefined(m_context, iconPathData)) {
281 // result->setIconPath(toString(iconPathData));
284 result->setProviderName(accountServiceProviderId);
290 EventAccountPtr AccountConverter::toAccount(const JSValueRef account)
293 EventAccountPtr result(new EventAccount());
295 const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
296 const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
297 const ScopedJSStringRef providerIdStr(JSStringCreateWithUTF8CString("providerId"));
299 JSObjectRef arg = toJSObjectRef(account);
301 JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
302 JSValueRef iconPathData = JSObjectGetProperty(m_context, arg, iconPathStr.get(), NULL);
303 JSValueRef providerIdData = JSObjectGetProperty(m_context, arg, providerIdStr.get(), NULL);
305 if (!JSValueIsUndefined(m_context, displayNameData)) {
306 result->setDisplayName(toString(displayNameData));
309 if (!JSValueIsUndefined(m_context, iconPathData)) {
310 result->setIconPath(toString(iconPathData));
313 if (!JSValueIsUndefined(m_context, providerIdData)) {
314 result->setProviderId(toString(providerIdData));
320 void AccountConverter::toEventFilterConvertId(
321 const AccountFilterPtr &result,
322 const JSValueRef &filters)
324 const ScopedJSStringRef idStr(JSStringCreateWithUTF8CString("id"));
325 JSObjectRef filterObj = toJSObjectRef(filters);
326 if (JSObjectHasProperty(m_context, filterObj, idStr.get())) {
327 LogDebug("converting id");
328 JSValueRef value = JSObjectGetProperty(m_context, filterObj, idStr.get(), NULL);
329 result->setIdFilter(toString(value));
333 AccountFilterPtr AccountConverter::toAccountFilter(const JSValueRef &filters)
336 AccountFilterPtr result(new AccountFilter());
337 Validator validator(m_context);
338 if (!JSValueIsObject(m_context, filters)) {
339 LogError("Wrong filter parameter");
340 Throw(InvalidArgumentException);
342 if (!validator.checkArrayKeys(m_allowedAttributes, filters)) {
343 LogError("Wrong filter's attribue");
344 Throw(ConversionException);
347 toEventFilterConvertId(result, filters);
351 EventAccountListPtr AccountConverter::toVectorOfAccounts(JSValueRef events)
354 EventAccountListPtr result(new EventAccountList());
356 std::vector<EventAccountPtr> resultVector;
357 JSObjectRef objArg = toJSObjectRef(events);
358 LogDebug("array length " + JSGetArrayLength(m_context, objArg));
359 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
360 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
361 resultVector.push_back(toAccount(element));
364 *result = resultVector;
368 JSValueRef AccountConverter::toJSONString(const std::string& arg){
369 return JSValueMakeFromJSONString(m_context, toJSStringRef(arg));