Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / app / FApp_SqlDataControlImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FApp_SqlDataControlImpl.cpp
19  * @brief       This is the implementation for the %_SqlDataControlImpl class.
20  */
21
22 #include <typeinfo>
23 #include <new>
24 #include <unique_ptr.h>
25
26 #include <appsvc/appsvc.h>
27
28 #include <FBaseInteger.h>
29 #include <FBaseString.h>
30 #include <FBaseLongLong.h>
31 #include <FBaseColIList.h>
32 #include <FBaseColIMap.h>
33 #include <FBaseColIMapEnumerator.h>
34 #include <FBaseRtIEventArg.h>
35 #include <FBaseSysLog.h>
36 #include <FIoFile.h>
37 #include <FAppApp.h>
38 #include <FAppSqlDataControl.h>
39 #include <FAppISqlDataControlResponseListener.h>
40
41 #include <FBase_StringConverter.h>
42 #include <FIo_DataControlResultSetEnumerator.h>
43
44 #include "FApp_AppControlManager.h"
45 #include "FApp_SqlDataControlImpl.h"
46 #include "FApp_AppArg.h"
47 #include "FApp_DataControlManager.h"
48
49 #define DATACONTROL_PROTOCOL_VER_2_1_0_2 // ver_2.1.0.2
50
51 using namespace std;
52
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Collection;
55 using namespace Tizen::Base::Runtime;
56 using namespace Tizen::App;
57 using namespace Tizen::Io;
58
59 namespace Tizen { namespace App
60 {
61
62 static const int MAX_REQUEST_COUNT = 128;
63 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
64 static const int _MAX_REQUEST_ARGUMENT_SIZE = 1048576; // 1MB
65 static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/DataControlRequest/\0";
66
67 class _SqlDataControlEventArg
68         : public IEventArg
69 {
70 public:
71                 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
72                                 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
73                         : __requestType(requestType)
74                         , __reqId(reqId)
75                         , __providerId(providerId)
76                         , __dataId(dataId)
77                         , __pResultSetEnum(pResultSetEnum)
78                         , __insertRowId(insertRowId)
79                         , __providerResult(providerResult)
80                         , __pErrorMsg(pErrorMsg)
81                 {
82                 }
83                 ~_SqlDataControlEventArg(void)
84                 {
85                         delete __pResultSetEnum;
86                         delete __pErrorMsg;
87                 }
88
89                 _DataControlRequestType __requestType;
90                 RequestId __reqId;
91                 String __providerId;
92                 String __dataId;
93                 _DataControlResultSetEnumerator* __pResultSetEnum;
94                 long long __insertRowId;
95                 bool __providerResult;
96                 String* __pErrorMsg;
97 };
98
99 class _SqlDataControlEvent
100         : public Event
101 {
102 protected:
103                 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
104 };
105
106 void
107 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
108 {
109         const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
110         if (pArg != null)
111         {
112                 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
113                 if (pListener != null)
114                 {
115                         switch (pArg->__requestType)
116                         {
117                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
118                                 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
119                                                 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
120                                 break;
121                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
122                                 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
123                                                  pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
124                                 break;
125                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
126                                 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
127                                                 pArg->__providerResult, pArg->__pErrorMsg);
128                                 break;
129                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
130                                 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
131                                                 pArg->__providerResult, pArg->__pErrorMsg);
132                                 break;
133                         default:
134                                 break;
135                         }
136                 }
137         }
138 }
139
140 // private
141 _SqlDataControlImpl::_SqlDataControlImpl(void)
142         : __appId(L"")
143         , __providerId(L"")
144         , __access(_DATACONTROL_ACCESS_UNDEFINED)
145         , __pPreviousListener(null)
146         , __pSqlDataControlEvent(null)
147 {
148 }
149
150 _SqlDataControlImpl::~_SqlDataControlImpl(void)
151 {
152         delete __pSqlDataControlEvent;
153 }
154
155 _SqlDataControlImpl*
156 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
157 {
158         return dc.__pSqlDataControlImpl;
159 }
160
161 const _SqlDataControlImpl*
162 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
163 {
164         return dc.__pSqlDataControlImpl;
165 }
166
167 result
168 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
169 {
170         Integer* pReqId = null;
171         _DataControlRequestInfo* pReqInfo = null;
172         result r = E_SUCCESS;
173
174         int req = -1;
175         _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
176
177         // Check the request count of DataControl operation
178         int count = pAppManagerImpl->GetLaunchRequestCount();
179         SysLog(NID_APP, "Current launch request count: %d", count);
180
181         SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
182
183         _AppArg* pArg = new (std::nothrow) _AppArg;  // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
184         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
185
186         pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
187
188         if (__pSqlDataControlEvent)
189         {
190                 // reqId is system-wide id because the bundle is system-wide.
191 #if 0
192                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
193                 req = reqObj.GetRequestNumber();
194 #else
195                 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
196                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
197                 req = reqObj.GetRequestNumber();
198 #endif
199                 pReqId = new (std::nothrow) Integer(req);
200                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
201                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
202
203                 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
204                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
205                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
206
207                 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
208                 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
209
210                 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
211                 if (IsFailed(r))
212                 {
213                         SysPropagate(NID_APP, r);
214                         reqObj.Invalidate();
215                         pDcMgr->RemoveRequestInfo(*pReqId);
216                         delete pArg;
217                         return r;
218                 }
219
220                 if (pReq)
221                 {
222                         *pReq = req;
223                 }
224         }
225         else
226         {
227                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
228                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
229                 delete pArg;
230         }
231
232         return E_SUCCESS;
233
234 CATCH:
235         delete pArg;
236         delete pReqId;
237         delete pReqInfo;
238         return r;
239 }
240
241 result
242 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
243                 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
244 {
245         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
246         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
247         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
248                         "The SELECT query is not permitted by DataControl provider.");
249
250         const String* pColumn = null;
251         int id = 0;
252         result r = E_SUCCESS;
253         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
254
255         ArrayList* pArgList = new ArrayList();
256         pArgList->Construct();
257
258         pArgList->Add(*(new String(dataId))); // list(0): data ID
259         long long argSize = dataId.GetLength() * sizeof(wchar_t);
260
261         if (pColumnList != null)
262         {
263                 int columnCount = pColumnList->GetCount();
264                 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
265                                 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
266
267                 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
268                 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
269
270                 int i = 0;
271                 while (i < columnCount) // list(2): column list
272                 {
273                         pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
274                         SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
275                                         "[E_INVALID_ARG] The object is not String class.");
276
277                         pArgList->Add(*(new String(*pColumn)));
278                         argSize += pColumn->GetLength() * sizeof(wchar_t);
279                         SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
280                         i++;
281                 }
282         }
283         else
284         {
285                 pArgList->Add(*(new String(L"NULL")));
286         }
287
288         if (pWhere != null)     // list(3): where clause
289         {
290                 pArgList->Add(*(new String(*pWhere)));
291                 argSize += pWhere->GetLength() * sizeof(wchar_t);
292                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
293         }
294         else
295         {
296                 pArgList->Add(*(new String(L"NULL")));
297         }
298
299         if (pOrder != null)     // list(4): order clause
300         {
301                 pArgList->Add(*(new String(*pOrder)));
302                 argSize += pOrder->GetLength() * sizeof(wchar_t);
303                 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
304         }
305         else
306         {
307                 pArgList->Add(*(new String(L"NULL")));
308         }
309         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
310                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
311
312         pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
313
314         pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
315
316         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
317         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
318
319         reqId = static_cast< RequestId >(id);
320
321         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, req: %d, pageNo: %d, countPerPage: %d",
322                         dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
323
324         // fall through
325 CATCH:
326         pArgList->RemoveAll(true);
327         delete pArgList;
328
329         return r;
330 }
331
332 result
333 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
334 {
335         ArrayList* pResultList = null;
336         String* pResult = null;
337         String* pProviderId = null;
338         String* pDataId = null;
339         String* pErrorMessage = null;
340         String* pErrorMsg = null;
341         String* pTmpPath = null;
342         String* pInsertRowId = null;
343         _DataControlResultSetEnumerator* pResultSetEnum = null;
344         int requestType = 0;
345         int reqId = 0;
346         int launchReqId = 0;
347         int providerRes = 0;
348         bool providerResult = true;
349         //bundle* origBundle = null;
350         bundle* resBundle = null;
351         _SqlDataControlEventArg* pEventArg = null;
352         const char* p = null;
353         result r = E_SUCCESS;
354
355         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
356         SysLog(NID_APP, "appsvc result value: %d", res);
357
358         resBundle = pResArg->GetBundle();
359         if (resBundle)
360         {
361                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
362                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
363                 reqId = atoi(p);
364                 Integer key(reqId);
365
366                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
367                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
368                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
369                                 "[E_SYSTEM] Failed to get request info");
370
371                 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
372                 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
373                                 "[E_SYSTEM] invalid request info");
374
375                 pDcMgr->RemoveRequestInfo(key);
376
377                 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
378                 {
379                         // result list
380                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
381                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
382
383                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
384                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
385                         Integer::Parse(*pResult, providerRes);
386                         providerResult = static_cast< bool >(providerRes);
387
388                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
389                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
390                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
391                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
392                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
393
394                         // request info
395                         //origBundle = pArg->GetBundle();
396
397                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
398                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
399                         requestType = atoi(p);
400
401                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
402                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
403                         pProviderId = new (std::nothrow) String(p);
404                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
405                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
406
407                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
408                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
409                         pDataId = new (std::nothrow) String(p);
410                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
411                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
412
413                         SysSecureLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls ",
414                                         providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
415
416                         switch (static_cast <_DataControlRequestType>(requestType))
417                         {
418                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
419                         {
420                                 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
421                                 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
422                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
423
424                                 if (providerResult == true)
425                                 {
426                                         pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
427                                         if (pTmpPath == null)
428                                         {
429                                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
430                                                 delete pResultSetEnum;
431                                                 goto CATCH;
432                                         }
433
434                                         SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
435                                         if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
436                                         {
437                                                 pResultSetEnum->SetPath(*pTmpPath);
438                                         }
439                                 }
440
441                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
442                                                 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
443                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
444                                 pSqlDataControlEvent->Fire(*pEventArg);
445
446                                 if (pTmpPath)
447                                 {
448                                         r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
449                                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
450                                 }
451
452                                 break;
453                         }
454                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
455                         {
456                                 long long insertRowId = -1;
457                                 if (providerResult == true)
458                                 {
459                                         pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
460                                         SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
461
462                                         LongLong::Parse(*pInsertRowId, insertRowId);
463                                 }
464
465                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
466                                                 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
467                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
468                                 pSqlDataControlEvent->Fire(*pEventArg);
469                                 break;
470                         }
471                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
472                         {
473                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
474                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
475                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
476                                 pSqlDataControlEvent->Fire(*pEventArg);
477                                 break;
478                         }
479                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
480                         {
481                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
482                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
483                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
484                                 pSqlDataControlEvent->Fire(*pEventArg);
485                                 break;
486                         }
487                         default:
488                                 break;
489                         }
490
491                         pResultList->RemoveAll(true);
492                         delete pResultList;
493                         delete pProviderId;
494                 }
495         }
496
497         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
498         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
499         launchReqId = atoi(p);
500
501         // Remove the request count
502         SysLog(NID_APP, "Remove a launch request: req: %d", launchReqId);
503         _AppControlManager::GetInstance()->RemoveLaunchRequest(launchReqId);
504
505         return E_SUCCESS;
506
507 CATCH:
508         if (pResultList)
509         {
510                 pResultList->RemoveAll(true);
511                 delete pResultList;
512         }
513         delete pProviderId;
514
515         return r;
516 }
517
518 result
519 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
520 {
521         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
522                         "The INSERT query is not permitted by DataControl provider.");
523
524         int columnCount = 0;
525         int id = 0;
526         int i = 0;
527         int uniqueId = -1;
528         File request;
529         IMapEnumerator* pMapEnum = null;
530         result r = E_SUCCESS;
531         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
532
533         columnCount = insertMap.GetCount();
534         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
535
536         ArrayList* pArgList = new (std::nothrow) ArrayList();
537         pArgList->Construct();
538
539         pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
540         long long argSize = dataId.GetLength() * sizeof(wchar_t);
541
542         pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: inserted column count
543         SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
544
545 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
546         String tmpPath(_DATACONTROL_REQUEST_DIR);
547         tmpPath.Append(App::GetInstance()->GetAppId());
548
549         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
550         SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
551         uniqueId = pDcMgr->GetUniqueId();
552
553         tmpPath.Append(uniqueId);
554         SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
555
556         r = request.Construct(tmpPath, L"w+", true);
557         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
558                         GetErrorMessage(r), tmpPath.GetPointer());
559
560         pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path 
561 #endif
562
563         pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
564         while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
565         {
566 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
567                 int length = 0;
568                 String* pColumn = null;
569                 String* pValue = null;
570                 unique_ptr< char[] > pData(null);
571
572                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
573                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
574                                 "[E_INVALID_ARG] The object is not String class.");
575
576                 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
577                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
578                                 GetErrorMessage(GetLastResult()));
579
580                 length = strlen(pData.get());
581                 r = request.Write(&length, sizeof(int)); // data length
582                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
583
584                 r = request.Write(pData.get(), length); // data
585                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
586
587                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
588                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
589                                 "[E_INVALID_ARG] The object is not String class.");
590
591                 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
592                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
593                                 GetErrorMessage(GetLastResult()));
594
595                 length = strlen(pData.get());
596                 r = request.Write(&length, sizeof(int)); // data length
597                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
598
599                 r = request.Write(pData.get(), length); // data
600                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
601
602                 argSize += pColumn->GetLength() * sizeof(wchar_t);
603                 argSize += pValue->GetLength() * sizeof(wchar_t);
604 #else
605                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
606                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
607                                 "[E_INVALID_ARG] The object is not String class.");
608
609                 pArgList->Add(*(new String(*pColumn)));
610                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
611
612                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
613                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
614                                 "[E_INVALID_ARG] The object is not String class.");
615
616                 pArgList->Add(*(new String(*pValue)));
617                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
618
619                 argSize += pColumn->GetLength() * sizeof(wchar_t);
620                 argSize += pValue->GetLength() * sizeof(wchar_t);
621                 i++;
622 #endif
623         }
624         SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
625                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
626
627 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
628         request.Flush();
629 #endif
630
631         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
632         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
633
634         reqId = static_cast< RequestId >(id);
635
636         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, insertMap: 0x%x, req: %d", dataId.GetPointer(), &insertMap, reqId);
637
638         // fall through
639 CATCH:
640         pArgList->RemoveAll(true);
641         delete pArgList;
642         delete pMapEnum;
643
644         return r;
645 }
646
647 result
648 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
649 {
650         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
651                         "The UPDATE query is not permitted by DataControl provider.");
652
653         int columnCount = 0;
654         int id = 0;
655         int i = 0;
656         int uniqueId = -1;
657         File request;
658         IMapEnumerator* pMapEnum = null;
659         result r = E_SUCCESS;
660         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
661
662         columnCount = updateMap.GetCount();
663         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
664
665         ArrayList* pArgList = new ArrayList();
666         pArgList->Construct();
667
668         pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
669         long long argSize = dataId.GetLength() * sizeof(wchar_t);
670
671         pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: updated column count
672         SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
673
674 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
675         String tmpPath(_DATACONTROL_REQUEST_DIR);
676         tmpPath.Append(App::GetInstance()->GetAppId());
677
678         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
679         SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
680         uniqueId = pDcMgr->GetUniqueId();
681
682         tmpPath.Append(uniqueId);
683         SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
684
685         r = request.Construct(tmpPath, L"w+", true);
686         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
687                         GetErrorMessage(r), tmpPath.GetPointer());
688
689         pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path 
690 #endif
691
692         pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
693         while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
694         {
695 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
696                 int length = 0;
697                 String* pColumn = null;
698                 String* pValue = null;
699                 unique_ptr< char[] > pData(null);
700
701                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
702                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
703                                 "[E_INVALID_ARG] The object is not String class.");
704
705                 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
706                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
707                                 GetErrorMessage(GetLastResult()));
708
709                 length = strlen(pData.get());
710                 r = request.Write(&length, sizeof(int)); // data length
711                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
712
713                 r = request.Write(pData.get(), length); // data
714                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
715
716                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
717                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
718                                 "[E_INVALID_ARG] The object is not String class.");
719
720                 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
721                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
722                                 GetErrorMessage(GetLastResult()));
723
724                 length = strlen(pData.get());
725                 r = request.Write(&length, sizeof(int)); // data length
726                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
727
728                 r = request.Write(pData.get(), length); // data
729                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
730
731                 argSize += pColumn->GetLength() * sizeof(wchar_t);
732                 argSize += pValue->GetLength() * sizeof(wchar_t);
733 #else
734                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
735                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
736                                 "[E_INVALID_ARG] The object is not String class.");
737
738                 pArgList->Add(*(new String(*pColumn)));
739                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
740
741                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
742                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
743                                 "[E_INVALID_ARG] The object is not String class.");
744
745                 pArgList->Add(*(new String(*pValue)));
746                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
747
748                 argSize += pColumn->GetLength() * sizeof(wchar_t);
749                 argSize += pValue->GetLength() * sizeof(wchar_t);
750                 i++;
751 #endif
752         }
753
754 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
755         request.Flush();
756 #endif
757
758         if (pWhere != null)     // list: where clause
759         {
760                 pArgList->Add(new (std::nothrow) String(*pWhere));
761                 argSize += pWhere->GetLength() * sizeof(wchar_t);
762                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
763         }
764         else
765         {
766                 pArgList->Add(new (std::nothrow) String(L"NULL"));
767         }
768         SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
769                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
770
771         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
772         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
773
774         reqId = static_cast< RequestId >(id);
775
776         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, updateMap: 0x%x, pWhere: 0x%x, req: %d",
777                                 dataId.GetPointer(), &updateMap, pWhere, reqId);
778
779         // fall through
780 CATCH:
781         pArgList->RemoveAll(true);
782         delete pArgList;
783         delete pMapEnum;
784
785         return r;
786 }
787
788 result
789 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
790 {
791         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
792                         "The DELETE query is not permitted by DataControl provider.");
793
794         int id = 0;
795         result r = E_SUCCESS;
796         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
797
798         ArrayList* pArgList = new ArrayList();
799         pArgList->Construct();
800
801         pArgList->Add(*(new String(dataId))); // list(0): data ID
802         long long argSize = dataId.GetLength() * sizeof(wchar_t);
803
804         if (pWhere != null)     // list(1): where clause
805         {
806                 pArgList->Add(*(new String(*pWhere)));
807                 argSize += pWhere->GetLength() * sizeof(wchar_t);
808                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
809         }
810         else
811         {
812                 pArgList->Add(*(new String(L"NULL")));
813         }
814         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
815                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
816
817         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
818         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
819
820         reqId = static_cast< RequestId >(id);
821
822         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pWhere: 0x%x, req: %d", dataId.GetPointer(), pWhere, reqId);
823
824         // fall through
825 CATCH:
826         pArgList->RemoveAll(true);
827         delete pArgList;
828
829         return r;
830 }
831
832 result
833 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
834 {
835         result r = E_SUCCESS;
836
837         if (__pPreviousListener != null)
838         {
839                 r =  __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
840                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
841                  __pPreviousListener = null;
842         }
843
844         if (pListener != null)
845         {
846                 r =  __pSqlDataControlEvent->AddListener(*pListener);
847                 if (IsFailed(r))
848                 {
849                         switch (r)
850                         {
851                         case E_OBJ_ALREADY_EXIST:
852                                 return E_SUCCESS;
853                         case E_INVALID_OPERATION:
854                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
855                                 return E_SYSTEM;
856                         default:
857                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
858                                 return r;
859                         }
860                 }
861         }
862
863         __pPreviousListener = pListener;
864
865         return E_SUCCESS;
866 }
867
868 // private
869 SqlDataControl*
870 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
871 {
872         unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
873         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
874
875         _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
876         pDcImpl->__appId = appId;
877         pDcImpl->__providerId = providerId;
878         unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
879         SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
880
881         pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
882
883         if (access == L"readonly")
884         {
885                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
886         }
887         else if (access == L"writeonly")
888         {
889                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
890         }
891         else if (access == L"readwrite")
892         {
893                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
894         }
895         else
896         {
897                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
898                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
899         }
900
901         return pDc.release();
902 }
903
904 }} // Tizen::App
905