e6eeb1756741ca5b2d9584c7a64681f9f11a3ddf
[framework/web/wrt-plugins-tizen.git] / src / DataControl / JSSelectObjectArrayValues.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 #include <algorithm>
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <JSTizenExceptionFactory.h>
22 #include <JSTizenException.h>
23 #include "DataControlFactory.h"
24 #include "RowData.h"
25 #include "DataControlConverter.h"
26 #include "JSSelectObjectArrayValues.h"
27 #include "DataControlConverter.h"
28
29 #define FUNCTION_CONCAT "concat"
30 #define FUNCTION_JOIN "join"
31 #define FUNCTION_POP "pop"
32 #define FUNCTION_PUSH "push"
33 #define FUNCTION_REVERSE "reverse"
34 #define FUNCTION_SHIFT "shift"
35 #define FUNCTION_SLICE "slice"
36 #define FUNCTION_SORT "sort"
37 #define FUNCTION_SPLICE "splice"
38 #define FUNCTION_TOSTRING "toString"
39 #define FUNCTION_UNSHIFT "unshift"
40 #define FUNCTION_VALUEOF "valueOf"
41 #define ARRAY "Array"
42 #define ATTRIBUTE_LENGTH "length"
43
44 namespace DeviceAPI {
45 namespace DataControl {
46
47 using namespace DeviceAPI::Common;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49
50 JSClassDefinition JSSelectObjectArrayValues::m_classInfo = {
51         0,
52         kJSClassAttributeNone,
53         ARRAY,
54         0,
55         m_property,
56         m_function,
57         initialize,
58         finalize,
59         hasProperty,
60         getProperty,
61         NULL,
62         NULL,
63         getPropertyNames,
64         NULL, //callAsFunction,
65         NULL, //callAsConstructor,
66         NULL, //hasInstance,
67         NULL, //convertToType,
68 };
69
70 JSStaticValue JSSelectObjectArrayValues::m_property[] = {
71         { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSSelectObjectArrayValues::m_function[] = {
76         { FUNCTION_CONCAT, concat, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
77         { FUNCTION_JOIN, join, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
78         { FUNCTION_POP, pop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
79         { FUNCTION_PUSH, push, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
80         { FUNCTION_REVERSE, reverse, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
81         { FUNCTION_SHIFT, shift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
82         { FUNCTION_SLICE, slice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
83         { FUNCTION_SORT, sort, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
84         { FUNCTION_SPLICE, splice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
85         { FUNCTION_TOSTRING, toString, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
86         { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
87         { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete },
88         { 0, 0, 0 }
89 };
90
91 JSClassRef JSSelectObjectArrayValues::m_jsClassRef = JSClassCreate(
92                 JSSelectObjectArrayValues::getClassInfo());
93
94 JSValueRef JSSelectObjectArrayValues::getLength(JSContextRef context,
95                 JSObjectRef object,
96                 JSStringRef propertyName,
97                 JSValueRef* exception)
98 {
99         Try
100         {
101                 JSSelectObjectArrayValuesPriv* priv = static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
102
103                 if (!priv) 
104                 {
105                         Throw(WrtDeviceApis::Commons::NullPointerException);
106                 }
107                 
108                 DataControlConverter converter(priv->getContext());
109                 ISelectDataObjectPtr SelectDataOjbect = priv->getObject();
110
111                 int number = SelectDataOjbect->getRowNumber();
112                 return converter.toJSValueRef(number);  
113         }
114         Catch(WrtDeviceApis::Commons::Exception)
115         {
116                 LogError("invalid property");
117         }
118
119         return JSValueMakeUndefined(context);
120 }
121
122
123 const JSClassDefinition* JSSelectObjectArrayValues::getClassInfo()
124 {
125         return &(m_classInfo);
126 }
127
128 bool JSSelectObjectArrayValues::isObjectOfClass(JSContextRef context, JSValueRef value)
129 {
130         return JSValueIsObjectOfClass(context, value, getClassRef());
131 }
132
133
134 JSClassRef JSSelectObjectArrayValues::getClassRef()
135 {
136         if (!m_jsClassRef) {
137                 m_jsClassRef = JSClassCreate(&m_classInfo);
138         }
139         return m_jsClassRef;
140 }
141
142 void JSSelectObjectArrayValues::initialize(JSContextRef context,
143                 JSObjectRef object)
144 {
145 }
146
147 void JSSelectObjectArrayValues::finalize(JSObjectRef object)
148 {
149         JSSelectObjectArrayValuesPriv* priv =
150                 static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
151         delete priv;
152         JSObjectSetPrivate(object, NULL);
153 }
154
155 bool JSSelectObjectArrayValues::hasProperty(JSContextRef context,
156                 JSObjectRef object,
157                 JSStringRef propertyName)
158 {
159         Try
160         {
161                 LogDebug("Enter");
162                 JSSelectObjectArrayValuesPriv* priv = static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
163
164                 if (!priv) 
165                 {
166                         Throw(WrtDeviceApis::Commons::NullPointerException);
167                 }
168                 
169                 DataControlConverter converter(priv->getContext());
170                 size_t index = converter.toSizeT(propertyName);
171
172                 LogDebug(index);
173                 ISelectDataObjectPtr SelectDataOjbect = priv->getObject();
174
175                 if (index < SelectDataOjbect->getRowNumber()) 
176                 {
177                         return true;
178                 }
179         }
180         Catch(WrtDeviceApis::Commons::Exception)
181         {
182                 LogError("invalid property");
183         }
184         return false;
185 }
186
187 JSValueRef JSSelectObjectArrayValues::getProperty(JSContextRef context,
188                 JSObjectRef object,
189                 JSStringRef propertyName,
190                 JSValueRef* exception)
191 {
192         Try
193         {
194                 LogDebug("Eneter");
195                 JSSelectObjectArrayValuesPriv* priv = static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
196
197                 if (!priv) 
198                 {
199                         Throw(WrtDeviceApis::Commons::NullPointerException);
200                 }
201                 
202                 DataControlConverter converter(priv->getContext());
203                 size_t index = converter.toSizeT(propertyName);
204
205                 LogDebug(index);
206                 ISelectDataObjectPtr SelectDataOjbect = priv->getObject();
207
208                 if (index >= SelectDataOjbect->getRowNumber())
209                 {
210                         Throw(WrtDeviceApis::Commons::OutOfRangeException);
211                 }
212
213                 EventGetIndexedRowPtr event(new EventGetIndexedRow);
214                 event->setIndex(index);
215                 
216                 if (event->setForSynchronousCall()) 
217                 { 
218
219                         SelectDataOjbect->getIndexedRow(event);
220
221                         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None)
222                         {
223                                 RowDataPtr data = event->getRowData();
224                                 return converter.toJSRowData(data);
225                         }
226                         else 
227                         {
228                                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
229                         }
230                 }
231         }
232         Catch(WrtDeviceApis::Commons::Exception)
233         {
234                 LogError("invalid property");
235         }
236
237         return JSValueMakeUndefined(context);
238 }
239
240 JSObjectRef JSSelectObjectArrayValues::createJSObject(JSContextRef context, const std::string &filepath)
241 {
242         ISelectDataObjectPtr SelectDataObject(DataControlFactory::getInstance().getSelectDataObject());
243         SelectDataObject->openResultPath(filepath);
244 //      SelectDataObject->loadHeader();
245 //      SelectDataObject->printHeader();
246         JSSelectObjectArrayValuesPriv * priv = new JSSelectObjectArrayValuesPriv( context, SelectDataObject);
247         return JSObjectMake(context, getClassRef(), priv);
248 }
249
250
251 bool JSSelectObjectArrayValues::setProperty(JSContextRef context,
252                 JSObjectRef object,
253                 JSStringRef propertyName,
254                 JSValueRef value,
255                 JSValueRef* exception)
256 {
257         
258 /*      ContactConverterFactory::ConverterType converter =
259                         ContactConverterFactory::getConverter(context);
260         Try
261         {
262                 size_t index = converter->toSizeT(propertyName);
263                 AddressBookPtr addressBook(NULL);
264                 if (!JSValueIsUndefined(context, value)) {
265                         addressBook = converter->toAddressBook(value);
266                 }
267                 JSSelectObjectArrayValuesPriv* priv =
268                         static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
269                 if (!priv) {
270                         Throw(WrtDeviceApis::Commons::NullPointerException);
271                 }
272                 AddressBookArrayPtr addressBooks = priv->getObject();
273                 if (!addressBooks) {
274                         Throw(WrtDeviceApis::Commons::NullPointerException);
275                 }
276                 if (addressBooks->size() <= index) {
277                         addressBooks->resize(index + 1);
278                 }
279                 (*addressBooks)[index] = addressBook;
280                 return true;
281         }
282         Catch(WrtDeviceApis::Commons::Exception)
283         {
284                 LogError("error occured");
285                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
286         }*/
287         return false;
288 }
289
290 bool JSSelectObjectArrayValues::deleteProperty(JSContextRef context,
291                 JSObjectRef object,
292                 JSStringRef propertyName,
293                 JSValueRef* exception)
294 {
295 /*      ContactConverterFactory::ConverterType converter =
296                         ContactConverterFactory::getConverter(context);
297         Try
298         {
299                 size_t index = converter->toSizeT(propertyName);
300                 AddressBookPtr addressBook(NULL);
301                 JSSelectObjectArrayValuesPriv* priv =
302                         static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
303                 if (!priv) {
304                         Throw(WrtDeviceApis::Commons::NullPointerException);
305                 }
306                 AddressBookArrayPtr addressBooks = priv->getObject();
307                 if (!addressBooks) {
308                         Throw(WrtDeviceApis::Commons::NullPointerException);
309                 }
310                 if (addressBooks->size() > index) {
311                         (*addressBooks)[index] = addressBook;
312                 }
313                 return true;
314         }
315         Catch(WrtDeviceApis::Commons::Exception)
316         {
317                 LogError("error occured");
318                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
319         }*/
320         return false;
321 }
322
323 void JSSelectObjectArrayValues::getPropertyNames(JSContextRef context,
324                 JSObjectRef object,
325                 JSPropertyNameAccumulatorRef propertyNames)
326 {
327         Try
328         {
329                 LogDebug("Eneter");
330                 JSSelectObjectArrayValuesPriv* priv = static_cast<JSSelectObjectArrayValuesPriv*>(JSObjectGetPrivate(object));
331
332                 if (!priv) 
333                 {
334                         Throw(WrtDeviceApis::Commons::NullPointerException);
335                 }
336                 
337                 DataControlConverter converter(priv->getContext());
338                 ISelectDataObjectPtr SelectDataOjbect = priv->getObject();
339                 int number = SelectDataOjbect->getRowNumber();
340
341                 for (int i=0; i < number; i++)
342                 {
343                         ScopedJSStringRef name(converter.toJSStringRef(converter.toString(i)));
344                         JSPropertyNameAccumulatorAddName(propertyNames, name.get());
345                 }
346         }
347         Catch(WrtDeviceApis::Commons::Exception)
348         {
349                 LogError("invalid property");
350         }
351 }
352
353 JSValueRef JSSelectObjectArrayValues::concat(JSContextRef context,
354                 JSObjectRef function,
355                 JSObjectRef thisObject,
356                 size_t argumentCount,
357                 const JSValueRef arguments[],
358                 JSValueRef* exception)
359 {
360         return JSValueMakeUndefined(context);
361 }
362
363 JSValueRef JSSelectObjectArrayValues::join(JSContextRef context,
364                 JSObjectRef function,
365                 JSObjectRef thisObject,
366                 size_t argumentCount,
367                 const JSValueRef arguments[],
368                 JSValueRef* exception)
369 {
370         return JSValueMakeUndefined(context);
371 }
372
373 JSValueRef JSSelectObjectArrayValues::pop(JSContextRef context,
374                 JSObjectRef function,
375                 JSObjectRef thisObject,
376                 size_t argumentCount,
377                 const JSValueRef arguments[],
378                 JSValueRef* exception)
379 {
380         return JSValueMakeUndefined(context);
381 }
382
383 JSValueRef JSSelectObjectArrayValues::push(JSContextRef context,
384                 JSObjectRef function,
385                 JSObjectRef thisObject,
386                 size_t argumentCount,
387                 const JSValueRef arguments[],
388                 JSValueRef* exception)
389 {
390         return JSValueMakeUndefined(context);
391 }
392
393 JSValueRef JSSelectObjectArrayValues::reverse(JSContextRef context,
394                 JSObjectRef function,
395                 JSObjectRef thisObject,
396                 size_t argumentCount,
397                 const JSValueRef arguments[],
398                 JSValueRef* exception)
399 {
400         return JSValueMakeUndefined(context);
401 }
402
403 JSValueRef JSSelectObjectArrayValues::shift(JSContextRef context,
404                 JSObjectRef function,
405                 JSObjectRef thisObject,
406                 size_t argumentCount,
407                 const JSValueRef arguments[],
408                 JSValueRef* exception)
409 {
410         return JSValueMakeUndefined(context);
411 }
412
413 JSValueRef JSSelectObjectArrayValues::slice(JSContextRef context,
414                 JSObjectRef function,
415                 JSObjectRef thisObject,
416                 size_t argumentCount,
417                 const JSValueRef arguments[],
418                 JSValueRef* exception)
419 {
420         return JSValueMakeUndefined(context);
421 }
422
423 JSValueRef JSSelectObjectArrayValues::sort(JSContextRef context,
424                 JSObjectRef function,
425                 JSObjectRef thisObject,
426                 size_t argumentCount,
427                 const JSValueRef arguments[],
428                 JSValueRef* exception)
429 {
430         return JSValueMakeUndefined(context);
431 }
432
433 JSValueRef JSSelectObjectArrayValues::splice(JSContextRef context,
434                 JSObjectRef function,
435                 JSObjectRef thisObject,
436                 size_t argumentCount,
437                 const JSValueRef arguments[],
438                 JSValueRef* exception)
439 {
440         return JSValueMakeUndefined(context);
441 }
442
443 JSValueRef JSSelectObjectArrayValues::toString(JSContextRef context,
444                 JSObjectRef function,
445                 JSObjectRef thisObject,
446                 size_t argumentCount,
447                 const JSValueRef arguments[],
448                 JSValueRef* exception)
449 {
450         return join(context, function, thisObject, 0, arguments, exception);
451 }
452
453 JSValueRef JSSelectObjectArrayValues::unshift(JSContextRef context,
454                 JSObjectRef function,
455                 JSObjectRef thisObject,
456                 size_t argumentCount,
457                 const JSValueRef arguments[],
458                 JSValueRef* exception)
459 {
460         return JSValueMakeUndefined(context);
461 }
462
463 JSValueRef JSSelectObjectArrayValues::valueOf(JSContextRef context,
464                 JSObjectRef function,
465                 JSObjectRef thisObject,
466                 size_t argumentCount,
467                 const JSValueRef arguments[],
468                 JSValueRef* exception)
469 {
470         return JSValueMakeUndefined(context);
471 }
472
473 } // Contact
474 } // TizenApis