2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <Commons/Exception.h>
21 #include "SqlDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
27 #include <package-manager.h>
29 // to get package name by appid
31 #include <app_manager.h>
34 #include <security-server.h>
36 #include "DataControlAsyncCallbackManager.h"
39 namespace DataControl {
44 static const char OSP_V_CALLER_TYPE_OSP[] = "osp";
45 static const char OSP_V_LAUNCH_TYPE_LAUNCH[] = "launch";
46 static const char OSP_V_LAUNCH_TYPE_APPCONTROL[] = "appcontrol";
47 static const char OSP_V_LAUNCH_TYPE_DATACONTROL[] = "datacontrol";
48 static const char OSP_V_LAUNCH_TYPE_CONDTION[] = "condition";
49 static const char OSP_V_REQUEST_TYPE_SQL_QUERY[] = "sql_query";
50 static const char OSP_V_REQUEST_TYPE_SQL_INSERT[] = "sql_insert";
51 static const char OSP_V_REQUEST_TYPE_SQL_UPDATE[] = "sql_update";
52 static const char OSP_V_REQUEST_TYPE_SQL_DELETE[] = "sql_delete";
53 static const char OSP_V_REQUEST_TYPE_MAP_QEURY[] = "map_query";
54 static const char OSP_V_REQUEST_TYPE_MAP_INSERT[] = "map_insert";
55 static const char OSP_V_REQUEST_TYPE_MAP_UPDATE[] = "map_update";
56 static const char OSP_V_REQUEST_TYPE_MAP_DELETE[] = "map_delete";
57 static const char BUNDLE_KEY_WINDOW[] = "__APP_SVC_K_WIN_ID__";
60 #define OSP_K_CALLER_TYPE "__OSP_CALLER_TYPE__"
61 #define OSP_K_LAUNCH_TYPE "__OSP_LAUNCH_TYPE__"
62 #define OSP_K_ARG "__OSP_ARGS__"
63 #define OSP_K_COND "__OSP_COND_NAME__"
64 #define OSP_K_APPID "__OSP_APPID__"
65 #define OSP_K_REQUEST_ID "__OSP_REQUEST_ID__"
66 #define OSP_K_APPCONTROL_PROVIDER "__OSP_APPCONTROL_PROVIDER__"
67 #define OSP_K_APPCONTROL_OPERATION "__OSP_APPCONTROL_OPERATION__"
68 #define OSP_K_APPCONTROL_CATEGORY "__OSP_APPCONTROL_CATEGORY__"
69 #define OSP_K_APPCONTROL_MIME "__OSP_APPCONTROL_MIME__"
70 #define OSP_K_APPCONTROL_URI "__OSP_APPCONTROL_URI__"
71 #define OSP_K_DATACONTROL_PROVIDER "__OSP_DATACONTROL_PROVIDER__"
72 #define OSP_K_DATACONTROL_REQUEST_TYPE "__OSP_DATACONTROL_REQUEST_TYPE__"
73 #define RESULT_TRUE_FROM_OSP "1"
74 #define RESULT_FALSE_FROM_OSP "0"
75 #define OSP_K_DATACONTROL_PROTOCOL_VERSION "__OSP_DATACONTROL_PROTOCOL_VERSION__"
76 #define OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE "ver_2.1.0.3"
78 #define DATACONTROL_PROTOCOL_DIR_TOP "/tmp/osp"
79 #define DATACONTROL_PROTOCOL_DIR_MIDDLE "/tmp/osp/data-control"
80 #define DATACONTROL_PROTOCOL_DIR "/tmp/osp/data-control/request"
82 static std::vector<std::string> getDataArrayFromBundle(bundle *b, std::string key)
84 std::vector<std::string> result;
89 array = appsvc_get_data_array(b, key.c_str(), &length);
91 if (array == NULL || length == 0)
93 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result data fail from datacontrol provider");
96 for (index = 0; index < length; index++)
98 result.push_back(array[index]);
105 static void sqldataControlSelectCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
109 EventSelectPendingEvent *pendingEvent = NULL;
110 SQLDataControlConsumer *consumer = NULL;
111 EventSelectPtr event;
112 const char *bundleKey = NULL;
118 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
121 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
123 if (bundleKey == NULL)
125 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
128 pendingEvent = (EventSelectPendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
129 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
131 if (DataControlAsyncCallbackManagerSingleton::Instance().isSQLDataControlGC((void*)consumer))
133 LoggerD("private object is garbage collected");
137 event = pendingEvent->getEvent();
139 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
141 for (size_t index = 0; index < result.size(); index++)
143 LoggerD(result[index]);
146 if (result.size() < 3)
148 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
151 // 0 : result true or false??
152 if (RESULT_TRUE_FROM_OSP != result[0])
155 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
158 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
159 // 2 : result set file path
160 event->setResultSetPath(result[2]);
163 catch (const WrtDeviceApis::Commons::Exception& ex)
165 LoggerE("Exception: " << ex.GetMessage());
167 if (event.Get() == NULL)
169 LoggerD("event removed, invalid cb");
173 event->setExceptionCode(ex.getCode());
174 event->setErrorMsg(ex.GetMessage());
176 consumer->handlePendingEvent(event);
185 static void sqldataControlInsertCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
190 EventInsertPendingEvent *pendingEvent = NULL;
191 SQLDataControlConsumer *consumer = NULL;
192 EventInsertPtr event;
193 const char *bundleKey = NULL;
199 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
202 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
204 if (bundleKey == NULL)
206 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
209 pendingEvent = (EventInsertPendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
210 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
212 if (DataControlAsyncCallbackManagerSingleton::Instance().isSQLDataControlGC((void*)consumer))
214 LoggerD("private object is garbage collected");
219 event = pendingEvent->getEvent();
221 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
223 for (size_t index = 0; index < result.size(); index++)
225 LoggerD(result[index]);
228 if (result.size() < 2)
230 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
233 // 0 : result true or false??
234 if (RESULT_TRUE_FROM_OSP != result[0])
237 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
241 std::stringstream sstr(result[2]);
242 long insertRowid = 0;
244 LoggerD(result[2] << insertRowid);
245 event->setRowId(insertRowid);
246 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
249 catch (const WrtDeviceApis::Commons::Exception& ex)
251 LoggerE("Exception: " << ex.GetMessage());
253 if (event.Get() == NULL)
255 LoggerD("event removed, invalid cb");
259 event->setExceptionCode(ex.getCode());
260 event->setErrorMsg(ex.GetMessage());
262 consumer->handlePendingEvent(event);
271 static void sqldataControlDeleteCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
275 EventDeletePendingEvent* pendingEvent = NULL;
276 SQLDataControlConsumer *consumer = NULL;
277 EventDeletePtr event;
278 const char* bundleKey = NULL;
284 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
287 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
289 if (bundleKey == NULL)
291 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
294 pendingEvent = (EventDeletePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
295 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
297 if (DataControlAsyncCallbackManagerSingleton::Instance().isSQLDataControlGC((void*)consumer))
299 LoggerD("private object is garbage collected");
304 event = pendingEvent->getEvent();
306 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
308 for (size_t index = 0; index < result.size(); index++)
310 LoggerD(result[index]);
313 if (result.size() < 2)
315 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
318 // 0 : result true or false??
319 if (RESULT_TRUE_FROM_OSP != result[0])
322 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
325 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
329 catch (const WrtDeviceApis::Commons::Exception& ex)
331 LoggerE("Exception: " << ex.GetMessage());
333 if (event.Get() == NULL)
335 LoggerD("event removed, invalid cb");
339 event->setExceptionCode(ex.getCode());
340 event->setErrorMsg(ex.GetMessage());
342 consumer->handlePendingEvent(event);
350 static void sqldataControlUpdateCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
354 EventUpdatePendingEvent* pendingEvent = NULL;
355 SQLDataControlConsumer *consumer = NULL;
356 EventUpdatePtr event;
357 const char *bundleKey = NULL;
363 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
366 bundleKey = appsvc_get_data(b, OSP_K_REQUEST_ID);
368 if (bundleKey == NULL)
370 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get key fail from datacontrol provider");
373 pendingEvent = (EventUpdatePendingEvent *)DataControlAsyncCallbackManagerSingleton::Instance().removeSQLUserData(bundleKey);
374 consumer = (SQLDataControlConsumer*)pendingEvent->getThisObject();
376 if (DataControlAsyncCallbackManagerSingleton::Instance().isSQLDataControlGC((void*)consumer))
378 LoggerD("private object is garbage collected");
383 event = pendingEvent->getEvent();
385 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
387 for (size_t index = 0; index < result.size(); index++)
389 LoggerD(result[index]);
392 if (result.size() < 2)
394 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
397 // 0 : result true or false??
398 if (RESULT_TRUE_FROM_OSP != result[0])
401 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
404 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
407 catch (const WrtDeviceApis::Commons::Exception& ex)
409 LoggerE("Exception: " << ex.GetMessage());
411 if (event.Get() == NULL)
413 LoggerD("event removed, invalid cb");
417 event->setExceptionCode(ex.getCode());
418 event->setErrorMsg(ex.GetMessage());
420 consumer->handlePendingEvent(event);
428 static void sqldataControlCommonCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
434 LoggerD("Bundle null, Error");
438 const char *reqType = appsvc_get_data(b, OSP_K_DATACONTROL_REQUEST_TYPE);
442 LoggerD("UnkownRequest");
446 std::istringstream buffer(reqType);
449 buffer >> reqTypeInt;
456 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
457 sqldataControlSelectCallback(b, request_code, res, data);
459 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
460 sqldataControlInsertCallback(b, request_code, res, data);
462 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
463 sqldataControlDeleteCallback(b, request_code, res, data);
465 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
466 sqldataControlUpdateCallback(b, request_code, res, data);
469 LoggerD("Unknown Request");
475 SQLDataControlConsumer::SQLDataControlConsumer(std::string& provId, std::string& dataId, std::string& type)
481 m_providerId = provId;
483 m_appId = getApplicationId(provId);
484 m_ProviderPkgId = getProviderPkgId(m_appId);
485 security_server_app_give_access(m_ProviderPkgId.c_str(), -1);
487 m_currentAppId = getCurrentApplicationId();
489 DataControlAsyncCallbackManagerSingleton::Instance().setSQLDataControlGC((void*)this, false);
492 SQLDataControlConsumer::~SQLDataControlConsumer()
495 DataControlAsyncCallbackManagerSingleton::Instance().setSQLDataControlGC((void*)this, true);
498 DPL::Mutex SQLDataControlConsumer::m_mutex;
499 std::vector<unsigned int> SQLDataControlConsumer::m_currentReqIds;
501 std::string SQLDataControlConsumer::getCurrentApplicationId()
504 std::string appId = "";
505 int parent_pid = getppid();
506 LoggerD("parent pid : " << parent_pid);
507 int ret = app_manager_get_app_id(parent_pid, &app_id);
509 if((ret != APP_ERROR_NONE) || (app_id == NULL))
511 LoggerE("Can not get app id from current pid (" << ret << ")");
512 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "could not get information about application");
519 void SQLDataControlConsumer::removeReqId(unsigned int reqId)
523 std::vector<unsigned int>::iterator it, found;
525 for (it = m_currentReqIds.begin(); it != m_currentReqIds.end(); ++it)
536 DPL::Mutex::ScopedLock lock(&m_mutex);
537 m_currentReqIds.erase(found);
541 bool SQLDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
543 std::string reqIdStr;
544 std::stringstream ssReqId;
547 reqIdStr = ssReqId.str();
548 return DataControlAsyncCallbackManagerSingleton::Instance().checkReqIdUnique(reqIdStr);
552 std::string SQLDataControlConsumer::getDataId()
557 std::string SQLDataControlConsumer::getProviderId()
562 std::string SQLDataControlConsumer::getType()
567 std::string SQLDataControlConsumer::getProviderPkgId(const std::string& appId)
570 app_info_h handle = NULL;
572 if (m_ProviderPkgId.length() != 0)
574 return m_ProviderPkgId;
577 int ret = app_manager_get_app_info(appId.c_str(), &handle);
579 if (ret != APP_ERROR_NONE)
581 LoggerD("Fail to get appinfo");
582 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
585 ret = app_info_get_package(handle, &pkgId);
587 if ((ret != APP_ERROR_NONE) || (pkgId == NULL))
589 LoggerD("Fail to get pkg_name");
590 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get pkdid error");
592 m_ProviderPkgId = pkgId;
593 app_info_destroy(handle);
595 return m_ProviderPkgId;
598 std::string SQLDataControlConsumer::getApplicationId(const std::string& provId)
600 std::string appIdStr = "";
604 const char *passId = provId.c_str();
606 if (m_appId.length() == 0)
609 if( pkgmgr_datacontrol_get_info(passId, OSP_PKGINFO_SQL_TYPE, &appId, &access) < 0)
611 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
633 std::string SQLDataControlConsumer::generateFileName(unsigned int reqId)
635 std::stringstream ssdata;
638 std::string parent = DATACONTROL_PROTOCOL_DIR;
639 std::string filename = parent + "/" + m_currentAppId + ssdata.str();
643 void SQLDataControlConsumer::saveArrayToFile(std::string filename, RowData *rowData)
645 std::fstream insertUpdateFile;
646 std::map<std::string, std::string>::iterator it;
648 insertUpdateFile.open(filename.c_str(), std::ios::out | std::ios::binary);
650 if (!insertUpdateFile.is_open())
652 LoggerD("fail open" << filename);
653 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "DataControl IPC Error");
656 for (it = rowData->m_Data.begin(); it != rowData->m_Data.end(); ++it)
658 strLength = it->first.size();
659 insertUpdateFile.write((const char*)&strLength, sizeof(int));
660 insertUpdateFile.write(it->first.c_str(), strLength);
662 strLength = it->second.size();
663 insertUpdateFile.write((const char*)&strLength, sizeof(int));
664 insertUpdateFile.write(it->second.c_str(), strLength);
667 insertUpdateFile.close();
670 void SQLDataControlConsumer::addArrayToBundle(bundle* passData, std::vector<std::string>& array)
672 size_t arraySize = array.size();
676 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "size = 0");
679 const char** arr = NULL;
680 arr = (const char**)calloc(sizeof(char*), arraySize);
684 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "null");
687 for (size_t index = 0; index < arraySize; index++)
689 arr[index] = array[index].c_str();
693 bundle_add_str_array(passData, OSP_K_ARG, arr, arraySize);
700 void SQLDataControlConsumer::handlePendingEvent(const EventInsertPtr& event)
703 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(event);
705 CommonPendingEvent* userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
709 SendAppControlLaunchToProvider(userData);
712 catch (const WrtDeviceApis::Commons::Exception& ex)
714 LoggerE("Exception: " << ex.GetMessage());
718 void SQLDataControlConsumer::handlePendingEvent(const EventDeletePtr& event)
720 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(event);
722 CommonPendingEvent* userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
726 SendAppControlLaunchToProvider(userData);
729 catch (const WrtDeviceApis::Commons::Exception& ex)
731 LoggerE("Exception: " << ex.GetMessage());
736 void SQLDataControlConsumer::handlePendingEvent(const EventSelectPtr& event)
738 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(event);
740 CommonPendingEvent* userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
744 SendAppControlLaunchToProvider(userData);
747 catch (const WrtDeviceApis::Commons::Exception& ex)
749 LoggerE("Exception: " << ex.GetMessage());
754 void SQLDataControlConsumer::handlePendingEvent(const EventUpdatePtr& event)
756 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(event);
758 CommonPendingEvent* userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
762 SendAppControlLaunchToProvider(userData);
765 catch (const WrtDeviceApis::Commons::Exception& ex)
767 LoggerE("Exception: " << ex.GetMessage());
773 void SQLDataControlConsumer::insertData(const EventInsertPtr& event)
775 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::PostRequest(event);
778 void SQLDataControlConsumer::deleteData(const EventDeletePtr& event)
780 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::PostRequest(event);
783 void SQLDataControlConsumer::selectData(const EventSelectPtr& event)
785 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::PostRequest(event);
788 void SQLDataControlConsumer::updateData(const EventUpdatePtr& event)
790 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::PostRequest(event);
793 void SQLDataControlConsumer::createResultDir()
796 memset(&info, 0, sizeof(struct stat));
798 int status = lstat(DATACONTROL_PROTOCOL_DIR_TOP, &info);
800 if (status != 0 && errno == ENOENT)
802 if (mkdir(DATACONTROL_PROTOCOL_DIR_TOP, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
804 LoggerD("make error");
805 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
809 status = lstat(DATACONTROL_PROTOCOL_DIR_MIDDLE, &info);
811 if (status != 0 && errno == ENOENT)
813 if (mkdir(DATACONTROL_PROTOCOL_DIR_MIDDLE, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
815 LoggerD("make error");
816 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
820 status = lstat(DATACONTROL_PROTOCOL_DIR, &info);
825 LoggerD("already exist");
828 else if (errno == ENOENT)
830 if (mkdir(DATACONTROL_PROTOCOL_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
832 LoggerD("make error");
833 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
837 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could be error during checking status");
840 std::string SQLDataControlConsumer::convertIntToString(unsigned int data)
842 std::stringstream ssbuffer;
846 return ssbuffer.str();
849 void SQLDataControlConsumer::SendAppControlLaunchToProvider(void* event)
852 bundle* passData = NULL;
853 std::string dataId = getDataId();
854 std::vector<std::string> queryItem;
855 std::stringstream ssBuffer;
856 std::string stringBuffer;
857 CommonPendingEvent* pEvent = (CommonPendingEvent*)(event);
858 std::string ipcFilename;
860 unsigned int reqId = 0;
861 unsigned int columnSize = 0;
865 passData = bundle_create();
867 if (passData == NULL)
869 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
874 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "event null");
877 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
878 appsvc_set_appid(passData, m_appId.c_str());
880 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
881 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
882 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
883 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
884 bundle_add(passData, AUL_K_CALLER_APPID, m_currentAppId.c_str());
886 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
888 EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
889 EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
891 reqId = insertEvent->getReqId();
892 columnSize = insertEvent->getColumnSize();
894 stringBuffer = convertIntToString(reqId);
895 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
897 ipcFilename = generateFileName(reqId);
898 stringBuffer = convertIntToString(columnSize);
900 queryItem.push_back(dataId);
901 queryItem.push_back(stringBuffer);
902 queryItem.push_back(ipcFilename);
904 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_INSERT);
905 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
907 addArrayToBundle(passData, queryItem);
909 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
911 EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
912 EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
914 reqId = updateEvent->getReqId();
915 columnSize = updateEvent->getColumnSize();
916 where = updateEvent->getWhere();
918 stringBuffer = convertIntToString(reqId);
919 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
921 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE);
922 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
924 ipcFilename = generateFileName(reqId);
925 stringBuffer = convertIntToString(columnSize);
927 queryItem.push_back(dataId);
928 queryItem.push_back(stringBuffer);
929 queryItem.push_back(ipcFilename);
931 if (where.size() == 0) // where
933 queryItem.push_back("NULL");
937 queryItem.push_back(where);
940 addArrayToBundle(passData, queryItem);
943 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
945 EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
946 EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
948 reqId = selectEvent->getReqId();
949 std::vector<std::string> columns = selectEvent->getColumns();
950 columnSize = columns.size();
951 where = selectEvent->getWhere();
952 std::string order = selectEvent->getOrder();
953 std::string page = selectEvent->getPage();
954 std::string numberPerPage = selectEvent->getNumerPerPage();
956 stringBuffer = convertIntToString(reqId);
957 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
959 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_QUERY);
960 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
962 queryItem.push_back(dataId);
966 queryItem.push_back("NULL");
970 stringBuffer = convertIntToString(columnSize);
971 queryItem.push_back(stringBuffer);
973 for (int index = 0; index < columnSize; index++)
975 queryItem.push_back(columns[index]);
979 if (where.size() == 0)
981 queryItem.push_back("NULL");
985 queryItem.push_back(where);
988 if (order.size() == 0)
990 queryItem.push_back("NULL");
994 queryItem.push_back(order);
997 if (page.size() == 0) // page
999 queryItem.push_back("1");
1003 queryItem.push_back(page);
1006 if (numberPerPage.size() == 0) // numberOfPage
1008 queryItem.push_back("20");
1012 queryItem.push_back(numberPerPage);
1015 addArrayToBundle(passData, queryItem);
1017 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
1019 EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
1020 EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
1021 reqId = deleteEvent->getReqId();
1022 stringBuffer = convertIntToString(reqId);
1023 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
1025 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_DELETE);
1026 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
1028 queryItem.push_back(dataId); // dataid
1029 where = deleteEvent->getWhere();
1031 if (where.size() == 0) // where
1033 queryItem.push_back("NULL");
1037 queryItem.push_back(where);
1040 addArrayToBundle(passData, queryItem);
1045 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "no type request type");
1048 int pid = appsvc_run_service(passData, reqId, sqldataControlCommonCallback, (void*)NULL);
1052 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Launch Error");
1055 LoggerD("Launch OK : pid(" << pid << "), reqid : (" << reqId << ")");
1057 catch (const WrtDeviceApis::Commons::Exception& ex)
1059 LoggerE("Exception: " << ex.GetMessage());
1062 bundle_free(passData);
1065 ThrowMsg(WrtDeviceApis::Commons::PlatformException, ex.GetMessage());
1071 void SQLDataControlConsumer::OnRequestReceived(const EventInsertPtr& event)
1077 RowData* rowData = event->getRowData();
1078 unsigned int reqId = event->getReqId();
1079 std::string ipcFilename = generateFileName(reqId);
1080 std::string reqIdStr = convertIntToString(reqId);
1082 if (checkReqIdUniqueness(reqId) == false)
1084 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1087 if (rowData == NULL || rowData->m_Data.size() == 0)
1089 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No insertion data");
1092 saveArrayToFile(ipcFilename, rowData);
1095 EventInsertPendingEvent* pendingEvent = new EventInsertPendingEvent((void*)this, event);
1097 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1099 SendAppControlLaunchToProvider(pendingEvent);
1101 event->switchToManualAnswer();
1102 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1106 catch (const WrtDeviceApis::Commons::Exception& ex)
1108 event->setExceptionCode(ex.getCode());
1109 event->setErrorMsg(ex.GetMessage());
1110 LoggerE("Exception: " << ex.GetMessage());
1115 void SQLDataControlConsumer::OnRequestReceived(const EventDeletePtr& event)
1118 bundle* passData = NULL;
1122 unsigned int reqId = event->getReqId();
1123 std::string reqIdStr = convertIntToString(reqId);
1125 if (checkReqIdUniqueness(reqId) == false)
1127 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1130 EventDeletePendingEvent* pendingEvent = new EventDeletePendingEvent(this, event);
1132 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1134 SendAppControlLaunchToProvider(pendingEvent);
1136 event->switchToManualAnswer();
1137 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1140 catch (const WrtDeviceApis::Commons::Exception& ex)
1142 LoggerE("Exception: " << ex.GetMessage());
1143 event->setExceptionCode(ex.getCode());
1144 event->setErrorMsg(ex.GetMessage());
1149 bundle_free(passData);
1154 void SQLDataControlConsumer::OnRequestReceived(const EventSelectPtr& event)
1158 unsigned int reqId = event->getReqId();
1159 std::string reqIdStr = convertIntToString(reqId);
1161 if (checkReqIdUniqueness(reqId) == false)
1163 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1166 EventSelectPendingEvent* pendingEvent = new EventSelectPendingEvent(this, event);
1168 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1170 SendAppControlLaunchToProvider(pendingEvent);
1173 event->switchToManualAnswer();
1174 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1176 catch (const WrtDeviceApis::Commons::Exception& ex)
1178 LoggerE("Exception: " << ex.GetMessage());
1179 event->setExceptionCode(ex.getCode());
1180 event->setErrorMsg(ex.GetMessage());
1185 void SQLDataControlConsumer::OnRequestReceived(const EventUpdatePtr& event)
1191 unsigned int reqId = event->getReqId();
1192 RowData* rowData = event->getRowData();
1193 std::string ipcFilename = generateFileName(reqId);
1194 std::stringstream ssReqId;
1197 if (checkReqIdUniqueness(reqId) == false)
1199 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1202 if (rowData == NULL || rowData->m_Data.size() == 0)
1204 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No update data");
1207 saveArrayToFile(ipcFilename, rowData);
1210 EventUpdatePendingEvent* pendingEvent = new EventUpdatePendingEvent(this, event);
1212 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1214 SendAppControlLaunchToProvider(pendingEvent);
1216 event->switchToManualAnswer();
1217 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(ssReqId.str(), (void*)pendingEvent);
1220 catch (const WrtDeviceApis::Commons::Exception& ex)
1222 LoggerE("Exception: " << ex.GetMessage());
1223 event->setExceptionCode(ex.getCode());
1224 event->setErrorMsg(ex.GetMessage());