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 FilterConverter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @description Reference from CalendarConverter.cpp
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/ScopedJSStringRef.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/JSUtils.h>
29 #include "JSAttributeFilter.h"
30 #include "JSAttributeRangeFilter.h"
31 #include "JSCompositeFilter.h"
32 #include "JSSortMode.h"
33 #include "FilterConverter.h"
35 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE "type"
36 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER "order"
37 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS "filters"
38 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_IDS "ids"
39 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME "attributeName"
40 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG "matchFlag"
41 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES "matchValues"
42 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE "initialValue"
43 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE "endValue"
45 namespace DeviceAPI {
\rnamespace Tizen {
47 using namespace DeviceAPI::Tizen;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
53 FilterConverter::FilterConverter(JSContextRef context) : Converter(context)
58 FilterConverter::~FilterConverter()
63 JSValueRef FilterConverter::toJSValueRef(FilterPtr arg)
66 ThrowMsg(NullPointerException, "Filter is NULL.");
68 JSValueRef result = NULL;
70 if(arg->getFilterType() == UNION_FILTER || arg->getFilterType() == INTERSECTION_FILTER)
72 CompositeFilterPtr filter = DPL::DynamicPointerCast<CompositeFilter>(arg);
74 result = toJSValueRef(filter);
76 else if(arg->getFilterType() == ATTRIBUTE_FILTER)
78 AttributeFilterPtr filter = DPL::DynamicPointerCast<AttributeFilter>(arg);
80 result = toJSValueRef(filter);
82 else if(arg->getFilterType() == ATTRIBUTE_RANGE_FILTER)
84 AttributeRangeFilterPtr filter = DPL::DynamicPointerCast<AttributeRangeFilter>(arg);
86 result = toJSValueRef(filter);
92 FilterPtr FilterConverter::toFilter(const JSValueRef& arg)
95 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
97 if(!JSValueIsObject(m_context, arg))
98 return FilterPtr(NULL);
99 //ThrowMsg(InvalidArgumentException, "Filter is not object.");
101 if(JSCompositeFilter::isObjectOfClass(m_context, arg))
102 return DPL::StaticPointerCast<IFilter>(JSCompositeFilter::getCompositeFilter(m_context, arg));
104 if(JSAttributeFilter::isObjectOfClass(m_context, arg))
105 return DPL::StaticPointerCast<IFilter>(JSAttributeFilter::getAttributeFilter(m_context, arg));
107 if(JSAttributeRangeFilter::isObjectOfClass(m_context, arg))
108 return DPL::StaticPointerCast<IFilter>(JSAttributeRangeFilter::getAttributeRangeFilter(m_context, arg));
110 //ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
111 return FilterPtr(NULL);
114 JSValueRef FilterConverter::toJSValueRef(const FilterArrayPtr& arg)
117 ThrowMsg(NullPointerException, "FilterArray is NULL.");
119 int size = arg->size();
121 JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
123 ThrowMsg(ConversionException, "Can not create array object.");
125 for(int i = 0; i < size; i++)
127 JSValueRef jsvalue = toJSValueRef(arg->at(i));
131 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
132 ThrowMsg(ConversionException, "Can not fill Filter array.");
135 return static_cast<JSValueRef>(resultObject);
138 FilterArrayPtr FilterConverter::toFilterArray(const JSValueRef& arg)
141 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
143 if(!JSIsArrayValue(m_context, arg))
144 ThrowMsg(InvalidArgumentException, "JSValueRef is not Array.");
146 FilterArrayPtr retVal;
148 JSObjectRef obj = toJSObjectRef(arg);
149 retVal = FilterArrayPtr(new FilterArray());
151 unsigned int length = JSGetArrayLength(m_context, obj);
152 for(unsigned int i=0; i<length; i++)
154 JSValueRef value = JSGetArrayElement(m_context, obj, i);
157 LogWarning("value is NULL (" << i << ")");
161 FilterPtr filter(NULL);
162 filter = toFilter(value);
165 LogWarning("Element is not an AbstractFilter (" << i << ")");
169 retVal->push_back(filter);
175 JSValueRef FilterConverter::toJSValueRef(const CompositeFilterPtr& arg)
178 ThrowMsg(NullPointerException, "CompositeFilter is NULL.");
180 const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS));
181 JSValueRef jsValueFilters = toJSValueRef(arg->getFilters());
183 JSObjectRef jsValueResult = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSCompositeFilter::getClassRef(), arg);
185 JSObjectSetProperty(m_context, jsValueResult, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
187 return jsValueResult;
190 CompositeFilterPtr FilterConverter::toCompositeFilter(const JSValueRef& arg)
193 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
195 if(!JSCompositeFilter::isObjectOfClass(m_context, arg))
196 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
198 CompositeFilterPtr compositeFilter = JSCompositeFilter::getCompositeFilter(m_context, arg);
200 FilterArrayPtr filters = FilterArrayPtr(new FilterArray());
202 const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS));
203 JSObjectRef argObj = JSValueToObject(m_context, arg, NULL);
204 JSValueRef jsValueFilters = JSObjectGetProperty(m_context, argObj, jsStrFilters.get(), NULL);
205 filters = toFilterArray(jsValueFilters);
206 compositeFilter->setFilters(filters);
208 return compositeFilter;
211 JSValueRef FilterConverter::toJSValueRef(const AttributeFilterPtr& arg)
214 ThrowMsg(NullPointerException, "AttributeFilter is NULL.");
216 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSAttributeFilter::getClassRef(), arg);
219 AttributeFilterPtr FilterConverter::toAttributeFilter(const JSValueRef& arg)
222 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
224 if(!JSAttributeFilter::isObjectOfClass(m_context, arg))
225 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
227 return JSAttributeFilter::getAttributeFilter(m_context, arg);
230 JSValueRef FilterConverter::toJSValueRef(const AttributeRangeFilterPtr& arg)
233 ThrowMsg(NullPointerException, "AttributeRangeFilter is NULL.");
235 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSAttributeRangeFilter::getClassRef(), arg);
238 AttributeRangeFilterPtr FilterConverter::toAttributeRangeFilter(const JSValueRef& arg)
241 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
243 if(!JSAttributeRangeFilter::isObjectOfClass(m_context, arg))
244 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
246 return JSAttributeRangeFilter::getAttributeRangeFilter(m_context, arg);
249 JSValueRef FilterConverter::toJSValueRef(const SortModePtr& arg)
252 ThrowMsg(NullPointerException, "SortMode is NULL.");
254 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSSortMode::getClassRef(), arg);
257 SortModePtr FilterConverter::toSortMode(const JSValueRef& arg)
260 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
262 if(!JSSortMode::isObjectOfClass(m_context, arg))
263 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
265 return JSSortMode::getSortMode(m_context, arg);
268 JSValueRef FilterConverter::toJSValueRef(const SortModeArrayPtr& arg)
270 // Not used any more.
272 ThrowMsg(NullPointerException, "SortModeArray is NULL.");
274 int size = arg->size();
276 JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
278 ThrowMsg(ConversionException, "Can not create array object.");
280 for(int i = 0; i < size; i++)
282 JSValueRef jsvalue = toJSValueRef(arg->at(i));
283 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
284 ThrowMsg(ConversionException, "Can not fill SortMode array.");
287 return static_cast<JSValueRef>(resultObject);
290 SortModeArrayPtr FilterConverter::toSortModeArray(const JSValueRef& arg)
292 // Not used any more.
294 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
296 if(!JSIsArrayValue(m_context, arg))
297 ThrowMsg(InvalidArgumentException, "Not an array type.");
299 SortModeArrayPtr sortModeArray = SortModeArrayPtr(new SortModeArray());
300 JSObjectRef object = toJSObjectRef(arg);
302 unsigned int length = JSGetArrayLength(m_context, object);
303 for(unsigned int i=0; i<length; i++)
305 JSValueRef value = JSGetArrayElement(m_context, object, i);
307 if(JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value)) {
308 ThrowMsg(InvalidArgumentException, "CompositeFilter.filters has null elements.");
311 SortModePtr sortMode = toSortMode(value);
313 sortModeArray->push_back(sortMode);
316 return sortModeArray;
319 JSValueRef FilterConverter::toJSValueRef(const AnyPtr& arg)
322 ThrowMsg(NullPointerException, "Any is NULL.");
324 AnyTypeConverterFactory::ConverterType converter =
325 AnyTypeConverterFactory::getConverter(m_context);
327 return converter->toJSValueRef(arg);
330 AnyPtr FilterConverter::toAny(const JSValueRef& arg)
333 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
337 AnyTypeConverterFactory::ConverterType converter =
338 AnyTypeConverterFactory::getConverter(m_context);
340 return converter->toAny(arg);
343 JSValueRef FilterConverter::toJSValueRef(const AnyArrayPtr& arg)
346 ThrowMsg(NullPointerException, "AnyArray is NULL.");
348 AnyTypeConverterFactory::ConverterType converter =
349 AnyTypeConverterFactory::getConverter(m_context);
351 int size = arg->size();
353 JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
355 ThrowMsg(ConversionException, "Can not create array object.");
357 for(int i = 0; i < size; i++)
359 JSValueRef jsvalue = converter->toJSValueRef(arg->at(i));
360 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
361 ThrowMsg(ConversionException, "Can not fill any array.");
364 return static_cast<JSValueRef>(resultObject);
367 AnyArrayPtr FilterConverter::toAnyArray(const JSValueRef& arg)
370 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
374 retVal = AnyArrayPtr(new AnyArray());
376 JSObjectRef obj = toJSObjectRef(arg);
377 AnyTypeConverterFactory::ConverterType converter =
378 AnyTypeConverterFactory::getConverter(m_context);
380 unsigned int length = JSGetArrayLength(m_context, obj);
381 for(unsigned int i=0; i<length; i++)
383 JSValueRef value = JSGetArrayElement(m_context, obj, i);
385 if(JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value)) {
386 ThrowMsg(InvalidArgumentException, "IDFilter.ids has null elements.");
388 AnyPtr any = converter->toAny(value);
390 retVal->push_back(any);
396 JSValueRef FilterConverter::toJSValueRef(const FilterType& arg)
398 string compositeFilterTypeStr;
400 if(arg == UNION_FILTER)
401 compositeFilterTypeStr = "UNION";
402 else if(arg == INTERSECTION_FILTER)
403 compositeFilterTypeStr = "INTERSECTION";
405 ThrowMsg(ConversionException, "Can not create CompositeFilterType.");
407 return toJSValueRef(compositeFilterTypeStr);
410 FilterType FilterConverter::toCompositeFilterType(const JSValueRef& arg)
413 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
415 if(!JSValueIsString(m_context, arg))
416 ThrowMsg(InvalidArgumentException, "Value is not an string.");
418 string compositeFilterTypeStr = toString(arg);
419 FilterType compositeFilterType;
421 if(compositeFilterTypeStr == "UNION")
422 compositeFilterType = UNION_FILTER;
423 else if(compositeFilterTypeStr == "INTERSECTION")
424 compositeFilterType = INTERSECTION_FILTER;
426 ThrowMsg(InvalidArgumentException, "CompositeFilterType cannot have " << compositeFilterTypeStr);
428 return compositeFilterType;
431 JSValueRef FilterConverter::toJSValueRef(const MatchFlag& arg)
435 if(arg == MATCH_EXACTLY)
436 matchFlagStr = "EXACTLY";
437 else if(arg == MATCH_FULLSTRING)
438 matchFlagStr = "FULLSTRING";
439 else if(arg == MATCH_CONTAINS)
440 matchFlagStr = "CONTAINS";
441 else if(arg == MATCH_STARTSWITH)
442 matchFlagStr = "STARTSWITH";
443 else if(arg == MATCH_ENDSWITH)
444 matchFlagStr = "ENDSWITH";
445 else if(arg == MATCH_EXISTS)
446 matchFlagStr = "EXISTS";
448 ThrowMsg(ConversionException, "Can not create MatchFlag.");
450 return toJSValueRef(matchFlagStr);
453 MatchFlag FilterConverter::toMatchFlag(const JSValueRef& arg)
456 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
458 if(!JSValueIsString(m_context, arg))
459 ThrowMsg(InvalidArgumentException, "Value is not an string.");
461 string matchFlagStr = toString(arg);
464 if(matchFlagStr == "EXACTLY")
465 matchFlag = MATCH_EXACTLY;
466 else if(matchFlagStr == "FULLSTRING")
467 matchFlag = MATCH_FULLSTRING;
468 else if(matchFlagStr == "CONTAINS")
469 matchFlag = MATCH_CONTAINS;
470 else if(matchFlagStr == "STARTSWITH")
471 matchFlag = MATCH_STARTSWITH;
472 else if(matchFlagStr == "ENDSWITH")
473 matchFlag = MATCH_ENDSWITH;
474 else if(matchFlagStr == "EXISTS")
475 matchFlag = MATCH_EXISTS;
477 ThrowMsg(InvalidArgumentException, "FilterMatchFlag cannot have " << matchFlagStr);
482 JSValueRef FilterConverter::toJSValueRef(const SortOrder& arg)
486 if(arg == ASCENDING_SORT_ORDER)
487 sortOrderStr = "ASC";
488 else if(arg == DESCENDING_SORT_ORDER)
489 sortOrderStr = "DESC";
491 ThrowMsg(ConversionException, "Can not create SortModeOrder.");
493 return toJSValueRef(sortOrderStr);
496 SortOrder FilterConverter::toSortOrder(const JSValueRef& arg)
499 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
501 if(!JSValueIsString(m_context, arg))
502 ThrowMsg(InvalidArgumentException, "Value is not an string.");
504 string sortOrderStr = toString(arg);
507 if(sortOrderStr == "ASC")
508 sortOrder = ASCENDING_SORT_ORDER;
509 else if(sortOrderStr == "DESC")
510 sortOrder = DESCENDING_SORT_ORDER;
512 ThrowMsg(InvalidArgumentException, "SortModeOrder cannot have " << sortOrderStr);
517 void FilterConverter::initFilterAttrs()
519 m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE);
520 m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS);
522 m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
523 m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG);
524 m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES);
526 m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
527 m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE);
528 m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE);
530 m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER);
531 m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);