Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefRecordArray.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 #include <dpl/log/log.h>
18 #include <Tizen/Common/JSTizenExceptionFactory.h>
19 #include <Tizen/Common/JSTizenException.h>
20 #include "JSNdefRecordArray.h"
21 #include "JSNdefMessage.h"
22 #include "NFCConverter.h"
23 #include <API/NFC/NFCFactory.h>
24
25 namespace {
26 const char* FUNCTION_CONCAT = "concat";
27 const char* FUNCTION_JOIN = "join";
28 const char* FUNCTION_POP = "pop";
29 const char* FUNCTION_PUSH = "push";
30 const char* FUNCTION_REVERSE = "reverse";
31 const char* FUNCTION_SHIFT = "shift";
32 const char* FUNCTION_SLICE = "slice";
33 const char* FUNCTION_SORT = "sort";
34 const char* FUNCTION_SPLICE = "splice";
35 const char* FUNCTION_TOSTRING = "toString";
36 const char* FUNCTION_UNSHIFT = "unshift";
37 const char* FUNCTION_VALUEOF = "valueOf";
38 const char *ARRAY = "Array";
39 const char *ATTRIBUTE_LENGTH = "length";
40 }
41
42 namespace TizenApis {
43 namespace Tizen1_0 {
44 using namespace TizenApis::Commons;
45 using namespace Api::NFC;
46 using namespace WrtDeviceApis;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49         
50 JSClassDefinition JSNdefRecordArray::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         setProperty,
62         NULL, //deleteProperty,
63         NULL, //getPropertyNames,
64         NULL, //callAsFunction,
65         NULL, //callAsConstructor,
66         NULL, //hasInstance,
67         NULL, //convertToType,
68 };
69
70 JSStaticValue JSNdefRecordArray::m_property[] = {
71         { ATTRIBUTE_LENGTH, getLength, NULL, kJSPropertyAttributeReadOnly },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSNdefRecordArray::m_function[] = {
76         { FUNCTION_CONCAT, concat, kJSPropertyAttributeNone },
77         { FUNCTION_JOIN, join, kJSPropertyAttributeNone },
78         { FUNCTION_POP, pop, kJSPropertyAttributeNone },
79         { FUNCTION_PUSH, push, kJSPropertyAttributeNone },
80         { FUNCTION_REVERSE, reverse, kJSPropertyAttributeNone },
81         { FUNCTION_SHIFT, shift, kJSPropertyAttributeNone },
82         { FUNCTION_SLICE, slice, kJSPropertyAttributeNone },
83         { FUNCTION_SORT, sort, kJSPropertyAttributeNone },
84         { FUNCTION_SPLICE, splice, kJSPropertyAttributeNone },
85         { FUNCTION_TOSTRING, toString, kJSPropertyAttributeNone },
86         { FUNCTION_UNSHIFT, unshift, kJSPropertyAttributeNone },
87         { FUNCTION_VALUEOF, valueOf, kJSPropertyAttributeNone },
88         { 0, 0, 0 }
89 };
90
91 JSClassRef JSNdefRecordArray::m_jsClassRef = JSClassCreate(
92                 JSNdefRecordArray::getClassInfo());
93
94 JSValueRef JSNdefRecordArray::getLength(JSContextRef context,
95                 JSObjectRef object,
96                 JSStringRef propertyName,
97                 JSValueRef* exception)
98 {
99         LogDebug("Enter");
100         Try
101         {
102                 NdefMessagePrivObject* priv =
103                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
104                 if (!priv) {
105                         Throw(NullPointerException);
106                 }
107                 INdefMessagePtr privateDatas = priv->getObject();
108                 if (privateDatas) {
109                         Converter converter(context);
110                         return converter.toJSValueRefLong(privateDatas->getRecordCount());
111                 }
112         }
113         Catch(WrtDeviceApis::Commons::Exception)
114         {
115                 LogError("invalid conversion");
116         }
117         return JSValueMakeUndefined(context);
118 }
119
120 JSObjectRef JSNdefRecordArray::createArray(JSContextRef context,
121                 const INdefMessagePtr &privateDatas)
122 {
123         LogDebug("Enter");
124         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, privateDatas);
125         return JSObjectMake(context, getClassRef(), priv);
126 }
127
128 const JSClassDefinition* JSNdefRecordArray::getClassInfo()
129 {
130         return &(m_classInfo);
131 }
132
133 JSClassRef JSNdefRecordArray::getClassRef()
134 {
135         if (!m_jsClassRef) {
136                 m_jsClassRef = JSClassCreate(&m_classInfo);
137         }
138         return m_jsClassRef;
139 }
140
141 bool JSNdefRecordArray::isObjectOfClass(JSContextRef context, JSValueRef value)
142 {
143         return JSValueIsObjectOfClass(context, value, getClassRef());
144 }
145
146 INdefMessagePtr 
147         JSNdefRecordArray::getNdefRecordArray(JSContextRef context, JSValueRef value)
148 {
149         LogDebug("Enter");
150         if (!isObjectOfClass(context, value)) {
151                 Throw(InvalidArgumentException);
152         }
153         JSObjectRef object = JSValueToObject(context, value, NULL);
154         if (!object) {
155                 Throw(InvalidArgumentException);
156         }
157         NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
158         if (!priv) {
159                 Throw(NullPointerException);
160         }
161         return priv->getObject();
162 }
163
164 void JSNdefRecordArray::initialize(JSContextRef context,
165                 JSObjectRef object)
166 {
167         LogDebug("Enter");      
168 }
169
170 void JSNdefRecordArray::finalize(JSObjectRef object)
171 {
172         NdefMessagePrivObject* priv =
173                 static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
174         delete priv;
175         JSObjectSetPrivate(object, NULL);
176 }
177
178 bool JSNdefRecordArray::hasProperty(JSContextRef context,
179                 JSObjectRef object,
180                 JSStringRef propertyName)
181 {       
182         LogDebug("Enter");
183         Converter converter(context);
184         Try
185         {
186                 size_t index = converter.toSizeT(propertyName);
187                 NdefMessagePrivObject* priv =
188                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
189                 if (!priv) {
190                         Throw(NullPointerException);
191                 }
192                 INdefMessagePtr privateDatas = priv->getObject();
193                 if (static_cast<long>(index) < privateDatas->getRecordCount()) {
194                         return true;
195                 }
196         }
197         Catch(WrtDeviceApis::Commons::Exception)
198         {
199                 //not reporting error is intended
200         }
201         return false;
202 }
203
204 JSValueRef JSNdefRecordArray::getProperty(JSContextRef context,
205                 JSObjectRef object,
206                 JSStringRef propertyName,
207                 JSValueRef* exception)
208 {
209         LogDebug("Enter");      
210
211         Try
212         {
213                 NdefMessagePrivObject* priv =
214                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
215                 if (!priv) {
216                         Throw(NullPointerException);
217                 }
218
219                 NFCConverter converter(priv->getContext());
220                 size_t index = converter.toSizeT(propertyName);
221
222                 INdefMessagePtr privateDatas = priv->getObject();
223                 if (static_cast<long>(index) < privateDatas->getRecordCount()) {
224                         NdefRecordData result = privateDatas->getNDEFRecord(index);
225                         return converter.toJSValueRef(result);
226                 }
227         }
228         Catch(WrtDeviceApis::Commons::Exception)
229         {
230                 LogError("invalid property");
231         }
232         return JSValueMakeUndefined(context);
233 }
234
235 bool JSNdefRecordArray::setProperty(JSContextRef context,
236                 JSObjectRef object,
237                 JSStringRef propertyName,
238                 JSValueRef value,
239                 JSValueRef* exception)
240 {       
241         LogDebug("Enter");
242         NFCConverter converter(context);
243         Try
244         {
245                 size_t index = converter.toSizeT(propertyName);
246                 NdefMessagePrivObject* priv =
247                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
248                 if (!priv) {
249                         Throw(NullPointerException);
250                 }
251                 INdefMessagePtr privateDatas = priv->getObject();
252                 if (!privateDatas) {
253                         Throw(NullPointerException);
254                 }
255                 void *record = converter.getRecordHandle(value);
256                 if (privateDatas->getRecordCount() == static_cast<long>(index)) {
257                         privateDatas->appendNDEFRecord(record);
258                 } else if (privateDatas->getRecordCount() > static_cast<long>(index)) {
259                         privateDatas->removeNDEFRecord(static_cast<long>(index));
260                         privateDatas->insertNDEFRecord(static_cast<long>(index), record);
261                 } else
262                         Throw(InvalidArgumentException);
263
264                 return true;
265         } Catch (NullPointerException) {
266                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
267                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
268         } Catch (ConversionException) {
269                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
270                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
271         } Catch (InvalidArgumentException) {
272                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
273                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
274         } Catch(WrtDeviceApis::Commons::Exception) {
275                 LogError("Exception: " << _rethrown_exception.GetMessage());
276                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
277         }
278         return false;
279 }
280
281 JSValueRef JSNdefRecordArray::concat(JSContextRef context,
282                 JSObjectRef function,
283                 JSObjectRef thisObject,
284                 size_t argumentCount,
285                 const JSValueRef arguments[],
286                 JSValueRef* exception)
287 {
288         LogDebug("Enter");
289         Try
290         {
291                 //copy current privateDatas
292                 NdefMessagePrivObject* priv =
293                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
294                 if (!priv) {
295                         LogError("Private object is not set.");
296                         ThrowMsg(NullPointerException, "Private object not initialized");
297                 }
298
299                 INdefMessagePtr myMessage = priv->getObject();
300                 
301                 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(myMessage->toByte());
302
303                 if (argumentCount == 0)
304                         return createArray(priv->getContext(), NdefMessage);
305
306                 NFCConverter converter(context);
307                 for (int i = 0; i < argumentCount; i++) {
308                         if (JSIsArrayValue(context, arguments[i])) {
309                                 std::vector<void *> records = converter.toVectorOfRecordHandles(arguments[i]);
310                                 for (int j = 0; j < records.size(); j++)
311                                         NdefMessage->appendNDEFRecord(records[j]);
312                         } else if (isObjectOfClass(context, arguments[i])) {
313                                 NdefMessagePrivObject* argPriv =
314                                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(converter.toJSObjectRef(arguments[i])));
315                                 if (!argPriv) {
316                                         LogError(i << "record's private object is not set.");
317                                         ThrowMsg(InvalidArgumentException, "Record's private object not initialized");
318                                 }
319                                 INdefMessagePtr argMessage = argPriv->getObject();
320                                 for (long j = 0 ; j < argMessage->getRecordCount(); j++)
321                                         NdefMessage->appendNDEFRecord(argMessage->getRecordHandle(j));
322                         } else
323                                 ThrowMsg(ConversionException, "No Array");
324                 }
325                 
326                 return createArray(priv->getContext(), NdefMessage);;
327         } Catch(NullPointerException) {
328                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
329                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
330         } Catch(ConversionException) {
331                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
332                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
333         } Catch (InvalidArgumentException) {
334                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
335                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
336         } Catch (WrtDeviceApis::Commons::Exception) {
337                 LogError("Exception: " << _rethrown_exception.GetMessage());
338                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
339         }
340
341         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
342 }
343
344 JSValueRef JSNdefRecordArray::join(JSContextRef context,
345                 JSObjectRef function,
346                 JSObjectRef thisObject,
347                 size_t argumentCount,
348                 const JSValueRef arguments[],
349                 JSValueRef* exception)
350 {
351         LogDebug("Enter");
352         Try
353         {
354                 std::string result;
355                 std::string separator(",");
356                 NFCConverter converter(context);
357                 NdefMessagePrivObject* priv =
358                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
359                 INdefMessagePtr myMessage = priv->getObject();
360                 if (argumentCount > 0 && JSValueIsString(context, arguments[0])) {
361                         separator = converter.toString(arguments[0]);
362                 }
363                 for (size_t i = 0; i < myMessage->getRecordCount(); i++) {
364                         if (i != 0) {
365                                 result += separator;
366                         }
367                         result += "[object " + converter.toRecordClassName(myMessage->getNDEFRecord(i)) + "]";
368                 }
369                 return converter.toJSValueRef(result);
370         }
371         Catch(WrtDeviceApis::Commons::Exception)
372         {
373                 LogError("error occured");
374         }
375         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
376 }
377
378 JSValueRef JSNdefRecordArray::pop(JSContextRef context,
379                 JSObjectRef function,
380                 JSObjectRef thisObject,
381                 size_t argumentCount,
382                 const JSValueRef arguments[],
383                 JSValueRef* exception)
384 {       
385         LogDebug("Enter");
386         Try
387         {
388                 NdefMessagePrivObject* priv =
389                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
390                 INdefMessagePtr myMessage = priv->getObject();
391
392                 NFCConverter converter(priv->getContext());
393                 long recordCount = myMessage->getRecordCount();
394                 if (recordCount > 0) {
395                         NdefRecordData result = myMessage->getNDEFRecord(recordCount - 1);
396                         myMessage->removeNDEFRecord(recordCount - 1);
397                         return converter.toJSValueRef(result);
398                 }
399                 return JSCreateArrayObject(context, 0, NULL);
400         } Catch(NullPointerException) {
401                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
402                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
403         } Catch(ConversionException) {
404                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
405                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
406         } Catch (InvalidArgumentException) {
407                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
408                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
409         } Catch (WrtDeviceApis::Commons::Exception) {
410                 LogError("Exception: " << _rethrown_exception.GetMessage());
411                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
412         }
413         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
414 }
415
416 JSValueRef JSNdefRecordArray::push(JSContextRef context,
417                 JSObjectRef function,
418                 JSObjectRef thisObject,
419                 size_t argumentCount,
420                 const JSValueRef arguments[],
421                 JSValueRef* exception)
422 {       
423         LogDebug("Enter");
424         Try
425         {
426                 NFCConverter converter(context);
427                 NdefMessagePrivObject* priv =
428                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
429                 INdefMessagePtr myMessage = priv->getObject();
430
431                 for (size_t i = 0; i < argumentCount; ++i) {
432                         void *record = converter.getRecordHandle(arguments[i]);
433                         myMessage->appendNDEFRecord(record);
434                 }
435                 return converter.toJSValueRefLong(myMessage->getRecordCount());
436         }
437         Catch(WrtDeviceApis::Commons::Exception)
438         {
439                 LogError("error occured");
440         }
441         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
442 }
443
444 JSValueRef JSNdefRecordArray::reverse(JSContextRef context,
445                 JSObjectRef function,
446                 JSObjectRef thisObject,
447                 size_t argumentCount,
448                 const JSValueRef arguments[],
449                 JSValueRef* exception)
450 {
451         LogDebug("Enter");      
452         Try
453         {
454                 NFCConverter converter(context);
455                 NdefMessagePrivObject* priv =
456                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
457                 INdefMessagePtr myMessage = priv->getObject();
458
459                 long recordCounts = myMessage->getRecordCount();
460                 if (recordCounts > 0) {
461                         std::vector<void *> records;
462                         for (long i = recordCounts; i > 0; i--) {
463                                 records.push_back(myMessage->getRecordHandle(i-1));
464                         }
465                         myMessage->changeAllRecords(records);
466                 }
467                 return thisObject;
468         }
469         Catch(WrtDeviceApis::Commons::Exception)
470         {
471                 LogError("error occured");
472         }
473         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
474 }
475
476 JSValueRef JSNdefRecordArray::shift(JSContextRef context,
477                 JSObjectRef function,
478                 JSObjectRef thisObject,
479                 size_t argumentCount,
480                 const JSValueRef arguments[],
481                 JSValueRef* exception)
482 {
483         LogDebug("Enter");      
484         Try
485         {
486                 NdefMessagePrivObject* priv =
487                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
488                 INdefMessagePtr myMessage = priv->getObject();
489
490                 NFCConverter converter(priv->getContext());
491                 long recordCount = myMessage->getRecordCount();
492                 if (recordCount > 0) {
493                         NdefRecordData result = myMessage->getNDEFRecord(0);
494                         myMessage->removeNDEFRecord(0);
495                         return converter.toJSValueRef(result);
496                 }
497                 return JSCreateArrayObject(context, 0, NULL);
498         }
499         Catch(WrtDeviceApis::Commons::Exception)
500         {
501                 LogError("error occured");
502         }
503         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
504 }
505
506 JSValueRef JSNdefRecordArray::slice(JSContextRef context,
507                 JSObjectRef function,
508                 JSObjectRef thisObject,
509                 size_t argumentCount,
510                 const JSValueRef arguments[],
511                 JSValueRef* exception)
512 {
513         LogDebug("Enter");      
514         Try
515         {
516                 if (argumentCount < 1) {
517                         return JSValueMakeUndefined(context);
518                 }
519
520                 NdefMessagePrivObject* priv =
521                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
522                 INdefMessagePtr myMessage = priv->getObject();
523
524                 NFCConverter converter(priv->getContext());
525                 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0]))
526                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not number");
527
528                 if ((argumentCount > 1) && (!JSValueIsNull(context, arguments[1]) && (JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1]))))
529                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not number");
530                 
531                 long recordCount = myMessage->getRecordCount();
532                 long start = converter.toLong(arguments[0]);
533                 long end = recordCount;
534                 if ((argumentCount > 1) && !JSValueIsNull(context, arguments[1]))
535                         end = converter.toLong(arguments[1]) < recordCount ? converter.toLong(arguments[1]) : recordCount;
536
537                 if (start < 0)
538                         start = recordCount + start;
539
540                 if (start > end) {
541                         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject();
542                         return createArray(priv->getContext(), NdefMessage);
543                 }
544
545                 std::vector<void *> sliceRecords;
546                 for (long i = start; i < end; i++) {
547                         void * record = myMessage->getRecordHandle(i);
548                         sliceRecords.push_back(record);
549                 }
550                 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(sliceRecords);
551                 return createArray(priv->getContext(), NdefMessage);
552 }
553         Catch(WrtDeviceApis::Commons::Exception)
554         {
555                 LogError("error occured");
556         }
557         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
558 }
559
560 JSValueRef JSNdefRecordArray::sort(JSContextRef context,
561                 JSObjectRef function,
562                 JSObjectRef thisObject,
563                 size_t argumentCount,
564                 const JSValueRef arguments[],
565                 JSValueRef* exception)
566 {
567         LogDebug("Enter");      
568         return thisObject;
569 }
570
571 JSValueRef JSNdefRecordArray::splice(JSContextRef context,
572                 JSObjectRef function,
573                 JSObjectRef thisObject,
574                 size_t argumentCount,
575                 const JSValueRef arguments[],
576                 JSValueRef* exception)
577 {
578         Try
579         {
580                 if (argumentCount < 1) {
581                         return JSCreateArrayObject(context, 0, NULL);
582                 }
583         
584                 NdefMessagePrivObject* priv =
585                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
586                 INdefMessagePtr myMessage = priv->getObject();
587
588                 NFCConverter converter(priv->getContext());
589                 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0]))
590                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not number");
591
592                 if ( (argumentCount > 1) && (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1])))
593                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not number");
594                 
595                 long recordCount = myMessage->getRecordCount();
596                 long start = converter.toLong(arguments[0]);
597                 long howmany = 0;
598                 if (argumentCount > 1)
599                         howmany= converter.toLong(arguments[1]);
600
601                 if (start < 0)
602                         start = recordCount + start;
603
604                 JSObjectRef result;
605                 if (howmany > 0 ) {
606                         if ((start + howmany) > recordCount)
607                                 howmany = recordCount - start;
608
609                         std::vector<void *> spliceRecords;
610                         for (long i = 0; i < howmany; i++) {
611                                 void * record = myMessage->getRecordHandle(i + start);
612                                 spliceRecords.push_back(record);
613                         }
614                         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(spliceRecords);
615
616                         result = createArray(priv->getContext(), NdefMessage);
617                 } else
618                         result = JSCreateArrayObject(context, 0, NULL);
619
620                 if (argumentCount > 2) {
621                         for (long j = 2; j < argumentCount; j++) {
622                                 void *record = converter.getRecordHandle(arguments[j]);
623                                 if (start < recordCount)
624                                         myMessage->appendNDEFRecord(record);
625                                 else
626                                         myMessage->insertNDEFRecord(start++, record);
627                         }
628                 }
629                 return result;
630         } Catch(NullPointerException) {
631                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
632                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
633         } Catch(ConversionException) {
634                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
635                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
636         } Catch (InvalidArgumentException) {
637                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
638                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
639         } Catch (WrtDeviceApis::Commons::Exception) {
640                 LogError("Exception: " << _rethrown_exception.GetMessage());
641                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
642         }
643         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
644 }
645
646 JSValueRef JSNdefRecordArray::toString(JSContextRef context,
647                 JSObjectRef function,
648                 JSObjectRef thisObject,
649                 size_t argumentCount,
650                 const JSValueRef arguments[],
651                 JSValueRef* exception)
652 {
653         
654         return join(context, function, thisObject, 0, arguments, exception);
655 }
656
657 JSValueRef JSNdefRecordArray::unshift(JSContextRef context,
658                 JSObjectRef function,
659                 JSObjectRef thisObject,
660                 size_t argumentCount,
661                 const JSValueRef arguments[],
662                 JSValueRef* exception)
663 {
664         Try
665         {
666                 NdefMessagePrivObject* priv =
667                         static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
668                 INdefMessagePtr myMessage = priv->getObject();
669         
670                 NFCConverter converter(priv->getContext());
671
672                 if (argumentCount > 0) {
673                         for (int i = 0; i < argumentCount; i++) {
674                                 if (converter.isNdefRecord(arguments[i]) == false)
675                                         ThrowMsg(ConversionException, "parameter is Not record");
676                         }
677
678                         for (long i = 0 ; i < argumentCount; i++) {
679                                 void *record = converter.getRecordHandle(arguments[i]);
680                                 myMessage->insertNDEFRecord(i, record);                 
681                         }
682                 }
683                 return converter.toJSValueRefLong(myMessage->getRecordCount());
684         }
685         Catch(WrtDeviceApis::Commons::Exception)
686         {
687                 LogError("error occured");
688         }
689         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
690 }
691
692 JSValueRef JSNdefRecordArray::valueOf(JSContextRef context,
693                 JSObjectRef function,
694                 JSObjectRef thisObject,
695                 size_t argumentCount,
696                 const JSValueRef arguments[],
697                 JSValueRef* exception)
698 {
699         
700         return join(context, function, thisObject, 0, arguments, exception);
701 }
702 }
703 }