Fixed jira issues
[apps/osp/MusicPlayer.git] / src / MpPlaylistDB.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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                MpPlaylistDB.cpp
19  * @brief               This is the implementation file for PlaylistDB class.
20  */
21
22 #include <FApp.h>
23 #include "MpCommonUtil.h"
24 #include "MpPlaylistDB.h"
25 #include "MpTypes.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Io;
31
32 PlaylistDB::PlaylistDB(void)
33 {
34         AppLogDebug("ENTER");
35         AppLogDebug("EXIT");
36 }
37
38 PlaylistDB::~PlaylistDB(void)
39 {
40         AppLogDebug("ENTER");
41         AppLogDebug("EXIT");
42 }
43
44 result
45 PlaylistDB::CreatePlaylistDatabase(void)
46 {
47         AppLogDebug("ENTER");
48         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
49         Tizen::Base::String sql;
50
51         result r = E_SUCCESS;
52
53         if (File::IsFileExist(dbName))
54         {
55                 return r;
56         }
57         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
58         TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.[%s]", GetErrorMessage(r));
59         r = pDatabase->Construct(dbName, true);
60         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.[%s]", GetErrorMessage(r));
61         sql.Append(L"CREATE TABLE IF NOT EXISTS playlisttable ( primaryKey INTEGER PRIMARY KEY AUTOINCREMENT, contentId TEXT, playCount INTEGER, playTime TEXT )");
62         r = pDatabase->BeginTransaction();
63         TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.[%s]", GetErrorMessage(r));
64         r = pDatabase->ExecuteSql(sql, true);
65         TryCatch(r == E_SUCCESS, r = E_FAILURE, "ExecuteSql failed.[%s]", GetErrorMessage(r));
66         r = pDatabase->CommitTransaction();
67         TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.[%s]", GetErrorMessage(r));
68         delete pDatabase;
69         pDatabase = null;
70
71         AppLogDebug("EXIT");
72         return E_SUCCESS;
73
74 CATCH:
75         if (pDatabase)
76         {
77                 delete pDatabase;
78                 pDatabase = null;
79         }
80         if (Database::Exists(dbName))
81         {
82                 Database::Delete(dbName);
83         }
84         return r;
85 }
86
87 result
88 PlaylistDB::Insert(Tizen::Base::String& contentId, int playCount, Tizen::Base::String& playTime)
89 {
90         AppLogDebug("ENTER");
91         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
92         result r = E_SUCCESS;
93
94         DbStatement* pStmt = null;
95         Tizen::Base::String statement = null;
96         DbEnumerator* pEnum = null;
97
98         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
99         TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
100         r = pDatabase->Construct(dbName, false);
101         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
102         r = pDatabase->BeginTransaction();
103         TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
104
105         statement.Append(L"INSERT INTO playlisttable (contentId, playCount, playTime) VALUES (?, ?, ?)");
106         pStmt = pDatabase->CreateStatementN(statement);
107         TryCatch(pStmt != null, r = E_FAILURE,"CreateStatementN failed with %s", GetErrorMessage(GetLastResult()));
108         r = pStmt->BindString(0, contentId);
109         TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
110         r = pStmt->BindInt(1, playCount);
111         TryCatch(r == E_SUCCESS, r = E_FAILURE, "playCount BindInt failed.%n[%ls]", GetErrorMessage(r));
112         r = pStmt->BindString(2, playTime);
113         TryCatch(r == E_SUCCESS, r = E_FAILURE, "playTime BindString failed.%n[%ls]", GetErrorMessage(r));
114         pEnum = pDatabase->ExecuteStatementN(*pStmt);
115 //      AppAssert(!pEnum);
116         r = pDatabase->CommitTransaction();
117         TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
118
119         delete pEnum;
120         pEnum = null;
121
122         delete pStmt;
123         pStmt = null;
124
125         delete pDatabase;
126         pDatabase = null;
127         AppLogDebug("EXIT");
128         return r;
129
130 CATCH:
131         if (pEnum != null)
132         {
133                 delete pEnum;
134                 pEnum = null;
135         }
136         if (pStmt != null)
137         {
138                 delete pStmt;
139                 pStmt = null;
140         }
141         if (pDatabase)
142         {
143                 delete pDatabase;
144                 pDatabase = null;
145         }
146         if (Database::Exists(dbName))
147         {
148                 Database::Delete(dbName);
149         }
150         return r;
151 }
152
153 result
154 PlaylistDB::Update(Tizen::Base::String& contentId, int playCount, Tizen::Base::String& playTime)
155 {
156         AppLogDebug("ENTER");
157         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
158
159         result r = E_SUCCESS;
160
161         DbStatement* pStmt = null;
162         Tizen::Base::String statement = null;
163         DbEnumerator* pEnum = null;
164
165         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
166         TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
167         r = pDatabase->Construct(dbName, false);
168         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
169         r = pDatabase->BeginTransaction();
170         TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
171
172         statement.Append(L"UPDATE playlisttable SET playCount = ?, playTime = ? WHERE contentId = ?");
173         pStmt = pDatabase->CreateStatementN(statement);
174
175         r = pStmt->BindInt(0, playCount);
176         TryCatch(r == E_SUCCESS, r = E_FAILURE, "playCount BindInt failed.%n[%ls]", GetErrorMessage(r));
177         r = pStmt->BindString(1, playTime);
178         TryCatch(r == E_SUCCESS, r = E_FAILURE, "playTime BindString failed.%n[%ls]", GetErrorMessage(r));
179         r = pStmt->BindString(2, contentId);
180         TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
181
182         pEnum = pDatabase->ExecuteStatementN(*pStmt);
183 //      AppAssert(!pEnum);
184
185         r = pDatabase->CommitTransaction();
186         TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
187
188         delete pEnum;
189         pEnum = null;
190
191         delete pStmt;
192         pStmt = null;
193
194         delete pDatabase;
195         pDatabase = null;
196         AppLogDebug("EXIT");
197         return r;
198
199 CATCH:
200         if (pEnum != null)
201         {
202                 delete pEnum;
203                 pEnum = null;
204         }
205         if (pStmt != null)
206         {
207                 delete pStmt;
208                 pStmt = null;
209         }
210         if (pDatabase)
211         {
212                 delete pDatabase;
213                 pDatabase = null;
214         }
215         if (Database::Exists(dbName))
216         {
217                 Database::Delete(dbName);
218         }
219         return r;
220 }
221
222 result
223 PlaylistDB::Delete(Tizen::Base::String& contentId)
224 {
225         AppLogDebug("ENTER");
226         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
227         result r = E_SUCCESS;
228
229         DbStatement* pStmt = null;
230         Tizen::Base::String statement = null;
231         DbEnumerator* pEnum = null;
232
233         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
234         TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
235         r = pDatabase->Construct(dbName, false);
236         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.%n[%ls]", GetErrorMessage(r));
237         r = pDatabase->BeginTransaction();
238         TryCatch(r == E_SUCCESS, r = E_FAILURE, "BeginTransaction failed.%n[%ls]", GetErrorMessage(r));
239
240         statement.Append(L"DELETE FROM playlisttable where contentId = ?");
241         pStmt = pDatabase->CreateStatementN(statement);
242         r = pStmt->BindString(0, contentId);
243         TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.%n[%ls]", GetErrorMessage(r));
244         pEnum = pDatabase->ExecuteStatementN(*pStmt);
245 //      AppAssert(!pEnum);
246         r = pDatabase->CommitTransaction();
247         TryCatch(r == E_SUCCESS, r = E_FAILURE, "CommitTransaction failed.%n[%ls]", GetErrorMessage(r));
248
249         delete pEnum;
250         pEnum = null;
251
252         delete pStmt;
253         pStmt = null;
254
255         delete pDatabase;
256         pDatabase = null;
257         AppLogDebug("EXIT");
258         return r;
259
260 CATCH:
261         if (pEnum != null)
262         {
263                 delete pEnum;
264                 pEnum = null;
265         }
266         if (pStmt != null)
267         {
268                 delete pStmt;
269                 pStmt = null;
270         }
271         if (pDatabase)
272         {
273                 delete pDatabase;
274                 pDatabase = null;
275         }
276         if (Database::Exists(dbName))
277         {
278                 Database::Delete(dbName);
279         }
280         return r;
281 }
282
283 Tizen::Io::DbEnumerator*
284 PlaylistDB::SearchN(int index)
285 {
286         AppLogDebug("ENTER");
287         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
288
289         result r = E_SUCCESS;
290
291         Tizen::Base::String statement;
292         DbEnumerator* pEnum = null;
293
294         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
295         r = pDatabase->Construct(dbName, false);
296
297         TryReturn(r == E_SUCCESS,null,"pDatabase->Construct failed with error %s",GetErrorMessage(r));
298
299         if (index == MOST_PLAYED_CONTENT)
300         {
301                 statement.Append(L"SELECT contentId FROM playlisttable WHERE playCount > 0 ORDER BY playCount DESC");
302         }
303         else if (index == RECENTLY_PLAYED_CONTENT)
304         {
305                 statement.Append(L"SELECT contentId FROM playlisttable WHERE playTime IS NOT '' ORDER BY playTime DESC");
306         }
307         else
308         {
309                 AppLogDebug("EXIT");
310                 return null;
311         }
312
313         pEnum = pDatabase->QueryN(statement);
314
315         AppLogDebug("EXIT");
316         return pEnum;
317 }
318
319 result
320 PlaylistDB::Read(Tizen::Base::String& contentId)
321 {
322         AppLogDebug("ENTER");
323         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
324
325         result r = E_KEY_NOT_FOUND;
326
327         DbStatement* pStmt = null;
328         Tizen::Base::String statement = null;
329         DbEnumerator* pEnum = null;
330 //      int playCount = 0;
331
332         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
333         //      TryCatch(pDatabase != null, r = E_FAILURE, "Database can not used new operator.%n[%ls]", GetErrorMessage(r));
334         r = pDatabase->Construct(dbName, false);
335         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Database can not construct.[%s]", GetErrorMessage(r));
336         statement.Append(L"SELECT contentId FROM playlisttable WHERE contentId = ?");
337         pStmt = pDatabase->CreateStatementN(statement);
338         TryCatch(pStmt != null, r = E_FAILURE, "CreateStatementN failed with error.[%s]", GetErrorMessage(r));
339
340         r = pStmt->BindString(0, contentId);
341         TryCatch(r == E_SUCCESS, r = E_FAILURE, "contentId BindString failed.[%s]", GetErrorMessage(r));
342
343         pEnum = pDatabase->ExecuteStatementN(*pStmt);
344
345 //      AppAssert(!pEnum);
346
347         if (pEnum != null)
348         {
349                 while (pEnum->MoveNext()== E_SUCCESS)
350                 {
351                         pEnum->GetStringAt(0, contentId);
352                         r = E_KEY_ALREADY_EXIST;
353                         break;
354                 }
355                 delete pEnum;
356                 pEnum = null;
357         }
358         
359         CATCH:
360         if(pStmt != null)
361         {
362                 delete pStmt;
363                 pStmt = null;
364         }
365
366         delete pDatabase;
367         pDatabase = null;
368         //AppLogDebug("Read 8 : %d", playCount);
369         AppLogDebug("EXIT");
370         return r;
371 }
372
373 Tizen::Base::Collection::ArrayList*
374 PlaylistDB::ReadValueN(Tizen::Base::String& contentId)
375 {
376         AppLogDebug("ENTER");
377         Tizen::Base::String dbName(App::GetInstance()->GetAppRootPath() + DATABASE_PATH);
378
379         result r = E_SUCCESS;
380
381         Tizen::Base::String statement = null;
382         DbEnumerator* pEnum = null;
383
384         Tizen::Io::Database* pDatabase = new (std::nothrow) Database();
385         r = pDatabase->Construct(dbName, false);
386         statement.Append(L"SELECT playCount, playTime FROM playlisttable");
387
388         pEnum = pDatabase->QueryN(statement);
389         int playCount = 0;
390         Tizen::Base::String playTime;
391
392         Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
393         pTempList->Construct();
394
395         if (pEnum != null)
396         {
397                 while (pEnum->MoveNext()== E_SUCCESS)
398                 {
399                         pEnum->GetIntAt(0, playCount);
400                         pEnum->GetStringAt(1, playTime);
401                         pTempList->Add(*(new (std::nothrow) Integer(playCount)));
402                         pTempList->Add(*(new (std::nothrow) String(playTime)));
403                 }
404                 AppLogDebug("EXIT");
405         }
406         else
407         {
408                 pTempList = null;
409         }
410
411         delete pEnum;
412         pEnum = null;
413
414         delete pDatabase;
415         pDatabase = null;
416
417         AppLogDebug("EXIT");
418         return pTempList;
419 }