summaryrefslogtreecommitdiff
path: root/lib-apps-common/inc/Model/DataControlConsumer.h
blob: 3cb0b2470c3a34228ec6cc4a454c56211b02624f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright 2017 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MODEL_DATA_CONTROL_CONSUMER_H
#define MODEL_DATA_CONTROL_CONSUMER_H

#include "Model/DataProvider.h"
#include "Utils/Bundle.h"
#include "Utils/CallbackManager.h"
#include "Utils/Range.h"

#include <data_control_sql.h>

namespace Model
{
	/**
	 * @brief Represents the data consumer of the data control.
	 */
	class EXPORT_API DataControlConsumer
	{
	public:
		typedef DataProvider::DataList DataList;

		/**
		 * @brief List of columns for select query.
		 *        The first must be the index column.
		 */
		typedef Utils::Range<const char **> ColumnList;

		/**
		 * @brief Called after data item changed.
		 * @param[in]   Which item data was changed
		 * @param[in]   Change type
		 */
		typedef Utils::CallbackManager<int, data_control_data_change_type_e> DataItemChangeCallback;

		/**
		 * @brief Called when requested Data Item(s) is received.
		 * @param[in]   dataList    List of received Data Items
		 */
		typedef std::function<void(DataList dataList)> GetCallback;

		/**
		 * @brief Called when result of insert, update or delete is received.
		 * @param[in]   isSuccess   Whether operation was successful
		 * @param[in]   id          Data Item's id if it was inserted
		 */
		typedef std::function<void(bool isSuccess, int id)> ResultCallback;

		/**
		 * @brief Create data consumer.
		 * @param[in]   providerId  The data provider's id
		 * @param[in]   tableId     The table's id
		 * @param[in]   columnList  The list of columns for select query
		 */
		DataControlConsumer(const char *providerId, const char *tableId, ColumnList columnList);
		virtual ~DataControlConsumer();

		DataControlConsumer(const DataControlConsumer &that) = delete;
		DataControlConsumer &operator=(const DataControlConsumer &that) = delete;

		/**
		 * @brief Retrieve item from database by ID.
		 * @param[in]   id          ID
		 * @param[in]   callback    Callback to be called when data item is received
		 */
		virtual void getDataItem(int id, GetCallback callback);

		/**
		 * @brief Retrieve all data items from database.
		 * @param[in]   callback    Callback to be called when data items are received
		 */
		virtual void getDataItems(GetCallback callback);

		/**
		 * @brief Convenience wrapper to insert new or update existing data item.
		 * @see insertDataItem()
		 * @see updateDataItem()
		 */
		void saveDataItem(const DataItem &item, ResultCallback callback);

		/**
		 * @brief Insert data item into database.
		 * @param[in]   item        Data item to insert
		 * @param[in]   callback    Callback to be called when operation is complete
		 */
		void insertDataItem(const DataItem &item, ResultCallback callback = nullptr);

		/**
		 * @brief Update existing data item in the database.
		 * @param[in]   item        Data item to update
		 * @param[in]   callback    Callback to be called when operation is complete
		 */
		void updateDataItem(const DataItem &item, ResultCallback callback = nullptr);

		/**
		 * @brief Delete data item from the database.
		 * @param[in]   id          Data item ID
		 * @param[in]   callback    Callback to be called when operation is complete
		 */
		void deleteDataItem(int id, ResultCallback callback = nullptr);

		/**
		 * @brief Add/remove data item change callback.
		 */
		DataItemChangeCallback &onDataItemChanged();

	protected:
		/**
		 * @brief Create data item from the result cursor.
		 * @param[in]   cursor   The result cursor
		 * @return Data item or nullptr.
		 */
		virtual DataItem *createDataItem(result_set_cursor cursor) = 0;

		/**
		 * @brief Create bundle from data item.
		 * @param[in]   item   Data item
		 * @return Bundle.
		 */
		virtual Utils::Bundle createBundle(const DataItem &item) = 0;

		/**
		 * @brief Invoke select query to database.
		 * @param[in]   where      The select query condition
		 * @param[in]   callback   Callback to be called when operation is completed
		 */
		void selectDataItems(const char *where, GetCallback callback);

	private:
		void onSelectResponse(int requestId, data_control_h provider,
				result_set_cursor result, bool isSuccess, const char *error);
		void onInsertResponse(int requestId, data_control_h provider,
				long long id, bool isSuccess, const char *error);
		void onUpdateResponse(int requestId, data_control_h provider,
				bool isSuccess, const char *error);
		void onDeleteResponse(int requestId, data_control_h provider,
				bool isSuccess, const char *error);
		void onDataChanged(data_control_h provider,
				data_control_data_change_type_e type, bundle *data);
		void onDataCallbackAdded(data_control_h provider,
				data_control_error_e result, int callbackId);

		data_control_h m_Provider;
		ColumnList m_ColumnList;
		int m_ChangeCallbackId;
		DataItemChangeCallback m_OnDataItemChanged;
		std::vector<std::pair<int, ResultCallback>> m_ResultCallbacks;
		std::vector<std::pair<int, GetCallback>> m_GetCallbacks;
	};
}

#endif /* MODEL_DATA_CONTROL_CONSUMER_H */