Added type column in Bookmark Table
[apps/osp/Internet.git] / src / IntBookmarkPresentationModel.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (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: IntBookmarkPresentationModel.cpp
20  *@brief: Used to define BookmarkPresentationModel
21  */
22
23 #include <cstdlib>
24 #include "IntBookmarkData.h"
25 #include "IntBookmarkPresentationModel.h"
26 #include "IntTypes.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base::Utility;
32 using namespace Tizen::Io;
33 using namespace Tizen::Locales;
34 using namespace Tizen::System;
35
36 extern const wchar_t* BOOKMARK_URL_TABLE;
37 //extern const wchar_t* BOOKMARK_FOLDER_TABLE;
38 extern const wchar_t* DEFAULT_VALUE_PARENTID ;
39
40 BookmarkPresentationModel* BookmarkPresentationModel::__pBookmarkPresentationModel = null;
41
42 BookmarkPresentationModel::BookmarkPresentationModel(void)
43 {
44
45 }
46
47 BookmarkPresentationModel::~BookmarkPresentationModel(void)
48 {
49
50 }
51
52 BookmarkPresentationModel::BookmarkPresentationModel(const BookmarkPresentationModel& settingModelObj)
53 {
54
55 }
56
57 BookmarkPresentationModel& BookmarkPresentationModel::operator=(const BookmarkPresentationModel& settingModelObj)
58 {
59         return *this;
60 }
61
62 result
63 BookmarkPresentationModel::Construct(void)
64 {
65         result r = E_SUCCESS;
66         DB_FILE_PATH = L"/opt/usr/dbspace/.internet_bookmark.db";
67
68         r = PresentationModelBase::Initialize();
69         TryCatch( !IsFailed(r),,"Failed to intitialize bookmarkPresentation Model %s",GetErrorMessage(r));
70     CATCH: return r;
71 }
72
73 void
74 BookmarkPresentationModel::CreateInstance(void)
75 {
76         if (__pBookmarkPresentationModel == null)
77                 __pBookmarkPresentationModel = new(std::nothrow) BookmarkPresentationModel();
78         result r = __pBookmarkPresentationModel->Construct();
79         if (IsFailed(r))
80         {
81                 delete __pBookmarkPresentationModel;
82                 __pBookmarkPresentationModel = null;
83                 return;
84         }
85     std::atexit(DestroyInstance);
86 }
87
88
89 void BookmarkPresentationModel::DestroyInstance(void)
90 {
91         if (__pBookmarkPresentationModel)
92         {
93                 delete __pBookmarkPresentationModel;
94                 __pBookmarkPresentationModel = null;
95         }
96 }
97
98
99 BookmarkPresentationModel*
100 BookmarkPresentationModel::GetInstance(void)
101 {
102         if(__pBookmarkPresentationModel == null)
103         {
104                 CreateInstance();
105         }
106         return __pBookmarkPresentationModel;
107 }
108
109 result
110 BookmarkPresentationModel::SaveFolder(BookmarkData& bookmark)
111 {
112         bookmark.SetParentId("-1");
113         SaveBookmark(bookmark);
114         /*
115         result r = E_FAILURE;
116         String bookmarkFolderTable(BOOKMARK_FOLDER_TABLE);
117         String query;
118         String columnNames;
119         int resultCount = -1;
120         int rowId = -1;
121         DateTime dt;
122         String bookmarkId;
123         String bookmarkTitle = bookmark.GetBookmarkTitle();
124         bookmarkTitle.Replace(L"'", L"''");
125
126         PresentationModelBase::GetCurrentDateTime(dt);
127         if (bookmark.GetBookmarkId() == "")
128         {
129                 columnNames.Append(L"TITLE, ");
130                 columnNames.Append(L"CREATED_TIME, ");
131                 columnNames.Append(L"MODIFIED_TIME");
132
133                 query.Append(L"INSERT INTO ");
134                 query.Append(bookmarkFolderTable);
135                 query.Append("(");
136                 query.Append(columnNames);
137                 query.Append(") ");
138                 query.Append("VALUES(");
139                 query.Append("'");
140                 query.Append(bookmarkTitle);
141                 query.Append("', ");
142                 query.Append("'");
143                 query.Append(dt.ToString());
144                 query.Append("', ");
145                 query.Append("'");
146                 query.Append(dt.ToString());
147                 query.Append("')");
148         }
149         else
150         {
151                 query.Append(L"UPDATE ");
152                 query.Append(bookmarkFolderTable);
153                 query.Append(" SET TITLE = ");
154                 query.Append("'");
155                 query.Append(bookmarkTitle);
156                 query.Append("'");
157                 query.Append(", MODIFIED_TIME = ");
158                 query.Append("'");
159                 query.Append(dt.ToString());
160                 query.Append("'");
161                 query.Append(" WHERE ID = ");
162                 query.Append(bookmark.GetBookmarkId());
163         }
164
165
166         r = PresentationModelBase::ExecuteQuery(query, resultCount);
167         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveFolder query failed  %s",GetErrorMessage(r));
168
169         r = PresentationModelBase::CommitDb();
170
171         r = GetLastInsertedId(bookmarkFolderTable, rowId);
172         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveFolder GetLastInsertedId failed %s",GetErrorMessage(r));
173
174         if (rowId < 0)
175                 return E_FAILURE;
176
177         bookmarkId.Append(rowId);
178
179         //Set the generated ID to Bookmark
180         bookmark.SetBookmarkId(bookmarkId);
181
182     CATCH: return r;
183 */}
184
185 result
186 BookmarkPresentationModel::SaveBookmark(BookmarkData& bookmark)
187 {
188         result r = E_FAILURE;
189         String bookmarkTable(BOOKMARK_URL_TABLE);
190         String query;
191         String columnNames;
192         String attachment;
193         int resultCount = -1;
194         int rowId = -1;
195         DateTime dt;
196         String bookmarkId;
197         String bookmarkTitle = bookmark.GetBookmarkTitle();
198         bookmarkTitle.Replace(L"'", L"''");
199         String bookmarkUrl = bookmark.GetUrl();
200         bookmarkUrl.Replace(L"'", L"''");
201
202
203         PresentationModelBase::GetCurrentDateTime(dt);
204         if (bookmark.GetBookmarkId() == "")
205         {
206                 columnNames.Append(L"TYPE, ");
207                 columnNames.Append(L"PARENT, ");
208                 columnNames.Append(L"ADDRESS, ");
209                 columnNames.Append(L"TITLE, ");
210                 columnNames.Append(L"CREATIONDATE, ");
211                 columnNames.Append(L"UPDATEDATE");
212                 //columnNames.Append(L"ICON_PATH, ");
213                 //columnNames.Append(L"FAVICON_ID");
214
215                 query.Append(L"INSERT INTO ");
216                 query.Append(bookmarkTable);
217                 query.Append("(");
218                 query.Append(columnNames);
219                 query.Append(") ");
220                 query.Append("VALUES(");
221                 query.Append(bookmark.GetBookmarkType());
222                 query.Append(", ");
223                 query.Append(bookmark.GetParentId());
224                 query.Append(", ");
225                 query.Append("'");
226                 query.Append(bookmarkUrl);
227                 query.Append("', ");
228                 query.Append("'");
229                 query.Append(bookmarkTitle);
230                 query.Append("', ");
231                 query.Append("'");
232                 query.Append(dt.ToString());
233                 query.Append("', ");
234                 query.Append("'");
235                 query.Append(dt.ToString());
236                 /*query.Append("', ");
237                 query.Append("'");
238                 query.Append(bookmark.GetIconPath());
239                 query.Append("'");
240                 query.Append(", ");
241                 query.Append("'");
242                 query.Append(bookmark.GetFaviconId());*/
243                 query.Append("'");
244                 query.Append(")");
245         }
246         else
247         {
248                 query.Append(L"UPDATE ");
249                 query.Append(bookmarkTable);
250                 query.Append(" SET PARENT = ");
251                 query.Append(bookmark.GetParentId());
252                 query.Append(", ADDRESS = ");
253                 query.Append("'");
254                 query.Append(bookmarkUrl);
255                 query.Append("'");
256                 query.Append(", TITLE = ");
257                 query.Append("'");
258                 query.Append(bookmarkTitle);
259                 query.Append("'");
260                 query.Append(", UPDATEDATE = ");
261                 query.Append("'");
262                 query.Append(dt.ToString());
263                 query.Append("'");
264                 query.Append(" WHERE ID = ");
265                 query.Append(bookmark.GetBookmarkId());
266                 /*query.Append(L"UPDATE ");
267                 query.Append(bookmarkTable);
268                 query.Append(" SET TITLE = ");
269                 query.Append("'");
270                 query.Append(bookmarkTitle);
271                 query.Append("'");
272                 query.Append(", URL = ");
273                 query.Append("'");
274                 query.Append(bookmarkUrl);
275                 query.Append("'");
276                 query.Append(", PARENT_BOOKMARK_ID = ");
277                 query.Append(bookmark.GetParentId());
278                 query.Append(", MODIFIED_TIME = ");
279                 query.Append("'");
280                 query.Append(dt.ToString());
281                 query.Append("'");
282                 query.Append(", ICON_PATH = ");
283                 query.Append("'");
284                 query.Append(bookmark.GetIconPath());
285                 query.Append("'");
286                 query.Append(", FAVICON_ID = ");
287                 query.Append("'");
288                 query.Append(bookmark.GetFaviconId());
289                 query.Append("'");
290                 query.Append(" WHERE ID = ");
291                 query.Append(bookmark.GetBookmarkId());*/
292         }
293
294         r = PresentationModelBase::ExecuteQuery(query, resultCount);
295         AppLog("BookmarkPresentationModel::SaveBookmark query is %S",query.GetPointer());
296
297         if (r == E_SUCCESS)
298         {
299                 AppLog("BookmarkPresentationModel::SaveBookmark query is success");
300         }
301         else
302         {
303                 AppLog("BookmarkPresentationModel::SaveBookmark query is failure");
304         }
305
306         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveBookmark query failed  %s",GetErrorMessage(r));
307
308         r = PresentationModelBase::CommitDb();
309
310         r = GetLastInsertedId(bookmarkTable, rowId);
311         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::SaveBookmark GetLastInsertedId failed %s",GetErrorMessage(r));
312
313         if (rowId < 0)
314                 return E_FAILURE;
315
316         bookmarkId.Append(rowId);
317
318         //Set the generated ID to Bookmark
319         bookmark.SetBookmarkId(bookmarkId);
320
321     CATCH: return r;
322 }
323
324 result
325 BookmarkPresentationModel::DeleteFolder(int folderId)
326 {
327         result r = E_SUCCESS;
328         String query;
329         String bookmarkTable(BOOKMARK_URL_TABLE);
330         int resultCount = -1;
331
332         if (folderId < 1)
333         {
334                 return E_INVALID_ARG;
335         }
336
337         //strQuery.Format(MAX_DB_QUERY_SIZE, L"DELETE FROM %ls WHERE ID = %d", strBookmarkTable.GetPointer(), bookmarkId);
338         query.Append(L"DELETE FROM ");
339         query.Append(bookmarkTable);
340         query.Append(" WHERE ID = ");
341         query.Append(folderId);
342
343         r = BookmarkPresentationModel::ExecuteQuery(query, resultCount);
344         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteBookmark query failed  %s",GetErrorMessage(r));
345         r = BookmarkPresentationModel::CommitDb();
346         DeleteBookmarkByFolderId(folderId);
347
348         CATCH: return r;
349 }
350
351 result
352 BookmarkPresentationModel::DeleteBookmark(int bookmarkId)
353 {
354         result r = E_SUCCESS;
355         String query;
356         String bookmarkTable(BOOKMARK_URL_TABLE);
357         int resultCount = -1;
358
359         if (bookmarkId < 1)
360         {
361                 return E_INVALID_ARG;
362         }
363
364         //strQuery.Format(MAX_DB_QUERY_SIZE, L"DELETE FROM %ls WHERE ID = %d", strBookmarkTable.GetPointer(), bookmarkId);
365         query.Append(L"DELETE FROM ");
366         query.Append(bookmarkTable);
367         query.Append(" WHERE ID = ");
368         query.Append(bookmarkId);
369
370         r = BookmarkPresentationModel::ExecuteQuery(query, resultCount);
371         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteBookmark query failed  %s",GetErrorMessage(r));
372         r = BookmarkPresentationModel::CommitDb();
373
374         CATCH: return r;
375 }
376
377 result
378 BookmarkPresentationModel::DeleteBookmarkByFolderId(int folderId)
379 {
380         result r = E_SUCCESS;
381         String query;
382         String bookmarkTable(BOOKMARK_URL_TABLE);
383         int resultCount = -1;
384
385         if (folderId < 1)
386         {
387                 return E_INVALID_ARG;
388         }
389
390         //strQuery.Format(MAX_DB_QUERY_SIZE, L"DELETE FROM %ls WHERE ID = %d", strBookmarkTable.GetPointer(), bookmarkId);
391         query.Append(L"DELETE FROM ");
392         query.Append(bookmarkTable);
393         query.Append(" WHERE PARENT = ");
394         query.Append(folderId);
395
396         r = BookmarkPresentationModel::ExecuteQuery(query, resultCount);
397         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteBookmark query failed  %s",GetErrorMessage(r));
398         r = BookmarkPresentationModel::CommitDb();
399
400         CATCH: return r;
401 }
402
403
404 result
405 BookmarkPresentationModel::GetFolderBookmarkCount(const String& parentID, int& Count)
406 {
407         result r = E_SUCCESS;
408         int bookmarkCount = 0;
409         int folderCount = 0;
410         r = GetBookmarkCount(parentID,bookmarkCount);
411         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetBookmarkCount query failed  %s",GetErrorMessage(r));
412
413         r = GetFolderCount(parentID,folderCount);
414         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetBookmarkCount query failed  %s",GetErrorMessage(r));
415         Count = folderCount+ bookmarkCount;
416         return E_SUCCESS;
417         CATCH:
418         return r;
419 }
420
421 result
422 BookmarkPresentationModel::GetBookmarkCount(const String& parentID, int& bookmarkCount)
423 {
424         int count = -1;
425         int intVal = -1;
426         String query;
427         String bookmarkTable(BOOKMARK_URL_TABLE);
428         result r = E_FAILURE;
429         bool nextRowPresent = false;
430
431         //query.Format(MAX_DB_QUERY_SIZE, L"SELECT COUNT(ID) FROM %ls WHERE PARENT_ID = %ls", strTable.GetPointer(),notebookId.GetPointer());
432         query.Append(L"SELECT COUNT(ID) FROM ");
433         query.Append(bookmarkTable);
434         query.Append(" WHERE PARENT = ");
435         query.Append(parentID);
436         query.Append(" AND ADDRESS != ''");
437
438
439         r = PresentationModelBase::ExecuteQuery(query, count);
440         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetBookmarkCount query failed  %s",GetErrorMessage(r));
441         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
442         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DbIsNextRowPresent query failed  %s",GetErrorMessage(r));
443         if (nextRowPresent == true)
444         {
445                 r = PresentationModelBase::GetColumn(0, intVal);
446                 if (IsFailed(r))
447                 {
448                         AppLogDebug("Error:BookmarkPresentationModel::GetColumn:%s", GetErrorMessage(r));
449                         return r;
450                 }
451         }
452         bookmarkCount = intVal;
453         AppLog("Count:%d", bookmarkCount);
454
455         CATCH: return r;
456 }
457
458 result
459 BookmarkPresentationModel::GetFolderCount(const String& parentID, int& folderCount)
460 {
461
462         int count = -1;
463         int intVal = -1;
464         String query;
465         String bookmarkTable(BOOKMARK_URL_TABLE);
466         result r = E_FAILURE;
467         bool nextRowPresent = false;
468
469         //query.Format(MAX_DB_QUERY_SIZE, L"SELECT COUNT(ID) FROM %ls WHERE PARENT_ID = %ls", strTable.GetPointer(),notebookId.GetPointer());
470         query.Append(L"SELECT COUNT(ID) FROM ");
471         query.Append(bookmarkTable);
472         query.Append(" WHERE PARENT = ");
473         query.Append(parentID);
474         query.Append(" AND ADDRESS = ''");
475
476         r = PresentationModelBase::ExecuteQuery(query, count);
477         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetBookmarkCount query failed  %s",GetErrorMessage(r));
478         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
479         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DbIsNextRowPresent query failed  %s",GetErrorMessage(r));
480         if (nextRowPresent == true)
481         {
482                 r = PresentationModelBase::GetColumn(0, intVal);
483                 if (IsFailed(r))
484                 {
485                         AppLogDebug("Error:BookmarkPresentationModel::GetColumn:%s", GetErrorMessage(r));
486                         return r;
487                 }
488         }
489         folderCount = intVal;
490         AppLog("Count:%d", folderCount);
491
492         CATCH: return r;
493
494
495         /*
496         int count = -1;
497         int intVal = -1;
498         String query;
499         String bookmarkFolderTable(BOOKMARK_FOLDER_TABLE);
500         result r = E_FAILURE;
501         bool nextRowPresent = false;
502
503         if (parentID.CompareTo(DEFAULT_VALUE_PARENTID) != 0)
504         {
505                 folderCount = 0;
506                 return E_SUCCESS;
507         }
508         query.Append(L"SELECT COUNT(ID) FROM ");
509         query.Append(bookmarkFolderTable);
510
511         r = PresentationModelBase::ExecuteQuery(query, count);
512         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetFolderCount query failed  %s",GetErrorMessage(r));
513
514         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
515
516         if (nextRowPresent == true)
517         {
518                 r = PresentationModelBase::GetColumn(0, intVal);
519         }
520         folderCount = intVal;
521
522         CATCH: return r;
523 */}
524
525 result
526 BookmarkPresentationModel::GetFoldersBookmarks(const String& parentID, int startIndex, int limit, ArrayList& pBookmarkList)
527 {
528         ArrayList arrayList;
529         arrayList.Construct();
530         GetFolder(parentID,startIndex,limit,arrayList);
531         for(int index = arrayList.GetCount()-1; index >= 0; index--)
532         {
533                 pBookmarkList.Add(*arrayList.GetAt(index));
534         }
535         arrayList.RemoveAll(false);
536         GetBookmark(parentID,startIndex,limit,arrayList);
537         for(int index = arrayList.GetCount()-1; index >= 0; index--)
538         {
539                 pBookmarkList.Add(*arrayList.GetAt(index));
540         }
541         return E_SUCCESS;
542 }
543
544 result
545 BookmarkPresentationModel::GetBookmark(const String& parentID, int startIndex, int limit, ArrayList& pBookmarkList)
546 {
547         int count = -1;
548         String query;
549         String bookmarkTable(BOOKMARK_URL_TABLE);
550         result r = E_FAILURE;
551
552         query.Append(L"SELECT * FROM ");
553         query.Append(bookmarkTable);
554         query.Append(" WHERE PARENT = ");
555         query.Append(parentID);
556         query.Append(" AND ADDRESS != ''");
557
558         //Append LIMIT
559         if (limit > 0)
560         {
561                 query.Append(" LIMIT ");
562                 query.Append(limit);
563
564                 //Append OFFESET
565                 if (startIndex >= 0)
566                 {
567                         query.Append(" OFFSET ");
568                         query.Append(startIndex);
569                 }
570         }
571         r = PresentationModelBase::ExecuteQuery(query, count);
572         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetBookmark query failed  %s",GetErrorMessage(r));
573
574         r = CreateBookmarkList(count, pBookmarkList);
575         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::CreateBookmarkList failed  %s",GetErrorMessage(r));
576
577         CATCH: return r;
578 }
579
580 result
581 BookmarkPresentationModel::GetFolder(const String& parentID, int startIndex, int limit, ArrayList& folderList)
582 {
583         int count = -1;
584         String query;
585         String bookmarkTable(BOOKMARK_URL_TABLE);
586         result r = E_FAILURE;
587
588         if (parentID.CompareTo(DEFAULT_VALUE_PARENTID ) != 0)
589         {
590                 return E_SUCCESS;
591         }
592         query.Append(L"SELECT * FROM ");
593         query.Append(bookmarkTable);
594         query.Append(" WHERE ADDRESS = ''");
595
596         //Append LIMIT
597         if (limit > 0)
598         {
599                 query.Append(" LIMIT ");
600                 query.Append(limit);
601
602                 //Append OFFESET
603                 if (startIndex >= 0)
604                 {
605                         query.Append(" OFFSET ");
606                         query.Append(startIndex);
607                 }
608         }
609
610         r = PresentationModelBase::ExecuteQuery(query, count);
611         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetFolder query failed  %s",GetErrorMessage(r));
612
613         r = CreateFolderList(count, folderList);
614         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::CreateBookmarkList failed  %s",GetErrorMessage(r));
615
616         CATCH: return r;
617 }
618
619 result
620 BookmarkPresentationModel::GetSearchFolderBookmarkCount(int& searchBookmarkCount, String& text)
621 {
622         int searchUrlCount = 0;
623         int searchFolderCount = 0;
624         GetSearchBookmarkCount(searchUrlCount,text);
625         GetSearchFolderCount(searchFolderCount,text);
626         searchBookmarkCount = searchUrlCount + searchFolderCount;
627         return E_SUCCESS;
628 }
629
630 result
631 BookmarkPresentationModel::GetSearchBookmarkCount(int& searchBookmarkCount, String& text)
632 {
633
634         int count = -1;
635         int intVal = -1;
636         String query;
637         String bookmarkTable(BOOKMARK_URL_TABLE);
638         result r = E_FAILURE;
639         bool nextRowPresent = false;
640         text.Replace(L"'", L"''");
641
642         query.Append(L"SELECT COUNT(ID) FROM ");
643         query.Append(bookmarkTable);
644         query.Append(L" WHERE (TITLE LIKE '%");
645         query.Append(text);
646         query.Append("%'");
647         query.Append(" OR ");
648         query.Append(L"ADDRESS LIKE '%");
649         query.Append(text);
650         query.Append("%')");
651         query.Append(" AND ADDRESS != ''");
652
653         r = BookmarkPresentationModel::ExecuteQuery(query, count);
654         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchBookmarkCount query failed  %s",GetErrorMessage(r));
655
656         r = BookmarkPresentationModel::DbIsNextRowPresent(nextRowPresent);
657
658         if (nextRowPresent == true)
659         {
660                 r = BookmarkPresentationModel::GetColumn(0, intVal);
661                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetColumn failed  %s",GetErrorMessage(r));
662         }
663         searchBookmarkCount = intVal;
664
665         CATCH: return r;
666 }
667
668
669 result
670 BookmarkPresentationModel::GetSearchFolderCount(int& searchFolderCount, String& text)
671 {
672
673         int count = -1;
674         int intVal = -1;
675         String query;
676         String bookmarkTable(BOOKMARK_URL_TABLE);
677         result r = E_FAILURE;
678         bool nextRowPresent = false;
679         text.Replace(L"'", L"''");
680
681         query.Append(L"SELECT COUNT(ID) FROM ");
682         query.Append(bookmarkTable);
683         query.Append(L" WHERE TITLE LIKE '%");
684         query.Append(text);
685         query.Append("%'");
686         query.Append(" AND ADDRESS = ''");
687
688         r = BookmarkPresentationModel::ExecuteQuery(query, count);
689         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchBookmarkCount query failed  %s",GetErrorMessage(r));
690
691         r = BookmarkPresentationModel::DbIsNextRowPresent(nextRowPresent);
692
693         if (nextRowPresent == true)
694         {
695                 r = BookmarkPresentationModel::GetColumn(0, intVal);
696                 TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetColumn failed  %s",GetErrorMessage(r));
697         }
698         searchFolderCount = intVal;
699
700         CATCH: return r;
701 }
702
703
704 result
705 BookmarkPresentationModel::GetSearchFoldersBookmarks(int startIndex, int limit, ArrayList& pFolderList, String& text)
706 {
707         ArrayList arrayList;
708         arrayList.Construct();
709         GetSearchFolder(startIndex, limit, arrayList,text);
710         for(int index = arrayList.GetCount()-1; index >= 0; index--)
711         {
712                 pFolderList.Add(*arrayList.GetAt(index));
713         }
714         arrayList.RemoveAll(false);
715         GetSearchBookmark(startIndex, limit, arrayList,text);
716         for(int index = arrayList.GetCount()-1; index >= 0; index--)
717         {
718                 pFolderList.Add(*arrayList.GetAt(index));
719         }
720         return E_SUCCESS;
721 }
722
723 result
724 BookmarkPresentationModel::GetSearchBookmark(int startIndex, int limit, ArrayList& pBookmarkList, String& text)
725 {
726         int count = -1;
727         String query;
728         String bookmarkTable(BOOKMARK_URL_TABLE);
729         result r = E_FAILURE;
730
731         text.Replace(L"'", L"''");
732         query.Append(L"SELECT * FROM ");
733         query.Append(bookmarkTable);
734         query.Append(L" WHERE (TITLE LIKE '%");
735         query.Append(text);
736         query.Append("%')");
737         query.Append(" OR ");
738         query.Append(L"(ADDRESS LIKE '%");
739         query.Append(text);
740         query.Append("%')");
741         query.Append(" AND ADDRESS != ''");
742         //Append LIMIT
743         if (limit > 0)
744         {
745                 query.Append(" LIMIT ");
746                 query.Append(limit);
747
748                 //Append OFFESET
749                 if (startIndex >= 0)
750                 {
751                         query.Append(" OFFSET ");
752                         query.Append(startIndex);
753                 }
754         }
755
756         r = BookmarkPresentationModel::ExecuteQuery(query, count);
757         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchBookmark query failed  %s",GetErrorMessage(r));
758
759         r = CreateBookmarkList(count, pBookmarkList);
760         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::CreateBookmarkList failed  %s",GetErrorMessage(r));
761
762         CATCH: return r;
763 }
764
765 result
766 BookmarkPresentationModel::GetSearchFolder(int startIndex, int limit, ArrayList& pFolderList, String& text)
767 {
768         int count = -1;
769         String query;
770         String bookmarkTable(BOOKMARK_URL_TABLE);
771         result r = E_FAILURE;
772         text.Replace(L"'", L"''");
773
774         query.Append(L"SELECT * FROM ");
775         query.Append(bookmarkTable);
776         query.Append(L" WHERE TITLE LIKE '%");
777         query.Append(text);
778         //query.Append("%')");
779         query.Append("%'");
780         query.Append(" AND ADDRESS = ''");
781
782         //Append LIMIT
783         if (limit > 0)
784         {
785                 query.Append(" LIMIT ");
786                 query.Append(limit);
787
788                 //Append OFFESET
789                 if (startIndex >= 0)
790                 {
791                         query.Append(" OFFSET ");
792                         query.Append(startIndex);
793                 }
794         }
795
796         r = BookmarkPresentationModel::ExecuteQuery(query, count);
797         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::GetSearchBookmark query failed  %s",GetErrorMessage(r));
798
799         r = CreateFolderList(count, pFolderList);
800         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::CreateBookmarkList failed  %s",GetErrorMessage(r));
801
802         CATCH: return r;
803 }
804
805 result
806 BookmarkPresentationModel::CreateFolderList(int bookmarkCount, ArrayList& folderList)
807 {
808         BookmarkData* pBookmark = null;
809         int intVal = -1;
810         String value;
811         DateTime dateValue;
812         result r = E_FAILURE;
813         String bookmarkId;
814         String parentId;
815         String faviconId;
816         DateTime createdTime;
817         DateTime modifiedTime;
818         String iconPath;
819         bool nextRowPresent = false;
820
821         if (bookmarkCount < 1)
822         {
823                 return E_SUCCESS;
824         }
825         for (int l_bookmarkCount = 0; l_bookmarkCount < bookmarkCount; l_bookmarkCount++)
826         {
827                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
828                 if (IsFailed(r))
829                         return r;
830                 if (nextRowPresent == true)
831                 {
832                         pBookmark = new(std::nothrow) BookmarkData;
833
834                         if (pBookmark)
835                         {
836                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
837                                 {
838
839                                         switch (columnCount)
840                                         {
841                                         case BOOKMARK_ID:
842                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
843                                                 if (!IsFailed(r))
844                                                 {
845                                                         if (intVal > 0)
846                                                         {
847                                                                 bookmarkId.Clear();
848                                                                 bookmarkId.Append(intVal);
849                                                                 if (bookmarkId.GetLength() > 0)
850                                                                         pBookmark->SetBookmarkId(bookmarkId);
851                                                         }
852
853                                                 }
854                                                 break;
855                                         case BOOKMARK_PARENT:
856                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
857                                                 if (!IsFailed(r))
858                                                 {
859                                                         if (intVal > 0)
860                                                         {
861                                                                 parentId.Clear();
862                                                                 parentId.Append(intVal);
863                                                                 if (parentId.GetLength() > 0)
864                                                                         pBookmark->SetParentId(parentId);
865                                                         }
866                                                 }
867                                                 break;
868                                         case BOOKMARK_ADDRESS:
869                                                 r = PresentationModelBase::GetColumn(columnCount, value);
870                                                 if (!IsFailed(r))
871                                                 {
872                                                         pBookmark->SetUrl(value);
873                                                 }
874                                                 break;
875                                         case BOOKMARK_TITLE:
876                                                 r = PresentationModelBase::GetColumn(columnCount, value);
877                                                 if (!IsFailed(r))
878                                                 {
879                                                         pBookmark->SetBookmarkTitle(value);
880                                                 }
881                                                 break;
882                                         case BOOKMARK_CREATIONDATE:
883                                                 r = PresentationModelBase::GetColumn(columnCount, dateValue);
884                                                 if (!IsFailed(r))
885                                                 {
886                                                         createdTime = dateValue;
887                                                         pBookmark->SetCreatedTime(createdTime);
888                                                 }
889                                                 break;
890
891                                         case BOOKMARK_UPDATEDATA:
892                                                 r = PresentationModelBase::GetColumn(columnCount, dateValue);
893                                                 if (!IsFailed(r))
894                                                 {
895                                                         modifiedTime = dateValue;
896                                                         pBookmark->SetModifiedTime(modifiedTime);
897                                                 }
898                                                 break;
899
900                                                 /*case 6:
901                                                 r = PresentationModelBase::GetColumn(columnCount, value);
902                                                 if (!IsFailed(r))
903                                                 {
904                                                         iconPath = value;
905                                                         pBookmark->SetIconPath(iconPath);
906                                                 }
907                                                 break;
908                                         case 7:
909
910                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
911                                                 if (!IsFailed(r))
912                                                 {
913                                                         if (intVal >= 0)
914                                                         {
915                                                                 faviconId.Clear();
916                                                                 faviconId.Append(intVal);
917                                                                 if (faviconId.GetLength() > 0)
918                                                                 {
919                                                                         pBookmark->SetFaviconId(faviconId);
920                                                                 }
921                                                         }
922                                                 }
923                                                 break;*/
924
925                                         default:
926                                                 break;
927                                         }
928
929                                 }
930
931                                 r = folderList.Add(*pBookmark);
932                                 if (IsFailed(r))
933                                 {
934                                         delete pBookmark;
935                                         return r;
936                                 }
937                         }
938                 }
939         }
940         return r;
941 }
942
943 result
944 BookmarkPresentationModel::CreateBookmarkList(int bookmarkCount, ArrayList& bookmarkList)
945 {
946         BookmarkData* pBookmark = null;
947         int intVal = -1;
948         String value;
949         DateTime dateValue;
950         result r = E_FAILURE;
951         String bookmarkId;
952         String parentId;
953         String faviconId;
954         DateTime createdTime;
955         DateTime modifiedTime;
956         String iconPath;
957         bool nextRowPresent = false;
958
959         if (bookmarkCount < 1)
960         {
961                 return E_SUCCESS;
962         }
963         for (int l_bookmarkCount = 0; l_bookmarkCount < bookmarkCount; l_bookmarkCount++)
964         {
965                 r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
966                 if (IsFailed(r))
967                         return r;
968                 if (nextRowPresent == true)
969                 {
970                         pBookmark = new(std::nothrow) BookmarkData;
971                         if (pBookmark)
972                         {
973                                 for (int columnCount = 0; columnCount < MAX_NOTE_TABLE_COLUMN; columnCount++)
974                                 {
975
976                                         switch (columnCount)
977                                         {
978                                         case BOOKMARK_ID:
979                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
980                                                 if (!IsFailed(r))
981                                                 {
982                                                         if (intVal > 0)
983                                                         {
984                                                                 bookmarkId.Clear();
985                                                                 bookmarkId.Append(intVal);
986                                                                 if (bookmarkId.GetLength() > 0)
987                                                                         pBookmark->SetBookmarkId(bookmarkId);
988                                                         }
989
990                                                 }
991                                                 break;
992                                         case BOOKMARK_PARENT:
993                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
994                                                 if (!IsFailed(r))
995                                                 {
996                                                         if (intVal > 0)
997                                                         {
998                                                                 parentId.Clear();
999                                                                 parentId.Append(intVal);
1000                                                                 if (parentId.GetLength() > 0)
1001                                                                         pBookmark->SetParentId(parentId);
1002                                                         }
1003                                                 }
1004                                                 break;
1005                                         case BOOKMARK_ADDRESS:
1006                                                 r = PresentationModelBase::GetColumn(columnCount, value);
1007                                                 if (!IsFailed(r))
1008                                                 {
1009                                                         pBookmark->SetUrl(value);
1010                                                 }
1011                                                 break;
1012                                         case BOOKMARK_TITLE:
1013                                                 r = PresentationModelBase::GetColumn(columnCount, value);
1014                                                 if (!IsFailed(r))
1015                                                 {
1016                                                         pBookmark->SetBookmarkTitle(value);
1017                                                 }
1018                                                 break;
1019                                         case BOOKMARK_CREATIONDATE:
1020                                                 r = PresentationModelBase::GetColumn(columnCount, dateValue);
1021                                                 if (!IsFailed(r))
1022                                                 {
1023                                                         createdTime = dateValue;
1024                                                         pBookmark->SetCreatedTime(createdTime);
1025                                                 }
1026                                                 break;
1027
1028                                         case BOOKMARK_UPDATEDATA:
1029                                                 r = PresentationModelBase::GetColumn(columnCount, dateValue);
1030                                                 if (!IsFailed(r))
1031                                                 {
1032                                                         modifiedTime = dateValue;
1033                                                         pBookmark->SetModifiedTime(modifiedTime);
1034                                                 }
1035                                                 break;
1036
1037                                                 /*case 6:
1038                                                 r = PresentationModelBase::GetColumn(columnCount, value);
1039                                                 if (!IsFailed(r))
1040                                                 {
1041                                                         iconPath = value;
1042                                                         pBookmark->SetIconPath(iconPath);
1043                                                 }
1044                                                 break;
1045                                         case 7:
1046
1047                                                 r = PresentationModelBase::GetColumn(columnCount, intVal);
1048                                                 if (!IsFailed(r))
1049                                                 {
1050                                                         if (intVal >= 0)
1051                                                         {
1052                                                                 faviconId.Clear();
1053                                                                 faviconId.Append(intVal);
1054                                                                 if (faviconId.GetLength() > 0)
1055                                                                 {
1056                                                                         pBookmark->SetFaviconId(faviconId);
1057                                                                 }
1058                                                         }
1059                                                 }
1060                                                 break;*/
1061
1062                                         default:
1063                                                 break;
1064                                         }
1065
1066                                 }
1067
1068                                 r = bookmarkList.Add(*pBookmark);
1069                                 if (IsFailed(r))
1070                                 {
1071                                         delete pBookmark;
1072                                         return r;
1073                                 }
1074                         }
1075                 }
1076         }
1077         return r;
1078 }
1079
1080 result
1081 BookmarkPresentationModel::DoesBookmarkExist(const Tizen::Base::String& Url, bool &exist, const String& parentId, bool checkParentId)
1082 {
1083         int bookmarkCount = -1;
1084         int intVal = -1;
1085         String query;
1086         String bookmarkTable(BOOKMARK_URL_TABLE);
1087         result r = E_FAILURE;
1088         bool nextRowPresent = false;
1089         int count = 0;
1090
1091         query.Append(L"SELECT COUNT(ID) FROM ");
1092         query.Append(bookmarkTable);
1093         query.Append(" WHERE ADDRESS = '");
1094         query.Append(Url);
1095         if(checkParentId == true)
1096         {
1097                 query.Append(L"' AND PARENT = '");
1098                 query.Append(parentId);
1099         }
1100         query.Append("'");
1101
1102         AppLog("BookmarkPresentationModel::DoesBookmarkExist query is %S",query.GetPointer());
1103
1104         r = PresentationModelBase::ExecuteQuery(query, bookmarkCount);
1105         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DoesBookmarkExist query failed  %s",GetErrorMessage(r));
1106
1107         r = PresentationModelBase::DbIsNextRowPresent(nextRowPresent);
1108
1109         if (nextRowPresent == true)
1110         {
1111                 r = PresentationModelBase::GetColumn(0, intVal);
1112         }
1113         count = intVal;
1114
1115         if (count == 0)
1116         {
1117                 exist = false;
1118         }
1119         else
1120         {
1121                 exist = true ;
1122         }
1123
1124         CATCH: return r;
1125 }
1126
1127 result
1128 BookmarkPresentationModel::DeleteBookmark(const Tizen::Base::String& url)
1129 {
1130         result r = E_SUCCESS;
1131         String query;
1132         String bookmarkTable(BOOKMARK_URL_TABLE);
1133         int resultCount = -1;
1134
1135         query.Append(L"DELETE FROM ");
1136         query.Append(bookmarkTable);
1137         query.Append(" WHERE ADDRESS = '");
1138         query.Append(url);
1139         query.Append(L"'");
1140
1141         r = BookmarkPresentationModel::ExecuteQuery(query, resultCount);
1142         TryCatch( r == E_SUCCESS,,"BookmarkPresentationModel::DeleteBookmark query failed  %s",GetErrorMessage(r));
1143
1144         r = BookmarkPresentationModel::CommitDb();
1145
1146         CATCH: return r;
1147 }