Update change log and spec for wrt-plugins-tizen_0.4.22
[framework/web/wrt-plugins-tizen.git] / src / DataControl / SqlDataControlConsumer.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 <cassert>
19 #include <Commons/Exception.h>
20 #include "DataType.h"
21 #include "SqlDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
23 #include <string>
24 #include <vector>
25 #include <aul/aul.h>
26 #include <sstream>
27 #include <package-manager.h>
28
29
30 namespace DeviceAPI {
31 namespace DataControl {
32
33
34 namespace {
35
36 static const char OSP_V_CALLER_TYPE_OSP[] = "osp";
37 static const char OSP_V_LAUNCH_TYPE_LAUNCH[] = "launch";
38 static const char OSP_V_LAUNCH_TYPE_APPCONTROL[] = "appcontrol";
39 static const char OSP_V_LAUNCH_TYPE_DATACONTROL[] = "datacontrol";
40 static const char OSP_V_LAUNCH_TYPE_CONDTION[] = "condition";
41 static const char OSP_V_REQUEST_TYPE_SQL_QUERY[] = "sql_query";
42 static const char OSP_V_REQUEST_TYPE_SQL_INSERT[] = "sql_insert";
43 static const char OSP_V_REQUEST_TYPE_SQL_UPDATE[] = "sql_update";
44 static const char OSP_V_REQUEST_TYPE_SQL_DELETE[] = "sql_delete";
45 static const char OSP_V_REQUEST_TYPE_MAP_QEURY[] = "map_query";
46 static const char OSP_V_REQUEST_TYPE_MAP_INSERT[] = "map_insert";
47 static const char OSP_V_REQUEST_TYPE_MAP_UPDATE[] = "map_update";
48 static const char OSP_V_REQUEST_TYPE_MAP_DELETE[] = "map_delete";
49 static const char BUNDLE_KEY_WINDOW[] = "__APP_SVC_K_WIN_ID__";
50
51
52 #define OSP_K_CALLER_TYPE   "__OSP_CALLER_TYPE__"
53 #define OSP_K_LAUNCH_TYPE   "__OSP_LAUNCH_TYPE__"
54 #define OSP_K_ARG           "__OSP_ARGS__"
55 #define OSP_K_COND          "__OSP_COND_NAME__"
56 #define OSP_K_APPID         "__OSP_APPID__"
57 #define OSP_K_REQUEST_ID    "__OSP_REQUEST_ID__"
58 #define OSP_K_APPCONTROL_PROVIDER   "__OSP_APPCONTROL_PROVIDER__"
59 #define OSP_K_APPCONTROL_OPERATION  "__OSP_APPCONTROL_OPERATION__"
60 #define OSP_K_APPCONTROL_CATEGORY   "__OSP_APPCONTROL_CATEGORY__"
61 #define OSP_K_APPCONTROL_MIME       "__OSP_APPCONTROL_MIME__"
62 #define OSP_K_APPCONTROL_URI        "__OSP_APPCONTROL_URI__"
63 #define OSP_K_DATACONTROL_PROVIDER      "__OSP_DATACONTROL_PROVIDER__"
64 #define OSP_K_DATACONTROL_REQUEST_TYPE  "__OSP_DATACONTROL_REQUEST_TYPE__"
65
66 #define RESULT_TRUE_FROM_OSP "1"
67 #define RESULT_FALSE_FROM_OSP "0"
68
69
70
71 static std::vector<std::string> getDataArrayFromBundle(bundle *b, std::string key)
72 {
73         std::vector<std::string> result;
74         const char **array;
75         int length = 0;
76         int index = 0;
77         
78         array = appsvc_get_data_array(b, key.c_str(), &length);
79
80         if (array == NULL || length == 0)
81         {
82                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result data fail from datacontrol provider");
83         }
84         
85         for (index = 0; index < length; index++)
86         {
87                 result.push_back(array[index]);
88         }
89
90         return result;
91 }
92
93
94 static void sqldataControlSelectCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
95 {
96         LogDebug("Enter");
97         
98         EventSelectPendingEvent *pendingEvent = NULL;
99         SQLDataControlConsumer *consumer = NULL;
100         EventSelectPtr event;
101
102         try 
103         {
104                 if (data == NULL)
105                 {
106                         LogDebug("data null, can not send result to JS Layer");
107                         return;
108                 }
109
110                 pendingEvent = (EventSelectPendingEvent *)data;
111                 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
112                 event = pendingEvent->getEvent();
113
114                 if (b == NULL)
115                 {
116                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
117                 }
118                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
119
120                 for (size_t index = 0; index < result.size(); index++)
121                 {
122                         LogDebug(result[index]);
123                 }
124
125                 if (result.size() < 3)
126                 {
127                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
128                 }
129                 
130                 // 0 : result true or false??
131                 if (RESULT_TRUE_FROM_OSP != result[0])
132                 {
133                         // 1 : error msg 
134                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
135                 }
136
137                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
138                 // 2 : result set file path
139                 event->setResultSetPath(result[2]);
140                 
141         }
142         catch (const WrtDeviceApis::Commons::Exception& ex) 
143         {
144                 LogError("Exception: " << ex.GetMessage());
145                 event->setExceptionCode(ex.getCode());
146                 event->setErrorMsg(ex.GetMessage());            
147         }
148         consumer->handlePendingEvent(event);
149
150         if (pendingEvent)
151         {
152                 delete pendingEvent;
153         }
154
155 }
156
157 static void sqldataControlInsertCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
158 {
159         LogDebug("Enter");
160
161         
162         EventInsertPendingEvent *pendingEvent = NULL;
163         SQLDataControlConsumer *consumer = NULL;
164         EventInsertPtr event;
165
166         try 
167         {
168                 if (data == NULL)
169                 {
170                         LogDebug("data null, can not send result to JS Layer");
171                         return;
172                 }
173
174                 pendingEvent = (EventInsertPendingEvent *)data;
175                 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
176                 event = pendingEvent->getEvent();
177
178                 if (b == NULL)
179                 {
180                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
181                 }
182                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
183
184                 for (size_t index = 0; index < result.size(); index++)
185                 {
186                         LogDebug(result[index]);
187                 }
188
189                 if (result.size() < 2)
190                 {
191                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
192                 }
193                 
194                 // 0 : result true or false??
195                 if (RESULT_TRUE_FROM_OSP != result[0])
196                 {
197                         // 1 : error msg 
198                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
199                 }
200
201                 // 2 : insertrowid
202                 std::stringstream sstr(result[2]);
203                 long insertRowid = 0;
204                 sstr >> insertRowid;
205                 LogDebug(result[2] << insertRowid);
206                 event->setRowId(insertRowid);                   
207                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
208                 
209         }
210         catch (const WrtDeviceApis::Commons::Exception& ex) 
211         {
212                 LogError("Exception: " << ex.GetMessage());
213                 event->setExceptionCode(ex.getCode());
214                 event->setErrorMsg(ex.GetMessage());            
215         }
216         consumer->handlePendingEvent(event);
217
218         if (pendingEvent)
219         {
220                 delete pendingEvent;
221         }
222
223 }
224
225 static void sqldataControlDeleteCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
226 {
227         LogDebug("Enter");
228
229         if (data == NULL)
230         {
231                 LogDebug("Data or Bundle error");
232                 return;
233         }
234
235         EventDeletePendingEvent* pendingEvent = NULL;
236         SQLDataControlConsumer *consumer = NULL;
237         EventDeletePtr event;
238         
239         try 
240         {
241                 if (data == NULL)
242                 {
243                         LogDebug("data null, can not send result to JS Layer");
244                         return;
245                 }
246
247                 pendingEvent = (EventDeletePendingEvent *)data;
248                 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
249                 event = pendingEvent->getEvent();
250
251                 if (b == NULL)
252                 {
253                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
254                 }
255                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
256
257                 for (size_t index = 0; index < result.size(); index++)
258                 {
259                         LogDebug(result[index]);
260                 }
261
262                 if (result.size() < 2)
263                 {
264                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
265                 }
266                 
267                 // 0 : result true or false??
268                 if (RESULT_TRUE_FROM_OSP != result[0])
269                 {
270                         // 1 : error msg 
271                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
272                 }
273
274                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
275
276                 
277         }
278         catch (const WrtDeviceApis::Commons::Exception& ex) 
279         {
280                 LogError("Exception: " << ex.GetMessage());
281                 event->setExceptionCode(ex.getCode());
282                 event->setErrorMsg(ex.GetMessage());            
283         }
284         consumer->handlePendingEvent(event);
285
286         if (pendingEvent)
287         {
288                 delete pendingEvent;
289         }
290 }
291
292 static void sqldataControlUpdateCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
293 {
294         LogDebug("Enter");
295
296         EventUpdatePendingEvent* pendingEvent = NULL;
297         SQLDataControlConsumer *consumer = NULL;
298         EventUpdatePtr event;
299         
300         try 
301         {
302                 if (data == NULL)
303                 {
304                         LogDebug("data null, can not send result to JS Layer");
305                         return;
306                 }
307
308                 pendingEvent = (EventUpdatePendingEvent *)data;
309                 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
310                 event = pendingEvent->getEvent();
311
312                 if (b == NULL)
313                 {
314                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
315                 }
316                 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
317
318                 for (size_t index = 0; index < result.size(); index++)
319                 {
320                         LogDebug(result[index]);
321                 }
322
323                 if (result.size() < 2)
324                 {
325                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
326                 }
327                 
328                 // 0 : result true or false??
329                 if (RESULT_TRUE_FROM_OSP != result[0])
330                 {
331                         // 1 : error msg 
332                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
333                 }
334
335                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
336                 
337         }
338         catch (const WrtDeviceApis::Commons::Exception& ex) 
339         {
340                 LogError("Exception: " << ex.GetMessage());
341                 event->setExceptionCode(ex.getCode());
342                 event->setErrorMsg(ex.GetMessage());            
343         }
344         consumer->handlePendingEvent(event);
345
346         if (pendingEvent)
347         {
348                 delete pendingEvent;
349         }
350 }
351
352 }
353 SQLDataControlConsumer::SQLDataControlConsumer() 
354 {
355         LogDebug("Enter");
356
357         m_appId = "";
358         m_type = "";;
359         m_dataId = "";
360         m_providerId = "";
361
362
363 }
364
365 SQLDataControlConsumer::~SQLDataControlConsumer() 
366 {
367         LogDebug("Enter");
368 }
369
370 DPL::Mutex SQLDataControlConsumer::m_mutex;
371
372
373 void SQLDataControlConsumer::setType(std::string& type)
374 {
375         if (type != MAP_DATA_CONTROL && type != SQL_DATA_CONTROL)
376                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "type mismatch");
377         
378         m_type = type;
379 }
380
381 void SQLDataControlConsumer::setProviderId(const std::string& id )
382 {
383         m_providerId = id;
384 }
385
386 void SQLDataControlConsumer::setDataId(const std::string& id )
387 {
388         m_dataId = id;
389 }
390
391 void SQLDataControlConsumer::removeReqId(unsigned int reqId)
392 {
393         bool remove = false;
394         
395         std::vector<unsigned int>::iterator it, found;
396         
397         for (it = m_currentReqIds.begin(); it != m_currentReqIds.end(); ++it)
398         {
399                 if (*it == reqId)
400                 {
401                         found = it;     
402                         remove = true;
403                 }
404         }
405
406         if (remove)
407         {
408                 DPL::Mutex::ScopedLock lock(&m_mutex);
409                 m_currentReqIds.erase(found);
410         }
411 }
412
413 bool SQLDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
414 {
415         size_t index = 0; 
416
417         for (index = 0; index < m_currentReqIds.size(); index++)
418         {
419                 if (m_currentReqIds[index] == reqId)
420                         return false;
421         }
422
423         return true;
424 }
425
426
427 std::string SQLDataControlConsumer::getDataId()
428 {
429         return m_dataId;
430 }
431
432 std::string SQLDataControlConsumer::getProviderId()
433 {
434         return m_providerId;
435 }
436
437 std::string SQLDataControlConsumer::getType()
438 {
439         return m_type;
440 }
441
442
443 std::string SQLDataControlConsumer::getApplicationId(const std::string& provId)
444 {
445         std::string appIdStr = "";
446
447         char* appId = NULL;
448         char* access = NULL;
449         const char *passId = provId.c_str();
450         
451         if (m_appId.length() == 0)
452         {
453         
454                 LogDebug("need " << passId);
455
456                 if(     pkgmgr_datacontrol_get_info(passId, OSP_PKGINFO_SQL_TYPE, &appId, &access) < 0)
457                 {
458                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
459                 }
460                 
461                 LogDebug("need2");
462
463                 if (appId)
464                 {
465                         LogDebug(appId);
466                         appIdStr = appId;
467                         free(appId);
468                 }
469
470                 if (access)
471                 {
472                         free(access);
473                 }
474                 
475                 m_appId = appIdStr;
476
477
478         }
479         return m_appId;
480
481 }
482
483 void SQLDataControlConsumer::addArrayToBundle(bundle* passData, std::vector<std::string>& array)
484 {
485         size_t arraySize = array.size();
486
487         if (arraySize == 0)
488         {
489                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "size = 0");
490         }
491         
492         const char** arr = NULL;
493         arr = (const char**)calloc(sizeof(char*), arraySize);
494
495         if (arr == NULL)
496         {
497                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "null");
498         }
499
500         for (size_t index = 0; index < arraySize; index++) 
501         {
502                 arr[index] = array[index].c_str();
503         }
504
505         
506         bundle_add_str_array(passData, OSP_K_ARG, arr, arraySize);
507
508         free(arr);
509         arr = NULL;
510 }
511
512
513 void SQLDataControlConsumer::handlePendingEvent(const EventInsertPtr& event)
514 {
515         WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(event); 
516         removeReqId(event->getReqId());
517 }
518 void SQLDataControlConsumer::handlePendingEvent(const EventDeletePtr& event)
519 {
520         WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(event); 
521         removeReqId(event->getReqId());
522 }
523 void SQLDataControlConsumer::handlePendingEvent(const EventSelectPtr& event)
524 {
525         WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(event);
526         removeReqId(event->getReqId());
527 }
528
529 void SQLDataControlConsumer::handlePendingEvent(const EventUpdatePtr& event)
530 {
531         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(event); 
532         removeReqId(event->getReqId());
533 }
534
535
536 void SQLDataControlConsumer::insertData(const EventInsertPtr& event)
537 {
538         WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::PostRequest(event);
539 }
540
541 void SQLDataControlConsumer::deleteData(const EventDeletePtr& event)
542 {
543         WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::PostRequest(event);
544 }
545
546 void SQLDataControlConsumer::selectData(const EventSelectPtr& event)
547 {
548         WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::PostRequest(event);
549 }
550
551 void SQLDataControlConsumer::updateData(const EventUpdatePtr& event)
552 {
553         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::PostRequest(event);
554 }
555
556 void SQLDataControlConsumer::OnRequestReceived(const EventInsertPtr& event)
557 {
558         LogDebug("Enter");
559         bundle* passData = NULL;
560
561         try 
562         {
563                 std::string dataId = getDataId();
564                 RowDataPtr rowData = event->getRowData();
565                 unsigned int reqId = event->getReqId();
566                 std::stringstream ss, ssreqtype;
567                 std::string reqIdStr, reqtypestr;
568                 std::vector<std::string> queryItem;
569                 // column count
570                 int columnSize = rowData->m_Data.size();
571                 std::stringstream counts;
572                 std::string countStr;
573
574                 if (checkReqIdUniqueness(reqId) == false)
575                 {
576                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
577                 }
578                 
579                 if (columnSize == 0)
580                 {
581                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No insertion data");
582                 }
583                 
584                 counts << columnSize;
585                 countStr = counts.str();
586
587                 // conversion
588                 ss << reqId;
589                 reqIdStr = ss.str();
590
591                 ssreqtype << _DATACONTROL_REQUEST_TYPE_SQL_INSERT;
592                 reqtypestr = ssreqtype.str();
593
594                 // FIXME
595                 // qi993y8s4e.DataControlProviderService
596                 std::string appId = getApplicationId(m_providerId);
597                 //.DataControlProviderService";
598
599                 passData = bundle_create();
600
601                 if (passData == NULL)
602                 {
603                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
604                 }
605
606                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
607
608                 appsvc_set_appid(passData, appId.c_str());
609
610                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
611                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
612                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
613                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
614                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
615
616                 queryItem.push_back(dataId); // dataid
617                 queryItem.push_back(countStr);
618
619                 std::map<std::string, std::string>::iterator it;
620
621                 for (it = rowData->m_Data.begin(); it != rowData->m_Data.end(); ++it)
622                 {
623                         queryItem.push_back(it->first);
624                         queryItem.push_back(it->second);
625                 }
626
627                 addArrayToBundle(passData, queryItem);
628
629                 // FIXEME
630                 // reqid sholud be known
631                 EventInsertPendingEvent* pendingEvent = new EventInsertPendingEvent((void*)this, event);
632                 int pid = appsvc_run_service(passData, reqId, sqldataControlInsertCallback, (void*)pendingEvent);
633
634                 if (pid < 0) 
635                 {
636                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Error");
637                 }
638                 
639                 event->switchToManualAnswer();
640
641                 DPL::Mutex::ScopedLock lock(&m_mutex);          
642                 m_currentReqIds.push_back(reqId);
643                 
644         }       
645         catch (const WrtDeviceApis::Commons::Exception& ex) 
646         {
647                 event->setExceptionCode(ex.getCode());
648                 event->setErrorMsg(ex.GetMessage());
649                 LogError("Exception: " << ex.GetMessage());
650                 
651                 
652         }
653
654         if (passData)
655         {
656                 bundle_free(passData);
657                 passData = NULL;
658         }
659
660 }
661
662 void SQLDataControlConsumer::OnRequestReceived(const EventDeletePtr& event)
663 {
664         LogDebug("Enter");
665         bundle* passData = NULL;
666         
667         try 
668         {
669                 std::string dataId = getDataId();
670                 std::string where = event->getWhere();
671                 std::vector<std::string> queryItem;
672                 
673                 unsigned int reqId = event->getReqId();
674                 std::stringstream ss, ssreqtype;
675                 std::string reqIdStr, reqtypestr;
676
677                 ss << reqId;
678                 reqIdStr = ss.str();
679
680                 if (checkReqIdUniqueness(reqId) == false)
681                 {
682                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
683                 }
684                 
685
686                 ssreqtype << _DATACONTROL_REQUEST_TYPE_SQL_DELETE;
687                 reqtypestr = ssreqtype.str();
688
689                 std::string appId = getApplicationId(m_providerId);
690
691                 passData = bundle_create();
692
693                 
694                 if (passData == NULL)
695                 {
696                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
697                 }
698
699                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
700                 appsvc_set_appid(passData, appId.c_str());
701
702                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
703                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
704                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
705                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
706                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
707
708                 queryItem.push_back(dataId); // dataid
709
710                 if (where.size() == 0)  // where
711                 {
712                         queryItem.push_back("NULL");
713                 }
714                 else 
715                 {
716                         queryItem.push_back(where);
717                 }
718
719                 addArrayToBundle(passData, queryItem);
720                 EventDeletePendingEvent* pendingEvent = new EventDeletePendingEvent(this, event);
721
722                 int pid = appsvc_run_service(passData, reqId, sqldataControlDeleteCallback, (void*)pendingEvent);
723
724                 if (pid < 0) 
725                 {
726                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
727                 }
728
729                 event->switchToManualAnswer();
730
731                 DPL::Mutex::ScopedLock lock(&m_mutex);          
732                 m_currentReqIds.push_back(reqId);
733         }       
734         catch (const WrtDeviceApis::Commons::Exception& ex) 
735         {
736                 LogError("Exception: " << ex.GetMessage());
737                 event->setExceptionCode(ex.getCode());
738                 event->setErrorMsg(ex.GetMessage());
739         }
740
741         if (passData)
742         {
743                 bundle_free(passData);
744                 passData = NULL;
745         }
746
747 }
748
749
750
751 void SQLDataControlConsumer::OnRequestReceived(const EventSelectPtr& event)
752 {
753         LogDebug("Enter");
754         bundle* passData = NULL;
755         
756         try 
757         {
758                 std::string dataId = getDataId();
759                 std::string where = event->getWhere();
760                 std::vector<std::string> columns = event->getColumns();
761                 std::vector<std::string> queryItem;
762                 std::string order = event->getOrder();
763                 std::string page = event->getPage();
764                 std::string numberPerPage = event->getNumerPerPage();
765                 
766                 unsigned int reqId = event->getReqId();
767                 int columnSize = columns.size();
768                 std::stringstream ss, ssreqtype;
769                 std::string reqIdStr, reqtypestr;
770
771                 if (checkReqIdUniqueness(reqId) == false)
772                 {
773                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
774                 }
775         
776
777                 ss << reqId;
778                 reqIdStr = ss.str();
779
780                 ssreqtype << _DATACONTROL_REQUEST_TYPE_SQL_QUERY;
781                 reqtypestr = ssreqtype.str();
782
783                 std::string appId = getApplicationId(m_providerId);
784
785                 passData = bundle_create();
786
787                 if (passData == NULL)
788                 {
789                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
790                 }
791                         
792                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
793                 appsvc_set_appid(passData, appId.c_str());
794
795                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
796                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
797                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
798                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
799                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
800
801                 queryItem.push_back(dataId); // dataid
802
803                 if (columnSize == 0)
804                 {
805                         queryItem.push_back("NULL"); // column
806                 }
807                 else 
808                 {
809                         // column count
810                         std::stringstream counts;
811                         std::string countStr;
812
813                         counts << columnSize;
814                         countStr = counts.str();
815                         queryItem.push_back(countStr);
816
817                         // column
818                         for (int index = 0; index < columnSize; index++)
819                         {
820                                 queryItem.push_back(columns[index]);
821                         }
822                 }
823
824                 if (where.size() == 0)  // where
825                 {
826                         queryItem.push_back("NULL");
827                 }
828                 else 
829                 {
830                         queryItem.push_back(where);
831                 }
832
833                 if (order.size() == 0) // order
834                 {
835                         queryItem.push_back("NULL"); 
836                 }
837                 else
838                 {
839                         queryItem.push_back(order);
840                 }
841
842                 if (page.size() == 0) // page
843                 {
844                         queryItem.push_back("1"); 
845                 }
846                 else
847                 {
848                         queryItem.push_back(page);
849                 }
850
851                 if (numberPerPage.size() == 0) // numberOfPage
852                 {
853                         queryItem.push_back("20");
854                 }
855                 else
856                 {
857                         queryItem.push_back(numberPerPage);
858                 }
859
860                 addArrayToBundle(passData, queryItem);
861                 EventSelectPendingEvent* pendingEvent = new EventSelectPendingEvent(this, event);
862                 
863                 int pid = appsvc_run_service(passData, reqId, sqldataControlSelectCallback, (void*)pendingEvent);
864
865                 if (pid < 0) 
866                 {
867                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
868                 }
869
870                 event->switchToManualAnswer();
871                 
872                 DPL::Mutex::ScopedLock lock(&m_mutex);          
873                 m_currentReqIds.push_back(reqId);
874         }       
875         catch (const WrtDeviceApis::Commons::Exception& ex) 
876         {
877                 LogError("Exception: " << ex.GetMessage());
878                 event->setExceptionCode(ex.getCode());
879                 event->setErrorMsg(ex.GetMessage());
880         }
881
882         if (passData)
883         {
884                 bundle_free(passData);
885                 passData = NULL;
886         }
887         
888 }       
889
890 void SQLDataControlConsumer::OnRequestReceived(const EventUpdatePtr& event)
891 {
892         LogDebug("Enter");
893         bundle* passData = NULL;
894                 
895         try 
896         {
897                 std::string dataId = getDataId();
898                 std::string where = event->getWhere();
899                 RowDataPtr rowData = event->getRowData();
900                 std::vector<std::string> queryItem;
901                 
902                 unsigned int reqId = event->getReqId();
903                 int columnSize = rowData->m_Data.size();
904                 std::stringstream ss, ssreqtype;
905                 std::string reqIdStr, reqtypestr;
906
907
908                 if (checkReqIdUniqueness(reqId) == false)
909                 {
910                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
911                 }
912
913                 std::string appId = getApplicationId(m_providerId);                     
914
915                 passData = bundle_create();
916
917                 if (passData == NULL)
918                 {
919                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
920                 }
921
922                 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
923                 appsvc_set_appid(passData, appId.c_str());
924
925                 ss << reqId;
926                 reqIdStr = ss.str();
927
928                 ssreqtype << _DATACONTROL_REQUEST_TYPE_SQL_UPDATE;
929                 reqtypestr = ssreqtype.str();
930                 
931
932                 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
933                 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
934                 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
935                 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
936                 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
937
938                 queryItem.push_back(dataId); // dataid
939
940                 if (columnSize == 0)
941                 {
942                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "should not be update data as 0");
943                 }
944                 else 
945                 {
946                         // column count
947                         std::stringstream counts;
948                         std::string countStr;
949
950                         counts << columnSize;
951                         countStr = counts.str();
952                         queryItem.push_back(countStr);
953
954                         for (std::map<std::string, std::string>::iterator it= rowData->m_Data.begin();
955                                 it != rowData->m_Data.end(); ++it)
956                         {
957                                 queryItem.push_back((*it).first); // key - column
958                                 queryItem.push_back((*it).second); // value
959                         }
960
961                 }
962
963                 if (where.size() == 0)  // where
964                 {
965                         queryItem.push_back("NULL");
966                 }
967                 else 
968                 {
969                         queryItem.push_back(where);
970                 }
971
972                 addArrayToBundle(passData, queryItem);
973                 EventUpdatePendingEvent* pendingEvent = new EventUpdatePendingEvent(this, event);
974
975                 int pid = appsvc_run_service(passData, reqId, sqldataControlUpdateCallback, (void*)pendingEvent);
976
977                 if (pid < 0) 
978                 {
979                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
980                 }
981
982                 event->switchToManualAnswer();
983
984                 DPL::Mutex::ScopedLock lock(&m_mutex);          
985                 m_currentReqIds.push_back(reqId);
986         }       
987         catch (const WrtDeviceApis::Commons::Exception& ex) 
988         {
989                 LogError("Exception: " << ex.GetMessage());
990                 event->setExceptionCode(ex.getCode());
991                 event->setErrorMsg(ex.GetMessage());
992         }
993
994         if (passData)
995         {
996                 bundle_free(passData);
997                 passData = NULL;
998         }
999
1000 }
1001
1002
1003
1004 }
1005 }
1006
1007