FreeRDP
ManualBookmarkGateway.java
1 /*
2  Manual bookmarks database gateway
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.services;
12 
13 import android.content.ContentValues;
14 import android.database.Cursor;
15 import android.database.sqlite.SQLiteOpenHelper;
16 
17 import com.freerdp.freerdpcore.domain.BookmarkBase;
18 import com.freerdp.freerdpcore.domain.ManualBookmark;
19 
20 import java.util.ArrayList;
21 
23 {
24 
25  public ManualBookmarkGateway(SQLiteOpenHelper bookmarkDB)
26  {
27  super(bookmarkDB);
28  }
29 
30  @Override protected BookmarkBase createBookmark()
31  {
32  return new ManualBookmark();
33  }
34 
35  @Override protected String getBookmarkTableName()
36  {
37  return BookmarkDB.DB_TABLE_BOOKMARK;
38  }
39 
40  @Override
41  protected void addBookmarkSpecificColumns(BookmarkBase bookmark, ContentValues columns)
42  {
43  ManualBookmark bm = (ManualBookmark)bookmark;
44  columns.put(BookmarkDB.DB_KEY_BOOKMARK_HOSTNAME, bm.getHostname());
45  columns.put(BookmarkDB.DB_KEY_BOOKMARK_PORT, bm.getPort());
46 
47  // gateway settings
48  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_ENABLE, bm.getEnableGatewaySettings());
49  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_HOSTNAME, bm.getGatewaySettings().getHostname());
50  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_PORT, bm.getGatewaySettings().getPort());
51  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_USERNAME, bm.getGatewaySettings().getUsername());
52  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_PASSWORD, bm.getGatewaySettings().getPassword());
53  columns.put(BookmarkDB.DB_KEY_BOOKMARK_GW_DOMAIN, bm.getGatewaySettings().getDomain());
54  }
55 
56  @Override protected void addBookmarkSpecificColumns(ArrayList<String> columns)
57  {
58  columns.add(BookmarkDB.DB_KEY_BOOKMARK_HOSTNAME);
59  columns.add(BookmarkDB.DB_KEY_BOOKMARK_PORT);
60  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_ENABLE);
61  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_HOSTNAME);
62  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_PORT);
63  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_USERNAME);
64  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_PASSWORD);
65  columns.add(BookmarkDB.DB_KEY_BOOKMARK_GW_DOMAIN);
66  }
67 
68  @Override protected void readBookmarkSpecificColumns(BookmarkBase bookmark, Cursor cursor)
69  {
70  ManualBookmark bm = (ManualBookmark)bookmark;
71  bm.setHostname(
72  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_HOSTNAME)));
73  bm.setPort(cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_PORT)));
74 
75  bm.setEnableGatewaySettings(
76  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_ENABLE)) != 0);
77  readGatewaySettings(bm, cursor);
78  }
79 
80  public BookmarkBase findByLabelOrHostname(String pattern)
81  {
82  if (pattern.length() == 0)
83  return null;
84 
85  Cursor cursor =
86  queryBookmarks(BookmarkDB.DB_KEY_BOOKMARK_LABEL + " = '" + pattern + "' OR " +
87  BookmarkDB.DB_KEY_BOOKMARK_HOSTNAME + " = '" + pattern + "'",
88  BookmarkDB.DB_KEY_BOOKMARK_LABEL);
89  BookmarkBase bookmark = null;
90  if (cursor.moveToFirst() && (cursor.getCount() > 0))
91  bookmark = getBookmarkFromCursor(cursor);
92 
93  cursor.close();
94  return bookmark;
95  }
96 
97  public ArrayList<BookmarkBase> findByLabelOrHostnameLike(String pattern)
98  {
99  Cursor cursor =
100  queryBookmarks(BookmarkDB.DB_KEY_BOOKMARK_LABEL + " LIKE '%" + pattern + "%' OR " +
101  BookmarkDB.DB_KEY_BOOKMARK_HOSTNAME + " LIKE '%" + pattern + "%'",
102  BookmarkDB.DB_KEY_BOOKMARK_LABEL);
103  ArrayList<BookmarkBase> bookmarks = new ArrayList<>(cursor.getCount());
104 
105  if (cursor.moveToFirst() && (cursor.getCount() > 0))
106  {
107  do
108  {
109  bookmarks.add(getBookmarkFromCursor(cursor));
110  } while (cursor.moveToNext());
111  }
112 
113  cursor.close();
114  return bookmarks;
115  }
116 
117  private void readGatewaySettings(ManualBookmark bookmark, Cursor cursor)
118  {
119  ManualBookmark.GatewaySettings gatewaySettings = bookmark.getGatewaySettings();
120  gatewaySettings.setHostname(
121  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_HOSTNAME)));
122  gatewaySettings.setPort(
123  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_PORT)));
124  gatewaySettings.setUsername(
125  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_USERNAME)));
126  gatewaySettings.setPassword(
127  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_PASSWORD)));
128  gatewaySettings.setDomain(
129  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_GW_DOMAIN)));
130  }
131 }