Modify Scanner class
[platform/framework/native/appfw.git] / inc / FAppISqlDataControlProviderEventListener.h
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    FAppISqlDataControlProviderEventListener.h
20  * @brief   This is the header file for the %ISqlDataControlProviderEventListener interface.
21  *
22  * This header file contains the declarations of the %ISqlDataControlProviderEventListener interface.
23  */
24
25 #ifndef _FAPP_ISQL_DATACONTROL_REQUESTEVENT_LISTENER_H_
26 #define _FAPP_ISQL_DATACONTROL_REQUESTEVENT_LISTENER_H_
27
28 #include <FBaseDataType.h>
29 #include <FBaseString.h>
30 #include <FBaseColIMapT.h>
31 #include <FBaseRtIEventListener.h>
32
33 namespace Tizen { namespace App
34 {
35
36 /**
37  * @interface   ISqlDataControlProviderEventListener
38  * @brief               This interface defines a listener for dealing with SQL-type data control request.
39  *
40  * @since       2.0
41  *
42  * The %ISqlDataControlProviderEventListener interface defines a listener for dealing with SQL-type data control request.
43  *
44  * The following example demonstrates how to use the %ISqlDataControlProviderEventListener interface.
45  *
46  * @code
47 #include <FBase.h>
48 #include <FApp.h>
49 #include <FIo.h>
50
51 class MyServiceApp
52     : public Tizen::App::ServiceApp
53         : public Tizen::App::ISqlDataControlProviderEventListener
54 {
55 public:
56         MyServiceApp(void);
57         virtual ~MyServiceApp(void);
58
59         virtual bool OnAppInitializing(Tizen::App::AppRegistry& appRegistry);
60         virtual bool OnAppTerminating(Tizen::App::AppRegistry& appRegistry, bool forcedTermination = false);
61
62         virtual void OnSqlDataControlSelectRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
63                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IList* pColumnList,
64                         const Tizen::Base::String* pWhere, const Tizen::Base::String* pOrder);
65         virtual void OnSqlDataControlInsertRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
66                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& insertMap);
67         virtual void OnSqlDataControlUpdateRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
68                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& updateMap,
69                         const Tizen::Base::String* pWhere);
70         virtual void OnSqlDataControlDeleteRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
71                         const Tizen::Base::String& dataId, const Tizen::Base::String* pWhere);
72 };
73
74 bool
75 MyServiceApp::OnAppInitializing(Tizen::App::AppRegistry& appRegistry)
76 {
77     result r = E_SUCCESS;
78
79         DataControlProviderManager* pDcMgr = DataControlProviderManager::GetInstance();
80         if (pDcMgr == null)
81         {
82                 AppLog("Failed to get the instance of data control provider manager.");
83                 return false;
84         }
85
86         r = pDcMgr->SetSqlDataControlProviderEventListener(this);
87         if (IsFailed(r))
88         {
89                 AppLog("Failed to set the data control provider listener.");
90                 return false;
91         }
92
93         return true;
94 }
95
96 void
97 MyServiceApp::OnSqlDataControlSelectRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
98                 const Tizen::Base::String& dataId, const Tizen::Base::Collection::IList* pColumnList,
99                 const Tizen::Base::String* pWhere, const Tizen::Base::String* pOrder)
100 {
101         Database* pDb = null;
102         DbEnumerator* pDbEnum = null;
103         String errorMsg;
104         result r = E_SUCCESS;
105
106         if (providerId == L"http://tizen.org/datacontrol/provider/example")
107         {
108                 if (dataId == L"test")
109                 {
110                         String sql = SqlStatementBuilder::CreateSelectStatement(dataId, pColumnList, pWhere, pOrder,
111                                         null, null, null);
112
113                         String dbPath(App::GetInstance()->GetAppDataPath() + L"test.db");
114
115                         pDb = new Database();
116                         if (pDb == null)
117                         {
118                                 errorMsg.Append(L"The memory is insufficient.");
119                                 goto CATCH;
120                         }
121
122                         r = pDb->Construct(dbPath, "r");
123                         if (IsFailed(r))
124                         {
125                                 errorMsg.Append(L"The data control provider failed to open the database file.");
126                                 goto CATCH;
127                         }
128
129                         pDbEnum = pDb->QueryN(sql);
130                         r = GetLastResult();
131                         if (IsFailed(r))
132                         {
133                                 errorMsg.Append(L"The data control provider failed to execute SQL statement.");
134                                 goto CATCH;
135                         }
136                 }
137                 else
138                 {
139                         errorMsg.Append(L"The data ID is invalid.");
140                 }
141         }
142         else
143         {
144                 errorMsg.Append(L"The provider ID is invalid.");
145         }
146
147         r = DataControlProviderManager::GetInstance()->SendSqlDataControlSelectResult(reqId, pDbEnum);
148         if (IsFailed(r))
149         {
150                 AppLog("The data control provider failed to send the result.");
151         }
152
153         delete pDbEnum;
154         delete pDb;
155         return;
156
157 CATCH:
158         r = DataControlProviderManager::Getinstance()->SendDataControlError(reqId, errorMsg);
159         if (IsFailed(r))
160         {
161                 AppLog("The data control provider failed to send the result.");
162         }
163
164         delete pDbEnum;
165         delete pDb;
166         return;
167 }
168
169 void
170 MyServiceApp::OnSqlDataControlInsertRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
171                 const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& insertMap)
172 {
173         Database* pDb = null;
174         String errorMsg;
175         long long insertRowId = -1;
176         result r = E_SUCCESS;
177
178         if (providerId == L"http://tizen.org/datacontrol/provider/example")
179         {
180                 if (dataId == L"test")
181                 {
182                         String sql = SqlStatementBuilder::CreateInsertStatement(dataId, insertMap);
183
184                         String dbPath(App::GetInstance()->GetAppDataPath() + L"test.db");
185
186                         pDb = new Database();
187                         if (pDb == null)
188                         {
189                                 errorMsg.Append(L"The memory is insufficient.");
190                                 goto CATCH;
191                         }
192
193                         r = pDb->Construct(dbPath, "a+");
194                         if (IsFailed(r))
195                         {
196                                 errorMsg.Append(L"The data control provider failed to open the database file.");
197                                 goto CATCH;
198                         }
199
200                         r = pDb->ExcuteSql(sql, true);
201                         if (IsFailed(r))
202                         {
203                                 errorMsg.Append(L"The data control provider failed to execute SQL statement.");
204                                 goto CATCH;
205                         }
206
207                         insertRowId = pDb->GetLastInsertRowId();
208                 }
209                 else
210                 {
211                         errorMsg.Append(L"The data ID is invalid.");
212                 }
213         }
214         else
215         {
216                 errorMsg.Append(L"The provider ID is invalid.");
217         }
218
219         r = DataControlProviderManager::GetInstance()->SendSqlDataControlInsertResult(reqId, insertRowId);
220         if (IsFailed(r))
221         {
222                 AppLog("The data control provider failed to send the result.");
223         }
224
225         delete pDb;
226         return;
227
228 CATCH:
229         r = DataControlProviderManager::Getinstance()->SendDataControlError(reqId, errorMsg);
230         if (IsFailed(r))
231         {
232                 AppLog("The data control provider failed to send the result.");
233         }
234
235         delete pDb;
236         return;
237 }
238 * @endcode
239 */
240 class _OSP_EXPORT_ ISqlDataControlProviderEventListener
241         : virtual public Tizen::Base::Runtime::IEventListener
242 {
243
244 public:
245         /**
246          * This polymorphic destructor should be overridden if required.
247          * This way, the destructors of the derived classes are called when the destructor of this interface is called.
248          *
249          * @since       2.0
250          */
251         virtual ~ISqlDataControlProviderEventListener(void) {}
252
253         /**
254         * Called when a select request is received from an application using SQL-type data control. @n
255         * The provider must implement this listener for providing its own data.
256         *
257         * @since        2.0
258         *
259         * @param[in]    reqId                   The request ID
260         * @param[in]    providerId              The provider ID for identifying the data control
261         * @param[in]    dataId                  A string for identifying a specific table to query from @n
262         *                                                               The string consists of one or more components, separated by a slash('/').
263         *                                                               If the specified @c pColumnList is @c null, all columns are selected.
264         * @param[in]    pColumnList             A list of column names to query @n
265         *                                                               The type of objects contained in the specified @c pColumnList is
266         *                                                               Tizen::Base::String class.
267         * @param[in]    pWhere                  A filter to select desired rows to query @n
268         *                                                               It is an SQL 'WHERE' clause excluding the 'WHERE' itself such as
269         *                                                               column1 = 'stringValue' AND column2 = numericValue.
270         * @param[in]    pOrder                  The sorting order of rows to query @n
271         *                                                               It is an SQL 'ORDER BY' clause excluding the 'ORDER BY' itself.
272         * @remarks              For replying to the data control request, use DataControlProviderManager::SendSqlDataControlSelectResult()
273         *                               or DataControlProviderManager::SendDataControlError().
274         */
275         virtual void OnSqlDataControlSelectRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
276                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IList* pColumnList,
277                         const Tizen::Base::String* pWhere, const Tizen::Base::String* pOrder) = 0;
278
279         /**
280         * Called when an insert request is received from an application using SQL-type data control. @n
281         * The provider must implement this listener for providing its own data.
282         *
283         * @since        2.0
284         *
285         * @param[in]    reqId                   The request ID
286         * @param[in]    providerId              The provider ID for identifying the data control
287         * @param[in]    dataId                  A string for identifying a specific table to insert into @n
288         *                                                               The string consists of one or more components, separated by a slash('/').
289         * @param[in]    insertMap               The column-value pairs to insert @n
290         *                                                               The type of objects contained in the specified @c insertMap is
291         *                                                               Tizen::Base::String class.
292         * @remarks              For replying to the data control request, use DataControlProviderManager::SendSqlDataControlInsertResult()
293         *                               or DataControlProviderManager::SendDataControlError().
294         */
295         virtual void OnSqlDataControlInsertRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
296                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& insertMap) = 0;
297
298         /**
299         * Called when an update request is received from an application using SQL-type data control. @n
300         * The provider must implement this listener for providing its own data.
301         *
302         * @since        2.0
303         *
304         * @param[in]    reqId                   The request ID
305         * @param[in]    providerId              The provider ID for identifying the data control
306         * @param[in]    dataId                  A string for identifying a specific table to update @n
307         *                                                               The string consists of one or more components, separated by a slash('/').
308         * @param[in]    updateMap               The column-value pairs to update @n
309         *                                                               The type of objects contained in the specified @c updateMap is
310         *                                                               Tizen::Base::String class.
311         * @param[in]    pWhere                  A filter to select desired rows to update @n
312         *                                                               It is an SQL 'WHERE' clause excluding the 'WHERE' itself such as
313         *                                                               column1 = 'stringValue' AND column2 = numericValue.
314         * @remarks              For replying to the data control request, use DataControlProviderManager::SendSqlDataControlUpdateDeleteResult()
315         *                               or DataControlProviderManager::SendDataControlError().
316         */
317         virtual void OnSqlDataControlUpdateRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
318                         const Tizen::Base::String& dataId, const Tizen::Base::Collection::IMap& updateMap,
319                         const Tizen::Base::String* pWhere) = 0;
320
321         /**
322         * Called when a delete request is received from an application using SQL-type data control. @n
323         * The provider must implement this listener for providing its own data.
324         *
325         * @since        2.0
326         *
327         * @param[in]    reqId                   The request ID
328         * @param[in]    providerId              The provider ID for identifying the data control
329         * @param[in]    dataId                  A string for identifying a specific table to delete from @n
330         *                                                               The string consists of one or more components, separated by a slash('/').
331         * @param[in]    pWhere                  A filter to select desired rows to delete @n
332         *                                                               It is an SQL 'WHERE' clause excluding the 'WHERE' itself such as
333         *                                                               column1 = 'stringValue' AND column2 = numericValue.
334         * @remarks              For replying to the data control request, use DataControlProviderManager::SendSqlDataControlUpdateDeleteResult()
335         *                               or DataControlProviderManager::SendDataControlError().
336         */
337         virtual void OnSqlDataControlDeleteRequestReceived(RequestId reqId, const Tizen::Base::String& providerId,
338                         const Tizen::Base::String& dataId, const Tizen::Base::String* pWhere) = 0;
339
340 protected:
341         //
342         // This method is for internal use only.
343         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
344         //
345         // This method is reserved and may change its name at any time without prior notice.
346         //
347         // @since       2.0
348         //
349         virtual void ISqlDataControlProviderEventListener_Reserved1(void) {}
350
351         //
352         // This method is for internal use only.
353         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
354         //
355         // This method is reserved and may change its name at any time without prior notice.
356         //
357         // @since       2.0
358         //
359         virtual void ISqlDataControlProviderEventListener_Reserved2(void) {}
360
361         //
362         // This method is for internal use only.
363         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
364         //
365         // This method is reserved and may change its name at any time without prior notice.
366         //
367         // @since       2.0
368         //
369         virtual void ISqlDataControlProviderEventListener_Reserved3(void) {}
370
371         //
372         // This method is for internal use only.
373         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
374         //
375         // This method is reserved and may change its name at any time without prior notice.
376         //
377         // @since       2.0
378         //
379         virtual void ISqlDataControlProviderEventListener_Reserved4(void) {}
380
381         //
382         // This method is for internal use only.
383         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
384         //
385         // This method is reserved and may change its name at any time without prior notice.
386         //
387         // @since       2.0
388         //
389         virtual void ISqlDataControlProviderEventListener_Reserved5(void) {}
390
391 }; // ISqlDataControlProviderEventListener
392
393 }} // Tizen::App
394
395 #endif // _FAPP_ISQL_DATACONTROL_REQUESTEVENT_LISTENER_H_
396