b0f3bc131d6a8ddb1dd40e84be91d43bdb9160f1
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / FilterConverter.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file        FilterConverter.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @description Reference from CalendarConverter.cpp
22  */
23
24 #include <dpl/log/log.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include "FilterConverter.h"
28
29 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE                           "type"
30 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER                          "order"
31 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS                        "filters"
32 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_IDS                            "ids"
33 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME         "attributeName"
34 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG                     "matchFlag"
35 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES           "matchValues"
36 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_CASE_SENSITIVE         "caseSensitive"
37 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE          "initialValue"
38 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE                      "endValue"
39
40 namespace TizenApis {
41 namespace Tizen1_0 {
42 namespace Tizen {
43
44 using namespace TizenApis::Api::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47
48 using namespace std;
49
50 FilterConverter::FilterConverter(JSContextRef context) : Converter(context)
51 {
52         initFilterAttrs();
53 }
54
55 FilterConverter::~FilterConverter()
56 {
57         LogDebug("entered");
58 }
59
60 MatchFlagArrayPtr FilterConverter::toMatchFlagArray(JSValueRef matchFlagArray)
61 {
62         MatchFlagArrayPtr result;
63         Try {
64                 result = MatchFlagArrayPtr(new MatchFlagArray(toVectorOfStrings(matchFlagArray)));
65         } Catch(ConversionException) {
66                 ThrowMsg(InvalidArgumentException, "AttributeFilter.matchFlags has invalid type value.");
67         }
68         return result;
69 }
70
71 JSValueRef FilterConverter::toJSValueRef(FilterPtr arg)
72 {
73         JSValueRef result = NULL;
74
75         if(arg->getFilterType() == UNION_FILTER || arg->getFilterType() == INTERSECTION_FILTER)
76         {
77                 CompositeFilterPtr filter = DPL::DynamicPointerCast<CompositeFilter>(arg);
78
79                 result = toJSValueRef(filter);
80         }
81         else if(arg->getFilterType() == ATTRIBUTE_FILTER)
82         {
83                 AttributeFilterPtr filter = DPL::DynamicPointerCast<AttributeFilter>(arg);
84
85                 result = toJSValueRef(filter);
86         }
87         else if(arg->getFilterType() == ATTRIBUTE_RANGE_FILTER)
88         {
89                 AttributeRangeFilterPtr filter = DPL::DynamicPointerCast<AttributeRangeFilter>(arg);
90
91                 result = toJSValueRef(filter);
92         }
93
94         return result;
95 }
96
97 FilterPtr FilterConverter::toFilter(const JSValueRef& arg)
98 {
99         if(arg == NULL)
100                 ThrowMsg(NullPointerException, "Filter is NULL.");
101
102         if(!JSValueIsObject(m_context, arg))
103                 ThrowMsg(InvalidArgumentException, "Filter is not object.");
104
105         JSObjectRef jsObject = toJSObjectRef(arg);
106         if(jsObject == NULL)
107                 ThrowMsg(InvalidArgumentException, "Filter is not object.");
108
109         BasicValidator validator = ValidatorFactory<Validator>::getValidator(m_context);
110
111         if(validator->checkArrayKeys(m_compositeFilterAttrs, arg))
112         {
113                 CompositeFilterPtr compositeFilter = toCompositeFilter(arg);
114                 return DPL::StaticPointerCast<IFilter>(compositeFilter);
115         }
116         else if(validator->checkArrayKeys(m_attributeFilterAttrs, arg))
117         {
118                 AttributeFilterPtr attributeFilter = toAttributeFilter(arg);
119                 return DPL::StaticPointerCast<IFilter>(attributeFilter);
120         }
121         else if(validator->checkArrayKeys(m_attributeRangeFilterAttrs, arg))
122         {
123                 AttributeRangeFilterPtr attributeRangeFilter = toAttributeRangeFilter(arg);
124                 return DPL::StaticPointerCast<IFilter>(attributeRangeFilter);
125         }
126
127         ThrowMsg(InvalidArgumentException, "Not a Filter.");
128         return FilterPtr(NULL);
129 }
130
131 JSValueRef FilterConverter::toJSValueRef(const CompositeFilterPtr& arg)
132 {
133         JSObjectRef resultObject = JSObjectMake(m_context, NULL, NULL);
134
135         ScopedJSStringRef typeStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE));
136         FilterType filterType = arg->getFilterType();
137         string type;
138         if(filterType == UNION_FILTER)
139                 type = "UNION";
140         else if(filterType == INTERSECTION_FILTER)
141                 type = "INTERSECTION";
142         else
143                 ThrowMsg(ConversionException, "Invalid FilterType.");
144
145         ScopedJSStringRef typeValue(JSStringCreateWithUTF8CString(type.c_str()));
146         JSObjectSetProperty(m_context, resultObject,
147                         typeStr.get(),
148                         JSValueMakeString(m_context, typeValue.get()),
149                         kJSPropertyAttributeNone, NULL);
150
151         ScopedJSStringRef filtersStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS));
152         JSValueRef filters = toJSValueRef(arg->getFilters());
153         JSObjectSetProperty(m_context, resultObject,
154                         filtersStr.get(),
155                         filters,
156                         kJSPropertyAttributeNone, NULL);
157
158         return static_cast<JSValueRef>(resultObject);
159 }
160
161 CompositeFilterPtr FilterConverter::toCompositeFilter(const JSValueRef& arg)
162 {
163         JSObjectRef jsObject = toJSObjectRef(arg);
164
165         ScopedJSStringRef typeStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE));
166         JSValueRef typeValue = JSObjectGetProperty(m_context, jsObject, typeStr.get(), NULL);
167         if(!JSValueIsString(m_context, typeValue))
168                 ThrowMsg(InvalidArgumentException, "CompositeFilter.type is not an string.");
169         string type = toString(typeValue);
170         FilterType filterType;
171
172         if(type == "INTERSECTION")
173                 filterType = INTERSECTION_FILTER;
174         else if(type == "UNION")
175                 filterType = UNION_FILTER;
176         else
177                 ThrowMsg(InvalidArgumentException, "CompositeFilter.type is wrong.");
178
179         ScopedJSStringRef filtersStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS));
180         JSValueRef filtersValue = JSObjectGetProperty(m_context, jsObject, filtersStr.get(), NULL);
181         if(!JSIsArrayValue(m_context, filtersValue))
182                 ThrowMsg(InvalidArgumentException, "CompositeFilter.filters is not an array.");
183         FilterArrayPtr filters = toFilterArray(filtersValue);
184         if(filters->size() == 0)
185                 ThrowMsg(InvalidArgumentException, "CompositeFilter.filters array size is 0.");
186
187         return CompositeFilterPtr(new CompositeFilter(filterType, filters));
188 }
189
190 JSValueRef FilterConverter::toJSValueRef(const AttributeFilterPtr& arg)
191 {
192         AnyTypeConverterFactory::ConverterType converter =
193                         AnyTypeConverterFactory::getConverter(m_context);
194
195         JSObjectRef resultObject = JSObjectMake(m_context, NULL, NULL);
196
197         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
198         ScopedJSStringRef attributeNameValue(JSStringCreateWithUTF8CString(arg->getAttributeName().c_str()));
199         JSObjectSetProperty(m_context, resultObject,
200                         attributeNameStr.get(),
201                         JSValueMakeString(m_context, attributeNameValue.get()),
202                         kJSPropertyAttributeNone, NULL);
203
204         ScopedJSStringRef matchFlagStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG));
205         JSValueRef matchFlagValue = toJSValueRef(arg->getMatchFlag());
206         JSObjectSetProperty(m_context, resultObject,
207                         matchFlagStr.get(),
208                         matchFlagValue,
209                         kJSPropertyAttributeNone, NULL);
210
211         ScopedJSStringRef matchValuesStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES));
212         JSValueRef matchValuesValue = toJSValueRef(arg->getMatchValues());
213         JSObjectSetProperty(m_context, resultObject,
214                         matchValuesStr.get(),
215                         matchValuesValue,
216                         kJSPropertyAttributeNone, NULL);
217
218         ScopedJSStringRef caseSensitiveStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_CASE_SENSITIVE));
219         JSValueRef caseSensitiveValue = converter->toJSValueRef(arg->getCaseSensitive());
220         JSObjectSetProperty(m_context, resultObject,
221                         caseSensitiveStr.get(),
222                         caseSensitiveValue,
223                         kJSPropertyAttributeNone, NULL);
224
225         return static_cast<JSValueRef>(resultObject);
226 }
227
228 AttributeFilterPtr FilterConverter::toAttributeFilter(const JSValueRef& arg)
229 {
230         JSObjectRef jsObject = toJSObjectRef(arg);
231
232         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
233         JSValueRef attributeNameValue = JSObjectGetProperty(m_context, jsObject, attributeNameStr.get(), NULL);
234         if(!JSValueIsString(m_context, attributeNameValue))
235                 ThrowMsg(InvalidArgumentException, "AttributeFilter.attributeName is not an string.");
236         string attributeName = toString(attributeNameValue);
237         if(attributeName == "")
238                 ThrowMsg(InvalidArgumentException, "AttributeFilter.attributeName is empty string.");
239
240         ScopedJSStringRef matchFlagStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG));
241         JSValueRef matchFlagValue = JSObjectGetProperty(m_context, jsObject, matchFlagStr.get(), NULL);
242         string matchFlag;
243         if(JSValueIsUndefined(m_context, matchFlagValue)) {
244                 matchFlag = "EXACTLY";
245         } else if(!JSValueIsString(m_context, matchFlagValue)) {
246                 ThrowMsg(InvalidArgumentException, "AttributeFilter.matchFlag is not string.");
247         } else {
248                 matchFlag = toString(matchFlagValue);
249         }
250
251         ScopedJSStringRef matchValuesStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES));
252         JSValueRef matchValuesValue = JSObjectGetProperty(m_context, jsObject, matchValuesStr.get(), NULL);
253         AnyArrayPtr matchValues(NULL);
254
255         if(matchFlag == "EXISTS") {
256                 //matchValues = AnyArrayPtr(NULL);
257         } else if(JSValueIsUndefined(m_context, matchValuesValue)) {
258                 ThrowMsg(InvalidArgumentException, "AttributeFilter.matchValues is undefined.");
259         } else if(!JSIsArrayValue(m_context, matchValuesValue)) {
260                 ThrowMsg(InvalidArgumentException, "AttributeFilter.matchValues is not an array.");
261         } else {
262                 matchValues = toAnyArray(matchValuesValue);
263         }
264
265         ScopedJSStringRef caseSensitiveStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_CASE_SENSITIVE));
266         JSValueRef caseSensitiveValue = JSObjectGetProperty(m_context, jsObject, caseSensitiveStr.get(), NULL);
267         bool caseSensitive;
268         if(JSValueIsUndefined(m_context, caseSensitiveValue)) {
269                 if(matchFlag == "EXACTLY")
270                         caseSensitive = true;
271                 else
272                         caseSensitive = false;
273         } else {
274                 if(!JSValueIsBoolean(m_context, caseSensitiveValue)) {
275                         ThrowMsg(InvalidArgumentException, "AttributeFilter.caseSensitive is not boolean.");
276                 }
277                 caseSensitive = toBool(caseSensitiveValue);
278         }
279
280         return AttributeFilterPtr(new AttributeFilter(attributeName, matchValues, matchFlag, caseSensitive));
281 }
282
283 JSValueRef FilterConverter::toJSValueRef(const AttributeRangeFilterPtr& arg)
284 {
285         AnyTypeConverterFactory::ConverterType converter =
286                         AnyTypeConverterFactory::getConverter(m_context);
287
288         JSObjectRef resultObject = JSObjectMake(m_context, NULL, NULL);
289
290         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
291         ScopedJSStringRef attributeNameValue(JSStringCreateWithUTF8CString(arg->getAttributeName().c_str()));
292         JSObjectSetProperty(m_context, resultObject,
293                         attributeNameStr.get(),
294                         JSValueMakeString(m_context, attributeNameValue.get()),
295                         kJSPropertyAttributeNone, NULL);
296
297         ScopedJSStringRef initialValueStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE));
298         JSValueRef initialValueValue = converter->toJSValueRef(arg->getInitialValue());
299         JSObjectSetProperty(m_context, resultObject,
300                         initialValueStr.get(),
301                         initialValueValue,
302                         kJSPropertyAttributeNone, NULL);
303
304         ScopedJSStringRef endValueStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE));
305         JSValueRef endValueValue = converter->toJSValueRef(arg->getEndValue());
306         JSObjectSetProperty(m_context, resultObject,
307                         endValueStr.get(),
308                         endValueValue,
309                         kJSPropertyAttributeNone, NULL);
310
311         return static_cast<JSValueRef>(resultObject);
312 }
313
314 AttributeRangeFilterPtr FilterConverter::toAttributeRangeFilter(const JSValueRef& arg)
315 {
316         AnyTypeConverterFactory::ConverterType converter =
317                         AnyTypeConverterFactory::getConverter(m_context);
318
319         JSObjectRef jsObject = toJSObjectRef(arg);
320
321         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
322         JSValueRef attributeNameValue = JSObjectGetProperty(m_context, jsObject, attributeNameStr.get(), NULL);
323         if(!JSValueIsString(m_context, attributeNameValue))
324                 ThrowMsg(InvalidArgumentException, "AttributeRangeFilter.attributeName is not an string.");
325         string attributeName = toString(attributeNameValue);
326         if(attributeName == "")
327                 ThrowMsg(InvalidArgumentException, "AttributeRangeFilter.attributeName is empty string.");
328
329         ScopedJSStringRef initialValueStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE));
330         JSValueRef initialValueValue = JSObjectGetProperty(m_context, jsObject, initialValueStr.get(), NULL);
331         AnyPtr initialValue(NULL);
332         if(!JSValueIsUndefined(m_context, initialValueValue) && !JSValueIsNull(m_context, initialValueValue)) {
333                 initialValue = converter->toAny(initialValueValue, PrimitiveType_Int);
334         }
335
336         ScopedJSStringRef endValueStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE));
337         JSValueRef endValueValue = JSObjectGetProperty(m_context, jsObject, endValueStr.get(), NULL);
338         AnyPtr endValue(NULL);
339         if(!JSValueIsUndefined(m_context, endValueValue) && !JSValueIsNull(m_context, endValueValue)) {
340                 endValue = converter->toAny(endValueValue, PrimitiveType_Int);
341         }
342
343         return AttributeRangeFilterPtr(new AttributeRangeFilter(attributeName, initialValue, endValue));
344 }
345
346 JSValueRef FilterConverter::toJSValueRef(const SortModePtr& arg)
347 {
348         JSObjectRef resultObject = JSObjectMake(m_context, NULL, NULL);
349
350         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
351         ScopedJSStringRef attributeNameValue(JSStringCreateWithUTF8CString(arg->getAttributeName().c_str()));
352         JSObjectSetProperty(m_context, resultObject,
353                         attributeNameStr.get(),
354                         JSValueMakeString(m_context, attributeNameValue.get()),
355                         kJSPropertyAttributeNone, NULL);
356
357         ScopedJSStringRef orderStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER));
358         SortOrder sortOrder = arg->getOrder();
359         string order;
360         if(sortOrder == ASCENDING_SORT_ORDER)
361                 order = "ASC";
362         else if(sortOrder == DESCENDING_SORT_ORDER)
363                 order = "DESC";
364         else
365                 ThrowMsg(ConversionException, "Invalid SortMode.");
366
367         ScopedJSStringRef orderValue(JSStringCreateWithUTF8CString(order.c_str()));
368         JSObjectSetProperty(m_context, resultObject,
369                         orderStr.get(),
370                         JSValueMakeString(m_context, orderValue.get()),
371                         kJSPropertyAttributeNone, NULL);
372
373         return static_cast<JSValueRef>(resultObject);
374 }
375
376 SortModePtr FilterConverter::toSortMode(const JSValueRef& arg)
377 {
378         BasicValidator validator = ValidatorFactory<Validator>::getValidator(m_context);
379
380         if(validator->checkArrayKeys(m_compositeFilterAttrs, arg)) {
381                 ThrowMsg(InvalidArgumentException, "Not SortMode.");
382         }
383
384         JSObjectRef jsObject = toJSObjectRef(arg);
385
386         ScopedJSStringRef orderStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER));
387         JSValueRef orderValue = JSObjectGetProperty(m_context, jsObject, orderStr.get(), NULL);
388         if(!JSValueIsString(m_context, orderValue))
389                 ThrowMsg(InvalidArgumentException, "SortMode.order is not an string.");
390         string order = toString(orderValue);
391         SortOrder sortOrder;
392
393         if(order == "ASC")
394                 sortOrder = ASCENDING_SORT_ORDER;
395         else if(order == "DESC")
396                 sortOrder = DESCENDING_SORT_ORDER;
397         else
398                 ThrowMsg(InvalidArgumentException, "SortMode.order is wrong.");
399
400         ScopedJSStringRef attributeNameStr(JSStringCreateWithUTF8CString(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME));
401         JSValueRef attributeNameValue = JSObjectGetProperty(m_context, jsObject, attributeNameStr.get(), NULL);
402         if(!JSValueIsString(m_context, attributeNameValue))
403                 ThrowMsg(InvalidArgumentException, "SortMode.attributeName is not an string.");
404         string attributeName = toString(attributeNameValue);
405         if(attributeName == "")
406                 ThrowMsg(InvalidArgumentException, "SortMode.attributeName is empty string.");
407
408         return SortModePtr(new SortMode(attributeName, sortOrder));
409 }
410
411 JSValueRef FilterConverter::toJSValueRef(const SortModeArrayPtr& arg)
412 {
413         int size = arg->size();
414
415         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
416         if (!resultObject)
417                 ThrowMsg(ConversionException, "Can not create array object.");
418
419         for(int i = 0; i < size; i++)
420         {
421                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
422                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
423                         ThrowMsg(ConversionException, "Can not fill SortMode array.");
424         }
425
426         return static_cast<JSValueRef>(resultObject);
427 }
428
429 SortModeArrayPtr FilterConverter::toSortModeArray(const JSValueRef& arg)
430 {
431         if(!JSIsArrayValue(m_context, arg))
432                 ThrowMsg(InvalidArgumentException, "Not an array type.");
433
434         SortModeArrayPtr sortModeArray = SortModeArrayPtr(new SortModeArray());
435         JSObjectRef object = toJSObjectRef(arg);
436
437         unsigned int length = JSGetArrayLength(m_context, object);
438         for(unsigned int i=0; i<length; i++)
439         {
440                 JSValueRef value = JSGetArrayElement(m_context, object, i);
441
442                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
443                         ThrowMsg(InvalidArgumentException, "CompositeFilter.filters has null elements.");
444                 }
445
446                 SortModePtr sortMode = toSortMode(value);
447
448                 sortModeArray->push_back(sortMode);
449         }
450
451         return sortModeArray;
452 }
453
454 JSValueRef FilterConverter::toJSValueRef(const AnyArrayPtr& arg)
455 {
456         AnyTypeConverterFactory::ConverterType converter =
457                         AnyTypeConverterFactory::getConverter(m_context);
458
459         int size = arg->size();
460
461         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
462         if (!resultObject)
463                 ThrowMsg(ConversionException, "Can not create array object.");
464
465         for(int i = 0; i < size; i++)
466         {
467                 JSValueRef jsvalue = converter->toJSValueRef(arg->at(i));
468                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
469                         ThrowMsg(ConversionException, "Can not fill any array.");
470         }
471
472         return static_cast<JSValueRef>(resultObject);
473 }
474
475 AnyArrayPtr FilterConverter::toAnyArray(const JSValueRef& arg)
476 {
477         AnyArrayPtr retVal;
478
479         retVal = AnyArrayPtr(new AnyArray());
480
481         JSObjectRef obj = toJSObjectRef(arg);
482         AnyTypeConverterFactory::ConverterType converter =
483                         AnyTypeConverterFactory::getConverter(m_context);
484
485         unsigned int length = JSGetArrayLength(m_context, obj);
486         for(unsigned int i=0; i<length; i++)
487         {
488                 JSValueRef value = JSGetArrayElement(m_context, obj, i);
489
490                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
491                         ThrowMsg(InvalidArgumentException, "IDFilter.ids has null elements.");
492                 }
493                 AnyPtr any = converter->toAny(value, PrimitiveType_Int);
494
495                 retVal->push_back(any);
496         }
497
498         return retVal;
499 }
500
501 JSValueRef FilterConverter::toJSValueRef(const FilterArrayPtr& arg)
502 {
503         int size = arg->size();
504
505         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
506         if (!resultObject)
507                 ThrowMsg(ConversionException, "Can not create array object.");
508
509         for(int i = 0; i < size; i++)
510         {
511                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
512                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
513                         ThrowMsg(ConversionException, "Can not fill filter array.");
514         }
515
516         return static_cast<JSValueRef>(resultObject);
517 }
518
519 FilterArrayPtr FilterConverter::toFilterArray(const JSValueRef& arg)
520 {
521         FilterArrayPtr retVal;
522
523         JSObjectRef obj = toJSObjectRef(arg);
524         retVal = FilterArrayPtr(new FilterArray());
525
526         unsigned int length = JSGetArrayLength(m_context, obj);
527         for(unsigned int i=0; i<length; i++)
528         {
529                 JSValueRef value = JSGetArrayElement(m_context, obj, i);
530
531                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
532                         ThrowMsg(InvalidArgumentException, "CompositeFilter.filters has null elements.");
533                 }
534
535                 FilterPtr filter = toFilter(value);
536
537                 retVal->push_back(filter);
538         }
539
540         return retVal;
541 }
542
543 void FilterConverter::initFilterAttrs()
544 {
545         m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE);
546         m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS);
547
548         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
549         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG);
550         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES);
551         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_CASE_SENSITIVE);
552
553         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
554         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE);
555         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE);
556
557         m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER);
558         m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
559 }
560
561 } // Tizen
562 } // Tizen1_0
563 } // TizenApis