diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a0a4334..5ced480 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,6 +43,12 @@ repos: ] files: 'gn2pg' + - repo: https://github.com/amureki/pre-commit-pgFormatter + rev: v1.3.0 # Use the sha / tag you want to point at + hooks: + - id: pg_format + args: [-b,-f1,-C,-U2,-k,-g,-w80] + # sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date ci: autoupdate_schedule: weekly diff --git a/data/source_samples/geonature_export_sinp_with_cd_nomenclature.sql b/data/source_samples/geonature_export_sinp_with_cd_nomenclature.sql index 357978a..f30c61f 100644 --- a/data/source_samples/geonature_export_sinp_with_cd_nomenclature.sql +++ b/data/source_samples/geonature_export_sinp_with_cd_nomenclature.sql @@ -2,100 +2,134 @@ DROP VIEW IF EXISTS gn_exports.v_synthese_sinp_with_cd_nomenclature_for_gn2pg; CREATE VIEW gn_exports.v_synthese_sinp_with_cd_nomenclature_for_gn2pg AS WITH jdd_acteurs AS ( - SELECT d_1.id_dataset, - array_to_json(array_agg(DISTINCT jsonb_build_object('nom_organisme', orga.nom_organisme, 'uuid_organism', - orga.uuid_organisme, 'nom_role', roles.nom_role, - 'uuid_role', roles.uuid_role, 'type_role', - nomencl.cd_nomenclature))) AS actors - FROM gn_meta.t_datasets d_1 - JOIN gn_meta.cor_dataset_actor act ON act.id_dataset = d_1.id_dataset - JOIN ref_nomenclatures.t_nomenclatures nomencl ON nomencl.id_nomenclature = act.id_nomenclature_actor_role - LEFT JOIN utilisateurs.bib_organismes orga ON orga.id_organisme = act.id_organism - LEFT JOIN utilisateurs.t_roles roles ON roles.id_role = act.id_role - GROUP BY d_1.id_dataset + SELECT + d_1.id_dataset + , array_to_json(array_agg(DISTINCT jsonb_build_object('nom_organisme' + , orga.nom_organisme + , 'uuid_organism' + , orga.uuid_organisme + , 'nom_role' + , roles.nom_role + , 'uuid_role' + , roles.uuid_role + , 'type_role' + , nomencl.cd_nomenclature))) AS actors + FROM + gn_meta.t_datasets d_1 + JOIN gn_meta.cor_dataset_actor act ON act.id_dataset = d_1.id_dataset + JOIN ref_nomenclatures.t_nomenclatures nomencl ON + nomencl.id_nomenclature = act.id_nomenclature_actor_role + LEFT JOIN utilisateurs.bib_organismes orga ON orga.id_organisme = act.id_organism + LEFT JOIN utilisateurs.t_roles roles ON roles.id_role = act.id_role + GROUP BY + d_1.id_dataset ) -SELECT DISTINCT s.id_synthese, - s.entity_source_pk_value AS id_source, - s.unique_id_sinp AS id_perm_sinp, - s.unique_id_sinp_grp AS id_perm_grp_sinp, - s.date_min AS date_debut, - s.date_max AS date_fin, - s.cd_nom, - s.meta_v_taxref AS version_taxref, - s.nom_cite, - s.count_min AS nombre_min, - s.count_max AS nombre_max, - s.altitude_min, - s.altitude_max, - s.depth_min AS profondeur_min, - s.depth_max AS profondeur_max, - s.observers AS observateurs, - s.determiner AS determinateur, - s.validator AS validateur, - s.sample_number_proof AS numero_preuve, - s.digital_proof AS preuve_numerique, - s.non_digital_proof AS preuve_non_numerique, - s.comment_context AS comment_releve, - s.comment_description AS comment_occurrence, - coalesce(s.meta_update_date, s.meta_create_date) AS derniere_action, - td.unique_dataset_id AS jdd_uuid, - td.dataset_name AS jdd_nom, - jdd_acteurs.actors::TEXT AS jdd_acteurs, - af.unique_acquisition_framework_id AS ca_uuid, - af.acquisition_framework_name AS ca_nom, - s.reference_biblio, - s.cd_hab AS code_habitat, - h.lb_hab_fr AS habitat, - s.place_name AS nom_lieu, - s.precision, - s.additional_data AS donnees_additionnelles, - st_astext(st_transform(the_geom_local, 4326)) AS wkt_4326, - n1.cd_nomenclature AS nature_objet_geo, - n2.cd_nomenclature AS type_regroupement, - s.grp_method AS methode_regroupement, - n3.cd_nomenclature AS comportement, - n4.cd_nomenclature AS technique_obs, - n5.cd_nomenclature AS statut_biologique, - n6.cd_nomenclature AS etat_biologique, - n7.cd_nomenclature AS naturalite, - n8.cd_nomenclature AS preuve_existante, - n9.cd_nomenclature AS precision_diffusion, - n10.cd_nomenclature AS stade_vie, - n11.cd_nomenclature AS sexe, - n12.cd_nomenclature AS objet_denombrement, - n13.cd_nomenclature AS type_denombrement, - n14.cd_nomenclature AS niveau_sensibilite, - n15.cd_nomenclature AS statut_observation, - n16.cd_nomenclature AS floutage_dee, - n17.cd_nomenclature AS statut_source, - n18.cd_nomenclature AS type_info_geo, - n19.cd_nomenclature AS methode_determination, - n20.cd_nomenclature AS statut_validation -FROM gn_synthese.synthese s - LEFT JOIN gn_synthese.cor_area_synthese cas ON s.id_synthese = cas.id_synthese - JOIN jdd_acteurs ON jdd_acteurs.id_dataset = s.id_dataset - JOIN gn_meta.t_datasets td ON td.id_dataset = s.id_dataset - JOIN gn_meta.t_acquisition_frameworks af ON td.id_acquisition_framework = af.id_acquisition_framework - JOIN gn_synthese.t_sources sources ON sources.id_source = s.id_source - LEFT JOIN ref_habitats.habref h ON h.cd_hab = s.cd_hab - LEFT JOIN ref_nomenclatures.t_nomenclatures n1 ON s.id_nomenclature_geo_object_nature = n1.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n2 ON s.id_nomenclature_grp_typ = n2.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n3 ON s.id_nomenclature_behaviour = n3.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n4 ON s.id_nomenclature_obs_technique = n4.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n5 ON s.id_nomenclature_bio_status = n5.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n6 ON s.id_nomenclature_bio_condition = n6.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n7 ON s.id_nomenclature_naturalness = n7.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n8 ON s.id_nomenclature_exist_proof = n8.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n9 ON s.id_nomenclature_diffusion_level = n9.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n10 ON s.id_nomenclature_life_stage = n10.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n11 ON s.id_nomenclature_sex = n11.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n12 ON s.id_nomenclature_obj_count = n12.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n13 ON s.id_nomenclature_type_count = n13.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n14 ON s.id_nomenclature_sensitivity = n14.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n15 ON s.id_nomenclature_observation_status = n15.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n16 ON s.id_nomenclature_blurring = n16.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n17 ON s.id_nomenclature_source_status = n17.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n18 ON s.id_nomenclature_info_geo_type = n18.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n19 ON s.id_nomenclature_determination_method = n19.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n20 ON s.id_nomenclature_valid_status = n20.id_nomenclature -ORDER BY id_synthese; +SELECT DISTINCT + s.id_synthese + , s.entity_source_pk_value AS id_source + , s.unique_id_sinp AS id_perm_sinp + , s.unique_id_sinp_grp AS id_perm_grp_sinp + , s.date_min AS date_debut + , s.date_max AS date_fin + , s.cd_nom + , s.meta_v_taxref AS version_taxref + , s.nom_cite + , s.count_min AS nombre_min + , s.count_max AS nombre_max + , s.altitude_min + , s.altitude_max + , s.depth_min AS profondeur_min + , s.depth_max AS profondeur_max + , s.observers AS observateurs + , s.determiner AS determinateur + , s.validator AS validateur + , s.sample_number_proof AS numero_preuve + , s.digital_proof AS preuve_numerique + , s.non_digital_proof AS preuve_non_numerique + , s.comment_context AS comment_releve + , s.comment_description AS comment_occurrence + , coalesce(s.meta_update_date , s.meta_create_date) AS derniere_action + , td.unique_dataset_id AS jdd_uuid + , td.dataset_name AS jdd_nom + , jdd_acteurs.actors::TEXT AS jdd_acteurs + , af.unique_acquisition_framework_id AS ca_uuid + , af.acquisition_framework_name AS ca_nom + , s.reference_biblio + , s.cd_hab AS code_habitat + , h.lb_hab_fr AS habitat + , s.place_name AS nom_lieu + , s.precision + , s.additional_data AS donnees_additionnelles + , st_astext (st_transform (the_geom_local , 4326)) AS wkt_4326 + , n1.cd_nomenclature AS nature_objet_geo + , n2.cd_nomenclature AS type_regroupement + , s.grp_method AS methode_regroupement + , n3.cd_nomenclature AS comportement + , n4.cd_nomenclature AS technique_obs + , n5.cd_nomenclature AS statut_biologique + , n6.cd_nomenclature AS etat_biologique + , n7.cd_nomenclature AS naturalite + , n8.cd_nomenclature AS preuve_existante + , n9.cd_nomenclature AS precision_diffusion + , n10.cd_nomenclature AS stade_vie + , n11.cd_nomenclature AS sexe + , n12.cd_nomenclature AS objet_denombrement + , n13.cd_nomenclature AS type_denombrement + , n14.cd_nomenclature AS niveau_sensibilite + , n15.cd_nomenclature AS statut_observation + , n16.cd_nomenclature AS floutage_dee + , n17.cd_nomenclature AS statut_source + , n18.cd_nomenclature AS type_info_geo + , n19.cd_nomenclature AS methode_determination + , n20.cd_nomenclature AS statut_validation +FROM + gn_synthese.synthese s + LEFT JOIN gn_synthese.cor_area_synthese cas ON s.id_synthese = cas.id_synthese + JOIN jdd_acteurs ON jdd_acteurs.id_dataset = s.id_dataset + JOIN gn_meta.t_datasets td ON td.id_dataset = s.id_dataset + JOIN gn_meta.t_acquisition_frameworks af ON td.id_acquisition_framework = + af.id_acquisition_framework + JOIN gn_synthese.t_sources sources ON sources.id_source = s.id_source + LEFT JOIN ref_habitats.habref h ON h.cd_hab = s.cd_hab + LEFT JOIN ref_nomenclatures.t_nomenclatures n1 ON + s.id_nomenclature_geo_object_nature = n1.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n2 ON s.id_nomenclature_grp_typ + = n2.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n3 ON + s.id_nomenclature_behaviour = n3.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n4 ON + s.id_nomenclature_obs_technique = n4.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n5 ON + s.id_nomenclature_bio_status = n5.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n6 ON + s.id_nomenclature_bio_condition = n6.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n7 ON + s.id_nomenclature_naturalness = n7.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n8 ON + s.id_nomenclature_exist_proof = n8.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n9 ON + s.id_nomenclature_diffusion_level = n9.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n10 ON + s.id_nomenclature_life_stage = n10.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n11 ON s.id_nomenclature_sex = + n11.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n12 ON + s.id_nomenclature_obj_count = n12.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n13 ON + s.id_nomenclature_type_count = n13.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n14 ON + s.id_nomenclature_sensitivity = n14.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n15 ON + s.id_nomenclature_observation_status = n15.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n16 ON + s.id_nomenclature_blurring = n16.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n17 ON + s.id_nomenclature_source_status = n17.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n18 ON + s.id_nomenclature_info_geo_type = n18.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n19 ON + s.id_nomenclature_determination_method = n19.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n20 ON + s.id_nomenclature_valid_status = n20.id_nomenclature +ORDER BY + id_synthese; diff --git a/data/source_samples/geonature_export_sinp_with_metadata.sql b/data/source_samples/geonature_export_sinp_with_metadata.sql index cc7a70b..8b8defa 100644 --- a/data/source_samples/geonature_export_sinp_with_metadata.sql +++ b/data/source_samples/geonature_export_sinp_with_metadata.sql @@ -1,238 +1,388 @@ DROP VIEW IF EXISTS gn_exports.v_synthese_sinp_with_metadata_for_gn2pg; CREATE VIEW gn_exports.v_synthese_sinp_with_metadata_for_gn2pg AS -WITH af_actors AS (SELECT cafa.id_acquisition_framework, - json_build_object('type_role', - CASE - WHEN cafa.id_organism IS NOT NULL THEN 'organism'::TEXT - WHEN cafa.id_role IS NOT NULL THEN 'role'::TEXT - ELSE NULL::TEXT - END, 'uuid_actor', coalesce(borg.uuid_organisme, tro.uuid_role), - 'cd_nomenclature_actor_role', tn.cd_nomenclature, 'identity', - CASE - WHEN cafa.id_organism IS NOT NULL - THEN json_build_object('organism_name', borg.nom_organisme) - WHEN cafa.id_role IS NOT NULL THEN json_build_object('first_name', - tro.nom_role, - 'last_name', - tro.prenom_role) END, - 'email', coalesce(borg.email_organisme, tro.email)) AS json_data - FROM gn_meta.cor_acquisition_framework_actor cafa - LEFT JOIN utilisateurs.bib_organismes borg ON cafa.id_organism = borg.id_organisme - LEFT JOIN utilisateurs.t_roles tro ON cafa.id_role = tro.id_role - JOIN ref_nomenclatures.t_nomenclatures tn - ON cafa.id_nomenclature_actor_role = tn.id_nomenclature), - af_territories AS (SELECT caft.id_acquisition_framework, - array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS territories - FROM gn_meta.cor_acquisition_framework_territory caft - LEFT JOIN ref_nomenclatures.t_nomenclatures - ON caft.id_nomenclature_territory = - t_nomenclatures.id_nomenclature - GROUP BY caft.id_acquisition_framework), - af_objectives AS (SELECT cafo.id_acquisition_framework, - array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS objectives - FROM gn_meta.cor_acquisition_framework_objectif cafo - LEFT JOIN ref_nomenclatures.t_nomenclatures - ON cafo.id_nomenclature_objectif = - t_nomenclatures.id_nomenclature - GROUP BY cafo.id_acquisition_framework), - af_voletsinp AS (SELECT cafv.id_acquisition_framework, - array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS voletsinp - FROM gn_meta.cor_acquisition_framework_voletsinp cafv - LEFT JOIN ref_nomenclatures.t_nomenclatures - ON cafv.id_nomenclature_voletsinp = - t_nomenclatures.id_nomenclature - GROUP BY cafv.id_acquisition_framework), - af_publication AS (SELECT cafp.id_acquisition_framework, - array_agg(DISTINCT - jsonb_build_object('uuid', sinp_datatype_publications.unique_publication_id, - 'reference', - sinp_datatype_publications.publication_reference, - 'url', - sinp_datatype_publications.publication_url)) AS publications - FROM gn_meta.cor_acquisition_framework_publication cafp - LEFT JOIN gn_meta.sinp_datatype_publications - ON cafp.id_publication = sinp_datatype_publications.id_publication - GROUP BY cafp.id_acquisition_framework) +WITH af_actors AS ( + SELECT + cafa.id_acquisition_framework + , json_build_object('type_role' + , CASE WHEN cafa.id_organism IS NOT NULL THEN + 'organism'::TEXT + WHEN cafa.id_role IS NOT NULL THEN + 'role'::TEXT + ELSE + NULL::TEXT + END + , 'uuid_actor' + , coalesce(borg.uuid_organisme + , tro.uuid_role) + , 'cd_nomenclature_actor_role' + , tn.cd_nomenclature + , 'identity' + , CASE WHEN cafa.id_organism IS NOT NULL THEN + json_build_object('organism_name' + , borg.nom_organisme) + WHEN cafa.id_role IS NOT NULL THEN + json_build_object('first_name' + , tro.nom_role + , 'last_name' + , tro.prenom_role) + END + , 'email' + , coalesce(borg.email_organisme + , tro.email)) AS json_data + FROM + gn_meta.cor_acquisition_framework_actor cafa + LEFT JOIN utilisateurs.bib_organismes borg ON cafa.id_organism = borg.id_organisme + LEFT JOIN utilisateurs.t_roles tro ON cafa.id_role = tro.id_role + JOIN ref_nomenclatures.t_nomenclatures tn ON + cafa.id_nomenclature_actor_role = tn.id_nomenclature +) +, af_territories AS ( + SELECT + caft.id_acquisition_framework + , array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS territories + FROM + gn_meta.cor_acquisition_framework_territory caft + LEFT JOIN ref_nomenclatures.t_nomenclatures ON + caft.id_nomenclature_territory = t_nomenclatures.id_nomenclature + GROUP BY + caft.id_acquisition_framework +) +, af_objectives AS ( + SELECT + cafo.id_acquisition_framework + , array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS objectives + FROM + gn_meta.cor_acquisition_framework_objectif cafo + LEFT JOIN ref_nomenclatures.t_nomenclatures ON + cafo.id_nomenclature_objectif = t_nomenclatures.id_nomenclature + GROUP BY + cafo.id_acquisition_framework +) +, af_voletsinp AS ( + SELECT + cafv.id_acquisition_framework + , array_agg(DISTINCT t_nomenclatures.cd_nomenclature) AS voletsinp + FROM + gn_meta.cor_acquisition_framework_voletsinp cafv + LEFT JOIN ref_nomenclatures.t_nomenclatures ON + cafv.id_nomenclature_voletsinp = t_nomenclatures.id_nomenclature + GROUP BY + cafv.id_acquisition_framework +) +, af_publication AS ( + SELECT + cafp.id_acquisition_framework + , array_agg(DISTINCT jsonb_build_object('uuid' + , sinp_datatype_publications.unique_publication_id + , 'reference' + , sinp_datatype_publications.publication_reference + , 'url' + , sinp_datatype_publications.publication_url)) AS publications + FROM + gn_meta.cor_acquisition_framework_publication cafp + LEFT JOIN gn_meta.sinp_datatype_publications ON cafp.id_publication = + sinp_datatype_publications.id_publication + GROUP BY + cafp.id_acquisition_framework +) +, af AS ( + SELECT + taf.id_acquisition_framework + , jsonb_build_object('uuid' + , taf.unique_acquisition_framework_id + , 'name' + , taf.acquisition_framework_name + , 'desc' + , taf.acquisition_framework_desc + , 'start_date' + , taf.acquisition_framework_start_date + , 'end_date' + , taf.acquisition_framework_end_date + , 'initial_closing_date' + , taf.initial_closing_date + , 'territories' + , af_territories.territories + , 'territorial_level' + , ntl.cd_nomenclature + , 'territory_desc' + , taf.territory_desc + , 'objectives' + , af_objectives.objectives + , 'publications' + , af_publication.publications + , 'financing_type' + , nft.cd_nomenclature + , 'target_description' + , taf.target_description + , 'ecologic_or_geologic_target' + , taf.ecologic_or_geologic_target + , 'sinp_theme' + , af_voletsinp.voletsinp + , 'actors' + , json_agg(af_actors.json_data) + , 'is_parent' + , taf.is_parent + , 'parent_uuid' + , tafp.unique_acquisition_framework_id) AS af_data + FROM + gn_meta.t_acquisition_frameworks taf + LEFT JOIN gn_meta.t_acquisition_frameworks tafp ON + tafp.id_acquisition_framework = taf.acquisition_framework_parent_id + JOIN af_actors ON af_actors.id_acquisition_framework = taf.id_acquisition_framework + LEFT JOIN ref_nomenclatures.t_nomenclatures ntl ON + taf.id_nomenclature_territorial_level = ntl.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures nft ON + taf.id_nomenclature_financing_type = nft.id_nomenclature + LEFT JOIN af_territories ON af_territories.id_acquisition_framework = + taf.id_acquisition_framework + LEFT JOIN af_objectives ON af_objectives.id_acquisition_framework = + taf.id_acquisition_framework + LEFT JOIN af_voletsinp ON af_voletsinp.id_acquisition_framework = + taf.id_acquisition_framework + LEFT JOIN af_publication ON af_publication.id_acquisition_framework = + taf.id_acquisition_framework + GROUP BY + taf.id_acquisition_framework + , taf.acquisition_framework_name + , taf.acquisition_framework_desc + , taf.acquisition_framework_start_date + , taf.acquisition_framework_end_date + , taf.initial_closing_date + , ntl.cd_nomenclature + , nft.cd_nomenclature + , af_territories.territories + , af_objectives.objectives + , af_voletsinp.voletsinp + , af_publication.publications + , taf.is_parent + , tafp.unique_acquisition_framework_id +) +, ds_actors AS ( + SELECT + cda.id_dataset + , json_build_object('type_role' + , CASE WHEN cda.id_organism IS NOT NULL THEN + 'organism'::TEXT + WHEN cda.id_role IS NOT NULL THEN + 'role'::TEXT + ELSE + NULL::TEXT + END + , 'uuid_actor' + , coalesce(borg.uuid_organisme + , tro.uuid_role) + , 'cd_nomenclature_actor_role' + , tn.cd_nomenclature + , 'identity' + , CASE WHEN cda.id_organism IS NOT NULL THEN + json_build_object('organism_name' + , borg.nom_organisme) + WHEN cda.id_role IS NOT NULL THEN + json_build_object('first_name' + , tro.nom_role + , 'last_name' + , tro.prenom_role) + END + , 'email' + , coalesce(borg.email_organisme + , tro.email)) AS json_data + FROM + gn_meta.cor_dataset_actor cda + LEFT JOIN utilisateurs.bib_organismes borg ON cda.id_organism = borg.id_organisme + LEFT JOIN utilisateurs.t_roles tro ON cda.id_role = tro.id_role + JOIN ref_nomenclatures.t_nomenclatures tn ON + cda.id_nomenclature_actor_role = tn.id_nomenclature +) +, ds_protocols AS ( + SELECT + cdp.id_dataset + , jsonb_build_object('uuid' + , sdp.unique_protocol_id + , 'name' + , sdp.protocol_name + , 'desc' + , sdp.protocol_desc + , 'url' + , sdp.protocol_url + , 'type' + , t_nomenclatures.cd_nomenclature) AS protocols + FROM + gn_meta.cor_dataset_protocol cdp + JOIN gn_meta.sinp_datatype_protocols sdp ON cdp.id_protocol = sdp.id_protocol + LEFT JOIN ref_nomenclatures.t_nomenclatures ON + sdp.id_nomenclature_protocol_type = t_nomenclatures.id_nomenclature +) +, ds AS ( + SELECT + tds.id_dataset + , tds.id_acquisition_framework , - af AS - (SELECT taf.id_acquisition_framework, - jsonb_build_object('uuid', taf.unique_acquisition_framework_id, 'name', - taf.acquisition_framework_name, - 'desc', taf.acquisition_framework_desc, 'start_date', - taf.acquisition_framework_start_date, 'end_date', - taf.acquisition_framework_end_date, - 'initial_closing_date', taf.initial_closing_date, 'territories', - af_territories.territories, 'territorial_level', - ntl.cd_nomenclature, 'territory_desc', taf.territory_desc, 'objectives', - af_objectives.objectives, 'publications', af_publication.publications, - 'financing_type', nft.cd_nomenclature, - 'target_description', - taf.target_description, 'ecologic_or_geologic_target', - taf.ecologic_or_geologic_target, 'sinp_theme', af_voletsinp.voletsinp, 'actors', - json_agg(af_actors.json_data), 'is_parent', taf.is_parent, 'parent_uuid', - tafp.unique_acquisition_framework_id) AS af_data - FROM gn_meta.t_acquisition_frameworks taf - LEFT JOIN gn_meta.t_acquisition_frameworks tafp - ON tafp.id_acquisition_framework = taf.acquisition_framework_parent_id - JOIN af_actors ON af_actors.id_acquisition_framework = taf.id_acquisition_framework - LEFT JOIN ref_nomenclatures.t_nomenclatures ntl - ON taf.id_nomenclature_territorial_level = ntl.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures nft - ON taf.id_nomenclature_financing_type = nft.id_nomenclature - LEFT JOIN af_territories ON af_territories.id_acquisition_framework = taf.id_acquisition_framework - LEFT JOIN af_objectives ON af_objectives.id_acquisition_framework = taf.id_acquisition_framework - LEFT JOIN af_voletsinp ON af_voletsinp.id_acquisition_framework = taf.id_acquisition_framework - LEFT JOIN af_publication ON af_publication.id_acquisition_framework = taf.id_acquisition_framework - GROUP BY taf.id_acquisition_framework, taf.acquisition_framework_name, taf.acquisition_framework_desc, - taf.acquisition_framework_start_date, taf.acquisition_framework_end_date, taf.initial_closing_date, - ntl.cd_nomenclature, nft.cd_nomenclature, af_territories.territories, af_objectives.objectives, - af_voletsinp.voletsinp, af_publication.publications, taf.is_parent, - tafp.unique_acquisition_framework_id), - ds_actors AS (SELECT cda.id_dataset, - json_build_object('type_role', - CASE - WHEN cda.id_organism IS NOT NULL THEN 'organism'::TEXT - WHEN cda.id_role IS NOT NULL THEN 'role'::TEXT - ELSE NULL::TEXT - END, 'uuid_actor', coalesce(borg.uuid_organisme, tro.uuid_role), - 'cd_nomenclature_actor_role', tn.cd_nomenclature, 'identity', - CASE - WHEN cda.id_organism IS NOT NULL - THEN json_build_object('organism_name', borg.nom_organisme) - WHEN cda.id_role IS NOT NULL THEN json_build_object('first_name', - tro.nom_role, - 'last_name', - tro.prenom_role) END, - 'email', coalesce(borg.email_organisme, tro.email)) AS json_data - FROM gn_meta.cor_dataset_actor cda - LEFT JOIN utilisateurs.bib_organismes borg ON cda.id_organism = borg.id_organisme - LEFT JOIN utilisateurs.t_roles tro ON cda.id_role = tro.id_role - JOIN ref_nomenclatures.t_nomenclatures tn - ON cda.id_nomenclature_actor_role = tn.id_nomenclature), - ds_protocols AS (SELECT cdp.id_dataset, - jsonb_build_object( - 'uuid', sdp.unique_protocol_id, - 'name', sdp.protocol_name, - 'desc', sdp.protocol_desc, - 'url', sdp.protocol_url, - 'type', t_nomenclatures.cd_nomenclature) AS protocols - FROM gn_meta.cor_dataset_protocol cdp - JOIN gn_meta.sinp_datatype_protocols sdp - ON cdp.id_protocol = sdp.id_protocol - LEFT JOIN ref_nomenclatures.t_nomenclatures - ON sdp.id_nomenclature_protocol_type = t_nomenclatures.id_nomenclature) - , - ds AS (SELECT tds.id_dataset, - tds.id_acquisition_framework, - -- tds.additional_data, - jsonb_build_object('uuid', tds.unique_dataset_id, 'name', tds.dataset_name, 'desc', tds.dataset_desc, - 'shortname', tds.dataset_shortname, 'data_type', ndt.cd_nomenclature, - 'keywords', tds.keywords, 'marine_domain', tds.marine_domain, - 'terrestrial_domain', tds.terrestrial_domain, 'collecting_method', - ncm.cd_nomenclature, 'protocols', - ds_protocols.protocols, - 'data_origin', ndo.cd_nomenclature, - 'dataset_objectif', ndso.cd_nomenclature, 'resource_type', nrt.cd_nomenclature, - 'source_status', nss.cd_nomenclature, 'territories', array_agg(DISTINCT - ref_nomenclatures.get_cd_nomenclature(cdt.id_nomenclature_territory)), - 'actors', json_agg(ds_actors.json_data)) AS dataset_data - FROM gn_meta.t_datasets tds - JOIN ds_actors ON ds_actors.id_dataset = tds.id_dataset - LEFT JOIN gn_meta.cor_dataset_territory cdt ON cdt.id_dataset = tds.id_dataset - LEFT JOIN ds_protocols ON ds_protocols.id_dataset = tds.id_dataset - LEFT JOIN ref_nomenclatures.t_nomenclatures ndt - ON tds.id_nomenclature_data_type = ndt.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures ncm - ON tds.id_nomenclature_collecting_method = ncm.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures ndo - ON tds.id_nomenclature_data_origin = ndo.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures ndso - ON tds.id_nomenclature_dataset_objectif = ndso.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures nrt - ON tds.id_nomenclature_resource_type = nrt.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures nss - ON tds.id_nomenclature_source_status = nss.id_nomenclature - GROUP BY tds.id_dataset, tds.id_acquisition_framework, tds.unique_dataset_id, tds.dataset_name, - tds.dataset_desc, tds.dataset_shortname, ndt.cd_nomenclature, ncm.cd_nomenclature, - ndo.cd_nomenclature, ndso.cd_nomenclature, nrt.cd_nomenclature, nss.cd_nomenclature, - ds_protocols.protocols) -SELECT s.id_synthese, - s.entity_source_pk_value AS id_source, - s.unique_id_sinp AS id_perm_sinp, - s.unique_id_sinp_grp AS id_perm_grp_sinp, - s.date_min AS date_debut, - s.date_max AS date_fin, - s.cd_nom, - s.meta_v_taxref AS version_taxref, - s.nom_cite, - s.count_min AS nombre_min, - s.count_max AS nombre_max, - s.altitude_min, - s.altitude_max, - s.depth_min AS profondeur_min, - s.depth_max AS profondeur_max, - s.observers AS observateurs, - s.determiner AS determinateur, - s.validator AS validateur, - s.sample_number_proof AS numero_preuve, - s.digital_proof AS preuve_numerique, - s.non_digital_proof AS preuve_non_numerique, - s.comment_context AS comment_releve, - s.comment_description AS comment_occurrence, - ds.dataset_data AS jdd_data, - af.af_data AS ca_data, - s.reference_biblio, - s.cd_hab AS code_habitat, - h.lb_hab_fr AS habitat, - s.place_name AS nom_lieu, - s.precision, - s.additional_data AS donnees_additionnelles, - st_astext(s.the_geom_4326) AS wkt_4326, - n1.cd_nomenclature AS nature_objet_geo, - n2.cd_nomenclature AS type_regroupement, - s.grp_method AS methode_regroupement, - n3.cd_nomenclature AS comportement, - n4.cd_nomenclature AS technique_obs, - n5.cd_nomenclature AS statut_biologique, - n6.cd_nomenclature AS etat_biologique, - n7.cd_nomenclature AS naturalite, - n8.cd_nomenclature AS preuve_existante, - n9.cd_nomenclature AS precision_diffusion, - n10.cd_nomenclature AS stade_vie, - n11.cd_nomenclature AS sexe, - n12.cd_nomenclature AS objet_denombrement, - n13.cd_nomenclature AS type_denombrement, - n14.cd_nomenclature AS niveau_sensibilite, - n15.cd_nomenclature AS statut_observation, - n16.cd_nomenclature AS floutage_dee, - n17.cd_nomenclature AS statut_source, - n18.cd_nomenclature AS type_info_geo, - n19.cd_nomenclature AS methode_determination, - n20.cd_nomenclature AS statut_validation, - coalesce(s.meta_update_date, s.meta_create_date) AS derniere_action -FROM gn_synthese.synthese s - JOIN ds ON ds.id_dataset = s.id_dataset - JOIN af ON ds.id_acquisition_framework = af.id_acquisition_framework - LEFT JOIN ref_habitats.habref h ON h.cd_hab = s.cd_hab - LEFT JOIN ref_nomenclatures.t_nomenclatures n1 ON s.id_nomenclature_geo_object_nature = n1.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n2 ON s.id_nomenclature_grp_typ = n2.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n3 ON s.id_nomenclature_behaviour = n3.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n4 ON s.id_nomenclature_obs_technique = n4.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n5 ON s.id_nomenclature_bio_status = n5.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n6 ON s.id_nomenclature_bio_condition = n6.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n7 ON s.id_nomenclature_naturalness = n7.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n8 ON s.id_nomenclature_exist_proof = n8.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n9 ON s.id_nomenclature_diffusion_level = n9.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n10 ON s.id_nomenclature_life_stage = n10.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n11 ON s.id_nomenclature_sex = n11.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n12 ON s.id_nomenclature_obj_count = n12.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n13 ON s.id_nomenclature_type_count = n13.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n14 ON s.id_nomenclature_sensitivity = n14.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n15 ON s.id_nomenclature_observation_status = n15.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n16 ON s.id_nomenclature_blurring = n16.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n17 ON s.id_nomenclature_source_status = n17.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n18 ON s.id_nomenclature_info_geo_type = n18.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n19 ON s.id_nomenclature_determination_method = n19.id_nomenclature - LEFT JOIN ref_nomenclatures.t_nomenclatures n20 ON s.id_nomenclature_valid_status = n20.id_nomenclature -ORDER BY s.id_synthese; + -- tds.additional_data, + jsonb_build_object('uuid' + , tds.unique_dataset_id + , 'name' + , tds.dataset_name + , 'desc' + , tds.dataset_desc + , 'shortname' + , tds.dataset_shortname + , 'data_type' + , ndt.cd_nomenclature + , 'keywords' + , tds.keywords + , 'marine_domain' + , tds.marine_domain + , 'terrestrial_domain' + , tds.terrestrial_domain + , 'collecting_method' + , ncm.cd_nomenclature + , 'protocols' + , ds_protocols.protocols + , 'data_origin' + , ndo.cd_nomenclature + , 'dataset_objectif' + , ndso.cd_nomenclature + , 'resource_type' + , nrt.cd_nomenclature + , 'source_status' + , nss.cd_nomenclature + , 'territories' + , array_agg(DISTINCT ref_nomenclatures.get_cd_nomenclature + (cdt.id_nomenclature_territory)) + , 'actors' + , json_agg(ds_actors.json_data)) AS dataset_data + FROM + gn_meta.t_datasets tds + JOIN ds_actors ON ds_actors.id_dataset = tds.id_dataset + LEFT JOIN gn_meta.cor_dataset_territory cdt ON cdt.id_dataset = tds.id_dataset + LEFT JOIN ds_protocols ON ds_protocols.id_dataset = tds.id_dataset + LEFT JOIN ref_nomenclatures.t_nomenclatures ndt ON + tds.id_nomenclature_data_type = ndt.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures ncm ON + tds.id_nomenclature_collecting_method = ncm.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures ndo ON + tds.id_nomenclature_data_origin = ndo.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures ndso ON + tds.id_nomenclature_dataset_objectif = ndso.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures nrt ON + tds.id_nomenclature_resource_type = nrt.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures nss ON + tds.id_nomenclature_source_status = nss.id_nomenclature + GROUP BY + tds.id_dataset + , tds.id_acquisition_framework + , tds.unique_dataset_id + , tds.dataset_name + , tds.dataset_desc + , tds.dataset_shortname + , ndt.cd_nomenclature + , ncm.cd_nomenclature + , ndo.cd_nomenclature + , ndso.cd_nomenclature + , nrt.cd_nomenclature + , nss.cd_nomenclature + , ds_protocols.protocols +) +SELECT + s.id_synthese + , s.entity_source_pk_value AS id_source + , s.unique_id_sinp AS id_perm_sinp + , s.unique_id_sinp_grp AS id_perm_grp_sinp + , s.date_min AS date_debut + , s.date_max AS date_fin + , s.cd_nom + , s.meta_v_taxref AS version_taxref + , s.nom_cite + , s.count_min AS nombre_min + , s.count_max AS nombre_max + , s.altitude_min + , s.altitude_max + , s.depth_min AS profondeur_min + , s.depth_max AS profondeur_max + , s.observers AS observateurs + , s.determiner AS determinateur + , s.validator AS validateur + , s.sample_number_proof AS numero_preuve + , s.digital_proof AS preuve_numerique + , s.non_digital_proof AS preuve_non_numerique + , s.comment_context AS comment_releve + , s.comment_description AS comment_occurrence + , ds.dataset_data AS jdd_data + , af.af_data AS ca_data + , s.reference_biblio + , s.cd_hab AS code_habitat + , h.lb_hab_fr AS habitat + , s.place_name AS nom_lieu + , s.precision + , s.additional_data AS donnees_additionnelles + , st_astext (s.the_geom_4326) AS wkt_4326 + , n1.cd_nomenclature AS nature_objet_geo + , n2.cd_nomenclature AS type_regroupement + , s.grp_method AS methode_regroupement + , n3.cd_nomenclature AS comportement + , n4.cd_nomenclature AS technique_obs + , n5.cd_nomenclature AS statut_biologique + , n6.cd_nomenclature AS etat_biologique + , n7.cd_nomenclature AS naturalite + , n8.cd_nomenclature AS preuve_existante + , n9.cd_nomenclature AS precision_diffusion + , n10.cd_nomenclature AS stade_vie + , n11.cd_nomenclature AS sexe + , n12.cd_nomenclature AS objet_denombrement + , n13.cd_nomenclature AS type_denombrement + , n14.cd_nomenclature AS niveau_sensibilite + , n15.cd_nomenclature AS statut_observation + , n16.cd_nomenclature AS floutage_dee + , n17.cd_nomenclature AS statut_source + , n18.cd_nomenclature AS type_info_geo + , n19.cd_nomenclature AS methode_determination + , n20.cd_nomenclature AS statut_validation + , coalesce(s.meta_update_date , s.meta_create_date) AS derniere_action +FROM + gn_synthese.synthese s + JOIN ds ON ds.id_dataset = s.id_dataset + JOIN af ON ds.id_acquisition_framework = af.id_acquisition_framework + LEFT JOIN ref_habitats.habref h ON h.cd_hab = s.cd_hab + LEFT JOIN ref_nomenclatures.t_nomenclatures n1 ON + s.id_nomenclature_geo_object_nature = n1.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n2 ON s.id_nomenclature_grp_typ + = n2.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n3 ON + s.id_nomenclature_behaviour = n3.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n4 ON + s.id_nomenclature_obs_technique = n4.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n5 ON + s.id_nomenclature_bio_status = n5.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n6 ON + s.id_nomenclature_bio_condition = n6.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n7 ON + s.id_nomenclature_naturalness = n7.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n8 ON + s.id_nomenclature_exist_proof = n8.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n9 ON + s.id_nomenclature_diffusion_level = n9.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n10 ON + s.id_nomenclature_life_stage = n10.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n11 ON s.id_nomenclature_sex = + n11.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n12 ON + s.id_nomenclature_obj_count = n12.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n13 ON + s.id_nomenclature_type_count = n13.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n14 ON + s.id_nomenclature_sensitivity = n14.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n15 ON + s.id_nomenclature_observation_status = n15.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n16 ON + s.id_nomenclature_blurring = n16.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n17 ON + s.id_nomenclature_source_status = n17.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n18 ON + s.id_nomenclature_info_geo_type = n18.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n19 ON + s.id_nomenclature_determination_method = n19.id_nomenclature + LEFT JOIN ref_nomenclatures.t_nomenclatures n20 ON + s.id_nomenclature_valid_status = n20.id_nomenclature +ORDER BY + s.id_synthese; diff --git a/gn2pg/data/to_gnsynthese.sql b/gn2pg/data/to_gnsynthese.sql index e1263c2..8b99b9c 100644 --- a/gn2pg/data/to_gnsynthese.sql +++ b/gn2pg/data/to_gnsynthese.sql @@ -13,980 +13,1026 @@ 3. insert or update synthese data (from source/id_synthese, UUID may be NULL...) */ /* Acquisition Frameworks */ - -BEGIN -; +BEGIN; CREATE SCHEMA IF NOT EXISTS gn2pg_import; -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name (_uuid UUID, _name TEXT) -; +DROP FUNCTION IF EXISTS + gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name (_uuid UUID , + _name TEXT); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name(_uuid UUID, _name TEXT) +CREATE OR REPLACE FUNCTION + gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name (_uuid UUID , + _name TEXT) RETURNS INTEGER -AS -$func$ + AS $func$ DECLARE the_af_id INT; BEGIN - INSERT INTO gn_meta.t_acquisition_frameworks ( unique_acquisition_framework_id - , acquisition_framework_name - , acquisition_framework_desc - , acquisition_framework_start_date - , meta_create_date) - SELECT _uuid - , _name - , 'Description of acquisition framework : ' || _name - , now() - , now() - WHERE NOT exists(SELECT 1 - FROM gn_meta.t_acquisition_frameworks - WHERE unique_acquisition_framework_id = _uuid); - SELECT id_acquisition_framework - INTO the_af_id - FROM gn_meta.t_acquisition_frameworks - WHERE unique_acquisition_framework_id = _uuid; + INSERT INTO gn_meta.t_acquisition_frameworks + (unique_acquisition_framework_id , acquisition_framework_name , + acquisition_framework_desc , acquisition_framework_start_date , + meta_create_date) + SELECT + _uuid + , _name + , 'Description of acquisition framework : ' || _name + , now() + , now() + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_meta.t_acquisition_frameworks + WHERE + unique_acquisition_framework_id = _uuid); + SELECT + id_acquisition_framework INTO the_af_id + FROM + gn_meta.t_acquisition_frameworks + WHERE + unique_acquisition_framework_id = _uuid; RETURN the_af_id; END $func$ - LANGUAGE plpgsql -; +LANGUAGE plpgsql; + +COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name + (_uuid UUID , _name TEXT) IS 'function to basically create acquisition framework'; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name (_uuid UUID, _name TEXT) IS 'function to basically create acquisition framework' -; /* Datasets */ -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name (_uuid UUID, _name TEXT, _id_af INT) -; +DROP FUNCTION IF EXISTS + gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name (_uuid UUID , + _name TEXT , _id_af INT); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name(_uuid UUID, _name TEXT, _id_af INT) +CREATE OR REPLACE FUNCTION + gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name (_uuid UUID , + _name TEXT , _id_af INT) RETURNS INTEGER -AS -$func$ + AS $func$ DECLARE the_dataset_id INT; BEGIN - INSERT INTO gn_meta.t_datasets ( unique_dataset_id - , id_acquisition_framework - , dataset_name - , dataset_shortname - , dataset_desc - , marine_domain - , terrestrial_domain - , meta_create_date) - SELECT _uuid - , _id_af - , _name - , left(_name, - 30) - , 'A compléter' - , TRUE - , TRUE - , now() - WHERE NOT exists(SELECT 1 - FROM gn_meta.t_datasets - WHERE unique_dataset_id = _uuid); - SELECT id_dataset - INTO the_dataset_id - FROM gn_meta.t_datasets - WHERE unique_dataset_id = _uuid; + INSERT INTO gn_meta.t_datasets (unique_dataset_id , + id_acquisition_framework , dataset_name , dataset_shortname , + dataset_desc , marine_domain , terrestrial_domain , meta_create_date) + SELECT + _uuid + , _id_af + , _name + , + LEFT (_name + , 30) + , 'A compléter' + , TRUE + , TRUE + , now() +WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_meta.t_datasets + WHERE + unique_dataset_id = _uuid); + SELECT + id_dataset INTO the_dataset_id + FROM + gn_meta.t_datasets + WHERE + unique_dataset_id = _uuid; RETURN the_dataset_id; END $func$ - LANGUAGE plpgsql -; +LANGUAGE plpgsql; + +COMMENT ON FUNCTION + gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name (_uuid UUID , + _name TEXT , _id_af INT) IS 'function to basically create datasets'; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name (_uuid UUID, _name TEXT, _id_af INT) IS 'function to basically create datasets' -; /* Sources */ -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_or_insert_source (_source TEXT) -; +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_or_insert_source (_source TEXT); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_source(_source TEXT) +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_source (_source TEXT) RETURNS INTEGER -AS -$func$ + AS $func$ DECLARE the_source_id INT; BEGIN INSERT INTO gn_synthese.t_sources (name_source) - SELECT _source - WHERE NOT exists(SELECT 1 - FROM gn_synthese.t_sources - WHERE name_source = _source); - SELECT id_source - INTO the_source_id - FROM gn_synthese.t_sources - WHERE name_source = _source; + SELECT + _source + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_synthese.t_sources + WHERE + name_source = _source); + SELECT + id_source INTO the_source_id + FROM + gn_synthese.t_sources + WHERE + name_source = _source; RETURN the_source_id; END $func$ - LANGUAGE plpgsql -; +LANGUAGE plpgsql; + +COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_source (_source TEXT) IS + 'function to basically create new sources'; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_source (_source TEXT) IS 'function to basically create new sources' -; /* Nomenclatures */ -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_id_nomenclature_from_label (_type TEXT, _label TEXT) -; +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_get_id_nomenclature_from_label + (_type TEXT , _label TEXT); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_id_nomenclature_from_label(_type TEXT, _label TEXT) +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_id_nomenclature_from_label + (_type TEXT , _label TEXT) RETURNS INTEGER -AS -$func$ + AS $func$ DECLARE the_id_nomenclature INT; BEGIN - SELECT id_nomenclature - INTO the_id_nomenclature - FROM ref_nomenclatures.t_nomenclatures n - JOIN ref_nomenclatures.bib_nomenclatures_types t ON n.id_type = t.id_type - WHERE t.mnemonique LIKE _type - AND n.label_default = _label; + SELECT + id_nomenclature INTO the_id_nomenclature + FROM + ref_nomenclatures.t_nomenclatures n + JOIN ref_nomenclatures.bib_nomenclatures_types t ON n.id_type = t.id_type + WHERE + t.mnemonique LIKE _type + AND n.label_default = _label; RETURN the_id_nomenclature; END $func$ - LANGUAGE plpgsql -; +LANGUAGE plpgsql; + +COMMENT ON FUNCTION gn2pg_import.fct_c_get_id_nomenclature_from_label (_type + TEXT , _label TEXT) IS 'function to retrieve nomenclature ID from label'; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_id_nomenclature_from_label (_type TEXT, _label TEXT) IS 'function to retrieve nomenclature ID from label' -; /* Add unique constraint to synthese on id_source and /entity_source_pk_value */ -CREATE UNIQUE INDEX IF NOT EXISTS uidx_synthese_id_source_id_entity_source_pk_value ON gn_synthese.synthese (id_source, entity_source_pk_value) -; +CREATE UNIQUE INDEX IF NOT EXISTS + uidx_synthese_id_source_id_entity_source_pk_value ON gn_synthese.synthese + (id_source , entity_source_pk_value); -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_territories(_id_af INTEGER, _territories JSONB); +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_territories (_id_af + INTEGER , _territories JSONB); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_territories(_id_af INTEGER, _territories JSONB) RETURNS BOOLEAN +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_territories (_id_af + INTEGER , _territories JSONB) + RETURNS BOOLEAN LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - RAISE DEBUG '_id_af %, territories %', _id_af::INT, _territories; + RAISE DEBUG '_id_af %, territories %' , _id_af::INT , _territories; - FOR i IN (SELECT jsonb_array_elements_text(_territories) item) + FOR i IN ( + SELECT + jsonb_array_elements_text(_territories) item) LOOP - RAISE DEBUG 'iterritory % %',i, i.item; - INSERT INTO gn_meta.cor_acquisition_framework_territory (id_acquisition_framework, id_nomenclature_territory) - VALUES (_id_af, - ref_nomenclatures.get_id_nomenclature('TERRITOIRE', i.item)) - ON CONFLICT DO NOTHING; + RAISE DEBUG 'iterritory % %' , i , i.item; + INSERT INTO gn_meta.cor_acquisition_framework_territory + (id_acquisition_framework , id_nomenclature_territory) + VALUES (_id_af , ref_nomenclatures.get_id_nomenclature ('TERRITOIRE' , i.item)) + ON CONFLICT + DO NOTHING; END LOOP; -RETURN TRUE; + RETURN TRUE; END $$; +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_objectives (_id_af INTEGER + , _objectives JSONB); -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_objectives(_id_af INTEGER, _objectives JSONB); - -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_objectives(_id_af INTEGER, _objectives JSONB) RETURNS BOOLEAN +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_objectives (_id_af + INTEGER , _objectives JSONB) + RETURNS BOOLEAN LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - RAISE DEBUG '_id_af %, objectives %', _id_af::INT, _objectives; + RAISE DEBUG '_id_af %, objectives %' , _id_af::INT , _objectives; - FOR i IN (SELECT jsonb_array_elements_text(_objectives) item) + FOR i IN ( + SELECT + jsonb_array_elements_text(_objectives) item) LOOP - RAISE DEBUG 'iterritory % %',i, i.item; - INSERT INTO gn_meta.cor_acquisition_framework_objectif (id_acquisition_framework, id_nomenclature_objectif) - VALUES (_id_af, - ref_nomenclatures.get_id_nomenclature('CA_OBJECTIFS', i.item)) - ON CONFLICT DO NOTHING; + RAISE DEBUG 'iterritory % %' , i , i.item; + INSERT INTO gn_meta.cor_acquisition_framework_objectif + (id_acquisition_framework , id_nomenclature_objectif) + VALUES (_id_af , ref_nomenclatures.get_id_nomenclature ('CA_OBJECTIFS' , i.item)) + ON CONFLICT + DO NOTHING; END LOOP; -RETURN TRUE; + RETURN TRUE; END $$; +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_publications (_id_af + INTEGER , _objectives JSONB); -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_af_publications(_id_af INTEGER, _objectives JSONB); - -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_publications(_id_af INTEGER, _publications JSONB) RETURNS VOID +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_publications (_id_af + INTEGER , _publications JSONB) + RETURNS VOID LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - RAISE DEBUG '_id_af %, territories %', _id_af::INT, _objectives; + RAISE DEBUG '_id_af %, territories %' , _id_af::INT , _objectives; - FOR i IN (SELECT jsonb_array_elements_text(_objectives) item) + FOR i IN ( + SELECT + jsonb_array_elements_text(_objectives) item) LOOP - RAISE DEBUG 'iterritory % %',i, i.item; - INSERT INTO gn_meta.sinp_datatype_publications (id_acquisition_framework, id_publication) - VALUES (_id_af, - ref_nomenclatures.get_id_nomenclature('CA_OBJECTIFS', i.item)) - ON CONFLICT DO NOTHING; + RAISE DEBUG 'iterritory % %' , i , i.item; + INSERT INTO gn_meta.sinp_datatype_publications + (id_acquisition_framework , id_publication) + VALUES (_id_af , ref_nomenclatures.get_id_nomenclature ('CA_OBJECTIFS' , i.item)) + ON CONFLICT + DO NOTHING; END LOOP; END $$; -DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_ds_territories(_id_ds INTEGER, _territories JSONB); +DROP FUNCTION IF EXISTS gn2pg_import.fct_c_insert_ds_territories (_id_ds + INTEGER , _territories JSONB); -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_ds_territories(_id_ds INTEGER, _territories JSONB) RETURNS VOID +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_ds_territories (_id_ds + INTEGER , _territories JSONB) + RETURNS VOID LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - RAISE DEBUG '_id_ds %, territories %', _id_ds::INT, _territories; + RAISE DEBUG '_id_ds %, territories %' , _id_ds::INT , _territories; - FOR i IN (SELECT jsonb_array_elements_text(_territories) item) + FOR i IN ( + SELECT + jsonb_array_elements_text(_territories) item) LOOP - RAISE DEBUG 'iterritory % %',i, i.item; - INSERT INTO gn_meta.cor_dataset_territory (id_dataset, id_nomenclature_territory) - VALUES (_id_ds, - ref_nomenclatures.get_id_nomenclature('TERRITOIRE', i.item)) - ON CONFLICT DO NOTHING; + RAISE DEBUG 'iterritory % %' , i , i.item; + INSERT INTO gn_meta.cor_dataset_territory (id_dataset , id_nomenclature_territory) + VALUES (_id_ds , ref_nomenclatures.get_id_nomenclature ('TERRITOIRE' , i.item)) + ON CONFLICT + DO NOTHING; END LOOP; END $$; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_create_actors_in_usershub(_actor_role JSONB, _source CHARACTER VARYING) RETURNS INTEGER +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_create_actors_in_usershub + (_actor_role JSONB , _source CHARACTER VARYING) + RETURNS INTEGER LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE the_id_actor INTEGER; BEGIN - RAISE DEBUG '_actor_role %', _actor_role; + RAISE DEBUG '_actor_role %' , _actor_role; IF _actor_role ->> 'type_role' = 'organism' THEN - INSERT INTO utilisateurs.bib_organismes (uuid_organisme, nom_organisme, email_organisme, additional_data) - VALUES ( (_actor_role ->> 'uuid_actor')::UUID - , _actor_role #>> '{identity,organism_name}' - , _actor_role ->> 'email' - , jsonb_build_object('source', _source, 'module', 'gn2pg')) - ON CONFLICT (uuid_organisme) DO NOTHING; - SELECT id_organisme - INTO the_id_actor - FROM utilisateurs.bib_organismes - WHERE uuid_organisme = (_actor_role ->> 'uuid_actor')::UUID; + INSERT INTO utilisateurs.bib_organismes (uuid_organisme , nom_organisme + , email_organisme , additional_data) + VALUES ((_actor_role ->> 'uuid_actor')::UUID , _actor_role #>> + '{identity,organism_name}' , _actor_role ->> 'email' , + jsonb_build_object('source' , _source , 'module' + , 'gn2pg')) + ON CONFLICT (uuid_organisme) + DO NOTHING; + SELECT + id_organisme INTO the_id_actor + FROM + utilisateurs.bib_organismes + WHERE + uuid_organisme = (_actor_role ->> 'uuid_actor')::UUID; ELSIF _actor_role ->> 'type_role' = 'role' THEN - INSERT INTO utilisateurs.t_roles (uuid_role, nom_role, prenom_role, email, champs_addi) - VALUES ( (_actor_role ->> 'uuid_actor')::UUID - , _actor_role #>> '{identity,first_name}' - , _actor_role #>> '{identity,last_name}' - , NULL - , jsonb_build_object('source', _source, 'module', 'gn2pg', 'gn2pg_data', - jsonb_build_object('email', _actor_role ->> 'email'))) - ON CONFLICT (uuid_role) DO NOTHING; - SELECT id_role - INTO the_id_actor - FROM utilisateurs.t_roles - WHERE uuid_role = (_actor_role ->> 'uuid_actor')::UUID; + INSERT INTO utilisateurs.t_roles (uuid_role , nom_role , prenom_role , + email , champs_addi) + VALUES ((_actor_role ->> 'uuid_actor')::UUID , _actor_role #>> + '{identity,first_name}' , _actor_role #>> '{identity,last_name}' , NULL , + jsonb_build_object('source' , _source , 'module' + , 'gn2pg' , 'gn2pg_data' , + jsonb_build_object('email' , _actor_role ->> + 'email'))) + ON CONFLICT (uuid_role) + DO NOTHING; + SELECT + id_role INTO the_id_actor + FROM + utilisateurs.t_roles + WHERE + uuid_role = (_actor_role ->> 'uuid_actor')::UUID; END IF; RETURN the_id_actor; END $$; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_dataset_actor(_id_dataset INTEGER, _actor_roles JSONB, _source CHARACTER VARYING) RETURNS VOID +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_dataset_actor (_id_dataset + INTEGER , _actor_roles JSONB , _source CHARACTER VARYING) + RETURNS VOID LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - FOR i IN (SELECT jsonb_array_elements(_actor_roles) item) + FOR i IN ( + SELECT + jsonb_array_elements(_actor_roles) item) LOOP IF i.item ->> 'type_role' = 'organism' THEN - INSERT INTO gn_meta.cor_dataset_actor (id_dataset, id_organism, id_nomenclature_actor_role) - VALUES ( _id_dataset - , gn2pg_import.fct_c_get_or_create_actors_in_usershub(i.item, _source) - , ref_nomenclatures.get_id_nomenclature('ROLE_ACTEUR', i.item ->> 'cd_nomenclature_actor_role')) - ON CONFLICT DO NOTHING; + INSERT INTO gn_meta.cor_dataset_actor (id_dataset , id_organism + , id_nomenclature_actor_role) + VALUES (_id_dataset , + gn2pg_import.fct_c_get_or_create_actors_in_usershub + (i.item , _source) , + ref_nomenclatures.get_id_nomenclature ('ROLE_ACTEUR' + , i.item ->> 'cd_nomenclature_actor_role')) + ON CONFLICT + DO NOTHING; ELSIF i.item ->> 'type_role' = 'role' THEN - INSERT INTO gn_meta.cor_dataset_actor (id_dataset, id_role, id_nomenclature_actor_role) - VALUES ( _id_dataset - , gn2pg_import.fct_c_get_or_create_actors_in_usershub(i.item, _source) - , ref_nomenclatures.get_id_nomenclature('ROLE_ACTEUR', i.item ->> 'cd_nomenclature_actor_role')) - ON CONFLICT DO NOTHING; + INSERT INTO gn_meta.cor_dataset_actor (id_dataset , id_role , + id_nomenclature_actor_role) + VALUES (_id_dataset , + gn2pg_import.fct_c_get_or_create_actors_in_usershub + (i.item , _source) , + ref_nomenclatures.get_id_nomenclature ('ROLE_ACTEUR' + , i.item ->> 'cd_nomenclature_actor_role')) + ON CONFLICT + DO NOTHING; END IF; END LOOP; END $$; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_actors(_id_af INTEGER, _actor_roles JSONB, _source CHARACTER VARYING) RETURNS VOID +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_insert_af_actors (_id_af INTEGER + , _actor_roles JSONB , _source CHARACTER VARYING) + RETURNS VOID LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE i RECORD; BEGIN - RAISE DEBUG '_id_af %', _id_af::INT; - FOR i IN (SELECT jsonb_array_elements(_actor_roles) item) + RAISE DEBUG '_id_af %' , _id_af::INT; + FOR i IN ( + SELECT + jsonb_array_elements(_actor_roles) item) LOOP - IF i.item ->> 'type_role' = 'organism' THEN - INSERT INTO gn_meta.cor_acquisition_framework_actor (id_acquisition_framework, id_organism, id_nomenclature_actor_role) - VALUES ( _id_af - , gn2pg_import.fct_c_get_or_create_actors_in_usershub(i.item, _source) - , ref_nomenclatures.get_id_nomenclature('ROLE_ACTEUR', i.item ->> 'cd_nomenclature_actor_role')) - ON CONFLICT DO NOTHING; + INSERT INTO gn_meta.cor_acquisition_framework_actor + (id_acquisition_framework , id_organism , + id_nomenclature_actor_role) + VALUES (_id_af , + gn2pg_import.fct_c_get_or_create_actors_in_usershub + (i.item , _source) , + ref_nomenclatures.get_id_nomenclature ('ROLE_ACTEUR' + , i.item ->> 'cd_nomenclature_actor_role')) + ON CONFLICT + DO NOTHING; ELSIF i.item ->> 'type_role' = 'role' THEN - INSERT INTO gn_meta.cor_acquisition_framework_actor (id_acquisition_framework, id_role, id_nomenclature_actor_role) - VALUES ( _id_af - , gn2pg_import.fct_c_get_or_create_actors_in_usershub(i.item, _source) - , ref_nomenclatures.get_id_nomenclature('ROLE_ACTEUR', i.item ->> 'cd_nomenclature_actor_role')) - ON CONFLICT DO NOTHING; + INSERT INTO gn_meta.cor_acquisition_framework_actor + (id_acquisition_framework , id_role , + id_nomenclature_actor_role) + VALUES (_id_af , + gn2pg_import.fct_c_get_or_create_actors_in_usershub + (i.item , _source) , + ref_nomenclatures.get_id_nomenclature ('ROLE_ACTEUR' + , i.item ->> 'cd_nomenclature_actor_role')) + ON CONFLICT + DO NOTHING; END IF; END LOOP; END $$; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata(_af_data JSONB, _source CHARACTER VARYING) RETURNS INTEGER +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata + (_af_data JSONB , _source CHARACTER VARYING) + RETURNS INTEGER LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE the_af_id INT; BEGIN - INSERT INTO gn_meta.t_acquisition_frameworks ( unique_acquisition_framework_id - , acquisition_framework_name - , acquisition_framework_desc - , acquisition_framework_start_date - , acquisition_framework_end_date - , ecologic_or_geologic_target - , id_nomenclature_financing_type - , id_nomenclature_territorial_level - , initial_closing_date - , target_description --- , additional_data - , meta_create_date - , meta_update_date) - SELECT (_af_data #>> '{uuid}')::UUID - , (_af_data #>> '{name}')::VARCHAR - , (_af_data #>> '{desc}')::VARCHAR - , (_af_data #>> '{start_date}')::DATE - , (_af_data #>> '{end_date}')::DATE - , (_af_data #>> '{ecologic_or_geologic_target}')::VARCHAR - , (ref_nomenclatures.get_id_nomenclature('TYPE_FINANCEMENT', _af_data #>> '{financing_type}'))::INT - , (ref_nomenclatures.get_id_nomenclature('NIVEAU_TERRITORIAL', _af_data #>> '{territorial_level}'))::INT - , (_af_data #>> '{initial_closing_date}')::TIMESTAMP - , (_af_data #>> '{target_description}')::VARCHAR --- , jsonb_build_object('source', _source, 'module', 'gn2pg') - , now() - , now() - WHERE NOT exists(SELECT 1 - FROM gn_meta.t_acquisition_frameworks - WHERE unique_acquisition_framework_id = (_af_data #>> '{uuid}')::UUID); - SELECT id_acquisition_framework - INTO the_af_id - FROM gn_meta.t_acquisition_frameworks - WHERE unique_acquisition_framework_id = (_af_data #>> '{uuid}')::UUID; - RAISE DEBUG 'the_af_id %', the_af_id; + INSERT INTO gn_meta.t_acquisition_frameworks + (unique_acquisition_framework_id , acquisition_framework_name , + acquisition_framework_desc , acquisition_framework_start_date , + acquisition_framework_end_date , ecologic_or_geologic_target , + id_nomenclature_financing_type , id_nomenclature_territorial_level , + initial_closing_date , target_description + -- , additional_data + , meta_create_date , meta_update_date) + SELECT + (_af_data #>> '{uuid}')::UUID + , (_af_data #>> '{name}')::VARCHAR + , (_af_data #>> '{desc}')::VARCHAR + , (_af_data #>> '{start_date}')::DATE + , (_af_data #>> '{end_date}')::DATE + , (_af_data #>> '{ecologic_or_geologic_target}')::VARCHAR + , (ref_nomenclatures.get_id_nomenclature ('TYPE_FINANCEMENT' , _af_data + #>> '{financing_type}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('NIVEAU_TERRITORIAL' , _af_data + #>> '{territorial_level}'))::INT + , (_af_data #>> '{initial_closing_date}')::TIMESTAMP + , (_af_data #>> '{target_description}')::VARCHAR + , now() + , now() + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_meta.t_acquisition_frameworks + WHERE + unique_acquisition_framework_id = (_af_data #>> '{uuid}')::UUID); + SELECT + id_acquisition_framework INTO the_af_id + FROM + gn_meta.t_acquisition_frameworks + WHERE + unique_acquisition_framework_id = (_af_data #>> '{uuid}')::UUID; + RAISE DEBUG 'the_af_id %' , the_af_id; PERFORM - gn2pg_import.fct_c_insert_af_actors(the_af_id, _af_data -> 'actors', _source); + gn2pg_import.fct_c_insert_af_actors (the_af_id , _af_data -> 'actors' , _source); RETURN the_af_id; END $$; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata(JSONB, VARCHAR) IS 'function to create acquisition framework from json structured data'; +COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata (JSONB + , VARCHAR) IS 'function to create acquisition framework from json structured data'; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata(_ds_data JSONB, _id_af INTEGER, _source CHARACTER VARYING) RETURNS INTEGER +CREATE OR REPLACE FUNCTION + gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata (_ds_data JSONB , + _id_af INTEGER , _source CHARACTER VARYING) + RETURNS INTEGER LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE the_dataset_id INT; BEGIN - RAISE DEBUG 'data_type %', _ds_data #>> '{data_type}'; - INSERT INTO gn_meta.t_datasets ( unique_dataset_id - , id_acquisition_framework - , dataset_name - , dataset_shortname - , dataset_desc - , marine_domain - , terrestrial_domain - , id_nomenclature_source_status - , id_nomenclature_resource_type - , id_nomenclature_dataset_objectif - , id_nomenclature_data_origin - , id_nomenclature_collecting_method - , id_nomenclature_data_type --- , additional_data - , meta_create_date - , meta_update_date) - SELECT (_ds_data #>> '{uuid}')::UUID - , _id_af - , (_ds_data #>> '{name}')::VARCHAR - , (_ds_data #>> '{shortname}')::VARCHAR - , coalesce((_ds_data #>> '{desc}')::VARCHAR, '...') - , coalesce((_ds_data #>> '{marine_domain}')::BOOL, FALSE) - , coalesce((_ds_data #>> '{terrestrial_domain}')::BOOL, FALSE) - , (ref_nomenclatures.get_id_nomenclature('STATUT_SOURCE', _ds_data #>> '{source_status}'))::INT - , (ref_nomenclatures.get_id_nomenclature('RESOURCE_TYP', _ds_data #>> '{resource_type}'))::INT - , (ref_nomenclatures.get_id_nomenclature('JDD_OBJECTIFS', _ds_data #>> '{dataset_objectif}'))::INT - , (ref_nomenclatures.get_id_nomenclature('DS_PUBLIQUE', _ds_data #>> '{data_origin}'))::INT - , (ref_nomenclatures.get_id_nomenclature('METHO_RECUEIL', _ds_data #>> '{collecting_method}'))::INT - , (ref_nomenclatures.get_id_nomenclature('DATA_TYP', _ds_data #>> '{data_type}'))::INT --- , jsonb_build_object('source', _source, 'module', 'gn2pg') - , now() - , now() - WHERE NOT exists(SELECT 1 - FROM gn_meta.t_datasets - WHERE unique_dataset_id = (_ds_data #>> '{uuid}')::UUID); - SELECT id_dataset - INTO the_dataset_id - FROM gn_meta.t_datasets - WHERE unique_dataset_id = (_ds_data #>> '{uuid}')::UUID; + RAISE DEBUG 'data_type %' , _ds_data #>> '{data_type}'; + INSERT INTO gn_meta.t_datasets (unique_dataset_id , + id_acquisition_framework , dataset_name , dataset_shortname , + dataset_desc , marine_domain , terrestrial_domain , + id_nomenclature_source_status , id_nomenclature_resource_type , + id_nomenclature_dataset_objectif , id_nomenclature_data_origin , + id_nomenclature_collecting_method , id_nomenclature_data_type + -- , additional_data + , meta_create_date , meta_update_date) + SELECT + (_ds_data #>> '{uuid}')::UUID + , _id_af + , (_ds_data #>> '{name}')::VARCHAR + , (_ds_data #>> '{shortname}')::VARCHAR + , coalesce((_ds_data #>> '{desc}')::VARCHAR , '...') + , coalesce((_ds_data #>> '{marine_domain}')::BOOL , FALSE) + , coalesce((_ds_data #>> '{terrestrial_domain}')::BOOL , FALSE) + , (ref_nomenclatures.get_id_nomenclature ('STATUT_SOURCE' , _ds_data + #>> '{source_status}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('RESOURCE_TYP' , _ds_data + #>> '{resource_type}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('JDD_OBJECTIFS' , _ds_data + #>> '{dataset_objectif}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('DS_PUBLIQUE' , _ds_data + #>> '{data_origin}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('METHO_RECUEIL' , _ds_data + #>> '{collecting_method}'))::INT + , (ref_nomenclatures.get_id_nomenclature ('DATA_TYP' , _ds_data + #>> '{data_type}'))::INT + , now() + , now() + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_meta.t_datasets + WHERE + unique_dataset_id = (_ds_data #>> '{uuid}')::UUID); + SELECT + id_dataset INTO the_dataset_id + FROM + gn_meta.t_datasets + WHERE + unique_dataset_id = (_ds_data #>> '{uuid}')::UUID; PERFORM - gn2pg_import.fct_c_insert_dataset_actor(the_dataset_id, _ds_data -> 'actors', _source); + gn2pg_import.fct_c_insert_dataset_actor (the_dataset_id , _ds_data -> + 'actors' , _source); IF jsonb_array_length(_ds_data -> 'territories') > 0 THEN PERFORM - gn2pg_import.fct_c_insert_ds_territories(the_dataset_id, _ds_data -> 'territories'); + gn2pg_import.fct_c_insert_ds_territories (the_dataset_id , _ds_data -> 'territories'); END IF; RETURN the_dataset_id; END $$; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata(JSONB, INTEGER, VARCHAR) IS 'function to basically create datasets'; +COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata + (JSONB , INTEGER , VARCHAR) IS 'function to basically create datasets'; -CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_source(_source TEXT) RETURNS INTEGER +CREATE OR REPLACE FUNCTION gn2pg_import.fct_c_get_or_insert_source (_source TEXT) + RETURNS INTEGER LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE the_source_id INT; BEGIN INSERT INTO gn_synthese.t_sources (name_source) - SELECT _source - WHERE NOT exists(SELECT 1 - FROM gn_synthese.t_sources - WHERE name_source = _source); - SELECT id_source - INTO the_source_id - FROM gn_synthese.t_sources - WHERE name_source = _source; + SELECT + _source + WHERE + NOT EXISTS ( + SELECT + 1 + FROM + gn_synthese.t_sources + WHERE + name_source = _source); + SELECT + id_source INTO the_source_id + FROM + gn_synthese.t_sources + WHERE + name_source = _source; RETURN the_source_id; END $$; -COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_source(TEXT) IS 'function to basically create new sources'; +COMMENT ON FUNCTION gn2pg_import.fct_c_get_or_insert_source (TEXT) IS 'function to basically create new sources'; -- UPSERT -CREATE OR REPLACE FUNCTION gn2pg_import.fct_tri_c_upsert_data_to_geonature() RETURNS TRIGGER +CREATE OR REPLACE FUNCTION gn2pg_import.fct_tri_c_upsert_data_to_geonature () + RETURNS TRIGGER LANGUAGE plpgsql -AS -$$ + AS $$ DECLARE - _local_srid INT; - the_unique_id_sinp UUID; - the_unique_id_sinp_grp UUID; - the_id_source INT; - the_entity_source_pk_value INT; - the_id_af INT; - the_id_dataset INT; - the_id_nomenclature_geo_object_nature INT; - the_id_nomenclature_grp_typ INT; - the_grp_method VARCHAR; - the_id_nomenclature_obs_technique INT; - the_id_nomenclature_bio_status INT; - the_id_nomenclature_bio_condition INT; - the_id_nomenclature_naturalness INT; - the_id_nomenclature_exist_proof INT; - the_id_nomenclature_valid_status INT; - the_id_nomenclature_diffusion_level INT; - the_id_nomenclature_life_stage INT; - the_id_nomenclature_sex INT; - the_id_nomenclature_obj_count INT; - the_id_nomenclature_type_count INT; - the_id_nomenclature_sensitivity INT; - the_id_nomenclature_observation_status INT; - the_id_nomenclature_blurring INT; - the_id_nomenclature_source_status INT; - the_id_nomenclature_info_geo_type INT; - the_id_nomenclature_behaviour INT; - the_id_nomenclature_biogeo_status INT; - the_reference_biblio VARCHAR; - the_count_min INT; - the_count_max INT; - the_cd_nom INT; - the_cd_hab INT; - the_nom_cite VARCHAR; - the_meta_v_taxref VARCHAR; - the_sample_number_proof TEXT; - the_digital_proof TEXT; - the_non_digital_proof TEXT; - the_altitude_min INT; - the_altitude_max INT; - the_depth_min INT; - the_depth_max INT; - the_place_name VARCHAR; - _the_geom_4326 GEOMETRY; - _the_geom_point GEOMETRY; - _the_geom_local GEOMETRY; - the_precision INT; - the_date_min TIMESTAMP; - the_date_max TIMESTAMP; - the_validator VARCHAR; - the_validation_comment TEXT; - the_observers TEXT; - the_determiner TEXT; - the_id_digitiser INT; + _local_srid INT; + the_unique_id_sinp UUID; + the_unique_id_sinp_grp UUID; + the_id_source INT; + the_entity_source_pk_value INT; + the_id_af INT; + the_id_dataset INT; + the_id_nomenclature_geo_object_nature INT; + the_id_nomenclature_grp_typ INT; + the_grp_method VARCHAR; + the_id_nomenclature_obs_technique INT; + the_id_nomenclature_bio_status INT; + the_id_nomenclature_bio_condition INT; + the_id_nomenclature_naturalness INT; + the_id_nomenclature_exist_proof INT; + the_id_nomenclature_valid_status INT; + the_id_nomenclature_diffusion_level INT; + the_id_nomenclature_life_stage INT; + the_id_nomenclature_sex INT; + the_id_nomenclature_obj_count INT; + the_id_nomenclature_type_count INT; + the_id_nomenclature_sensitivity INT; + the_id_nomenclature_observation_status INT; + the_id_nomenclature_blurring INT; + the_id_nomenclature_source_status INT; + the_id_nomenclature_info_geo_type INT; + the_id_nomenclature_behaviour INT; + the_id_nomenclature_biogeo_status INT; + the_reference_biblio VARCHAR; + the_count_min INT; + the_count_max INT; + the_cd_nom INT; + the_cd_hab INT; + the_nom_cite VARCHAR; + the_meta_v_taxref VARCHAR; + the_sample_number_proof TEXT; + the_digital_proof TEXT; + the_non_digital_proof TEXT; + the_altitude_min INT; + the_altitude_max INT; + the_depth_min INT; + the_depth_max INT; + the_place_name VARCHAR; + _the_geom_4326 GEOMETRY; + _the_geom_point GEOMETRY; + _the_geom_local GEOMETRY; + the_precision INT; + the_date_min TIMESTAMP; + the_date_max TIMESTAMP; + the_validator VARCHAR; + the_validation_comment TEXT; + the_observers TEXT; + the_determiner TEXT; + the_id_digitiser INT; the_id_nomenclature_determination_method INT; - the_comment_context TEXT; - the_comment_description TEXT; - the_additional_data JSONB; - the_meta_validation_date TIMESTAMP; + the_comment_context TEXT; + the_comment_description TEXT; + the_additional_data JSONB; + the_meta_validation_date TIMESTAMP; BEGIN - SELECT find_srid('gn_synthese', 'synthese', 'the_geom_local') - INTO _local_srid; - SELECT new.uuid - INTO the_unique_id_sinp; - SELECT cast(new.item #>> '{id_perm_grp_sinp}' AS UUID) - INTO the_unique_id_sinp_grp; - SELECT gn2pg_import.fct_c_get_or_insert_source(new.source) - INTO the_id_source; - SELECT new.item #>> '{id_synthese}' - INTO the_entity_source_pk_value; - SELECT CASE new.type - WHEN 'synthese_with_metadata' THEN gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata( - new.item #> '{ca_data}', new.source) - ELSE gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name(cast(new.item #>> '{ca_uuid}' AS UUID), - new.item #>> '{ca_nom}') - END AS the_id_af - INTO the_id_af; - SELECT CASE new.type - WHEN 'synthese_with_metadata' THEN gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata( - new.item #> '{jdd_data}', - the_id_af, - new.source - ) - ELSE gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name( - cast(new.item #>> '{jdd_uuid}' AS UUID), - new.item #>> '{jdd_nom}', - the_id_af - ) - END AS id_dataset - INTO the_id_dataset; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('NAT_OBJ_GEO', - new.item #>> - '{nature_objet_geo}') - ELSE ref_nomenclatures.get_id_nomenclature('NAT_OBJ_GEO', new.item #>> '{nature_objet_geo}') - END AS id_nomenclature_geo_object_nature - INTO the_id_nomenclature_geo_object_nature; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('TYP_GRP', new.item #>> - '{type_regroupement}') - ELSE ref_nomenclatures.get_id_nomenclature('TYP_GRP', new.item #>> '{type_regroupement}') - END AS id_nomenclature_grp_typ - INTO the_id_nomenclature_grp_typ; - SELECT new.item #>> '{methode_regroupement}' - INTO the_grp_method; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('METH_OBS', - new.item #>> - '{technique_obs}') - ELSE ref_nomenclatures.get_id_nomenclature('METH_OBS', new.item #>> '{technique_obs}') - END AS id_nomenclature_obs_technique - INTO the_id_nomenclature_obs_technique; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STATUT_BIO', - new.item #>> - '{statut_biologique}') - ELSE ref_nomenclatures.get_id_nomenclature('STATUT_BIO', new.item #>> '{statut_biologique}') - END AS id_nomenclature_bio_status - INTO the_id_nomenclature_bio_status; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('ETA_BIO', new.item #>> - '{etat_biologique}') - ELSE ref_nomenclatures.get_id_nomenclature('ETA_BIO', new.item #>> '{etat_biologique}') - END AS id_nomenclature_bio_condition - INTO the_id_nomenclature_bio_condition; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('NATURALITE', - new.item #>> - '{naturalite}') - ELSE ref_nomenclatures.get_id_nomenclature('NATURALITE', new.item #>> '{naturalite}') - END AS id_nomenclature_naturalness - INTO the_id_nomenclature_naturalness; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('PREUVE_EXIST', - new.item #>> - '{preuve_existante}') - ELSE ref_nomenclatures.get_id_nomenclature('PREUVE_EXIST', new.item #>> '{preuve_existante}') - END AS id_nomenclature_exist_proof - INTO the_id_nomenclature_exist_proof; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STATUT_VALID', - new.item #>> - '{statut_validation}') - ELSE ref_nomenclatures.get_id_nomenclature('STATUT_VALID', new.item #>> '{statut_validation}') - END AS id_nomenclature_valid_status - INTO the_id_nomenclature_valid_status; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('NIV_PRECIS', - new.item #>> - '{precision_diffusion}') - ELSE ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', new.item #>> '{precision_diffusion}') - END AS id_nomenclature_diffusion_level - INTO the_id_nomenclature_diffusion_level; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STADE_VIE', - new.item #>> - '{stade_vie}') - ELSE ref_nomenclatures.get_id_nomenclature('STADE_VIE', new.item #>> '{stade_vie}') - END AS id_nomenclature_life_stage - INTO the_id_nomenclature_life_stage; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('SEXE', - new.item #>> '{sexe}') - ELSE ref_nomenclatures.get_id_nomenclature('SEXE', new.item #>> '{sexe}') - END AS id_nomenclature_sex - INTO the_id_nomenclature_sex; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('OBJ_DENBR', - new.item #>> - '{objet_denombrement}') - ELSE ref_nomenclatures.get_id_nomenclature('OBJ_DENBR', new.item #>> '{objet_denombrement}') - END AS id_nomenclature_obj_count - INTO the_id_nomenclature_obj_count; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('TYP_DENBR', - new.item #>> - '{type_denombrement}') - ELSE ref_nomenclatures.get_id_nomenclature('TYP_DENBR', new.item #>> '{type_denombrement}') - END AS id_nomenclature_type_count - INTO the_id_nomenclature_type_count; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('SENSIBILITE', - new.item #>> - '{niveau_sensibilite}') - ELSE ref_nomenclatures.get_id_nomenclature('SENSIBILITE', new.item #>> '{niveau_sensibilite}') - END AS id_nomenclature_sensitivity - INTO the_id_nomenclature_sensitivity; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STATUT_OBS', - new.item #>> - '{statut_observation}') - ELSE ref_nomenclatures.get_id_nomenclature('STATUT_OBS', new.item #>> '{statut_observation}') - END AS id_nomenclature_observation_status - INTO the_id_nomenclature_observation_status; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('DEE_FLOU', - new.item #>> - '{floutage_dee}') - ELSE ref_nomenclatures.get_id_nomenclature('DEE_FLOU', new.item #>> '{floutage_dee}') - END AS id_nomenclature_blurring - INTO the_id_nomenclature_blurring; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STATUT_SOURCE', - new.item #>> - '{statut_source}') - ELSE ref_nomenclatures.get_id_nomenclature('STATUT_SOURCE', new.item #>> '{statut_source}') - END AS id_nomenclature_source_status - INTO the_id_nomenclature_source_status; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('TYP_INF_GEO', - new.item #>> - '{type_info_geo}') - ELSE ref_nomenclatures.get_id_nomenclature('TYP_INF_GEO', new.item #>> '{type_info_geo}') - END AS id_nomenclature_info_geo_type - INTO the_id_nomenclature_info_geo_type; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('OCC_COMPORTEMENT', - new.item #>> - '{comportement}') - ELSE ref_nomenclatures.get_id_nomenclature('OCC_COMPORTEMENT', new.item #>> '{comportement}') - END AS id_nomenclature_behaviour - INTO the_id_nomenclature_behaviour; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('STAT_BIOGEO', - new.item #>> - '{statut_biogeo}') - ELSE ref_nomenclatures.get_id_nomenclature('STAT_BIOGEO', new.item #>> '{statut_biogeo}') - END AS id_nomenclature_biogeo_status - INTO the_id_nomenclature_biogeo_status; - SELECT new.item #>> '{reference_biblio}' - INTO the_reference_biblio; - SELECT new.item #>> '{nombre_min}' - INTO the_count_min; - SELECT new.item #>> '{nombre_max}' - INTO the_count_max; - SELECT new.item #>> '{cd_nom}' - INTO the_cd_nom; - SELECT new.item #>> '{cd_hab}' - INTO the_cd_hab; - SELECT new.item #>> '{nom_cite}' - INTO the_nom_cite; - SELECT new.item #>> '{version_taxref}' - INTO the_meta_v_taxref; - SELECT new.item #>> '{numero_preuve}' - INTO the_sample_number_proof; - SELECT new.item #>> '{preuve_numerique}' - INTO the_digital_proof; - SELECT new.item #>> '{preuve_non_numerique}' - INTO the_non_digital_proof; - SELECT new.item #>> '{altitude_min}' - INTO the_altitude_min; - SELECT new.item #>> '{altitude_max}' - INTO the_altitude_max; - SELECT new.item #>> '{profondeur_min}' - INTO the_depth_min; - SELECT new.item #>> '{profondeur_max}' - INTO the_depth_max; - SELECT new.item #>> '{nom_lieu}' - INTO the_place_name; - SELECT st_setsrid(st_geomfromtext(new.item #>> '{wkt_4326}'), 4326) - INTO _the_geom_4326; - SELECT st_centroid(_the_geom_4326) - INTO _the_geom_point; - SELECT st_transform(_the_geom_4326, _local_srid) - INTO _the_geom_local; - SELECT cast(new.item #>> '{precision}' AS INT) - INTO the_precision; - SELECT cast(new.item #>> '{date_debut}' AS DATE) - INTO the_date_min; - SELECT cast(new.item #>> '{date_fin}' AS DATE) - INTO the_date_max; - SELECT new.item #>> '{validateur}' - INTO the_validator; - SELECT new.item #>> '{comment_validation}' - INTO the_validation_comment; - SELECT new.item #>> '{observateurs}' - INTO the_observers; - SELECT new.item #>> '{determinateur}' - INTO the_determiner; - SELECT NULL - INTO the_id_digitiser; - SELECT CASE new.type - WHEN 'synthese_with_label' THEN gn2pg_import.fct_c_get_id_nomenclature_from_label('TYPE', - new.item #>> '{label}') - ELSE ref_nomenclatures.get_id_nomenclature('TYPE', new.item #>> '{label}') - END AS id_nomenclature_determination_method - INTO the_id_nomenclature_determination_method; - SELECT new.item #>> '{comment_releve}' - INTO the_comment_context; - SELECT new.item #>> '{comment_occurrence}' - INTO the_comment_description; - SELECT new.item #> '{donnees_additionnelles}' - INTO the_additional_data; - SELECT NULL - INTO the_meta_validation_date; + SELECT + find_srid ('gn_synthese' , 'synthese' , 'the_geom_local') INTO _local_srid; + SELECT + NEW.uuid INTO the_unique_id_sinp; + SELECT + cast(NEW.item #>> '{id_perm_grp_sinp}' AS UUID) INTO the_unique_id_sinp_grp; + SELECT + gn2pg_import.fct_c_get_or_insert_source (NEW.source) INTO the_id_source; + SELECT + NEW.item #>> '{id_synthese}' INTO the_entity_source_pk_value; + SELECT + CASE NEW.type + WHEN 'synthese_with_metadata' THEN + gn2pg_import.fct_c_get_or_insert_af_from_af_jsondata (NEW.item #> + '{ca_data}' , NEW.source) + ELSE + gn2pg_import.fct_c_get_or_insert_basic_af_from_uuid_name + (cast(NEW.item #>> '{ca_uuid}' AS UUID) , NEW.item #>> + '{ca_nom}') + END AS the_id_af INTO the_id_af; + SELECT + CASE NEW.type + WHEN 'synthese_with_metadata' THEN + gn2pg_import.fct_c_get_or_insert_dataset_from_jsondata (NEW.item #> + '{jdd_data}' , the_id_af , NEW.source) + ELSE + gn2pg_import.fct_c_get_or_insert_basic_dataset_from_uuid_name + (cast(NEW.item #>> '{jdd_uuid}' AS UUID) , NEW.item #>> + '{jdd_nom}' , the_id_af) + END AS id_dataset INTO the_id_dataset; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('NAT_OBJ_GEO' + , NEW.item #>> '{nature_objet_geo}') + ELSE + ref_nomenclatures.get_id_nomenclature ('NAT_OBJ_GEO' , NEW.item #>> '{nature_objet_geo}') + END AS id_nomenclature_geo_object_nature INTO the_id_nomenclature_geo_object_nature; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('TYP_GRP' + , NEW.item #>> '{type_regroupement}') + ELSE + ref_nomenclatures.get_id_nomenclature ('TYP_GRP' , NEW.item #>> '{type_regroupement}') + END AS id_nomenclature_grp_typ INTO the_id_nomenclature_grp_typ; + SELECT + NEW.item #>> '{methode_regroupement}' INTO the_grp_method; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('METH_OBS' + , NEW.item #>> '{technique_obs}') + ELSE + ref_nomenclatures.get_id_nomenclature ('METH_OBS' , NEW.item #>> '{technique_obs}') + END AS id_nomenclature_obs_technique INTO the_id_nomenclature_obs_technique; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STATUT_BIO' + , NEW.item #>> '{statut_biologique}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_BIO' , NEW.item #>> '{statut_biologique}') + END AS id_nomenclature_bio_status INTO the_id_nomenclature_bio_status; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('ETA_BIO' + , NEW.item #>> '{etat_biologique}') + ELSE + ref_nomenclatures.get_id_nomenclature ('ETA_BIO' , NEW.item #>> '{etat_biologique}') + END AS id_nomenclature_bio_condition INTO the_id_nomenclature_bio_condition; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('NATURALITE' + , NEW.item #>> '{naturalite}') + ELSE + ref_nomenclatures.get_id_nomenclature ('NATURALITE' , NEW.item #>> '{naturalite}') + END AS id_nomenclature_naturalness INTO the_id_nomenclature_naturalness; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('PREUVE_EXIST' + , NEW.item #>> '{preuve_existante}') + ELSE + ref_nomenclatures.get_id_nomenclature ('PREUVE_EXIST' , NEW.item #>> '{preuve_existante}') + END AS id_nomenclature_exist_proof INTO the_id_nomenclature_exist_proof; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STATUT_VALID' + , NEW.item #>> '{statut_validation}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_VALID' , NEW.item #>> '{statut_validation}') + END AS id_nomenclature_valid_status INTO the_id_nomenclature_valid_status; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('NIV_PRECIS' + , NEW.item #>> '{precision_diffusion}') + ELSE + ref_nomenclatures.get_id_nomenclature ('NIV_PRECIS' , NEW.item #>> '{precision_diffusion}') + END AS id_nomenclature_diffusion_level INTO the_id_nomenclature_diffusion_level; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STADE_VIE' + , NEW.item #>> '{stade_vie}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STADE_VIE' , NEW.item #>> '{stade_vie}') + END AS id_nomenclature_life_stage INTO the_id_nomenclature_life_stage; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('SEXE' + , NEW.item #>> '{sexe}') + ELSE + ref_nomenclatures.get_id_nomenclature ('SEXE' , NEW.item #>> '{sexe}') + END AS id_nomenclature_sex INTO the_id_nomenclature_sex; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('OBJ_DENBR' + , NEW.item #>> '{objet_denombrement}') + ELSE + ref_nomenclatures.get_id_nomenclature ('OBJ_DENBR' , NEW.item #>> '{objet_denombrement}') + END AS id_nomenclature_obj_count INTO the_id_nomenclature_obj_count; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('TYP_DENBR' + , NEW.item #>> '{type_denombrement}') + ELSE + ref_nomenclatures.get_id_nomenclature ('TYP_DENBR' , NEW.item #>> '{type_denombrement}') + END AS id_nomenclature_type_count INTO the_id_nomenclature_type_count; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('SENSIBILITE' + , NEW.item #>> '{niveau_sensibilite}') + ELSE + ref_nomenclatures.get_id_nomenclature ('SENSIBILITE' , NEW.item #>> '{niveau_sensibilite}') + END AS id_nomenclature_sensitivity INTO the_id_nomenclature_sensitivity; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STATUT_OBS' + , NEW.item #>> '{statut_observation}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_OBS' , NEW.item #>> '{statut_observation}') + END AS id_nomenclature_observation_status INTO the_id_nomenclature_observation_status; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('DEE_FLOU' + , NEW.item #>> '{floutage_dee}') + ELSE + ref_nomenclatures.get_id_nomenclature ('DEE_FLOU' , NEW.item #>> '{floutage_dee}') + END AS id_nomenclature_blurring INTO the_id_nomenclature_blurring; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STATUT_SOURCE' + , NEW.item #>> '{statut_source}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_SOURCE' , NEW.item #>> '{statut_source}') + END AS id_nomenclature_source_status INTO the_id_nomenclature_source_status; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('TYP_INF_GEO' + , NEW.item #>> '{type_info_geo}') + ELSE + ref_nomenclatures.get_id_nomenclature ('TYP_INF_GEO' , NEW.item #>> '{type_info_geo}') + END AS id_nomenclature_info_geo_type INTO the_id_nomenclature_info_geo_type; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('OCC_COMPORTEMENT' + , NEW.item #>> '{comportement}') + ELSE + ref_nomenclatures.get_id_nomenclature ('OCC_COMPORTEMENT' , NEW.item #>> '{comportement}') + END AS id_nomenclature_behaviour INTO the_id_nomenclature_behaviour; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('STAT_BIOGEO' + , NEW.item #>> '{statut_biogeo}') + ELSE + ref_nomenclatures.get_id_nomenclature ('STAT_BIOGEO' , NEW.item #>> '{statut_biogeo}') + END AS id_nomenclature_biogeo_status INTO the_id_nomenclature_biogeo_status; + SELECT + NEW.item #>> '{reference_biblio}' INTO the_reference_biblio; + SELECT + NEW.item #>> '{nombre_min}' INTO the_count_min; + SELECT + NEW.item #>> '{nombre_max}' INTO the_count_max; + SELECT + NEW.item #>> '{cd_nom}' INTO the_cd_nom; + SELECT + NEW.item #>> '{cd_hab}' INTO the_cd_hab; + SELECT + NEW.item #>> '{nom_cite}' INTO the_nom_cite; + SELECT + NEW.item #>> '{version_taxref}' INTO the_meta_v_taxref; + SELECT + NEW.item #>> '{numero_preuve}' INTO the_sample_number_proof; + SELECT + NEW.item #>> '{preuve_numerique}' INTO the_digital_proof; + SELECT + NEW.item #>> '{preuve_non_numerique}' INTO the_non_digital_proof; + SELECT + NEW.item #>> '{altitude_min}' INTO the_altitude_min; + SELECT + NEW.item #>> '{altitude_max}' INTO the_altitude_max; + SELECT + NEW.item #>> '{profondeur_min}' INTO the_depth_min; + SELECT + NEW.item #>> '{profondeur_max}' INTO the_depth_max; + SELECT + NEW.item #>> '{nom_lieu}' INTO the_place_name; + SELECT + st_setsrid (st_geomfromtext (NEW.item #>> '{wkt_4326}') , 4326) INTO _the_geom_4326; + SELECT + st_centroid (_the_geom_4326) INTO _the_geom_point; + SELECT + st_transform (_the_geom_4326 , _local_srid) INTO _the_geom_local; + SELECT + cast(NEW.item #>> '{precision}' AS INT) INTO the_precision; + SELECT + cast(NEW.item #>> '{date_debut}' AS DATE) INTO the_date_min; + SELECT + cast(NEW.item #>> '{date_fin}' AS DATE) INTO the_date_max; + SELECT + NEW.item #>> '{validateur}' INTO the_validator; + SELECT + NEW.item #>> '{comment_validation}' INTO the_validation_comment; + SELECT + NEW.item #>> '{observateurs}' INTO the_observers; + SELECT + NEW.item #>> '{determinateur}' INTO the_determiner; + SELECT + NULL INTO the_id_digitiser; + SELECT + CASE NEW.type + WHEN 'synthese_with_label' THEN + gn2pg_import.fct_c_get_id_nomenclature_from_label ('TYPE' + , NEW.item #>> '{label}') + ELSE + ref_nomenclatures.get_id_nomenclature ('TYPE' , NEW.item #>> '{label}') + END AS id_nomenclature_determination_method INTO + the_id_nomenclature_determination_method; + SELECT + NEW.item #>> '{comment_releve}' INTO the_comment_context; + SELECT + NEW.item #>> '{comment_occurrence}' INTO the_comment_description; + SELECT + NEW.item #> '{donnees_additionnelles}' INTO the_additional_data; + SELECT + NULL INTO the_meta_validation_date; + /* Proceed to upsert */ - INSERT INTO gn_synthese.synthese ( unique_id_sinp - , unique_id_sinp_grp - , id_source - , entity_source_pk_value - , id_dataset - , id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ - , grp_method - , id_nomenclature_obs_technique - , id_nomenclature_bio_status - , id_nomenclature_bio_condition - , id_nomenclature_naturalness - , id_nomenclature_exist_proof - , id_nomenclature_valid_status - , id_nomenclature_diffusion_level - , id_nomenclature_life_stage - , id_nomenclature_sex - , id_nomenclature_obj_count - , id_nomenclature_type_count - , id_nomenclature_sensitivity - , id_nomenclature_observation_status - , id_nomenclature_blurring - , id_nomenclature_source_status - , id_nomenclature_info_geo_type - , id_nomenclature_behaviour - , id_nomenclature_biogeo_status - , reference_biblio - , count_min - , count_max - , cd_nom - , cd_hab - , nom_cite - , meta_v_taxref - , sample_number_proof - , digital_proof - , non_digital_proof - , altitude_min - , altitude_max - , depth_min - , depth_max - , place_name - , the_geom_4326 - , the_geom_point - , the_geom_local - , precision - , date_min - , date_max - , validator - , validation_comment - , observers - , determiner - , id_digitiser - , id_nomenclature_determination_method - , comment_context - , comment_description - , additional_data - , meta_validation_date - , last_action) - VALUES ( the_unique_id_sinp - , the_unique_id_sinp_grp - , the_id_source - , the_entity_source_pk_value - , the_id_dataset - , the_id_nomenclature_geo_object_nature - , the_id_nomenclature_grp_typ - , the_grp_method - , the_id_nomenclature_obs_technique - , the_id_nomenclature_bio_status - , the_id_nomenclature_bio_condition - , the_id_nomenclature_naturalness - , the_id_nomenclature_exist_proof - , the_id_nomenclature_valid_status - , the_id_nomenclature_diffusion_level - , the_id_nomenclature_life_stage - , the_id_nomenclature_sex - , the_id_nomenclature_obj_count - , the_id_nomenclature_type_count - , the_id_nomenclature_sensitivity - , the_id_nomenclature_observation_status - , the_id_nomenclature_blurring - , the_id_nomenclature_source_status - , the_id_nomenclature_info_geo_type - , the_id_nomenclature_behaviour - , the_id_nomenclature_biogeo_status - , the_reference_biblio - , the_count_min - , the_count_max - , the_cd_nom - , the_cd_hab - , the_nom_cite - , the_meta_v_taxref - , the_sample_number_proof - , the_digital_proof - , the_non_digital_proof - , the_altitude_min - , the_altitude_max - , the_depth_min - , the_depth_max - , the_place_name - , _the_geom_4326 - , _the_geom_point - , _the_geom_local - , the_precision - , the_date_min - , the_date_max - , the_validator - , the_validation_comment - , the_observers - , the_determiner - , the_id_digitiser - , the_id_nomenclature_determination_method - , the_comment_context - , the_comment_description - , the_additional_data - , the_meta_validation_date - , 'I') - ON CONFLICT (id_source, entity_source_pk_value) - DO UPDATE SET unique_id_sinp = the_unique_id_sinp - , unique_id_sinp_grp = the_unique_id_sinp_grp - , id_source = the_id_source - , entity_source_pk_value = the_entity_source_pk_value - , id_dataset = the_id_dataset - , id_nomenclature_geo_object_nature = the_id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ = the_id_nomenclature_grp_typ - , grp_method = the_grp_method - , id_nomenclature_obs_technique = the_id_nomenclature_obs_technique - , id_nomenclature_bio_status = the_id_nomenclature_bio_status - , id_nomenclature_bio_condition = the_id_nomenclature_bio_condition - , id_nomenclature_naturalness = the_id_nomenclature_naturalness - , id_nomenclature_exist_proof = the_id_nomenclature_exist_proof - , id_nomenclature_valid_status = the_id_nomenclature_valid_status - , id_nomenclature_diffusion_level = the_id_nomenclature_diffusion_level - , id_nomenclature_life_stage = the_id_nomenclature_life_stage - , id_nomenclature_sex = the_id_nomenclature_sex - , id_nomenclature_obj_count = the_id_nomenclature_obj_count - , id_nomenclature_type_count = the_id_nomenclature_type_count - , id_nomenclature_sensitivity = the_id_nomenclature_sensitivity - , id_nomenclature_observation_status = the_id_nomenclature_observation_status - , id_nomenclature_blurring = the_id_nomenclature_blurring - , id_nomenclature_source_status = the_id_nomenclature_source_status - , id_nomenclature_info_geo_type = the_id_nomenclature_info_geo_type - , id_nomenclature_behaviour = the_id_nomenclature_behaviour - , id_nomenclature_biogeo_status = the_id_nomenclature_biogeo_status - , reference_biblio = the_reference_biblio - , count_min = the_count_min - , count_max = the_count_max - , cd_nom = the_cd_nom - , cd_hab = the_cd_hab - , nom_cite = the_nom_cite - , meta_v_taxref = the_meta_v_taxref - , sample_number_proof = the_sample_number_proof - , digital_proof = the_digital_proof - , non_digital_proof = the_non_digital_proof - , altitude_min = the_altitude_min - , altitude_max = the_altitude_max - , depth_min = the_depth_min - , depth_max = the_depth_max - , place_name = the_place_name - , the_geom_4326 = _the_geom_4326 - , the_geom_point = _the_geom_point - , the_geom_local = _the_geom_local - , precision = the_precision - , date_min = the_date_min - , date_max = the_date_max - , validator = the_validator - , validation_comment = the_validation_comment - , observers = the_observers - , determiner = the_determiner - , id_digitiser = the_id_digitiser - , id_nomenclature_determination_method = the_id_nomenclature_determination_method - , comment_context = the_comment_context - , comment_description = the_comment_description - , additional_data = the_additional_data - , meta_validation_date = the_meta_validation_date - , last_action = 'U'; + INSERT INTO gn_synthese.synthese (unique_id_sinp , unique_id_sinp_grp , + id_source , entity_source_pk_value , id_dataset , + id_nomenclature_geo_object_nature , id_nomenclature_grp_typ , + grp_method , id_nomenclature_obs_technique , id_nomenclature_bio_status + , id_nomenclature_bio_condition , id_nomenclature_naturalness , + id_nomenclature_exist_proof , id_nomenclature_valid_status , + id_nomenclature_diffusion_level , id_nomenclature_life_stage , + id_nomenclature_sex , id_nomenclature_obj_count , + id_nomenclature_type_count , id_nomenclature_sensitivity , + id_nomenclature_observation_status , id_nomenclature_blurring , + id_nomenclature_source_status , id_nomenclature_info_geo_type , + id_nomenclature_behaviour , id_nomenclature_biogeo_status , + reference_biblio , count_min , count_max , cd_nom , cd_hab , nom_cite , + meta_v_taxref , sample_number_proof , digital_proof , non_digital_proof + , altitude_min , altitude_max , depth_min , depth_max , place_name , + the_geom_4326 , the_geom_point , the_geom_local , precision , date_min + , date_max , validator , validation_comment , observers , determiner , + id_digitiser , id_nomenclature_determination_method , comment_context , + comment_description , additional_data , meta_validation_date , + last_action) + VALUES (the_unique_id_sinp , the_unique_id_sinp_grp , the_id_source , + the_entity_source_pk_value , the_id_dataset , + the_id_nomenclature_geo_object_nature , the_id_nomenclature_grp_typ + , the_grp_method , the_id_nomenclature_obs_technique , + the_id_nomenclature_bio_status , the_id_nomenclature_bio_condition + , the_id_nomenclature_naturalness , the_id_nomenclature_exist_proof + , the_id_nomenclature_valid_status , + the_id_nomenclature_diffusion_level , + the_id_nomenclature_life_stage , the_id_nomenclature_sex , + the_id_nomenclature_obj_count , the_id_nomenclature_type_count , + the_id_nomenclature_sensitivity , + the_id_nomenclature_observation_status , + the_id_nomenclature_blurring , the_id_nomenclature_source_status , + the_id_nomenclature_info_geo_type , the_id_nomenclature_behaviour , + the_id_nomenclature_biogeo_status , the_reference_biblio , + the_count_min , the_count_max , the_cd_nom , the_cd_hab , + the_nom_cite , the_meta_v_taxref , the_sample_number_proof , + the_digital_proof , the_non_digital_proof , the_altitude_min , + the_altitude_max , the_depth_min , the_depth_max , the_place_name , + _the_geom_4326 , _the_geom_point , _the_geom_local , the_precision + , the_date_min , the_date_max , the_validator , + the_validation_comment , the_observers , the_determiner , + the_id_digitiser , the_id_nomenclature_determination_method , + the_comment_context , the_comment_description , the_additional_data + , the_meta_validation_date , 'I') + ON CONFLICT (id_source , entity_source_pk_value) + DO UPDATE SET + unique_id_sinp = the_unique_id_sinp , unique_id_sinp_grp = + the_unique_id_sinp_grp , id_source = the_id_source , + entity_source_pk_value = the_entity_source_pk_value , + id_dataset = the_id_dataset , id_nomenclature_geo_object_nature + = the_id_nomenclature_geo_object_nature , + id_nomenclature_grp_typ = the_id_nomenclature_grp_typ , + grp_method = the_grp_method , id_nomenclature_obs_technique = + the_id_nomenclature_obs_technique , id_nomenclature_bio_status + = the_id_nomenclature_bio_status , + id_nomenclature_bio_condition = + the_id_nomenclature_bio_condition , id_nomenclature_naturalness + = the_id_nomenclature_naturalness , id_nomenclature_exist_proof + = the_id_nomenclature_exist_proof , + id_nomenclature_valid_status = the_id_nomenclature_valid_status + , id_nomenclature_diffusion_level = + the_id_nomenclature_diffusion_level , + id_nomenclature_life_stage = the_id_nomenclature_life_stage , + id_nomenclature_sex = the_id_nomenclature_sex , + id_nomenclature_obj_count = the_id_nomenclature_obj_count , + id_nomenclature_type_count = the_id_nomenclature_type_count , + id_nomenclature_sensitivity = the_id_nomenclature_sensitivity , + id_nomenclature_observation_status = + the_id_nomenclature_observation_status , + id_nomenclature_blurring = the_id_nomenclature_blurring , + id_nomenclature_source_status = + the_id_nomenclature_source_status , + id_nomenclature_info_geo_type = + the_id_nomenclature_info_geo_type , id_nomenclature_behaviour = + the_id_nomenclature_behaviour , id_nomenclature_biogeo_status = + the_id_nomenclature_biogeo_status , reference_biblio = + the_reference_biblio , count_min = the_count_min , count_max = + the_count_max , cd_nom = the_cd_nom , cd_hab = the_cd_hab , + nom_cite = the_nom_cite , meta_v_taxref = the_meta_v_taxref , + sample_number_proof = the_sample_number_proof , digital_proof = + the_digital_proof , non_digital_proof = the_non_digital_proof , + altitude_min = the_altitude_min , altitude_max = + the_altitude_max , depth_min = the_depth_min , depth_max = + the_depth_max , place_name = the_place_name , the_geom_4326 = + _the_geom_4326 , the_geom_point = _the_geom_point , + the_geom_local = _the_geom_local , precision = the_precision , + date_min = the_date_min , date_max = the_date_max , validator = + the_validator , validation_comment = the_validation_comment , + observers = the_observers , determiner = the_determiner , + id_digitiser = the_id_digitiser , + id_nomenclature_determination_method = + the_id_nomenclature_determination_method , comment_context = + the_comment_context , comment_description = + the_comment_description , additional_data = the_additional_data + , meta_validation_date = the_meta_validation_date , last_action + = 'U'; RETURN new; END; $$; -COMMENT ON FUNCTION gn2pg_import.fct_tri_c_upsert_data_to_geonature() IS 'Trigger function to upsert datas from import to synthese'; +COMMENT ON FUNCTION gn2pg_import.fct_tri_c_upsert_data_to_geonature () IS 'Trigger function to upsert datas from import to synthese'; DROP TRIGGER IF EXISTS tri_c_upsert_data_to_geonature ON gn2pg_import.data_json; + CREATE TRIGGER tri_c_upsert_data_to_geonature - AFTER INSERT OR UPDATE - ON gn2pg_import.data_json + AFTER INSERT OR UPDATE ON gn2pg_import.data_json FOR EACH ROW -EXECUTE PROCEDURE gn2pg_import.fct_tri_c_upsert_data_to_geonature(); + EXECUTE PROCEDURE gn2pg_import.fct_tri_c_upsert_data_to_geonature (); -- DELETE -CREATE OR REPLACE FUNCTION gn2pg_import.fct_tri_c_delete_data_from_geonature() RETURNS TRIGGER +CREATE OR REPLACE FUNCTION gn2pg_import.fct_tri_c_delete_data_from_geonature () + RETURNS TRIGGER LANGUAGE plpgsql -AS -$$ + AS $$ BEGIN - DELETE - FROM gn_synthese.synthese - WHERE (old.item #>> '{id_synthese}', gn2pg_import.fct_c_get_or_insert_source(old.source)) = - (synthese.entity_source_pk_value, synthese.id_source); + DELETE FROM gn_synthese.synthese + WHERE (OLD.item #>> '{id_synthese}' , + gn2pg_import.fct_c_get_or_insert_source (OLD.source)) = + (synthese.entity_source_pk_value + , synthese.id_source); IF NOT found THEN RETURN NULL; END IF; RETURN old; END; $$; -COMMENT ON FUNCTION gn2pg_import.fct_tri_c_delete_data_from_geonature() IS 'Trigger function to delete datas'; + +COMMENT ON FUNCTION gn2pg_import.fct_tri_c_delete_data_from_geonature () IS 'Trigger function to delete datas'; DROP TRIGGER IF EXISTS tri_c_delete_data_from_geonature ON gn2pg_import.data_json; + CREATE TRIGGER tri_c_delete_data_from_geonature - AFTER DELETE - ON gn2pg_import.data_json + AFTER DELETE ON gn2pg_import.data_json FOR EACH ROW - WHEN (old.type IN ('synthese_with_label', 'synthese_with_cd_nomenclature', 'synthese_with_metadata')) -EXECUTE PROCEDURE gn2pg_import.fct_tri_c_delete_data_from_geonature(); + WHEN (old.type IN ('synthese_with_label' , 'synthese_with_cd_nomenclature' , 'synthese_with_metadata')) + EXECUTE PROCEDURE gn2pg_import.fct_tri_c_delete_data_from_geonature (); COMMIT;