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