Fixed Nabi Issues N_SE-56908,56903,56917,56940
[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
268                 r = PresentationModelBase::ExecuteQuery(query, resultCount);
269                 TryCatch( !IsFailed(r),,"PresentationModelBase::DropDBTables query failed %s",GetErrorMessage(r));
270         }
271
272         CATCH: return r;
273 }
274
275 result
276 PresentationModelBase::ExecuteQuery(const String& formatQuery, int& count)
277 {
278         AppLogDebug("rahul Query: %ls", formatQuery.GetPointer());
279
280         result r = E_FAILURE;
281         String queryType;
282
283         if (__pDataBase == null)
284         {
285                 return E_INIT_FAILED;
286         }
287
288         if (__pDbEnum != null)
289         {
290                 delete __pDbEnum;
291                 __pDbEnum = null;
292         }
293
294         if (__pDbStatement != null)
295         {
296                 delete __pDbStatement;
297                 __pDbStatement = null;
298         }
299
300
301 #if defined(FBK_DB_QUERY_LOG)
302         {
303                 File file;
304                 file.Construct(L"/Home/Query.txt", "a+", true);
305                 file.Write(formatQuery);
306                 file.Write(String("\r\n"));
307         }
308 #endif
309
310         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
311         if (r == E_SUCCESS)
312         {
313                 if (queryType == "SELECT")
314                 {
315                         __pDbEnum = __pDataBase->QueryN(formatQuery);
316                         r = GetLastResult();
317                         if (IsFailed(r))
318                         {
319                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
320                                 return r;
321                         }
322                         count = 0;
323
324                         if (__pDbEnum != null)
325                         {
326                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
327                                 {
328                                         count++;
329                                 }
330                                 __pDbEnum->Reset();
331                         }
332
333                 }
334                 else
335                 {
336                         if (__isBeginTransaction == false &&
337                                         (queryType == L"INSERT" || queryType == L"UPDATE" || queryType == L"DELETE"))
338                         {
339                                 r = __pDataBase->BeginTransaction();
340                                 if (IsFailed(r))
341                                 {
342                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
343                                         return r;
344                                 }
345
346                                 __isBeginTransaction = true;
347                         }
348
349                         r = __pDataBase->ExecuteSql(formatQuery, false);
350                         if (IsFailed(r))
351                         {
352                                 AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
353                                 return r;
354                         }
355                         count = 0;
356                 }
357         }
358
359         return r;
360 }
361
362 result
363 PresentationModelBase::ExecuteBookmarkQuery(const Tizen::Base::String& formatQuery, int& count, BookmarkData& bookmark)
364 {
365         AppLogDebug("PresentationModelBase::ExecuteBookmarkQuery Query: %ls", formatQuery.GetPointer());
366
367         result r = E_FAILURE;
368         String queryType;
369
370         DateTime dt;
371         String bookmarkId;
372         String bookmarkTitle = bookmark.GetBookmarkTitle();
373         bookmarkTitle.Replace(L"'", L"\'");
374         String bookmarkUrl = bookmark.GetUrl();
375         bookmarkUrl.Replace(L"'", L"\'");
376
377         PresentationModelBase::GetCurrentDateTime(dt);
378
379         if (__pDataBase == null)
380         {
381                 return E_INIT_FAILED;
382         }
383
384         if (__pDbEnum != null)
385         {
386                 delete __pDbEnum;
387                 __pDbEnum = null;
388         }
389
390         if (__pDbStatement != null)
391         {
392                 delete __pDbStatement;
393                 __pDbStatement = null;
394         }
395
396
397 #if defined(FBK_DB_QUERY_LOG)
398         {
399                 File file;
400                 file.Construct(L"/Home/Query.txt", "a+", true);
401                 file.Write(formatQuery);
402                 file.Write(String("\r\n"));
403         }
404 #endif
405
406         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
407         if (r == E_SUCCESS)
408         {
409                 if (queryType == "SELECT")
410                 {
411                         __pDbEnum = __pDataBase->QueryN(formatQuery);
412                         r = GetLastResult();
413                         if (IsFailed(r))
414                         {
415                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
416                                 return r;
417                         }
418                         count = 0;
419
420                         if (__pDbEnum != null)
421                         {
422                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
423                                 {
424                                         count++;
425                                 }
426                                 __pDbEnum->Reset();
427                         }
428                 }
429                 else
430                 {
431                         if (__isBeginTransaction == false && (queryType == L"INSERT" ))
432                         {
433                                 r = __pDataBase->BeginTransaction();
434                                 if (IsFailed(r))
435                                 {
436                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
437                                         return r;
438                                 }
439
440                                 __isBeginTransaction = true;
441                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
442                                 __pDbStatement->BindInt(0,bookmark.GetBookmarkType());
443                                 __pDbStatement->BindString(1,bookmark.GetParentId());
444                                 __pDbStatement->BindString(2,bookmarkUrl);
445                                 __pDbStatement->BindString(3,bookmarkTitle);
446                                 DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
447                                 pFormatter->ApplyPattern(L"yyyy-mm-dd hh:mm:ss");
448                                 String dateStr;
449                                 pFormatter->Format(dt, dateStr);
450                                 AppLog("DateStr: %ls", dateStr.GetPointer());
451                                 __pDbStatement->BindString(4,dateStr);
452                                 __pDbStatement->BindString(5,dateStr);
453
454                                 if(bookmark.GetFavIconBuffer() != null)
455                                 {
456                                         __pDbStatement->BindBlob(6,*(bookmark.GetFavIconBuffer()));
457                                 }
458                                 __pDbStatement->BindInt(7,bookmark.GetFavIconWidth());
459                                 __pDbStatement->BindInt(8,bookmark.GetFavIconHeight());
460
461                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
462
463                                 if (IsFailed(GetLastResult()))
464                                 {
465                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
466                                         return r;
467                                 }
468                                 else
469                                 {
470                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
471                                 }
472                                 delete pFormatter;
473                         }
474                         else if (__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
475                         {
476                                 r = __pDataBase->ExecuteSql(formatQuery, false);
477                                 if (IsFailed(r))
478                                 {
479                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
480                                         return r;
481                                 }
482                         }
483
484                         count = 0;
485                 }
486         }
487
488         return r;
489 }
490
491 result
492 PresentationModelBase::ExecuteHistoryQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
493 {
494
495         AppLogDebug("Query: %ls", formatQuery.GetPointer());
496
497         result r = E_FAILURE;
498         String queryType;
499         DateTime date;
500         int count;
501
502         String historyTitle = history.GetHistoryTitle();
503         historyTitle.Replace(L"'", L"\'");
504
505         String historyUrl = history.GetHistoryUrl();
506         historyUrl.Replace(L"'", L"\'");
507
508         GetCurrentDateTime(date);
509
510         if (__pDataBase == null)
511         {
512                 return E_INIT_FAILED;
513         }
514
515         if (__pDbEnum != null)
516         {
517                 delete __pDbEnum;
518                 __pDbEnum = null;
519         }
520
521         if (__pDbStatement != null)
522         {
523                 delete __pDbStatement;
524                 __pDbStatement = null;
525         }
526
527         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
528         if (r == E_SUCCESS)
529         {
530                 if (queryType == "SELECT")
531                 {
532                         __pDbEnum = __pDataBase->QueryN(formatQuery);
533                         r = GetLastResult();
534                         if (IsFailed(r))
535                         {
536                                 AppLogException("Failed Database::QueryN(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
537                                 return r;
538                         }
539                         count = 0;
540
541                         if (__pDbEnum != null)
542                         {
543                                 while (__pDbEnum->MoveNext() == E_SUCCESS)
544                                 {
545                                         count++;
546                                 }
547                                 __pDbEnum->Reset();
548                         }
549
550                 }
551                 else
552                 {
553                         if (__isBeginTransaction == false && (queryType == L"INSERT"))
554                         {
555                                 r = __pDataBase->BeginTransaction();
556                                 if (IsFailed(r))
557                                 {
558                                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
559                                         return r;
560                                 }
561
562                                 __isBeginTransaction = true;
563                                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
564                                 if(GetLastResult() != E_SUCCESS)
565                                 {
566                                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
567                                         return GetLastResult();
568                                 }
569                                 r = __pDbStatement->BindString(0,historyUrl);
570                                 AppLog("BindString result historyUrl %s",GetErrorMessage(r));
571                                 r = __pDbStatement->BindString(1,historyTitle);
572                                 AppLog("BindString result historyTitle %s historyTitle %ls",GetErrorMessage(r),historyTitle.GetPointer());
573
574                                 String visitedTime = GetStringFromDate(date);
575                                 /*DateTimeFormatter *pFormatter = DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT);
576                                 pFormatter->ApplyPattern(L"YYYY-MM-dd hh:mm:ss");
577                                 String dateStr;
578                                 pFormatter->Format(date, dateStr);
579                                 AppLog("DateStr: %ls", dateStr.GetPointer());*/
580                                 __pDbStatement->BindString(2, visitedTime);
581                                 if(history.GetFavIconBuffer() != null)
582                                 {
583                                         __pDbStatement->BindBlob(3,*(history.GetFavIconBuffer()));
584                                 }
585                                 __pDbStatement->BindInt(4,history.GetFavIconWidth());
586                                 __pDbStatement->BindInt(5,history.GetFavIconHeight());
587
588                                 __pDataBase->ExecuteStatementN(*__pDbStatement);
589
590                                 if (IsFailed(GetLastResult()))
591                                 {
592                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
593                                         return r;
594                                 }
595                                 else
596                                 {
597                                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
598                                 }
599                         }
600                         else if(__isBeginTransaction == false && (queryType == L"UPDATE" || queryType == L"DELETE"))
601                         {
602                                 r = __pDataBase->ExecuteSql(formatQuery, false);
603                                 if (IsFailed(r))
604                                 {
605                                         AppLogException("Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
606                                         return r;
607                                 }
608                         }
609                         count = 0;
610                 }
611         }
612
613         return r;
614 }
615
616 result PresentationModelBase::ExecuteUpdateHistoryFavIconQuery(const Tizen::Base::String& formatQuery, int& nRowCount, History& history)
617 {
618         result r = E_FAILURE;
619         String queryType;
620         int historyID;
621         r = Integer::Parse(history.GetHistoryId(), historyID);
622
623         if (__pDataBase == null)
624         {
625                 return E_INIT_FAILED;
626         }
627
628         if (__pDbEnum != null)
629         {
630                 delete __pDbEnum;
631                 __pDbEnum = null;
632         }
633
634         if (__pDbStatement != null)
635         {
636                 delete __pDbStatement;
637                 __pDbStatement = null;
638         }
639         r = formatQuery.SubString(0, QUERY_TYPE_LENGTH, queryType);
640
641         if (__isBeginTransaction == false )
642         {
643                 r = __pDataBase->BeginTransaction();
644                 if (IsFailed(r))
645                 {
646                         AppLogException("Failed Database::BeginTransaction() . (error: %s)", GetErrorMessage(r));
647                         return r;
648                 }
649
650                 __isBeginTransaction = true;
651                 __pDbStatement = __pDataBase->CreateStatementN(formatQuery);
652                 if(GetLastResult() != E_SUCCESS)
653                 {
654                         AppLog("CreateStatementN error: %s", GetErrorMessage(GetLastResult()));
655                         return GetLastResult();
656                 }
657                 __pDbStatement->BindBlob(0,*(history.GetFavIconBuffer()));
658                 __pDbStatement->BindInt(1,history.GetFavIconWidth());
659                 __pDbStatement->BindInt(2,history.GetFavIconHeight());
660                 __pDbStatement->BindInt(3,historyID);
661
662                 __pDataBase->ExecuteStatementN(*__pDbStatement);
663
664                 if (IsFailed(GetLastResult()))
665                 {
666                         AppLog("PresentationModelBase::ExecuteBookmarkQuery Failed Database::ExecuteSql(). (error: %s, query: %ls)", GetErrorMessage(r), formatQuery.GetPointer());
667                         return r;
668                 }
669                 else
670                 {
671                         AppLog("PresentationModelBase::ExecuteBookmarkQuery success database");
672                 }
673         }
674
675                 return r;
676 }
677
678 result
679 PresentationModelBase::DbIsNextRowPresent(bool& nextRowpresent)
680 {
681         result r = E_FAILURE;
682
683         if (null == __pDataBase || null == __pDbEnum)
684         {
685                 if (__pDbEnum == null)
686                 {
687                         AppLogException("PresentationModelBase : pdbenum is null");
688                 }
689                 if (__pDataBase == null)
690                 {
691                         AppLogException("PresentationModelBase : __pDataBase is null");
692                 }
693                 return E_OUT_OF_MEMORY;
694         }
695         r = __pDbEnum->MoveNext();
696         if (IsFailed(r))
697         {
698                 nextRowpresent = false;
699                 return r;
700         }
701         nextRowpresent = true;
702         return r;
703 }
704
705 result
706 PresentationModelBase::IsNullColumn(int index, bool& isColumNull)
707 {
708         if (null == __pDataBase || null == __pDbEnum)
709         {
710                 return E_DATABASE;
711         }
712         isColumNull = (__pDbEnum->GetColumnType(index) == DB_COLUMNTYPE_NULL ? true : false);
713
714         return E_SUCCESS;
715 }
716
717 result
718 PresentationModelBase::GetColumn(int index, String& stringValue)
719 {
720         result r = E_FAILURE;
721         DbColumnType columnType;
722
723         if (null == __pDataBase || null == __pDbEnum)
724         {
725                 return E_OUT_OF_MEMORY;
726         }
727         // Find column type
728         columnType = __pDbEnum->GetColumnType(index);
729
730         //if column type is null then assign empty string and return
731         if (columnType == DB_COLUMNTYPE_NULL)
732         {
733                 stringValue = "";
734                 return E_SUCCESS;
735         }
736         //Get String value
737         r = __pDbEnum->GetStringAt(index, stringValue);
738         if (IsFailed(r))
739         {
740                 AppLogDebug("GetStringAt: Failed");
741                 return r;
742         }
743         return r;
744 }
745
746 result
747 PresentationModelBase::GetColumn(int index, int& intValue)
748 {
749         result r = E_FAILURE;
750         DbColumnType columnType;
751
752         if (null == __pDataBase || null == __pDbEnum)
753         {
754                 return E_OUT_OF_MEMORY;
755         }
756         // Find column type
757         columnType = __pDbEnum->GetColumnType(index);
758
759         //if column type is null then assign empty string and return
760         if (columnType == DB_COLUMNTYPE_NULL)
761         {
762                 intValue = 0;
763                 return E_SUCCESS;
764         }
765         //Get String value
766         r = __pDbEnum->GetIntAt(index, intValue);
767         TryCatch(!IsFailed(r),,"GetIntAt:failed %s",GetErrorMessage(r));
768
769         CATCH:
770         return r;
771 }
772
773 result
774 PresentationModelBase::GetColumn(int index, DateTime& dateValue)
775 {
776         result r = E_FAILURE;
777         DbColumnType nType;
778
779         if (null == __pDataBase || null == __pDbEnum)
780         {
781                 AppLogDebug("__pDataBase or __pDbEnum is null");
782                 return E_OUT_OF_MEMORY;
783         }
784         // Find column type
785         nType = __pDbEnum->GetColumnType(index);
786         //if column type is null then assign empty string and return
787         if (nType == DB_COLUMNTYPE_NULL)
788         {
789                 return E_SUCCESS;
790         }
791         //Get String value
792         r = __pDbEnum->GetDateTimeAt(index, dateValue);
793         if (IsFailed(r))
794         {
795                 AppLogDebug("GetDateTimeAt: Failed");
796                 String temp;
797                 int currIndex = -1;
798                 int nextIndex = -1;
799
800                 int year = 0;
801                 int month = 0;
802                 int day = 0;
803                 int hour = 0;
804                 int min = 0;
805                 int sec = 0;
806
807                 int value = 0;
808                 String dbColoumn = "";
809                 __pDbEnum->GetStringAt(index, dbColoumn);
810                 dbColoumn.IndexOf("-", 0, currIndex);
811                 if(currIndex != -1)
812                 {
813                         dbColoumn.SubString(0, currIndex ,temp);
814                         Integer::Parse(temp, 10, year);
815                 }
816
817                 dbColoumn.IndexOf("-", currIndex + 1, nextIndex);
818                 if(currIndex != -1)
819                 {
820                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex - 1 ,temp);
821                         Integer::Parse(temp, 10, month);
822                 }
823                 currIndex = nextIndex;
824
825                 dbColoumn.IndexOf(" ", currIndex + 1, nextIndex);
826                 if(currIndex != -1)
827                 {
828                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
829                         Integer::Parse(temp, 10, day);
830                 }
831                 currIndex = nextIndex;
832
833                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
834                 if(currIndex != -1)
835                 {
836                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
837                         Integer::Parse(temp, 10, hour);
838                 }
839                 currIndex = nextIndex;
840
841                 dbColoumn.IndexOf(":", currIndex + 1, nextIndex);
842                 if(currIndex != -1)
843                 {
844                         dbColoumn.SubString(currIndex + 1, nextIndex - currIndex -1 ,temp);
845                         Integer::Parse(temp, 10, min);
846                 }
847                 currIndex = nextIndex;
848
849                 dbColoumn.SubString(currIndex + 1, temp);
850                 Integer::Parse(temp, 10, sec);
851
852                 dateValue.SetValue(year, month, day, hour, min, sec);
853                 return E_SUCCESS;
854         }
855
856         return r;
857 }
858
859 result
860 PresentationModelBase::GetColumn(int index, double& doubleValue)
861 {
862         result r = E_FAILURE;
863         DbColumnType columnType;
864
865         if (null == __pDataBase || null == __pDbEnum)
866         {
867                 return E_OUT_OF_MEMORY;
868         }
869         // Find column type
870         columnType = __pDbEnum->GetColumnType(index);
871
872         //if column type is null then assign empty string and return
873         if (columnType == DB_COLUMNTYPE_NULL)
874         {
875                 doubleValue = 0.0;
876                 return E_SUCCESS;
877         }
878         //Get String value
879         r = __pDbEnum->GetDoubleAt(index, doubleValue);
880         if (IsFailed(r))
881         {
882                 AppLogDebug("GetDateTimeAt: Failed");
883                 return r;
884         }
885
886         return r;
887 }
888
889 result
890 PresentationModelBase::GetColumn(int index,Tizen::Base::ByteBuffer& favIconBuffer)
891 {
892         result r = E_FAILURE;
893         DbColumnType columnType;
894
895         if (null == __pDataBase || null == __pDbEnum)
896         {
897                 return E_OUT_OF_MEMORY;
898         }
899         // Find column type
900         columnType = __pDbEnum->GetColumnType(index);
901
902         //if column type is null then assign empty string and return
903         if (columnType == DB_COLUMNTYPE_NULL)
904         {
905                 return E_SUCCESS;
906         }
907         favIconBuffer.Construct(__pDbEnum->GetColumnSize(index));
908         //Get String value
909         r = __pDbEnum->GetBlobAt(index, favIconBuffer);
910         AppLog("PresentationModelBase::GetColumn favicon result is %s", GetErrorMessage(r));
911
912
913         if (IsFailed(r))
914         {
915                 AppLogDebug("GetDateTimeAt: Failed");
916                 return r;
917         }
918
919         return r;
920 }
921
922 result
923 PresentationModelBase::RollbackDb()
924 {
925         result r = E_FAILURE;
926
927         if (__pDataBase != null && __isBeginTransaction == true)
928         {
929                 r = __pDataBase->RollbackTransaction();
930                 if (IsFailed(r))
931                 {
932                         return r;
933                 }
934                 __isBeginTransaction = false;
935         }
936         return r;
937 }
938
939 result
940 PresentationModelBase::CommitDb()
941 {
942
943         result r = E_FAILURE;
944
945         if (__pDataBase != null && __isBeginTransaction == true)
946         {
947                 r = __pDataBase->CommitTransaction();
948                 TryCatch(!IsFailed(r),,"PresentationModelBase::Commit Transaction:failed %s",GetErrorMessage(r));
949
950                 __isBeginTransaction = false;
951         }
952
953         CATCH:
954         return r;
955 }
956
957 result
958 PresentationModelBase::GetLastInsertedId(String& tableName, int& rowId)
959 {
960         String query;
961         result r = E_FAILURE;
962         int count = -1;
963         bool nextRowpresent = false;
964
965         query.Append(L"SELECT last_insert_rowid() FROM ");
966         query.Append(tableName);
967
968         r = PresentationModelBase::ExecuteQuery(query, count);
969         TryCatch(!IsFailed(r),,"PresentationModelBase::GetLastInsertedId query failed %s",GetErrorMessage(r));
970
971         r = PresentationModelBase::DbIsNextRowPresent(nextRowpresent);
972
973         if (nextRowpresent == true)
974         {
975                 r = PresentationModelBase::GetColumn(0, rowId);
976                 if (IsFailed(r))
977                 {
978                         AppLogDebug("PresentationModelBase::GetLstInsertedId -(%s)\n", GetErrorMessage(r));
979                         return r;
980                 }
981         }
982         CATCH: return r;
983 }
984
985 bool
986 PresentationModelBase::GetCurrentDateTime(DateTime& dt)
987 {
988         DateTime dtNow;
989         LocaleManager localManager;
990         result r = E_FAILURE;
991         TimeZone tz;
992
993         r = SystemTime::GetCurrentTime(dt);
994         return true;
995 }
996
997 String
998 PresentationModelBase::GetStringFromDate(DateTime& date)
999 {
1000         String dateFormat = L"";
1001         dateFormat.Append(date.GetYear());
1002         dateFormat.Append('-');
1003         if(date.GetMonth() < 10)
1004         {
1005                 dateFormat.Append('0');
1006         }
1007         dateFormat.Append(date.GetMonth());
1008         dateFormat.Append('-');
1009         if(date.GetDay() < 10)
1010         {
1011                 dateFormat.Append('0');
1012         }
1013         dateFormat.Append(date.GetDay());
1014         dateFormat.Append(' ');
1015         if(date.GetHour() < 10)
1016         {
1017                 dateFormat.Append('0');
1018         }
1019         dateFormat.Append(date.GetHour());
1020         dateFormat.Append(':');
1021         if(date.GetMinute() < 10)
1022         {
1023                 dateFormat.Append('0');
1024         }
1025         dateFormat.Append(date.GetMinute());
1026         dateFormat.Append(':');
1027         if(date.GetSecond() < 10)
1028         {
1029                 dateFormat.Append('0');
1030         }
1031         dateFormat.Append(date.GetSecond());
1032         return dateFormat;
1033 }