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);
699 void SQLDataControlConsumer::handleCommonErrorEvent(void* userData, unsigned int code, std::string msg)
701 if (userData == NULL)
703 LoggerD("userData null");
707 CommonPendingEvent* pEvent = (CommonPendingEvent*)userData;
711 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
713 EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
714 EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
715 insertEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
716 insertEvent->setErrorMsg(msg);
718 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(insertEvent);
720 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
722 EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
723 EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
724 updateEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
725 updateEvent->setErrorMsg(msg);
727 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(updateEvent);
729 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
731 EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
732 EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
733 selectEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
734 selectEvent->setErrorMsg(msg);
736 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(selectEvent);
738 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
740 EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
741 EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
742 deleteEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
743 deleteEvent->setErrorMsg(msg);
745 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(deleteEvent);
748 catch (const WrtDeviceApis::Commons::Exception& ex)
750 LoggerE("Exception: " << ex.GetMessage());
754 void SQLDataControlConsumer::handlePendingEvent(const EventInsertPtr& event)
757 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(event);
758 CommonPendingEvent* userData = NULL;
761 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
765 SendAppControlLaunchToProvider(userData);
768 catch (const WrtDeviceApis::Commons::Exception& ex)
770 if (event.Get() == NULL)
772 LoggerD("event removed, invalid cb");
775 LoggerE("Exception: " << ex.GetMessage());
779 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
784 void SQLDataControlConsumer::handlePendingEvent(const EventDeletePtr& event)
786 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(event);
787 CommonPendingEvent* userData = NULL;
790 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
794 SendAppControlLaunchToProvider(userData);
797 catch (const WrtDeviceApis::Commons::Exception& ex)
799 if (event.Get() == NULL)
801 LoggerD("event removed, invalid cb");
804 LoggerE("Exception: " << ex.GetMessage());
808 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
814 void SQLDataControlConsumer::handlePendingEvent(const EventSelectPtr& event)
816 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(event);
817 CommonPendingEvent* userData = NULL;
820 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
824 SendAppControlLaunchToProvider(userData);
827 catch (const WrtDeviceApis::Commons::Exception& ex)
829 if (event.Get() == NULL)
831 LoggerD("event removed, invalid cb");
834 LoggerE("Exception: " << ex.GetMessage());
838 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
846 void SQLDataControlConsumer::handlePendingEvent(const EventUpdatePtr& event)
848 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(event);
849 CommonPendingEvent* userData = NULL;
852 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingSQLOperation();
856 SendAppControlLaunchToProvider(userData);
859 catch (const WrtDeviceApis::Commons::Exception& ex)
861 if (event.Get() == NULL)
863 LoggerD("event removed, invalid cb");
866 LoggerE("Exception: " << ex.GetMessage());
870 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
877 void SQLDataControlConsumer::insertData(const EventInsertPtr& event)
879 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::PostRequest(event);
882 void SQLDataControlConsumer::deleteData(const EventDeletePtr& event)
884 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::PostRequest(event);
887 void SQLDataControlConsumer::selectData(const EventSelectPtr& event)
889 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::PostRequest(event);
892 void SQLDataControlConsumer::updateData(const EventUpdatePtr& event)
894 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::PostRequest(event);
897 void SQLDataControlConsumer::createResultDir()
900 memset(&info, 0, sizeof(struct stat));
902 int status = lstat(DATACONTROL_PROTOCOL_DIR_TOP, &info);
904 if (status != 0 && errno == ENOENT)
906 if (mkdir(DATACONTROL_PROTOCOL_DIR_TOP, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
908 LoggerD("make error");
909 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
913 status = lstat(DATACONTROL_PROTOCOL_DIR_MIDDLE, &info);
915 if (status != 0 && errno == ENOENT)
917 if (mkdir(DATACONTROL_PROTOCOL_DIR_MIDDLE, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
919 LoggerD("make error");
920 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
924 status = lstat(DATACONTROL_PROTOCOL_DIR, &info);
929 LoggerD("already exist");
932 else if (errno == ENOENT)
934 if (mkdir(DATACONTROL_PROTOCOL_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
936 LoggerD("make error");
937 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
941 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could be error during checking status");
944 std::string SQLDataControlConsumer::convertIntToString(unsigned int data)
946 std::stringstream ssbuffer;
950 return ssbuffer.str();
953 void SQLDataControlConsumer::SendAppControlLaunchToProvider(void* event)
956 bundle* passData = NULL;
957 std::string dataId = getDataId();
958 std::vector<std::string> queryItem;
959 std::stringstream ssBuffer;
960 std::string stringBuffer;
961 CommonPendingEvent* pEvent = (CommonPendingEvent*)(event);
962 std::string ipcFilename;
964 unsigned int reqId = 0;
965 unsigned int columnSize = 0;
969 passData = bundle_create();
971 if (passData == NULL)
973 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
978 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "event null");
981 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
982 appsvc_set_appid(passData, m_appId.c_str());
984 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
985 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
986 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
987 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
988 bundle_add(passData, AUL_K_CALLER_APPID, m_currentAppId.c_str());
990 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
992 EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
993 EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
995 reqId = insertEvent->getReqId();
996 columnSize = insertEvent->getColumnSize();
998 stringBuffer = convertIntToString(reqId);
999 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
1001 ipcFilename = generateFileName(reqId);
1002 stringBuffer = convertIntToString(columnSize);
1004 queryItem.push_back(dataId);
1005 queryItem.push_back(stringBuffer);
1006 queryItem.push_back(ipcFilename);
1008 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_INSERT);
1009 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
1011 addArrayToBundle(passData, queryItem);
1013 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
1015 EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
1016 EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
1018 reqId = updateEvent->getReqId();
1019 columnSize = updateEvent->getColumnSize();
1020 where = updateEvent->getWhere();
1022 stringBuffer = convertIntToString(reqId);
1023 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
1025 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE);
1026 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
1028 ipcFilename = generateFileName(reqId);
1029 stringBuffer = convertIntToString(columnSize);
1031 queryItem.push_back(dataId);
1032 queryItem.push_back(stringBuffer);
1033 queryItem.push_back(ipcFilename);
1035 if (where.size() == 0) // where
1037 queryItem.push_back("NULL");
1041 queryItem.push_back(where);
1044 addArrayToBundle(passData, queryItem);
1047 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
1049 EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
1050 EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
1052 reqId = selectEvent->getReqId();
1053 std::vector<std::string> columns = selectEvent->getColumns();
1054 columnSize = columns.size();
1055 where = selectEvent->getWhere();
1056 std::string order = selectEvent->getOrder();
1057 std::string page = selectEvent->getPage();
1058 std::string numberPerPage = selectEvent->getNumerPerPage();
1060 stringBuffer = convertIntToString(reqId);
1061 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
1063 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_QUERY);
1064 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
1066 queryItem.push_back(dataId);
1068 if (columnSize == 0)
1070 queryItem.push_back("NULL");
1074 stringBuffer = convertIntToString(columnSize);
1075 queryItem.push_back(stringBuffer);
1077 for (size_t index = 0; index < columnSize; index++)
1079 queryItem.push_back(columns[index]);
1083 if (where.size() == 0)
1085 queryItem.push_back("NULL");
1089 queryItem.push_back(where);
1092 if (order.size() == 0)
1094 queryItem.push_back("NULL");
1098 queryItem.push_back(order);
1101 if (page.size() == 0) // page
1103 queryItem.push_back("1");
1107 queryItem.push_back(page);
1110 if (numberPerPage.size() == 0) // numberOfPage
1112 queryItem.push_back("20");
1116 queryItem.push_back(numberPerPage);
1119 addArrayToBundle(passData, queryItem);
1121 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
1123 EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
1124 EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
1125 reqId = deleteEvent->getReqId();
1126 stringBuffer = convertIntToString(reqId);
1127 bundle_add(passData, OSP_K_REQUEST_ID, stringBuffer.c_str());
1129 stringBuffer = convertIntToString(_DATACONTROL_REQUEST_TYPE_SQL_DELETE);
1130 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, stringBuffer.c_str());
1132 queryItem.push_back(dataId); // dataid
1133 where = deleteEvent->getWhere();
1135 if (where.size() == 0) // where
1137 queryItem.push_back("NULL");
1141 queryItem.push_back(where);
1144 addArrayToBundle(passData, queryItem);
1149 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "no type request type");
1152 int pid = appsvc_run_service(passData, reqId, sqldataControlCommonCallback, (void*)NULL);
1156 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Launch Error");
1159 LoggerD("Launch OK : pid(" << pid << "), reqid : (" << reqId << ")");
1161 catch (const WrtDeviceApis::Commons::Exception& ex)
1163 LoggerE("Exception: " << ex.GetMessage());
1166 bundle_free(passData);
1169 ThrowMsg(WrtDeviceApis::Commons::PlatformException, ex.GetMessage());
1175 bundle_free(passData);
1181 void SQLDataControlConsumer::OnRequestReceived(const EventInsertPtr& event)
1187 RowData* rowData = event->getRowData();
1188 unsigned int reqId = event->getReqId();
1189 std::string ipcFilename = generateFileName(reqId);
1190 std::string reqIdStr = convertIntToString(reqId);
1192 if (checkReqIdUniqueness(reqId) == false)
1194 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1197 if (rowData == NULL || rowData->m_Data.size() == 0)
1199 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No insertion data");
1202 saveArrayToFile(ipcFilename, rowData);
1205 EventInsertPendingEvent* pendingEvent = new EventInsertPendingEvent((void*)this, event);
1207 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1209 SendAppControlLaunchToProvider(pendingEvent);
1211 event->switchToManualAnswer();
1212 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1216 catch (const WrtDeviceApis::Commons::Exception& ex)
1218 event->setExceptionCode(ex.getCode());
1219 event->setErrorMsg(ex.GetMessage());
1220 LoggerE("Exception: " << ex.GetMessage());
1225 void SQLDataControlConsumer::OnRequestReceived(const EventDeletePtr& event)
1228 bundle* passData = NULL;
1232 unsigned int reqId = event->getReqId();
1233 std::string reqIdStr = convertIntToString(reqId);
1235 if (checkReqIdUniqueness(reqId) == false)
1237 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1240 EventDeletePendingEvent* pendingEvent = new EventDeletePendingEvent(this, event);
1242 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1244 SendAppControlLaunchToProvider(pendingEvent);
1246 event->switchToManualAnswer();
1247 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1250 catch (const WrtDeviceApis::Commons::Exception& ex)
1252 LoggerE("Exception: " << ex.GetMessage());
1253 event->setExceptionCode(ex.getCode());
1254 event->setErrorMsg(ex.GetMessage());
1259 bundle_free(passData);
1264 void SQLDataControlConsumer::OnRequestReceived(const EventSelectPtr& event)
1268 unsigned int reqId = event->getReqId();
1269 std::string reqIdStr = convertIntToString(reqId);
1271 if (checkReqIdUniqueness(reqId) == false)
1273 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1276 EventSelectPendingEvent* pendingEvent = new EventSelectPendingEvent(this, event);
1278 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1280 SendAppControlLaunchToProvider(pendingEvent);
1283 event->switchToManualAnswer();
1284 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
1286 catch (const WrtDeviceApis::Commons::Exception& ex)
1288 LoggerE("Exception: " << ex.GetMessage());
1289 event->setExceptionCode(ex.getCode());
1290 event->setErrorMsg(ex.GetMessage());
1295 void SQLDataControlConsumer::OnRequestReceived(const EventUpdatePtr& event)
1301 unsigned int reqId = event->getReqId();
1302 RowData* rowData = event->getRowData();
1303 std::string ipcFilename = generateFileName(reqId);
1304 std::stringstream ssReqId;
1307 if (checkReqIdUniqueness(reqId) == false)
1309 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
1312 if (rowData == NULL || rowData->m_Data.size() == 0)
1314 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No update data");
1317 saveArrayToFile(ipcFilename, rowData);
1320 EventUpdatePendingEvent* pendingEvent = new EventUpdatePendingEvent(this, event);
1322 if (DataControlAsyncCallbackManagerSingleton::Instance().checkDoSQLOperation())
1324 SendAppControlLaunchToProvider(pendingEvent);
1326 event->switchToManualAnswer();
1327 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(ssReqId.str(), (void*)pendingEvent);
1330 catch (const WrtDeviceApis::Commons::Exception& ex)
1332 LoggerE("Exception: " << ex.GetMessage());
1333 event->setExceptionCode(ex.getCode());
1334 event->setErrorMsg(ex.GetMessage());