d4ab5a42d52426b57dcf764cae61d43611345b94
[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* pAppId = null;
84         String* pRequestType = null;
85         int type = 0;
86         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
87         String* pProviderId = null;
88         String* pReqId = null;
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         result r = E_SUCCESS;
101
102         _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
103         SysTryReturnResult(NID_APP, pAppMgr, E_SYSTEM, "Failed to get instance.");
104
105         _ResultInfo* pResultInfo = pAppMgr->__resultManager.FindItem(static_cast< int >(reqId));
106         SysTryReturnResult(NID_APP, pResultInfo, E_OBJ_NOT_FOUND,
107                         "The data control request specified with the reqId (%ld) did not exist.", reqId);
108
109         const _AppArg& arg = pResultInfo->arg;
110
111         pList = arg.GetArgListN(0);
112         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
113
114         // key-based request
115         pAppId = dynamic_cast< String* >(pList->GetAt(0)); // request key[0]
116         SysTryCatch(NID_APP, pAppId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
117
118         pRequestType = dynamic_cast< String* >(pList->GetAt(1)); // request key[1]
119         SysTryCatch(NID_APP, pRequestType, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
120         Integer::Parse(*pRequestType, type);
121         requestType = static_cast< _DataControlRequestType >(type);
122
123         if (providerResult == true && apiType != requestType)
124         {
125                 if ((apiType == _DATACONTROL_REQUEST_TYPE_SQL_UPDATE /*UpdateDelete*/ && requestType == _DATACONTROL_REQUEST_TYPE_SQL_DELETE) ||
126                                 apiType == _DATACONTROL_REQUEST_TYPE_UNDEFINED /*MAP*/)
127                 {
128                         r = E_SUCCESS;
129                 }
130                 else
131                 {
132                         r = E_INVALID_ARG;
133                         SysLog(NID_APP, "[E_INVALID_ARG] This method cannot send the result set for the specified reqId.");
134                         goto CATCH;
135                 }
136         }
137
138         pReqId = dynamic_cast< String* >(pList->GetAt(2)); // request key[2]
139         SysTryCatch(NID_APP, pReqId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
140
141         pProviderId = dynamic_cast< String* >(pList->GetAt(3)); // request key[3]
142         SysTryCatch(NID_APP, pProviderId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
143
144         // Serializes the result
145         pResultArgList = new (std::nothrow) ArrayList();
146         SysTryCatch(NID_APP, pResultArgList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
147                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
148         pResultArgList->Construct();
149
150         pResult = new (std::nothrow) String();
151         SysTryCatch(NID_APP, pResult, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
152                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
153         pResult->Append(static_cast< int >(providerResult));
154         pResultArgList->Add(*pResult); // result list[0]
155
156         if (pErrorMsg == null)
157         {
158                 pErrorMessage = new (std::nothrow) String();
159         }
160         else
161         {
162                 pErrorMessage = new (std::nothrow) String(*pErrorMsg);
163         }
164         SysTryCatch(NID_APP, pErrorMessage, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
165                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
166         SysTryCatch(NID_APP, pErrorMessage->GetLength() <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
167                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", pErrorMessage->GetLength());
168         pResultArgList->Add(*pErrorMessage); // result list[1]
169
170         switch (requestType)
171         {
172         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
173         {
174                 if (pDbEnum)
175                 {
176                         std::unique_ptr<_DataControlResultSetImpl> pResultSet(new (std::nothrow) _DataControlResultSetImpl(reqId));
177                         SysTryCatch(NID_APP, pResultSet, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
178                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
179
180                         r = pResultSet->FillWith(pDbEnum);
181                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM,
182                                         "[E_SYSTEM] The method cannot proceed due to a severe system error.");
183
184                         String tempFilePath(_DATACONTROL_RESULTSET_DIR);
185                         tempFilePath.Append(*pAppId);
186                         tempFilePath.Append(*pReqId);
187                         pTempFilePath = new (std::nothrow) String(tempFilePath);
188                 }
189                 else
190                 {
191                         pTempFilePath = new (std::nothrow) String(L"NoResultSet");
192                 }
193                 SysTryCatch(NID_APP, pTempFilePath, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
194                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
195
196                 pResultArgList->Add(*pTempFilePath); // result list[2]
197                 break;
198         }
199         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
200                 pResultArgList->Add(*(new String(LongLong::ToString(insertRowId)))); // result list[2]
201                 break;
202
203         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
204                 // fall through
205         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
206                 break;
207
208         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
209         {
210                 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList == null), r = E_INVALID_ARG, E_INVALID_ARG,
211                                 "[E_INVALID_ARG] The specified pResultValueList should not be null if the request is GetValue query.");
212
213                 if (pResultValueList)
214                 {
215                         // list-based request
216                         pNo = dynamic_cast< String* >(pList->GetAt(6)); // request list[2]
217                         SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
218                         Integer::Parse(*pNo, pageNo);
219
220                         pCount = dynamic_cast< String* >(pList->GetAt(7)); // request list[3]
221                         SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
222                         Integer::Parse(*pCount, countPerPage);
223
224                         String resultCount;
225                         int num = pResultValueList->GetCount();
226                         int currentoffset = (pageNo - 1) * countPerPage;
227                         int remainingNum = num - currentoffset;
228                         remainingNum = (remainingNum > 0) ? remainingNum : 0; // round off to zero if negative num is found
229                         int addItemCount = (countPerPage > remainingNum) ? remainingNum : countPerPage;
230                         resultCount.Append(addItemCount);
231
232                         pResultCount = new (std::nothrow) String(resultCount);
233                         SysTryCatch(NID_APP, pResultCount, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
234                                         "[E_OUT_OF_MEMORY] The memory was insufficient.");
235                         pResultArgList->Add(*pResultCount); // result list[2]
236
237                         long long argSize = 0;
238                         for (int i = currentoffset; i < num; i++)
239                         {
240                                 String* pTempValue = dynamic_cast< String* >(pResultValueList->GetAt(i));
241                                 SysTryCatch(NID_APP, pTempValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
242                                                 "[E_INVALID_ARG] The specified pResultValueList parameter should be String class.");
243
244                                 pResultValue = new (std::nothrow) String(*pTempValue);
245                                 SysTryCatch(NID_APP, pResultValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
246                                                 "[E_OUT_OF_MEMORY] The memory was insufficient.");
247                                 pResultArgList->Add(*pResultValue); // list[3] ~
248                                 argSize += pResultValue->GetLength() * sizeof(wchar_t);
249                         }
250                         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
251                                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
252                 }
253                 break;
254         }
255         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
256                 // fall through
257         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
258                 // fall through
259         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
260                 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList != null), r = E_INVALID_ARG, E_INVALID_ARG,
261                                 "[E_INVALID_ARG] The specified pResultValueList should be null if the request is \
262                                 one of AddValue, SetValue, RemoveValue queries.");
263                 break;
264
265         default:
266                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
267                 break;
268         }
269
270         r = resultArg.ConstructResult(arg, pResultArgList);
271         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, r, "[%s] Propagating.", GetErrorMessage(r));
272
273         //resultArg.Print();
274         r = _Aul::SendResult(resultArg.GetBundle(), static_cast< appsvc_result_val >(0));
275         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to send result.", GetErrorMessage(r));
276
277         // Erases _AppArg after sending the result back to the caller
278         pAppMgr->__resultManager.RemoveItem(static_cast< int >(reqId));
279
280 CATCH:
281         if (pList)
282         {
283                 pList->RemoveAll(true);
284         }
285         delete pList;
286
287         if (pResultArgList)
288         {
289                 pResultArgList->RemoveAll(true);
290         }
291         delete pResultArgList;
292
293         return r;
294 }
295
296 result
297 _DataControlProviderManagerImpl::SendSqlDataControlSelectResult(RequestId reqId, IDbEnumerator* pDbEnum)
298 {
299         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_QUERY,
300                         pDbEnum, null, -1, true, null);
301 }
302
303 result
304 _DataControlProviderManagerImpl::SendSqlDataControlInsertResult(RequestId reqId, long long insertRowId)
305 {
306         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_INSERT,
307                         null, null, insertRowId, true, null);
308 }
309
310 result
311 _DataControlProviderManagerImpl::SendSqlDataControlUpdateDeleteResult(RequestId reqId)
312 {
313         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_UPDATE,
314                         null, null, -1, true, null);
315 }
316
317 result
318 _DataControlProviderManagerImpl::SendMapDataControlResult(RequestId reqId, IList* pResultValueList)
319 {
320         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
321                         null, pResultValueList, -1, true, null);
322 }
323
324 result
325 _DataControlProviderManagerImpl::SendDataControlError(RequestId reqId, const String& errorMsg)
326 {
327         return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
328                         null, null, -1, false, &errorMsg);
329 }
330
331 DataControlProviderManager*
332 _DataControlProviderManagerImpl::GetInstance(void)
333 {
334         static DataControlProviderManager* pDcMgr = null;
335
336         if (pDcMgr == null)
337         {
338                 pDcMgr = new (std::nothrow) DataControlProviderManager();
339                 SysTryReturn(NID_APP, pDcMgr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
340         }
341
342         return pDcMgr;
343 }
344
345 // private
346 _DataControlProviderManagerImpl::_DataControlProviderManagerImpl(void)
347 {
348 }
349
350 _DataControlProviderManagerImpl::~_DataControlProviderManagerImpl(void)
351 {
352 }
353
354 } } // Tizen::App
355