Remove privacy string
[platform/framework/native/appfw.git] / src / app / FApp_DataControlProviderManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FApp_DataControlProviderManagerImpl.cpp
20  * @brief       This is the implementation for the %_DataControlProviderManagerImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseDataType.h>
26 #include <FBaseInteger.h>
27 #include <FBaseString.h>
28 #include <FBaseLongLong.h>
29 #include <FBaseColArrayList.h>
30 #include <FBaseColIList.h>
31 #include <FBaseErrors.h>
32 #include <FAppAppManager.h>
33 #include <FAppSqlDataControl.h>
34 #include <FAppMapDataControl.h>
35 #include <FAppDataControlProviderManager.h>
36
37 #include <FBaseSysLog.h>
38 #include <FIo_DataControlResultSetImpl.h>
39
40 #include "FApp_Aul.h"
41 #include "FApp_AppArg.h"
42 #include "FApp_AppInfo.h"
43 #include "FApp_AppImpl.h"
44 #include "FApp_AppControlManager.h"
45 #include "FApp_SqlDataControlImpl.h"
46 #include "FApp_MapDataControlImpl.h"
47 #include "FApp_DataControlProviderManagerImpl.h"
48
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Io;
52
53 extern const char* _DATACONTROL_RESULTSET_DIR;
54
55 namespace Tizen { namespace App
56 {
57
58 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
59
60 result
61 _DataControlProviderManagerImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
62 {
63         _AppImpl* pAppImpl = _AppImpl::GetInstance();
64         SysTryReturn(NID_APP, pAppImpl, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting _AppImpl instance failed.");
65
66         return pAppImpl->SetSqlDataControlProviderEventListener(pListener);
67 }
68
69 result
70 _DataControlProviderManagerImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
71 {
72         _AppImpl* pAppImpl = _AppImpl::GetInstance();
73         SysTryReturn(NID_APP, pAppImpl, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting _AppImpl instance failed.");
74
75         return pAppImpl->SetMapDataControlProviderEventListener(pListener);
76 }
77
78 result
79 _DataControlProviderManagerImpl::SendDataControlResult(RequestId reqId, _DataControlRequestType apiType,
80                 IDbEnumerator* pDbEnum, IList* pResultValueList, long long insertRowId, bool providerResult, const String* pErrorMsg)
81 {
82         ArrayList* pList = null;
83         String appId;
84         String reqType;
85         int type = 0;
86         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
87         String providerId;
88         String callerReqId;
89         ArrayList* pResultArgList = null;
90         String* pResult = null;
91         String* pErrorMessage = null;
92         String* pTempFilePath = null;
93         String* pNo = null;
94         int pageNo = 0;
95         String* pCount = null;
96         int countPerPage = 0;
97         String* pResultCount = null;
98         String* pResultValue = null;
99         _AppArg resultArg;
100         String* pDataId = null;
101         result r = E_SUCCESS;
102
103         _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
104         SysTryReturnResult(NID_APP, pAppMgr, E_SYSTEM, "Failed to get instance.");
105
106         _ResultInfo* pResultInfo = pAppMgr->__resultManager.FindItem(static_cast< int >(reqId)); // provider reqId
107         SysTryReturnResult(NID_APP, pResultInfo, E_OBJ_NOT_FOUND,
108                         "The data control request specified with the req (%ld) did not exist.", reqId);
109
110         const _AppArg& arg = pResultInfo->arg; // request info
111
112         // key-based request
113         reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
114         Integer::Parse(reqType, type);
115         requestType = static_cast< _DataControlRequestType >(type);
116         if (providerResult == true && apiType != requestType)
117         {
118                 if ((apiType == _DATACONTROL_REQUEST_TYPE_SQL_UPDATE /*UpdateDelete*/ && requestType == _DATACONTROL_REQUEST_TYPE_SQL_DELETE) ||
119                                 apiType == _DATACONTROL_REQUEST_TYPE_UNDEFINED /*MAP*/)
120                 {
121                         r = E_SUCCESS;
122                 }
123                 else
124                 {
125                         r = E_INVALID_ARG;
126                         SysLog(NID_APP, "[E_INVALID_ARG] This method cannot send the result set for the specified reqId.");
127                         goto CATCH;
128                 }
129         }
130
131         appId = arg.GetCallerAppId();
132         callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
133         providerId = arg.GetValue(OSP_K_DATACONTROL_PROVIDER);
134
135         // list-based request
136         pList = _AppArg::GetListN(arg.GetBundle(), OSP_K_ARG);
137         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
138
139         pDataId = dynamic_cast< String* >(pList->GetAt(0)); // request list[0]
140         SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
141
142         SysLog(NID_APP, "[DC_PROV_SEND] > app: %ls, requestType: %d, callerReq: %ls, provider: %ls, data: %ls",
143                         appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(), pDataId->GetPointer());
144
145         // Serializes result
146         pResultArgList = new (std::nothrow) ArrayList();
147         SysTryCatch(NID_APP, pResultArgList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
148                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
149         pResultArgList->Construct();
150
151         pResult = new (std::nothrow) String();
152         SysTryCatch(NID_APP, pResult, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
153                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
154         pResult->Append(static_cast< int >(providerResult));
155         pResultArgList->Add(*pResult); // result list[0]
156
157         if (pErrorMsg == null)
158         {
159                 pErrorMessage = new (std::nothrow) String();
160         }
161         else
162         {
163                 pErrorMessage = new (std::nothrow) String(*pErrorMsg);
164         }
165         SysTryCatch(NID_APP, pErrorMessage, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
166                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
167         SysTryCatch(NID_APP, pErrorMessage->GetLength() <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
168                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", pErrorMessage->GetLength());
169         pResultArgList->Add(*pErrorMessage); // result list[1]
170
171         switch (requestType)
172         {
173         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
174         {
175                 if (pDbEnum)
176                 {
177                         std::unique_ptr<_DataControlResultSetImpl> pResultSet(new (std::nothrow) _DataControlResultSetImpl(reqId));
178                         SysTryCatch(NID_APP, pResultSet, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
179                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
180
181                         r = pResultSet->FillWith(pDbEnum);
182                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM,
183                                         "[E_SYSTEM] The method cannot proceed due to a severe system error.");
184
185                         String tempFilePath(_DATACONTROL_RESULTSET_DIR);
186                         tempFilePath.Append(appId);
187                         tempFilePath.Append(callerReqId);
188                         pTempFilePath = new (std::nothrow) String(tempFilePath);
189                 }
190                 else
191                 {
192                         pTempFilePath = new (std::nothrow) String(L"NoResultSet");
193                 }
194                 SysTryCatch(NID_APP, pTempFilePath, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
195                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
196
197                 pResultArgList->Add(*pTempFilePath); // result list[2]
198                 break;
199         }
200         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
201                 pResultArgList->Add(*(new String(LongLong::ToString(insertRowId)))); // result list[2]
202                 break;
203
204         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
205                 // fall through
206         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
207                 break;
208
209         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
210         {
211                 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList == null), r = E_INVALID_ARG, E_INVALID_ARG,
212                                 "[E_INVALID_ARG] The specified pResultValueList should not be null if the request is GetValue query.");
213
214                 if (pResultValueList)
215                 {
216                         // list-based request
217                         pNo = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]
218                         SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
219                         Integer::Parse(*pNo, pageNo);
220
221                         pCount = dynamic_cast< String* >(pList->GetAt(3)); // request list[3]
222                         SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
223                         Integer::Parse(*pCount, countPerPage);
224                         SysLog(NID_APP, "[DC_PROV_SEND] pageNo: %d, countPerPage: %d", pageNo, countPerPage);
225
226                         String resultCount;
227                         int num = pResultValueList->GetCount();
228                         int currentoffset = (pageNo - 1) * countPerPage;
229                         int remainingNum = num - currentoffset;
230                         remainingNum = (remainingNum > 0) ? remainingNum : 0; // round off to zero if negative num is found
231                         int addItemCount = (countPerPage > remainingNum) ? remainingNum : countPerPage;
232                         resultCount.Append(addItemCount);
233
234                         pResultCount = new (std::nothrow) String(resultCount);
235                         SysTryCatch(NID_APP, pResultCount, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
236                                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
237                         pResultArgList->Add(*pResultCount); // result list[2]
238
239                         long long argSize = 0;
240                         for (int i = currentoffset; i < num; i++)
241                         {
242                                 String* pTempValue = dynamic_cast< String* >(pResultValueList->GetAt(i));
243                                 SysTryCatch(NID_APP, pTempValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
244                                                 "[E_INVALID_ARG] The specified pResultValueList parameter should be String class.");
245
246                                 pResultValue = new (std::nothrow) String(*pTempValue);
247                                 SysTryCatch(NID_APP, pResultValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
248                                                 "[E_OUT_OF_MEMORY] The memory was insufficient.");
249                                 pResultArgList->Add(*pResultValue); // list[3] ~
250                                 argSize += pResultValue->GetLength() * sizeof(wchar_t);
251                         }
252                         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
253                                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
254                 }
255                 break;
256         }
257         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
258                 // fall through
259         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
260                 // fall through
261         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
262                 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList != null), r = E_INVALID_ARG, E_INVALID_ARG,
263                                 "[E_INVALID_ARG] The specified pResultValueList should be null if the request is \
264                                 one of AddValue, SetValue, RemoveValue queries.");
265                 break;
266
267         default:
268                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
269                 break;
270         }
271
272         r = resultArg.ConstructResult(arg, pResultArgList);
273         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, r, "[%s] Propagating.", GetErrorMessage(r));
274         resultArg.UpdateKeyValue(OSP_K_REQUEST_ID, callerReqId);
275         resultArg.UpdateKeyValue(OSP_K_DATACONTROL_REQUEST_TYPE, reqType);
276         resultArg.UpdateKeyValue(OSP_K_DATACONTROL_PROVIDER, providerId);
277         resultArg.UpdateKeyValue(OSP_K_DATACONTROL_DATA, *pDataId);
278
279         //resultArg.Print();
280         r = _Aul::SendResult(resultArg.GetBundle(), static_cast< appsvc_result_val >(0));
281         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to send result.", GetErrorMessage(r));
282
283         // Erases _AppArg after sending the result back to the caller
284         pAppMgr->__resultManager.RemoveItem(static_cast< int >(reqId));
285
286 CATCH:
287         if (pList)
288         {
289                 pList->RemoveAll(true);
290         }
291         delete pList;
292
293         if (pResultArgList)
294         {
295                 pResultArgList->RemoveAll(true);
296         }
297         delete pResultArgList;
298
299         return r;
300 }
301
302 result
303 _DataControlProviderManagerImpl::SendSqlDataControlSelectResult(RequestId reqId, IDbEnumerator* pDbEnum)
304 {
305         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_QUERY,
306                         pDbEnum, null, -1, true, null);
307 }
308
309 result
310 _DataControlProviderManagerImpl::SendSqlDataControlInsertResult(RequestId reqId, long long insertRowId)
311 {
312         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_INSERT,
313                         null, null, insertRowId, true, null);
314 }
315
316 result
317 _DataControlProviderManagerImpl::SendSqlDataControlUpdateDeleteResult(RequestId reqId)
318 {
319         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_UPDATE,
320                         null, null, -1, true, null);
321 }
322
323 result
324 _DataControlProviderManagerImpl::SendMapDataControlResult(RequestId reqId, IList* pResultValueList)
325 {
326         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
327                         null, pResultValueList, -1, true, null);
328 }
329
330 result
331 _DataControlProviderManagerImpl::SendDataControlError(RequestId reqId, const String& errorMsg)
332 {
333         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
334                         null, null, -1, false, &errorMsg);
335 }
336
337 DataControlProviderManager*
338 _DataControlProviderManagerImpl::GetInstance(void)
339 {
340         static DataControlProviderManager* pDcMgr = null;
341
342         if (pDcMgr == null)
343         {
344                 pDcMgr = new (std::nothrow) DataControlProviderManager();
345                 SysTryReturn(NID_APP, pDcMgr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
346         }
347
348         return pDcMgr;
349 }
350
351 // private
352 _DataControlProviderManagerImpl::_DataControlProviderManagerImpl(void)
353 {
354 }
355
356 _DataControlProviderManagerImpl::~_DataControlProviderManagerImpl(void)
357 {
358 }
359
360 } } // Tizen::App
361