fe307959803fda65ca4c5f4d3be29dcdc340c6e7
[framework/web/wrt-plugins-tizen.git] / src / Tizen / FilterConverter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FilterConverter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @description Reference from CalendarConverter.cpp
23  */
24
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 "JSAbstractFilterArray.h"
33 #include "JSSortMode.h"
34 #include "FilterConverter.h"
35
36 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE                           "type"
37 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER                          "order"
38 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS                        "filters"
39 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_IDS                            "ids"
40 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME         "attributeName"
41 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG                     "matchFlag"
42 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES           "matchValues"
43 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE          "initialValue"
44 #define TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE                      "endValue"
45
46 namespace DeviceAPI {\rnamespace Tizen {
47
48 using namespace DeviceAPI::Tizen;
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
51
52 using namespace std;
53
54 FilterConverter::FilterConverter(JSContextRef context) : Converter(context)
55 {
56         initFilterAttrs();
57 }
58
59 FilterConverter::~FilterConverter()
60 {
61         LogDebug("entered");
62 }
63
64 JSValueRef FilterConverter::toJSValueRef(FilterPtr arg)
65 {
66         if(arg == NULL)
67                 ThrowMsg(NullPointerException, "Filter is NULL.");
68
69         JSValueRef result = NULL;
70
71         if(arg->getFilterType() == UNION_FILTER || arg->getFilterType() == INTERSECTION_FILTER)
72         {
73                 CompositeFilterPtr filter = DPL::DynamicPointerCast<CompositeFilter>(arg);
74
75                 result = toJSValueRef(filter);
76         }
77         else if(arg->getFilterType() == ATTRIBUTE_FILTER)
78         {
79                 AttributeFilterPtr filter = DPL::DynamicPointerCast<AttributeFilter>(arg);
80
81                 result = toJSValueRef(filter);
82         }
83         else if(arg->getFilterType() == ATTRIBUTE_RANGE_FILTER)
84         {
85                 AttributeRangeFilterPtr filter = DPL::DynamicPointerCast<AttributeRangeFilter>(arg);
86
87                 result = toJSValueRef(filter);
88         }
89
90         return result;
91 }
92
93 FilterPtr FilterConverter::toFilter(const JSValueRef& arg)
94 {
95         if(arg == NULL)
96                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
97
98         if(!JSValueIsObject(m_context, arg))
99                 ThrowMsg(InvalidArgumentException, "Filter is not object.");
100
101         if(JSCompositeFilter::isObjectOfClass(m_context, arg))
102                 return DPL::StaticPointerCast<IFilter>(JSCompositeFilter::getCompositeFilter(m_context, arg));
103
104         if(JSAttributeFilter::isObjectOfClass(m_context, arg))
105                 return DPL::StaticPointerCast<IFilter>(JSAttributeFilter::getAttributeFilter(m_context, arg));
106
107         if(JSAttributeRangeFilter::isObjectOfClass(m_context, arg))
108                 return DPL::StaticPointerCast<IFilter>(JSAttributeRangeFilter::getAttributeRangeFilter(m_context, arg));
109
110         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
111         return FilterPtr(NULL);
112 }
113
114 JSValueRef FilterConverter::toJSValueRef(const FilterArrayPtr& arg)
115 {
116         if(arg == NULL)
117                 ThrowMsg(NullPointerException, "FilterArray is NULL.");
118
119         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSAbstractFilterArray::getClassRef(), arg);
120 }
121
122 FilterArrayPtr FilterConverter::toFilterArray(const JSValueRef& arg)
123 {
124         if(arg == NULL)
125                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
126
127         if(JSAbstractFilterArray::isObjectOfClass(m_context, arg))
128                 return JSAbstractFilterArray::getAbstractFilterArray(m_context, arg);
129
130         if(!JSIsArrayValue(m_context, arg))
131                 ThrowMsg(InvalidArgumentException, "JSValueRef is not Array.");
132
133         FilterArrayPtr retVal;
134
135         JSObjectRef obj = toJSObjectRef(arg);
136         retVal = FilterArrayPtr(new FilterArray());
137
138         unsigned int length = JSGetArrayLength(m_context, obj);
139         for(unsigned int i=0; i<length; i++)
140         {
141                 JSValueRef value = JSGetArrayElement(m_context, obj, i);
142
143                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
144                         ThrowMsg(InvalidArgumentException, "CompositeFilter.filters has null elements.");
145                 }
146
147                 FilterPtr filter;
148                 Try {
149                         filter = toFilter(value);
150                 } Catch (Exception) {
151                         ThrowMsg(InvalidArgumentException, "Element is not an AbstractFilter (" << i << ")");
152                 }
153
154                 retVal->push_back(filter);
155         }
156
157         return retVal;
158 }
159
160 JSValueRef FilterConverter::toJSValueRef(const CompositeFilterPtr& arg)
161 {
162         if(arg == NULL)
163                 ThrowMsg(NullPointerException, "CompositeFilter is NULL.");
164
165         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSCompositeFilter::getClassRef(), arg);
166 }
167
168 CompositeFilterPtr FilterConverter::toCompositeFilter(const JSValueRef& arg)
169 {
170         if(arg == NULL)
171                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
172
173         if(!JSCompositeFilter::isObjectOfClass(m_context, arg))
174                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
175
176         return JSCompositeFilter::getCompositeFilter(m_context, arg);
177 }
178
179 JSValueRef FilterConverter::toJSValueRef(const AttributeFilterPtr& arg)
180 {
181         if(arg == NULL)
182                 ThrowMsg(NullPointerException, "AttributeFilter is NULL.");
183
184         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSAttributeFilter::getClassRef(), arg);
185 }
186
187 AttributeFilterPtr FilterConverter::toAttributeFilter(const JSValueRef& arg)
188 {
189         if(arg == NULL)
190                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
191
192         if(!JSAttributeFilter::isObjectOfClass(m_context, arg))
193                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
194
195         return JSAttributeFilter::getAttributeFilter(m_context, arg);
196 }
197
198 JSValueRef FilterConverter::toJSValueRef(const AttributeRangeFilterPtr& arg)
199 {
200         if(arg == NULL)
201                 ThrowMsg(NullPointerException, "AttributeRangeFilter is NULL.");
202
203         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSAttributeRangeFilter::getClassRef(), arg);
204 }
205
206 AttributeRangeFilterPtr FilterConverter::toAttributeRangeFilter(const JSValueRef& arg)
207 {
208         if(arg == NULL)
209                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
210
211         if(!JSAttributeRangeFilter::isObjectOfClass(m_context, arg))
212                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
213
214         return JSAttributeRangeFilter::getAttributeRangeFilter(m_context, arg);
215 }
216
217 JSValueRef FilterConverter::toJSValueRef(const SortModePtr& arg)
218 {
219         if(arg == NULL)
220                 ThrowMsg(NullPointerException, "SortMode is NULL.");
221
222         return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(m_context, JSSortMode::getClassRef(), arg);
223 }
224
225 SortModePtr FilterConverter::toSortMode(const JSValueRef& arg)
226 {
227         if(arg == NULL)
228                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
229
230         if(!JSSortMode::isObjectOfClass(m_context, arg))
231                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Wrong attribute");
232
233         return JSSortMode::getSortMode(m_context, arg);
234 }
235
236 JSValueRef FilterConverter::toJSValueRef(const SortModeArrayPtr& arg)
237 {
238         // Not used any more.
239         if(arg == NULL)
240                 ThrowMsg(NullPointerException, "SortModeArray is NULL.");
241
242         int size = arg->size();
243
244         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
245         if (!resultObject)
246                 ThrowMsg(ConversionException, "Can not create array object.");
247
248         for(int i = 0; i < size; i++)
249         {
250                 JSValueRef jsvalue = toJSValueRef(arg->at(i));
251                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
252                         ThrowMsg(ConversionException, "Can not fill SortMode array.");
253         }
254
255         return static_cast<JSValueRef>(resultObject);
256 }
257
258 SortModeArrayPtr FilterConverter::toSortModeArray(const JSValueRef& arg)
259 {
260         // Not used any more.
261         if(arg == NULL)
262                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
263
264         if(!JSIsArrayValue(m_context, arg))
265                 ThrowMsg(InvalidArgumentException, "Not an array type.");
266
267         SortModeArrayPtr sortModeArray = SortModeArrayPtr(new SortModeArray());
268         JSObjectRef object = toJSObjectRef(arg);
269
270         unsigned int length = JSGetArrayLength(m_context, object);
271         for(unsigned int i=0; i<length; i++)
272         {
273                 JSValueRef value = JSGetArrayElement(m_context, object, i);
274
275                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
276                         ThrowMsg(InvalidArgumentException, "CompositeFilter.filters has null elements.");
277                 }
278
279                 SortModePtr sortMode = toSortMode(value);
280
281                 sortModeArray->push_back(sortMode);
282         }
283
284         return sortModeArray;
285 }
286
287 JSValueRef FilterConverter::toJSValueRef(const AnyPtr& arg)
288 {
289         if(arg == NULL)
290                 ThrowMsg(NullPointerException, "Any is NULL.");
291
292         AnyTypeConverterFactory::ConverterType converter =
293                         AnyTypeConverterFactory::getConverter(m_context);
294
295         return converter->toJSValueRef(arg);
296 }
297
298 AnyPtr FilterConverter::toAny(const JSValueRef& arg)
299 {
300         if(arg == NULL)
301                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
302
303         AnyArrayPtr retVal;
304
305         AnyTypeConverterFactory::ConverterType converter =
306                         AnyTypeConverterFactory::getConverter(m_context);
307
308         return converter->toAny(arg);
309 }
310
311 JSValueRef FilterConverter::toJSValueRef(const AnyArrayPtr& arg)
312 {
313         if(arg == NULL)
314                 ThrowMsg(NullPointerException, "AnyArray is NULL.");
315
316         AnyTypeConverterFactory::ConverterType converter =
317                         AnyTypeConverterFactory::getConverter(m_context);
318
319         int size = arg->size();
320
321         JSObjectRef resultObject = JSCreateArrayObject(m_context, 0, NULL);
322         if (!resultObject)
323                 ThrowMsg(ConversionException, "Can not create array object.");
324
325         for(int i = 0; i < size; i++)
326         {
327                 JSValueRef jsvalue = converter->toJSValueRef(arg->at(i));
328                 if (!JSSetArrayElement(m_context, resultObject, i, jsvalue))
329                         ThrowMsg(ConversionException, "Can not fill any array.");
330         }
331
332         return static_cast<JSValueRef>(resultObject);
333 }
334
335 AnyArrayPtr FilterConverter::toAnyArray(const JSValueRef& arg)
336 {
337         if(arg == NULL)
338                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
339
340         AnyArrayPtr retVal;
341
342         retVal = AnyArrayPtr(new AnyArray());
343
344         JSObjectRef obj = toJSObjectRef(arg);
345         AnyTypeConverterFactory::ConverterType converter =
346                         AnyTypeConverterFactory::getConverter(m_context);
347
348         unsigned int length = JSGetArrayLength(m_context, obj);
349         for(unsigned int i=0; i<length; i++)
350         {
351                 JSValueRef value = JSGetArrayElement(m_context, obj, i);
352
353                 if(JSValueIsNull(m_context, value) ||  JSValueIsUndefined(m_context, value)) {
354                         ThrowMsg(InvalidArgumentException, "IDFilter.ids has null elements.");
355                 }
356                 AnyPtr any = converter->toAny(value);
357
358                 retVal->push_back(any);
359         }
360
361         return retVal;
362 }
363
364 JSValueRef FilterConverter::toJSValueRef(const FilterType& arg)
365 {
366         string compositeFilterTypeStr;
367
368         if(arg == UNION_FILTER)
369                 compositeFilterTypeStr = "UNION";
370         else if(arg == INTERSECTION_FILTER)
371                 compositeFilterTypeStr = "INTERSECTION";
372         else
373                 ThrowMsg(ConversionException, "Can not create CompositeFilterType.");
374
375         return toJSValueRef(compositeFilterTypeStr);
376 }
377
378 FilterType FilterConverter::toCompositeFilterType(const JSValueRef& arg)
379 {
380         if(arg == NULL)
381                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
382
383         if(!JSValueIsString(m_context, arg))
384                 ThrowMsg(InvalidArgumentException, "Value is not an string.");
385
386         string compositeFilterTypeStr = toString(arg);
387         FilterType compositeFilterType;
388
389         if(compositeFilterTypeStr == "UNION")
390                 compositeFilterType = UNION_FILTER;
391         else if(compositeFilterTypeStr == "INTERSECTION")
392                 compositeFilterType = INTERSECTION_FILTER;
393         else
394                 ThrowMsg(InvalidArgumentException, "CompositeFilterType cannot have " << compositeFilterTypeStr);
395
396         return compositeFilterType;
397 }
398
399 JSValueRef FilterConverter::toJSValueRef(const MatchFlag& arg)
400 {
401         string matchFlagStr;
402
403         if(arg == MATCH_EXACTLY)
404                 matchFlagStr = "EXACTLY";
405         else if(arg == MATCH_FULLSTRING)
406                 matchFlagStr = "FULLSTRING";
407         else if(arg == MATCH_CONTAINS)
408                 matchFlagStr = "CONTAINS";
409         else if(arg == MATCH_STARTSWITH)
410                 matchFlagStr = "STARTSWITH";
411         else if(arg == MATCH_ENDSWITH)
412                 matchFlagStr = "ENDSWITH";
413         else if(arg == MATCH_EXISTS)
414                 matchFlagStr = "EXISTS";
415         else
416                 ThrowMsg(ConversionException, "Can not create MatchFlag.");
417
418         return toJSValueRef(matchFlagStr);
419 }
420
421 MatchFlag FilterConverter::toMatchFlag(const JSValueRef& arg)
422 {
423         if(arg == NULL)
424                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
425
426         if(!JSValueIsString(m_context, arg))
427                 ThrowMsg(InvalidArgumentException, "Value is not an string.");
428
429         string matchFlagStr = toString(arg);
430         MatchFlag matchFlag;
431
432         if(matchFlagStr == "EXACTLY")
433                 matchFlag = MATCH_EXACTLY;
434         else if(matchFlagStr == "FULLSTRING")
435                 matchFlag = MATCH_FULLSTRING;
436         else if(matchFlagStr == "CONTAINS")
437                 matchFlag = MATCH_CONTAINS;
438         else if(matchFlagStr == "STARTSWITH")
439                 matchFlag = MATCH_STARTSWITH;
440         else if(matchFlagStr == "ENDSWITH")
441                 matchFlag = MATCH_ENDSWITH;
442         else if(matchFlagStr == "EXISTS")
443                 matchFlag = MATCH_EXISTS;
444         else
445                 ThrowMsg(InvalidArgumentException, "FilterMatchFlag cannot have " << matchFlagStr);
446
447         return matchFlag;
448 }
449
450 JSValueRef FilterConverter::toJSValueRef(const SortOrder& arg)
451 {
452         string sortOrderStr;
453
454         if(arg == ASCENDING_SORT_ORDER)
455                 sortOrderStr = "ASC";
456         else if(arg == DESCENDING_SORT_ORDER)
457                 sortOrderStr = "DESC";
458         else
459                 ThrowMsg(ConversionException, "Can not create SortModeOrder.");
460
461         return toJSValueRef(sortOrderStr);
462 }
463
464 SortOrder FilterConverter::toSortOrder(const JSValueRef& arg)
465 {
466         if(arg == NULL)
467                 ThrowMsg(NullPointerException, "JSValueRef is NULL.");
468
469         if(!JSValueIsString(m_context, arg))
470                 ThrowMsg(InvalidArgumentException, "Value is not an string.");
471
472         string sortOrderStr = toString(arg);
473         SortOrder sortOrder;
474
475         if(sortOrderStr == "ASC")
476                 sortOrder = ASCENDING_SORT_ORDER;
477         else if(sortOrderStr == "DESC")
478                 sortOrder = DESCENDING_SORT_ORDER;
479         else
480                 ThrowMsg(InvalidArgumentException, "SortModeOrder cannot have " << sortOrderStr);
481
482         return sortOrder;
483 }
484
485 void FilterConverter::initFilterAttrs()
486 {
487         m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_TYPE);
488         m_compositeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_FILTERS);
489
490         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
491         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_FLAG);
492         m_attributeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_MATCH_VALUES);
493
494         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
495         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_INITIAL_VALUE);
496         m_attributeRangeFilterAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_END_VALUE);
497
498         m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ORDER);
499         m_sortModeAttrs.push_back(TIZEN_FILTER_CONVERTER_ATTRIBUTE_ATTRIBUTE_NAME);
500 }
501
502 } // Tizen
503 } // DeviceAPI
504