summaryrefslogtreecommitdiffstats
path: root/src/scope.c
blob: 3f8e172d82ec96c1b5ed3843c81cb5e3906cbeb2 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

#include <syslog.h>
#include "dbi.h"
#include "utils.h"
#include "list.h"
#include "rest.h"

#define OG_QUERY_MAXLEN 4096

static int og_dbi_scope_get_computer(const struct og_dbi *dbi, json_t *array,
				     const char* query)
{
	const char *computer_name, *computer_ip;
	uint32_t computer_id;
	const char *msglog;
	dbi_result result;
	json_t *computer;

	result = dbi_conn_queryf(dbi->conn, query);
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		computer_id = dbi_result_get_uint(result, "idordenador");
		computer_name = dbi_result_get_string(result, "nombreordenador");
		computer_ip = dbi_result_get_string(result, "ip");

		computer = json_object();
		if (!computer) {
			dbi_result_free(result);
			return -1;
		}

		json_object_set_new(computer, "name", json_string(computer_name));
		json_object_set_new(computer, "type", json_string("computer"));
		json_object_set_new(computer, "id", json_integer(computer_id));
		json_object_set_new(computer, "scope", json_array());
		json_object_set_new(computer, "ip", json_string(computer_ip));
		json_array_append(array, computer);
		json_decref(computer);
	}
	dbi_result_free(result);

	return 0;
}

static int og_dbi_scope_get_computer_from_room(const struct og_dbi *dbi,
					       json_t *array, char *query,
					       const uint32_t room_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idordenador, nombreordenador, ip "
			   "FROM ordenadores WHERE idaula=%d and grupoid=0",
			   room_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_computer(dbi, array, query);
}

static int og_dbi_scope_get_computer_from_computers(const struct og_dbi *dbi,
						    json_t *array,
						    char *query,
						    const uint32_t computers_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idordenador, nombreordenador, ip "
			   "FROM ordenadores WHERE grupoid=%d",
			   computers_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_computer(dbi, array, query);
}

#define OG_COMPUTER_FOLDER_MARKER 0x00010000

static int og_dbi_scope_get_computers_from_computers(const struct og_dbi *dbi,
						     json_t *array,
						     char *query,
						     const uint32_t group_id);

static int og_dbi_scope_get_computers(const struct og_dbi *dbi, json_t *array,
				      char *query, bool in_room)
{
	const char *msglog, *computers_name;
	json_t *computers, *scope_array;
	uint32_t computers_id;
	uint32_t marker = 0;
	dbi_result result;

	if (in_room)
		marker = OG_COMPUTER_FOLDER_MARKER;

	result = dbi_conn_queryf(dbi->conn, query);
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		computers_id = dbi_result_get_uint(result, "idgrupo");
		computers_name = dbi_result_get_string(result,
						      "nombregrupoordenador");

		computers = json_object();
		if (!computers) {
			dbi_result_free(result);
			return -1;
		}

		json_object_set_new(computers, "name",
				    json_string(computers_name));
		json_object_set_new(computers, "type", json_string("folder"));
		json_object_set_new(computers, "id",
				    json_integer(computers_id | marker));
		json_object_set_new(computers, "scope", json_array());
		json_array_append(array, computers);
		json_decref(computers);

		scope_array = json_object_get(computers, "scope");
		if (!scope_array) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_computers_from_computers(dbi,
							      scope_array,
							      query,
							      computers_id)) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_computer_from_computers(dbi,
							     scope_array,
							     query,
							     computers_id)) {
			dbi_result_free(result);
			return -1;
		}
	}
	dbi_result_free(result);

	return 0;
}

static int og_dbi_scope_get_computers_from_room(const struct og_dbi *dbi,
						json_t *array, char *query,
						const uint32_t room_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idgrupo, nombregrupoordenador "
			   "FROM gruposordenadores "
			   "WHERE idaula=%d AND grupoid=0",
			   room_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_computers(dbi, array, query, true);
}

static int og_dbi_scope_get_computers_from_computers(const struct og_dbi *dbi,
						     json_t *array,
						     char *query,
						     const uint32_t group_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idgrupo, nombregrupoordenador "
			   "FROM gruposordenadores WHERE grupoid=%d",
			   group_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_computers(dbi, array, query, false);
}

static int og_dbi_scope_get_room(const struct og_dbi *dbi, json_t *array,
				 char *query)
{
	const char *msglog, *room_name;
	json_t *room, *scope_array;
	dbi_result result;
	uint32_t room_id;

	result = dbi_conn_queryf(dbi->conn, query);
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		room_id = dbi_result_get_uint(result, "idaula");
		room_name = dbi_result_get_string(result, "nombreaula");

		room = json_object();
		if (!room) {
			dbi_result_free(result);
			return -1;
		}

		json_object_set_new(room, "name", json_string(room_name));
		json_object_set_new(room, "type", json_string("room"));
		json_object_set_new(room, "id", json_integer(room_id));
		json_object_set_new(room, "scope", json_array());
		json_array_append(array, room);
		json_decref(room);

		scope_array = json_object_get(room, "scope");
		if (!scope_array) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_computers_from_room(dbi, scope_array,
							 query, room_id)) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_computer_from_room(dbi, scope_array,
							query, room_id)) {
			dbi_result_free(result);
			return -1;
		}
	}
	dbi_result_free(result);

	return 0;
}

static int og_dbi_scope_get_room_from_group(const struct og_dbi *dbi,
					    json_t *array,
					    char *query,
					    const uint32_t group_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idaula, nombreaula "
			   "FROM aulas WHERE grupoid=%d", group_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_room(dbi, array, query);
}

static int og_dbi_scope_get_room_from_center(const struct og_dbi *dbi,
					     json_t *array,
					     char *query,
					     const uint32_t center_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idaula, nombreaula "
			   "FROM aulas WHERE idcentro=%d AND grupoid=0",
			   center_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_room(dbi, array, query);
}

static int og_dbi_scope_get_group_from_group(const struct og_dbi *dbi,
					     json_t *array,
					     char *query,
					     const uint32_t group_id);

static int og_dbi_scope_get_group(const struct og_dbi *dbi,
				  json_t *array,
				  char *query)
{
	const char *msglog, *group_name;
	json_t *group, *scope_array;
	dbi_result result;
	uint32_t group_id;

	result = dbi_conn_queryf(dbi->conn, query);
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		group_id = dbi_result_get_uint(result, "idgrupo");
		group_name = dbi_result_get_string(result, "nombregrupo");

		group = json_object();
		if (!group) {
			dbi_result_free(result);
			return -1;
		}

		json_object_set_new(group, "name", json_string(group_name));
		json_object_set_new(group, "type", json_string("folder"));
		json_object_set_new(group, "id", json_integer(group_id));
		json_object_set_new(group, "scope", json_array());
		json_array_append(array, group);
		json_decref(group);

		scope_array = json_object_get(group, "scope");
		if (!scope_array) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_group_from_group(dbi, scope_array, query,
						      group_id)) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_room_from_group(dbi, scope_array, query,
						     group_id)) {
			dbi_result_free(result);
			return -1;
		}
	}
	dbi_result_free(result);

	return 0;
}

static int og_dbi_scope_get_group_from_group(const struct og_dbi *dbi,
					      json_t *array,
					      char *query,
					      const uint32_t group_id)
{
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idgrupo, nombregrupo "
			   "FROM grupos WHERE grupoid=%d", group_id);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_group(dbi, array, query);
}

static int og_dbi_scope_get_group_from_center(const struct og_dbi *dbi,
					      json_t *array,
					      char *query,
					      const uint32_t center_id)
{
	int group_type_room = 2;
	int ret = snprintf(query, OG_QUERY_MAXLEN,
			   "SELECT idgrupo, nombregrupo "
			   "FROM grupos "
			   "WHERE idcentro=%d AND grupoid=0 AND tipo=%d",
			   center_id, group_type_room);
	if (ret <= 0 || ret >= OG_QUERY_MAXLEN)
		return -1;

	return og_dbi_scope_get_group(dbi, array, query);
}

int og_dbi_scope_get(struct og_dbi *dbi, json_t *array)
{
	const char *msglog, *center_name;
	json_t *center, *scope_array;
	char query[OG_QUERY_MAXLEN];
	uint32_t center_id;
	dbi_result result;

	result = dbi_conn_queryf(dbi->conn,
				 "SELECT nombrecentro, idcentro FROM centros");
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		center_id = dbi_result_get_uint(result, "idcentro");
		center_name = dbi_result_get_string(result, "nombrecentro");

		center = json_object();
		if (!center) {
			dbi_result_free(result);
			return -1;
		}

		scope_array = json_array();
		if (!scope_array) {
			dbi_result_free(result);
			json_decref(center);
			return -1;
		}

		json_object_set_new(center, "name", json_string(center_name));
		json_object_set_new(center, "type", json_string("center"));
		json_object_set_new(center, "id", json_integer(center_id));
		json_object_set_new(center, "scope", scope_array);
		json_array_append(array, center);
		json_decref(center);

		if (og_dbi_scope_get_group_from_center(dbi, scope_array, query,
						       center_id)) {
			dbi_result_free(result);
			return -1;
		}

		if (og_dbi_scope_get_room_from_center(dbi, scope_array, query,
						      center_id)) {
			dbi_result_free(result);
			return -1;
		}
	}

	dbi_result_free(result);

	return 0;
}