Fixed Nabi issue N_SE-40542,N_SE-39949,N_SE-40229
[apps/osp/Internet.git] / src / IntPresentationModelBase.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 //!Internet
19 /*@file:        IntPresentationModelBase.cpp
20  *@brief:       Defines the database/storage functions.
21  */
22
23 #include <FBase.h>
24 #include <FLocales.h>
25
26 #include "IntPresentationModelBase.h"
27 #include "IntTypes.h"
28
29 const wchar_t* BOOKMARK_URL_TABLE = L"Bookmarks";
30 //const wchar_t* BOOKMARK_FOLDER_TABLE = L"BookmarkFolder";
31 const wchar_t* HISTORY_DATA_TABLE = L"History";
32
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Io;
38 using namespace Tizen::Locales;
39 using namespace Tizen::System;
40
41 const int PresentationModelBase::MAX_DB_QUERY_SIZE = 21474836;
42 const int PresentationModelBase::QUERY_TYPE_LENGTH = 6;
43
44 PresentationModelBase::PresentationModelBase(void)
45 {
46         __pDataBase = null;
47         __pDbEnum = null;
48         __pDbStatement = null;
49         __isBeginTransaction = false;
50         DB_FILE_PATH = L"/opt/usr/dbspace/.browser-history.db";
51 }
52
53 PresentationModelBase::~PresentationModelBase(void)
54 {
55
56 }
57
58 result
59 PresentationModelBase::Initialize(void)
60 {
61         result r = E_FAILURE;
62
63         r = OpenDb();
64
65         return r;
66 }
67
68 result
69 PresentationModelBase::UnInitialize(void)
70 {
71         result r = E_FAILURE;
72
73         r = PresentationModelBase::CloseDb();
74
75         return r;
76 }
77
78 result
79 PresentationModelBase::OpenDb(void)
80 {
81         result r = E_FAILURE;
82
83         if (__pDataBase != null)
84         {
85                 if (__pDbEnum != null)
86                 {
87                         delete __pDbEnum;
88                         __pDbEnum = null;
89                 }
90                 if (__pDbStatement != null)
91                 {
92                         delete __pDbStatement;
93                         __pDbStatement = null;
94                 }
95                 return r;
96         }
97         __pDataBase = new(std::nothrow) Database();
98         if (null == __pDataBase)
99         {
100                 return E_OUT_OF_MEMORY;
101         }
102         String dbName =  DB_FILE_PATH;
103
104         r = __pDataBase->Construct(dbName, true);
105         if (IsFailed(r))
106         {
107                 AppLogDebug("DB construction failed:%s", GetErrorMessage(r));
108                 delete __pDataBase;
109                 __pDataBase = null;
110                 return r;
111         }
112
113
114         __isBeginTransaction = false;
115         if (File::IsFileExist(dbName))
116         {
117                 r = InitializeDBTables();
118                 if (IsFailed(r))
119                 {
120                         delete __pDataBase;
121                         __pDataBase = null;
122                         return r;
123                 }
124         }
125         return r;
126 }
127
128 result
129 PresentationModelBase::CloseDb(void)
130 {
131         if (__pDbEnum != null)
132         {
133                 delete __pDbEnum;
134                 __pDbEnum = null;
135         }
136         if (__pDbStatement != null)
137         {
138                 delete __pDbStatement;
139                 __pDbStatement = null;
140         }
141         if (__pDataBase != null)
142         {
143                 delete __pDataBase;
144                 __pDataBase = null;
145         }
146
147         __isBeginTransaction = false;
148
149         return E_SUCCESS;
150 }
151
152 result
153 PresentationModelBase::CreateDBTables(BrowserDbTableTypes tableType)
154 {
155         result r = E_FAILURE;
156         String query;
157         int resultCount = -1;
158
159         if (DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.internet_bookmark.db") == 0)
160         {
161                 switch (tableType)
162                 {
163                 case DB_TYPE_BOOKMARKURLS:
164                 {
165                         query.Append(L"CREATE TABLE IF NOT EXISTS ");
166                         query.Append(BOOKMARK_URL_TABLE);
167                         //query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, URL TEXT, PARENT_BOOKMARK_ID INTEGER DEFAULT -1, CREATED_TIME DATETIME, MODIFIED_TIME DATETIME, ICON_PATH TEXT, FAVICON_ID INTEGER)");
168                         query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TYPE INTEGER, PARENT INTEGER DEFAULT -1, ADDRESS TEXT, TITLE TEXT, CREATIONDATE DATETIME, SEQUENCE INTEGER, UPDATEDATE DATETIME, VISITDATE DATETIME, EDITABLE INTEGER, ACCESSACCOUNT INTEGER, FAVICON BLOB, FAVICON_LENGTH INTEGER, FAVICON_W INTEGER, FAVICON_H INTEGER, CREATED_DATE DATETIME, ACCOUNT_NAME TEXT, ACCOUNT_TYPE TEXT, THUMBNAIL BLOB, THUMBNAIL_LENGTH INTEGER, THUMBNAIL_W INTEGER, THUMBNAIL_H INTEGER, VERSION INTEGER, SYNC TEXT, TAG1 TEXT, TAG2 TEXT, TAG3 TEXT, TAG4 TEXT)");
169
170                         break;
171                 }
172                 case DB_TYPE_FAVICON:
173                 {
174                         query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
175                         break;
176                 }
177                 default:
178                 {
179                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
180                         return E_SUCCESS;
181                 }
182                 }
183         }
184         else if(DB_FILE_PATH.CompareTo(L"/opt/usr/dbspace/.browser-history.db") == 0)
185         {
186                 switch (tableType)
187                 {
188                 case DB_TYPE_HISTORYDATA:
189                 {
190                         query.Append(L"CREATE TABLE IF NOT EXISTS ");
191                         query.Append(HISTORY_DATA_TABLE);
192                         //query.Append(L" ( ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, URL TEXT, VISITED_TIME DATETIME, ICON_PATH TEXT, FAVICON_ID INTEGER, THUMBNAIL_PATH TEXT)");
193                         query.Append(L"(ID INTEGER PRIMARY KEY AUTOINCREMENT, ADDRESS TEXT, TITLE TEXT, COUNTER INTEGER, VISITDATE DATETIME, FAVICON BLOB, FAVICON_LENGTH INTEGER, FAVICON_W INTEGER, FAVICON_H INTEGER, SNAPSHOT BLOB, SNAPSHOT_STRIDE INTEGER, SNAPSHOT_W INTEGER, SNAPSHOT_H INTEGER)");
194                         break;
195                 }
196                 case DB_TYPE_FAVICON:
197                 {
198                         query.Append(L"CREATE TABLE IF NOT EXISTS FaviconData ( ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, FILE_PATH TEXT )");
199                         break;
200                 }
201                 default:
202                 {
203                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableType);
204                         return E_SUCCESS;
205                 }
206                 }
207
208         }
209         r = PresentationModelBase::ExecuteQuery(query, resultCount);
210         TryCatch(!IsFailed(r),,"PresentationModelBase::CreateDBTables query failed %s",GetErrorMessage(r));
211
212         CATCH: return r;
213 }
214
215 result
216 PresentationModelBase::InitializeDBTables(void)
217 {
218         result r = E_FAILURE;
219         AppLog("DB_FILE_PATH :%ls", String(DB_FILE_PATH).GetPointer());
220         for (register int nDbTblIndex = DB_TYPE_INVALID + 1; nDbTblIndex < DB_TYPE_MAX; nDbTblIndex++)
221         {
222                 r = CreateDBTables((BrowserDbTableTypes) nDbTblIndex);
223                 TryCatch(!IsFailed(r),,"PresentationModelBase::InitializeDBTables failed %s",GetErrorMessage(r));
224         }
225
226         CATCH: return r;
227 }
228
229 result
230 PresentationModelBase::DropDBTables(void)
231 {
232         result r = E_FAILURE;
233         String query;
234         int resultCount = -1;
235
236         for (register int tableIndex = DB_TYPE_INVALID + 1; tableIndex < DB_TYPE_MAX; tableIndex++)
237         {
238                 switch (tableIndex)
239                 {
240                 case DB_TYPE_BOOKMARKURLS:
241                 {
242                         query.Clear();
243                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS Bookmark");
244                         break;
245                 }
246                 /*case DB_TYPE_BOOKMARKFOLDERS:
247                 {
248                         query.Clear();
249                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS BookmarkFolder");
250                         break;
251                 }*/
252
253                 case DB_TYPE_HISTORYDATA:
254                 {
255                         query.Clear();
256                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS HistoryData");
257                         break;
258                 }
259
260                 case DB_TYPE_FAVICON:
261                 {
262                         query.Clear();
263                         query.Format(MAX_DB_QUERY_SIZE, L"DROP TABLE IF EXISTS FaviconData");
264                         break;
265                 }
266
267                 default:
268                 {
269                         AppLogDebug("PresentationModelBase::CreateDBTables: Table Type:%d is invalid", tableIndex);
270                         break;
271                 }
272
273                 }
274
275                 r = PresentationModelBase::ExecuteQuery(query, resultCount);
276                 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
277         }
278
279         CATCH: return r;
280 }
281
282 result
283 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
284 {
285         AppLogDebug("rahul Query: %ls", formatQuery.GetPointer());
286
287         result r = E_FAILURE;
288         String queryType;
289
290         if (__pDataBase == null)
291         {
292                 return E_INIT_FAILED;
293         }
294
295         if (__pDbEnum != null)
296         {
297                 delete __pDbEnum;
298                 __pDbEnum = null;
299         }
300
301         if (__pDbStatement != null)
302         {
303                 delete __pDbStatement;
304                 __pDbStatement = null;
305         }
306
307
308 #if defined(FBK_DB_QUERY_LOG)
309         {
310                 File file;
311                 file.Construct(L"/Home/Query.txt", "a+", true);
312                 file.Write(formatQuery);
313                 file.Write(String("\r\n"));
314         }
315 #endif
316
317         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
318         if (r == E_SUCCESS)
319         {
320                 if (queryType == "SELECT")
321                 {
322                         __pDbEnum = __pDataBase->QueryN(formatQuery);
323                         r = GetLastResult();
324                         if (IsFailed(r))
325                         {
326                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
327                                 return r;
328                         }
329                         count = 0;
330
331                         if (__pDbEnum != null)
332                         {
333                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
334                                 {
335                                         count++;
336                                 }
337                                 __pDbEnum->Reset();
338                         }
339
340                 }
341                 else
342                 {
343                         if (__isBeginTransaction == false &&
344                                         (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
345                         {
346                                 r = __pDataBase->BeginTransaction();
347                                 if (IsFailed(r))
348                                 {
349                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
350                                         return r;
351                                 }
352
353                                 __isBeginTransaction = true;
354                         }
355
356                         r = __pDataBase->ExecuteSql(formatQuery, false);
357                         if (IsFailed(r))
358                         {
359                                 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
360                                 return r;
361                         }
362                         count = 0;
363                 }
364         }
365
366         return r;
367 }
368
369 result
370 PresentationModelBase::ExecuteBookmarkQuery(const Tizen::Base::String& formatQuery, int& count, BookmarkData& bookmark)
371 {
372         AppLogDebug("PresentationModelBase::ExecuteBookmarkQuery Query: %ls", formatQuery.GetPointer());
373
374         result r = E_FAILURE;
375         String queryType;
376
377         DateTime dt;
378         String bookmarkId;
379         String bookmarkTitle = bookmark.GetBookmarkTitle();
380         bookmarkTitle.Replace(L"'", L"''");
381         String bookmarkUrl = bookmark.GetUrl();
382         bookmarkUrl.Replace(L"'", L"''");
383
384         PresentationModelBase::GetCurrentDateTime(dt);
385
386         if (__pDataBase == null)
387         {
388                 return E_INIT_FAILED;
389         }
390
391         if (__pDbEnum != null)
392         {
393                 delete __pDbEnum;
394                 __pDbEnum = null;
395         }
396
397         if (__pDbStatement != null)
398         {
399                 delete __pDbStatement;
400                 __pDbStatement = null;
401         }
402
403
404 #if defined(FBK_DB_QUERY_LOG)
405         {
406                 File file;
407                 file.Construct(L"/Home/Query.txt", "a+", true);
408                 file.Write(formatQuery);
409                 file.Write(String("\r\n"));
410         }
411 #endif
412
413         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
414         if (r == E_SUCCESS)
415         {
416                 if (queryType == "SELECT")
417                 {
418                         __pDbEnum = __pDataBase->QueryN(formatQuery);
419                         r = GetLastResult();
420                         if (IsFailed(r))
421                         {
422                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
423                                 return r;
424                         }
425                         count = 0;
426
427                         if (__pDbEnum != null)
428                         {
429                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
430                                 {
431                                         count++;
432                                 }
433                                 __pDbEnum->Reset();
434                         }
435                 }
436                 else
437                 {
438                         if (__isBeginTransaction == false && (queryType == L"INSERT" ))
439                         {
440                                 r = __pDataBase->BeginTransaction();
441                                 if (IsFailed(r))
442                                 {
443                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
444                                         return r;
445                                 }
446
447                                 __isBeginTransaction = true;
448                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
449                                 __pDbStatement->BindInt(0,bookmark.GetBookmarkType());
450                                 __pDbStatement->BindString(1,bookmark.GetParentId());
451                                 __pDbStatement->BindString(2,bookmarkUrl);
452                                 __pDbStatement->BindString(3,bookmarkTitle);
453                                 DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
454                                 pFormatter->ApplyPattern(L"yyyy-mm-dd hh:mm:ss");
455                                 String dateStr;
456                                 pFormatter->Format(dt, dateStr);
457                                 AppLog("DateStr: %ls", dateStr.GetPointer());
458                                 __pDbStatement->BindString(4,dateStr);
459                                 __pDbStatement->BindString(5,dateStr);
460
461                                 if(bookmark.GetFavIconBuffer() != null)
462                                 {
463                                         __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
464                                 }
465                                 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
466                                 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
467
468                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
469
470                                 if (IsFailed(GetLastResult()))
471                                 {
472                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
473                                         return r;
474                                 }
475                                 else
476                                 {
477                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
478                                 }
479                         }
480                         else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
481                         {
482                                 r = __pDataBase->ExecuteSql(formatQuery, false);
483                                 if (IsFailed(r))
484                                 {
485                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
486                                         return r;
487                                 }
488                         }
489
490                         count = 0;
491                 }
492         }
493
494         return r;
495 }
496
497 result
498 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
499 {
500
501         AppLogDebug("Query: %ls", formatQuery.GetPointer());
502
503         result r = E_FAILURE;
504         String queryType;
505         DateTime date;
506         int count;
507
508         String historyTitle = history.GetHistoryTitle();
509         historyTitle.Replace(L"'", L"''");
510
511         String historyUrl = history.GetHistoryUrl();
512         historyUrl.Replace(L"'", L"''");
513
514         GetCurrentDateTime(date);
515
516         if (__pDataBase == null)
517         {
518                 return E_INIT_FAILED;
519         }
520
521         if (__pDbEnum != null)
522         {
523                 delete __pDbEnum;
524                 __pDbEnum = null;
525         }
526
527         if (__pDbStatement != null)
528         {
529                 delete __pDbStatement;
530                 __pDbStatement = null;
531         }
532
533         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
534         if (r == E_SUCCESS)
535         {
536                 if (queryType == "SELECT")
537                 {
538                         __pDbEnum = __pDataBase->QueryN(formatQuery);
539                         r = GetLastResult();
540                         if (IsFailed(r))
541                         {
542                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
543                                 return r;
544                         }
545                         count = 0;
546
547                         if (__pDbEnum != null)
548                         {
549                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
550                                 {
551                                         count++;
552                                 }
553                                 __pDbEnum->Reset();
554                         }
555
556                 }
557                 else
558                 {
559                         if (__isBeginTransaction == false && (queryType == L"INSERT"))
560                         {
561                                 r = __pDataBase->BeginTransaction();
562                                 if (IsFailed(r))
563                                 {
564                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
565                                         return r;
566                                 }
567
568                                 __isBeginTransaction = true;
569                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
570                                 if(GetLastResult() != E_SUCCESS)
571                                 {
572                                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
573                                         return GetLastResult();
574                                 }
575                                 __pDbStatement->BindString(0,historyUrl);
576                                 __pDbStatement->BindString(1,historyTitle);
577
578                                 String visitedTime = GetStringFromDate(date);
579                                 /*DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
580                                 pFormatter->ApplyPattern(L"YYYY-MM-dd hh:mm:ss");
581                                 String dateStr;
582                                 pFormatter->Format(date, dateStr);
583                                 AppLog("DateStr: %ls", dateStr.GetPointer());*/
584                                 __pDbStatement->BindString(2, visitedTime);
585                                 if(history.GetFavIconBuffer() != null)
586                                 {
587                                         __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
588                                 }
589                                 __pDbStatement->BindInt(4,history.GetFavIconWidth());
590                                 __pDbStatement->BindInt(5,history.GetFavIconHeight());
591
592                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
593
594                                 if (IsFailed(GetLastResult()))
595                                 {
596                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
597                                         return r;
598                                 }
599                                 else
600                                 {
601                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
602                                 }
603                         }
604                         else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
605                         {
606                                 r = __pDataBase->ExecuteSql(formatQuery, false);
607                                 if (IsFailed(r))
608                                 {
609                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
610                                         return r;
611                                 }
612                         }
613                         count = 0;
614                 }
615         }
616
617         return r;
618 }
619
620 result PresentationModelBase::ExecuteUpdateHistoryFavIconQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
621 {
622         result r = E_FAILURE;
623         String queryType;
624         int historyID;
625         r = Integer::Parse(history.GetHistoryId(), historyID);
626
627         if (__pDataBase == null)
628         {
629                 return E_INIT_FAILED;
630         }
631
632         if (__pDbEnum != null)
633         {
634                 delete __pDbEnum;
635                 __pDbEnum = null;
636         }
637
638         if (__pDbStatement != null)
639         {
640                 delete __pDbStatement;
641                 __pDbStatement = null;
642         }
643         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
644
645         if (__isBeginTransaction == false )
646         {
647                 r = __pDataBase->BeginTransaction();
648                 if (IsFailed(r))
649                 {
650                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
651                         return r;
652                 }
653
654                 __isBeginTransaction = true;
655                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
656                 if(GetLastResult() != E_SUCCESS)
657                 {
658                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
659                         return GetLastResult();
660                 }
661                 __pDbStatement->BindBlob(0,*(history.GetFavIconBuffer()));
662                 __pDbStatement->BindInt(1,history.GetFavIconWidth());
663                 __pDbStatement->BindInt(2,history.GetFavIconHeight());
664                 __pDbStatement->BindInt(3,historyID);
665
666                 __pDataBase->ExecuteStatementN(*__pDbStatement);
667
668                 if (IsFailed(GetLastResult()))
669                 {
670                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
671                         return r;
672                 }
673                 else
674                 {
675                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
676                 }
677         }
678
679                 return r;
680 }
681
682 result
683 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
684 {
685         result r = E_FAILURE;
686
687         if (null == __pDataBase || null == __pDbEnum)
688         {
689                 if (__pDbEnum == null)
690                 {
691                         AppLogException("PresentationModelBase : pdbenum is null");
692                 }
693                 if (__pDataBase == null)
694                 {
695                         AppLogException("PresentationModelBase : __pDataBase is null");
696                 }
697                 return E_OUT_OF_MEMORY;
698         }
699         r = __pDbEnum->MoveNext();
700         if (IsFailed(r))
701         {
702                 nextRowpresent = false;
703                 return r;
704         }
705         nextRowpresent = true;
706         return r;
707 }
708
709 result
710 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
711 {
712         if (null == __pDataBase || null == __pDbEnum)
713         {
714                 return E_DATABASE;
715         }
716         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
717
718         return E_SUCCESS;
719 }
720
721 result
722 PresentationModelBase::GetColumn(int index, String& stringValue)
723 {
724         result r = E_FAILURE;
725         DbColumnType columnType;
726
727         if (null == __pDataBase || null == __pDbEnum)
728         {
729                 return E_OUT_OF_MEMORY;
730         }
731         // Find column type
732         columnType = __pDbEnum->GetColumnType(index);
733
734         //if column type is null then assign empty string and return
735         if (columnType == DB_COLUMNTYPE_NULL)
736         {
737                 stringValue = "";
738                 return E_SUCCESS;
739         }
740         //Get String value
741         r = __pDbEnum->GetStringAt(index, stringValue);
742         if (IsFailed(r))
743         {
744                 AppLogDebug("GetStringAt: Failed");
745                 return r;
746         }
747         return r;
748 }
749
750 result
751 PresentationModelBase::GetColumn(int index, int& intValue)
752 {
753         result r = E_FAILURE;
754         DbColumnType columnType;
755
756         if (null == __pDataBase || null == __pDbEnum)
757         {
758                 return E_OUT_OF_MEMORY;
759         }
760         // Find column type
761         columnType = __pDbEnum->GetColumnType(index);
762
763         //if column type is null then assign empty string and return
764         if (columnType == DB_COLUMNTYPE_NULL)
765         {
766                 intValue = 0;
767                 return E_SUCCESS;
768         }
769         //Get String value
770         r = __pDbEnum->GetIntAt(index, intValue);
771         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
772
773         CATCH:
774         return r;
775 }
776
777 result
778 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
779 {
780         result r = E_FAILURE;
781         DbColumnType nType;
782
783         if (null == __pDataBase || null == __pDbEnum)
784         {
785                 AppLogDebug("__pDataBase or __pDbEnum is null");
786                 return E_OUT_OF_MEMORY;
787         }
788         // Find column type
789         nType = __pDbEnum->GetColumnType(index);
790         //if column type is null then assign empty string and return
791         if (nType == DB_COLUMNTYPE_NULL)
792         {
793                 return E_SUCCESS;
794         }
795         //Get String value
796         r = __pDbEnum->GetDateTimeAt(index, dateValue);
797         if (IsFailed(r))
798         {
799                 AppLogDebug("GetDateTimeAt: Failed");
800                 String temp;
801                 int currIndex = -1;
802                 int nextIndex = -1;
803
804                 int year = 0;
805                 int month = 0;
806                 int day = 0;
807                 int hour = 0;
808                 int min = 0;
809                 int sec = 0;
810
811                 int value = 0;
812                 String dbColoumn = "";
813                 __pDbEnum->GetStringAt(index, dbColoumn);
814                 dbColoumn.IndexOf("-", 0, currIndex);
815                 if(currIndex != -1)
816                 {
817                         dbColoumn.SubString(0, currIndex ,temp);
818                         Integer::Parse(temp, 10, year);
819                 }
820
821                 dbColoumn.IndexOf("-", currIndex + 1, nextIndex);
822                 if(currIndex != -1)
823                 {
824                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex - 1 ,temp);
825                         Integer::Parse(temp, 10, month);
826                 }
827                 currIndex = nextIndex;
828
829                 dbColoumn.IndexOf(" ", currIndex + 1, nextIndex);
830                 if(currIndex != -1)
831                 {
832                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
833                         Integer::Parse(temp, 10, day);
834                 }
835                 currIndex = nextIndex;
836
837                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
838                 if(currIndex != -1)
839                 {
840                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
841                         Integer::Parse(temp, 10, hour);
842                 }
843                 currIndex = nextIndex;
844
845                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
846                 if(currIndex != -1)
847                 {
848                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
849                         Integer::Parse(temp, 10, min);
850                 }
851                 currIndex = nextIndex;
852
853                 dbColoumn.SubString(currIndex + 1, temp);
854                 Integer::Parse(temp, 10, sec);
855
856                 dateValue.SetValue(year, month, day, hour, min, sec);
857                 return E_SUCCESS;
858         }
859
860         return r;
861 }
862
863 result
864 PresentationModelBase::GetColumn(int index, double& doubleValue)
865 {
866         result r = E_FAILURE;
867         DbColumnType columnType;
868
869         if (null == __pDataBase || null == __pDbEnum)
870         {
871                 return E_OUT_OF_MEMORY;
872         }
873         // Find column type
874         columnType = __pDbEnum->GetColumnType(index);
875
876         //if column type is null then assign empty string and return
877         if (columnType == DB_COLUMNTYPE_NULL)
878         {
879                 doubleValue = 0.0;
880                 return E_SUCCESS;
881         }
882         //Get String value
883         r = __pDbEnum->GetDoubleAt(index, doubleValue);
884         if (IsFailed(r))
885         {
886                 AppLogDebug("GetDateTimeAt: Failed");
887                 return r;
888         }
889
890         return r;
891 }
892
893 result
894 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
895 {
896         result r = E_FAILURE;
897         DbColumnType columnType;
898
899         if (null == __pDataBase || null == __pDbEnum)
900         {
901                 return E_OUT_OF_MEMORY;
902         }
903         // Find column type
904         columnType = __pDbEnum->GetColumnType(index);
905
906         //if column type is null then assign empty string and return
907         if (columnType == DB_COLUMNTYPE_NULL)
908         {
909                 return E_SUCCESS;
910         }
911         favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
912         //Get String value
913         r = __pDbEnum->GetBlobAt(index, favIconBuffer);
914         AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
915
916
917         if (IsFailed(r))
918         {
919                 AppLogDebug("GetDateTimeAt: Failed");
920                 return r;
921         }
922
923         return r;
924 }
925
926 result
927 PresentationModelBase::RollbackDb()
928 {
929         result r = E_FAILURE;
930
931         if (__pDataBase != null && __isBeginTransaction == true)
932         {
933                 r = __pDataBase->RollbackTransaction();
934                 if (IsFailed(r))
935                 {
936                         return r;
937                 }
938                 __isBeginTransaction = false;
939         }
940         return r;
941 }
942
943 result
944 PresentationModelBase::CommitDb()
945 {
946
947         result r = E_FAILURE;
948
949         if (__pDataBase != null && __isBeginTransaction == true)
950         {
951                 r = __pDataBase->CommitTransaction();
952                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
953
954                 __isBeginTransaction = false;
955         }
956
957         CATCH:
958         return r;
959 }
960
961 result
962 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
963 {
964         String query;
965         result r = E_FAILURE;
966         int count = -1;
967         bool nextRowpresent = false;
968
969         query.Append(L"SELECT last_insert_rowid() FROM ");
970         query.Append(tableName);
971
972         r = PresentationModelBase::ExecuteQuery(query, count);
973         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
974
975         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
976
977         if (nextRowpresent == true)
978         {
979                 r = PresentationModelBase::GetColumn(0, rowId);
980                 if (IsFailed(r))
981                 {
982                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
983                         return r;
984                 }
985         }
986         CATCH: return r;
987 }
988
989 bool
990 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
991 {
992         DateTime dtNow;
993         LocaleManager localManager;
994         result r = E_FAILURE;
995         TimeZone tz;
996
997         r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
998         if (IsFailed(r))
999         {
1000                 return false;
1001         }
1002         localManager.Construct();
1003         tz = localManager.GetSystemTimeZone();
1004         dt = tz.UtcTimeToWallTime(dtNow);
1005
1006         return true;
1007 }
1008
1009 String
1010 PresentationModelBase::GetStringFromDate(DateTime& date)
1011 {
1012         String dateFormat = L"";
1013         dateFormat.Append(date.GetYear());
1014         dateFormat.Append('-');
1015         if(date.GetMonth() < 10)
1016         {
1017                 dateFormat.Append('0');
1018         }
1019         dateFormat.Append(date.GetMonth());
1020         dateFormat.Append('-');
1021         if(date.GetDay() < 10)
1022         {
1023                 dateFormat.Append('0');
1024         }
1025         dateFormat.Append(date.GetDay());
1026         dateFormat.Append(' ');
1027         if(date.GetHour() < 10)
1028         {
1029                 dateFormat.Append('0');
1030         }
1031         dateFormat.Append(date.GetHour());
1032         dateFormat.Append(':');
1033         if(date.GetMinute() < 10)
1034         {
1035                 dateFormat.Append('0');
1036         }
1037         dateFormat.Append(date.GetMinute());
1038         dateFormat.Append(':');
1039         if(date.GetSecond() < 10)
1040         {
1041                 dateFormat.Append('0');
1042         }
1043         dateFormat.Append(date.GetSecond());
1044         return dateFormat;
1045 }