add to resume event
[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                                 DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
578                                 pFormatter->ApplyPattern(L"YYYY-MM-dd hh:mm:ss");
579                                 String dateStr;
580                                 pFormatter->Format(date, dateStr);
581                                 AppLog("DateStr: %ls", dateStr.GetPointer());
582                                 __pDbStatement->BindString(2, dateStr);
583                                 if(history.GetFavIconBuffer() != null)
584                                 {
585                                         __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
586                                 }
587                                 __pDbStatement->BindInt(4,history.GetFavIconWidth());
588                                 __pDbStatement->BindInt(5,history.GetFavIconHeight());
589
590                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
591
592                                 if (IsFailed(GetLastResult()))
593                                 {
594                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
595                                         return r;
596                                 }
597                                 else
598                                 {
599                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
600                                 }
601                         }
602                         else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
603                         {
604                                 r = __pDataBase->ExecuteSql(formatQuery, false);
605                                 if (IsFailed(r))
606                                 {
607                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
608                                         return r;
609                                 }
610                         }
611                         count = 0;
612                 }
613         }
614
615         return r;
616 }
617
618 result PresentationModelBase::ExecuteUpdateHistoryFavIconQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
619 {
620         result r = E_FAILURE;
621         String queryType;
622         int historyID;
623         r = Integer::Parse(history.GetHistoryId(), historyID);
624
625         if (__pDataBase == null)
626         {
627                 return E_INIT_FAILED;
628         }
629
630         if (__pDbEnum != null)
631         {
632                 delete __pDbEnum;
633                 __pDbEnum = null;
634         }
635
636         if (__pDbStatement != null)
637         {
638                 delete __pDbStatement;
639                 __pDbStatement = null;
640         }
641         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
642
643         if (__isBeginTransaction == false )
644         {
645                 r = __pDataBase->BeginTransaction();
646                 if (IsFailed(r))
647                 {
648                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
649                         return r;
650                 }
651
652                 __isBeginTransaction = true;
653                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
654                 if(GetLastResult() != E_SUCCESS)
655                 {
656                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
657                         return GetLastResult();
658                 }
659                 __pDbStatement->BindBlob(0,*(history.GetFavIconBuffer()));
660                 __pDbStatement->BindInt(1,history.GetFavIconWidth());
661                 __pDbStatement->BindInt(2,history.GetFavIconHeight());
662                 __pDbStatement->BindInt(3,historyID);
663
664                 __pDataBase->ExecuteStatementN(*__pDbStatement);
665
666                 if (IsFailed(GetLastResult()))
667                 {
668                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
669                         return r;
670                 }
671                 else
672                 {
673                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
674                 }
675         }
676
677                 return r;
678 }
679
680 result
681 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
682 {
683         result r = E_FAILURE;
684
685         if (null == __pDataBase || null == __pDbEnum)
686         {
687                 if (__pDbEnum == null)
688                 {
689                         AppLogException("PresentationModelBase : pdbenum is null");
690                 }
691                 if (__pDataBase == null)
692                 {
693                         AppLogException("PresentationModelBase : __pDataBase is null");
694                 }
695                 return E_OUT_OF_MEMORY;
696         }
697         r = __pDbEnum->MoveNext();
698         if (IsFailed(r))
699         {
700                 nextRowpresent = false;
701                 return r;
702         }
703         nextRowpresent = true;
704         return r;
705 }
706
707 result
708 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
709 {
710         if (null == __pDataBase || null == __pDbEnum)
711         {
712                 return E_DATABASE;
713         }
714         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
715
716         return E_SUCCESS;
717 }
718
719 result
720 PresentationModelBase::GetColumn(int index, String& stringValue)
721 {
722         result r = E_FAILURE;
723         DbColumnType columnType;
724
725         if (null == __pDataBase || null == __pDbEnum)
726         {
727                 return E_OUT_OF_MEMORY;
728         }
729         // Find column type
730         columnType = __pDbEnum->GetColumnType(index);
731
732         //if column type is null then assign empty string and return
733         if (columnType == DB_COLUMNTYPE_NULL)
734         {
735                 stringValue = "";
736                 return E_SUCCESS;
737         }
738         //Get String value
739         r = __pDbEnum->GetStringAt(index, stringValue);
740         if (IsFailed(r))
741         {
742                 AppLogDebug("GetStringAt: Failed");
743                 return r;
744         }
745         return r;
746 }
747
748 result
749 PresentationModelBase::GetColumn(int index, int& intValue)
750 {
751         result r = E_FAILURE;
752         DbColumnType columnType;
753
754         if (null == __pDataBase || null == __pDbEnum)
755         {
756                 return E_OUT_OF_MEMORY;
757         }
758         // Find column type
759         columnType = __pDbEnum->GetColumnType(index);
760
761         //if column type is null then assign empty string and return
762         if (columnType == DB_COLUMNTYPE_NULL)
763         {
764                 intValue = 0;
765                 return E_SUCCESS;
766         }
767         //Get String value
768         r = __pDbEnum->GetIntAt(index, intValue);
769         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
770
771         CATCH:
772         return r;
773 }
774
775 result
776 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
777 {
778         result r = E_FAILURE;
779         DbColumnType nType;
780
781         if (null == __pDataBase || null == __pDbEnum)
782         {
783                 AppLogDebug("__pDataBase or __pDbEnum is null");
784                 return E_OUT_OF_MEMORY;
785         }
786         // Find column type
787         nType = __pDbEnum->GetColumnType(index);
788         //if column type is null then assign empty string and return
789         if (nType == DB_COLUMNTYPE_NULL)
790         {
791                 return E_SUCCESS;
792         }
793         //Get String value
794         r = __pDbEnum->GetDateTimeAt(index, dateValue);
795         if (IsFailed(r))
796         {
797                 AppLogDebug("GetDateTimeAt: Failed");
798                 String temp;
799                 int currIndex = -1;
800                 int nextIndex = -1;
801
802                 int year = 0;
803                 int month = 0;
804                 int day = 0;
805                 int hour = 0;
806                 int min = 0;
807                 int sec = 0;
808
809                 int value = 0;
810                 String dbColoumn = "";
811                 __pDbEnum->GetStringAt(index, dbColoumn);
812                 dbColoumn.IndexOf("-", 0, currIndex);
813                 if(currIndex != -1)
814                 {
815                         dbColoumn.SubString(0, currIndex ,temp);
816                         Integer::Parse(temp, 10, year);
817                 }
818
819                 dbColoumn.IndexOf("-", currIndex + 1, nextIndex);
820                 if(currIndex != -1)
821                 {
822                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex - 1 ,temp);
823                         Integer::Parse(temp, 10, month);
824                 }
825                 currIndex = nextIndex;
826
827                 dbColoumn.IndexOf(" ", currIndex + 1, nextIndex);
828                 if(currIndex != -1)
829                 {
830                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
831                         Integer::Parse(temp, 10, day);
832                 }
833                 currIndex = nextIndex;
834
835                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
836                 if(currIndex != -1)
837                 {
838                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
839                         Integer::Parse(temp, 10, hour);
840                 }
841                 currIndex = nextIndex;
842
843                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
844                 if(currIndex != -1)
845                 {
846                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
847                         Integer::Parse(temp, 10, min);
848                 }
849                 currIndex = nextIndex;
850
851                 dbColoumn.SubString(currIndex + 1, temp);
852                 Integer::Parse(temp, 10, sec);
853
854                 dateValue.SetValue(year, month, day, hour, min, sec);
855                 return E_SUCCESS;
856         }
857
858         return r;
859 }
860
861 result
862 PresentationModelBase::GetColumn(int index, double& doubleValue)
863 {
864         result r = E_FAILURE;
865         DbColumnType columnType;
866
867         if (null == __pDataBase || null == __pDbEnum)
868         {
869                 return E_OUT_OF_MEMORY;
870         }
871         // Find column type
872         columnType = __pDbEnum->GetColumnType(index);
873
874         //if column type is null then assign empty string and return
875         if (columnType == DB_COLUMNTYPE_NULL)
876         {
877                 doubleValue = 0.0;
878                 return E_SUCCESS;
879         }
880         //Get String value
881         r = __pDbEnum->GetDoubleAt(index, doubleValue);
882         if (IsFailed(r))
883         {
884                 AppLogDebug("GetDateTimeAt: Failed");
885                 return r;
886         }
887
888         return r;
889 }
890
891 result
892 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
893 {
894         result r = E_FAILURE;
895         DbColumnType columnType;
896
897         if (null == __pDataBase || null == __pDbEnum)
898         {
899                 return E_OUT_OF_MEMORY;
900         }
901         // Find column type
902         columnType = __pDbEnum->GetColumnType(index);
903
904         //if column type is null then assign empty string and return
905         if (columnType == DB_COLUMNTYPE_NULL)
906         {
907                 return E_SUCCESS;
908         }
909         favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
910         //Get String value
911         r = __pDbEnum->GetBlobAt(index, favIconBuffer);
912         AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
913
914
915         if (IsFailed(r))
916         {
917                 AppLogDebug("GetDateTimeAt: Failed");
918                 return r;
919         }
920
921         return r;
922 }
923
924 result
925 PresentationModelBase::RollbackDb()
926 {
927         result r = E_FAILURE;
928
929         if (__pDataBase != null && __isBeginTransaction == true)
930         {
931                 r = __pDataBase->RollbackTransaction();
932                 if (IsFailed(r))
933                 {
934                         return r;
935                 }
936                 __isBeginTransaction = false;
937         }
938         return r;
939 }
940
941 result
942 PresentationModelBase::CommitDb()
943 {
944
945         result r = E_FAILURE;
946
947         if (__pDataBase != null && __isBeginTransaction == true)
948         {
949                 r = __pDataBase->CommitTransaction();
950                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
951
952                 __isBeginTransaction = false;
953         }
954
955         CATCH:
956         return r;
957 }
958
959 result
960 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
961 {
962         String query;
963         result r = E_FAILURE;
964         int count = -1;
965         bool nextRowpresent = false;
966
967         query.Append(L"SELECT last_insert_rowid() FROM ");
968         query.Append(tableName);
969
970         r = PresentationModelBase::ExecuteQuery(query, count);
971         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
972
973         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
974
975         if (nextRowpresent == true)
976         {
977                 r = PresentationModelBase::GetColumn(0, rowId);
978                 if (IsFailed(r))
979                 {
980                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
981                         return r;
982                 }
983         }
984         CATCH: return r;
985 }
986
987 bool
988 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
989 {
990         DateTime dtNow;
991         LocaleManager localManager;
992         result r = E_FAILURE;
993         TimeZone tz;
994
995         r = SystemTime::GetCurrentTime(UTC_TIME, dtNow);
996         if (IsFailed(r))
997         {
998                 return false;
999         }
1000         localManager.Construct();
1001         tz = localManager.GetSystemTimeZone();
1002         dt = tz.UtcTimeToWallTime(dtNow);
1003
1004         return true;
1005 }
1006
1007