-
Notifications
You must be signed in to change notification settings - Fork 2
/
07_functions.sql
435 lines (385 loc) · 14.9 KB
/
07_functions.sql
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
428
429
430
431
432
433
434
435
/*
FUNCTIONS
---------
A collection of various helper fonctions
*/
/* Function to get taxo group from visionature id_species */
BEGIN;
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxo_group_values_from_vn (_key
TEXT, _site TEXT, _id INTEGER,
OUT _result TEXT);
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn(_key TEXT, _site TEXT, _id INTEGER, OUT _result TEXT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
BEGIN
EXECUTE 'select item ->> $1 from src_vn_json.taxo_groups_json where taxo_groups_json.id = $3 and taxo_groups_json.site like $2 limit 1;' INTO _result
USING _key , _site , _id;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn (_key TEXT
, _site TEXT , _id INTEGER , OUT _result TEXT) IS 'Function to get taxo group from visionature id_species';
/* Function to get taxref datas from VN id_sp */
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxref_values_from_vn
(_field_name ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT);
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn(_field_name ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT)
RETURNS ANYELEMENT
LANGUAGE plpgsql
AS
$$
BEGIN
EXECUTE FORMAT(
'SELECT taxref.%I from taxonomie.cor_c_vn_taxref join taxonomie.taxref on cor_c_vn_taxref.cd_nom = taxref.cd_nom where vn_id = $1 limit 1',
_field_name) INTO _result
USING _id_species;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn (_field_name
ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) IS 'Function to get taxref datas from VN id_sp';
/* Function to get visionature species datas from VN id_sp */
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_species_values_from_vn (_key
ANYELEMENT, _id_species INTEGER,
OUT _result ANYELEMENT);
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_species_values_from_vn(_key
ANYELEMENT, _id_species INTEGER,
OUT _result ANYELEMENT)
RETURNS ANYELEMENT
LANGUAGE plpgsql
AS
$$
BEGIN
EXECUTE 'select item ->> $1 from src_vn_json.species_json where species_json.id = $2 limit 1;' INTO _result
USING _key , _id_species;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_species_values_from_vn (_key
ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) IS 'Function to get visionature species datas from VN id_sp';
/* Function to get observer full name from VisioNature observer universal id*/
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observer_full_name_from_vn
(_id_universal INTEGER, OUT _result TEXT);
CREATE FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn(_id_universal INTEGER, OUT _result TEXT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
BEGIN
EXECUTE FORMAT('select concat(UPPER(item ->> ''name''), '' '', item ->> ''surname'') as text from src_vn_json.observers_json where observers_json.id_universal = $1 limit 1') INTO _result
USING _id_universal;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn
(_id_universal INTEGER , OUT _result TEXT) IS 'Function to get observer full name from VisioNature observer universal id';
/* Function to get entity name from VisioNature observer universal id */
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_entity_from_observer_site_uid
(_uid INTEGER, _site TEXT, OUT _result TEXT);
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid(_uid INTEGER, _site TEXT, OUT _result TEXT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
BEGIN
SELECT INTO _result CASE
WHEN ent.item ->> 'short_name' = '-' THEN
NULL
ELSE
ent.item ->> 'short_name'
END
FROM src_vn_json.observers_json usr
JOIN src_vn_json.entities_json ent
ON (usr.site, CAST(usr.item ->> 'id_entity' AS INT)) = (ent.site, ent.id)
WHERE usr.id_universal = _uid
AND usr.site = _site;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid (_uid
INTEGER , _site TEXT , OUT _result TEXT) IS 'Function to get entity name from VisioNature observer universal id';
/* Function to generate an array of behaviours from VisioNature datas */
DROP FUNCTION IF EXISTS
src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array (_behaviours
jsonb, OUT _result TEXT[]);
CREATE
OR REPLACE FUNCTION
src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array(_behaviours
jsonb, OUT _result TEXT[])
RETURNS TEXT[]
LANGUAGE plpgsql
AS
$$
DECLARE
_array_id TEXT[];
BEGIN
IF
_behaviours IS NOT NULL THEN
SELECT ARRAY_AGG(u.x) ::TEXT[]
INTO _array_id
FROM (SELECT t.value ->> '@id' AS x
FROM JSONB_ARRAY_ELEMENTS(_behaviours) AS t) AS u;
SELECT INTO _result ARRAY_AGG(item ->> 'text')
FROM src_vn_json.field_details_json
WHERE id IN (SELECT UNNEST(_array_id));
ELSE
SELECT NULL
INTO _result;
END IF;
END;
$$;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array
(_behaviours jsonb , OUT _result TEXT[]) IS 'Function to generate an array of behaviours from VisioNature datas';
/* list visionature medias URL from medias details */
DROP FUNCTION IF EXISTS
src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array (_medias
jsonb, OUT _result TEXT);
CREATE
OR REPLACE FUNCTION
src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array(_medias
jsonb, OUT _result TEXT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
BEGIN
IF
_medias IS NOT NULL THEN
SELECT STRING_AGG(u.x, ', ') ::TEXT
INTO _result
FROM (SELECT CONCAT(t.value ->> 'path', '/', t.value ->> 'filename') AS x
FROM JSONB_ARRAY_ELEMENTS(_medias) AS t) AS u;
ELSE
SELECT NULL
INTO _result;
END IF;
END;
$$;
COMMENT
ON FUNCTION
src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array (_medias
jsonb , OUT _result TEXT) IS 'Function to list medias URL from VisioNature datas';
/* Function to get observation generated UUID */
/* NOTE: removed because uuid are now available in faune-france API */
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observation_uuid (_site
CHARACTER VARYING, _id INTEGER) CASCADE;
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_observation_uuid(_site
CHARACTER VARYING, _id INTEGER)
RETURNS uuid
AS
$$
DECLARE
the_uuid uuid DEFAULT NULL;
BEGIN
IF
(SELECT EXISTS (SELECT *
FROM information_schema.tables
WHERE table_schema = 'src_vn_json'
AND table_name = 'uuid_xref')) THEN
SELECT uuid
INTO the_uuid
FROM src_vn_json.uuid_xref
WHERE site LIKE _site
AND id = _id
LIMIT 1;
END IF;
RETURN the_uuid;
END;
$$
LANGUAGE plpgsql;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_observation_uuid(_site
CHARACTER VARYING , _id INTEGER) IS 'Function to get observation generated UUID';
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxon_diffusion_level(_cd_nom INT)
RETURNS INT
AS
$$
DECLARE
the_nomenclature_id INT;
BEGIN
SELECT id_nomenclature_diffusion_level
INTO the_nomenclature_id
FROM src_lpodatas.t_c_rules_diffusion_level
WHERE cd_nom = _cd_nom;
RETURN the_nomenclature_id;
END;
$$
LANGUAGE plpgsql;
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_committees_validation_status
(_committees_validation jsonb);
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_committees_validation_status(_committees_validation jsonb)
RETURNS TEXT[]
AS
$$
DECLARE
the_values TEXT[];
the_rec
RECORD;
BEGIN
FOR the_rec IN (SELECT JSONB_OBJECT_KEYS(_committees_validation) AS key)
LOOP
SELECT ARRAY_APPEND(the_values, _committees_validation ->> the_rec.key)
INTO the_values;
END LOOP;
RETURN the_values;
END;
$$
LANGUAGE plpgsql;
DROP FUNCTION IF EXISTS
src_lpodatas.fct_c_get_committees_validation_is_accepted
(_committees_validation jsonb);
CREATE
OR REPLACE FUNCTION
src_lpodatas.fct_c_get_committees_validation_is_accepted(_committees_validation jsonb)
RETURNS BOOLEAN
AS
$$
DECLARE
is_accepted BOOLEAN;
BEGIN
SELECT 'ACCEPTED' = ANY
(src_lpodatas.fct_c_get_committees_validation_status
(_committees_validation))
INTO is_accepted;
RETURN is_accepted;
END;
$$
LANGUAGE plpgsql;
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_check_hidden_rules(_cd_nom INT,
_date_min TIMESTAMP, _raw_bird_breed_code INT)
RETURNS bool
AS
$$
DECLARE
the_bird_breed_code INT;
the_has_hidden_rule
bool;
BEGIN
SELECT CASE
WHEN _raw_bird_breed_code = 99 THEN
NULL
WHEN _raw_bird_breed_code = 30 THEN
2
WHEN _raw_bird_breed_code = 40 THEN
4
WHEN _raw_bird_breed_code = 50 THEN
11
ELSE
_raw_bird_breed_code
END
INTO the_bird_breed_code;
SELECT EXISTS (SELECT *
FROM src_lpodatas.t_c_visionature_hidding_rules rule
WHERE rule.cd_nom = _cd_nom
AND (rule.all_time_restriction
OR (_date_min BETWEEN MAKE_DATE(EXTRACT(YEAR FROM
NOW())::INT, rule.restriction_start_month::INT,
rule.restriction_start_day::INT)
AND MAKE_DATE(EXTRACT(YEAR FROM NOW())::INT,
rule.restriction_end_month::INT,
rule.restriction_end_day::INT))
AND (rule.restriction_atlas_min_code IS NULL)
OR (rule.restriction_atlas_min_code IS NOT NULL
AND (the_bird_breed_code IS NOT NULL
AND the_bird_breed_code >= rule.restriction_atlas_min_code))))
INTO the_has_hidden_rule;
RETURN the_has_hidden_rule;
END;
$$
LANGUAGE plpgsql;
COMMENT
ON FUNCTION src_lpodatas.fct_c_check_hidden_rules (_cd_nom INT ,
_date_min TIMESTAMP , _raw_bird_breed_code INT) IS 'Detect observations hidden by sensitivity rules';
CREATE
OR REPLACE FUNCTION src_lpodatas.fct_c_get_diffusion_level(_cd_nom INT
, _date_min TIMESTAMP, _raw_bird_breed_code INT, _item jsonb)
RETURNS INT
AS
$$
DECLARE
the_id_nomenclature_diffusion_level INT DEFAULT NULL;
the_hidden_by_rules
bool DEFAULT FALSE;
BEGIN
/* Si la table des données de sensibilté visionature existe */
IF
(SELECT EXISTS (SELECT
FROM information_schema.tables
WHERE table_schema = 'src_lpodatas'
AND table_name = 't_c_visionature_hidding_rules')) THEN
/* Identifie si La donnée est cachée par une règle de sensibilité faune-france */
SELECT src_lpodatas.fct_c_check_hidden_rules(_cd_nom, _date_min,
_raw_bird_breed_code)
INTO the_hidden_by_rules;
/* Si la donnée est automatiquement cachée par une règle de sensibilité, alors on la diffuse précisément */
-- IF the_hidden_by_rules THEN
-- SELECT
-- ref_nomenclatures.get_id_nomenclature('NIV_PRECIS',
-- '5')
-- INTO the_id_nomenclature_diffusion_level;
END IF;
/* sinon, on fait le check courant */
SELECT CASE
-- Taxons sensibles, règle dans la table src_lpodatas.t_c_rules_diffusion_level >
-- diffusion au cas par cas
WHEN _cd_nom IN (SELECT cd_nom
FROM src_lpodatas.t_c_rules_diffusion_level) THEN
src_lpodatas.fct_c_get_taxon_diffusion_level(_cd_nom)
-- Observation "cachée" automatiquement par les règles de sensibilités de
-- visionature > diffusion précise
WHEN the_hidden_by_rules THEN
ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '5')
-- Observation a priori cachée par l'observateur
WHEN CAST(_item #>> '{observers,0,hidden}' IS NOT NULL AS bool) THEN
ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '2')
-- Observation "invalide" (> refused) ou en ffquestionnement (> question)
WHEN _item #>> '{observers,0,admin_hidden_type}' IN ('refused', 'question') THEN
ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '4')
ELSE
ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '5')
END
INTO the_id_nomenclature_diffusion_level;
RETURN the_id_nomenclature_diffusion_level;
END;
$$
LANGUAGE plpgsql;
COMMENT
ON FUNCTION src_lpodatas.fct_c_get_diffusion_level (_cd_nom INT
, _date_min TIMESTAMP , _raw_bird_breed_code INT , _item jsonb) IS 'Défini les règles de diffusion des données cachées';
CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_check_relation_exists(_the_relation_name TEXT) RETURNS bool AS
$$
DECLARE
the_sympetrum_area_exists bool DEFAULT FALSE;
BEGIN
SELECT EXISTS (SELECT oid::regclass::TEXT
FROM pg_class
WHERE oid::regclass::TEXT LIKE _the_relation_name)
INTO the_sympetrum_area_exists;
RETURN the_sympetrum_area_exists;
END;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION src_lpodatas.fct_get_geom_from_relation_name(_relation_name TEXT) RETURNS geometry AS
$$
DECLARE
the_geom geometry DEFAULT NULL;
the_query TEXT;
BEGIN
the_query := FORMAT('select geom from %s', _relation_name);
IF src_lpodatas.fct_c_check_relation_exists(_relation_name) THEN
EXECUTE the_query into the_geom ;
END IF;
return the_geom;
END;
$$ LANGUAGE plpgsql;
COMMIT;