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