
--------------------------------------INSERTION OF NEW FETURE----------------------------------------


Parameters define from front/person creating the feature:
-layers_id_geofence


Insertion of a new geofence from front:
-geometry
-message
-lower_limit
-vertical_reference
-upper_limit
-code_restriction_type
-code_zone_reason->vec
-condition_expression_type->vec
-code_uspace_class_type->vec 
-volume_limitation->vec -> 
-code_zone_type_type_code=2
-country_id=34
-region=1
-data_capture_prohibition=false
-type_period=3 (personalized)(example)
[[start_date,end_date,[days],[time_start,time_end,day]]]-> [[start_date1,end_date1,['M','T'],[time_start1,time_end1]],[start_date2,end_date2,['Th','Sa'],[time_start2,time_end2]]]
-[operator_id,drone_id,time]
-Limit for manual authorization required 

-creation_date_time -> now
-update_date_time -> now
-author -> OSM
-name_authority -> Enaire
-service_authority ->
-contact_name_authority
-site_url_authority
-email_authority->  info@enaire.es
-phone_authority -> 913210211
-active->true

----CREATION OF GEOFENCE------
1.Insertion of gemetrical parameters into the airspace volume:
INSERT INTO airspace_volume(lower_limit,lower_vertical_reference,
upper_limit,upper_vertical_reference,geom_buffer,layers_layer_id,active) 
VALUES(lower_limit,vertical_reference,upper_limit,vertical_reference, geom_buffer, layers_id_geofence,active) RETURNING vol_id;

3. Insert metadata:
INSERT INTO metadata(creation_date_time,update_date_time,author)
VALUES(creation_date_time,update_date_time,author) RETURNING metadata_id;

4. Insert authority:
INSERT INTO authority(name,service,contact_name,site_url,email,phone)
VALUES(name_authority,service_authority,contact_name_authority,site_url_authority,email_authority,phone_authority)
RETURNING authority_oid;

5. Insert definitions of geofence into uas_zone_version:
INSERT INTO uas_zone_version(country_id, name, airspace_volume_vol_id,code_zone_type_type_code,
region,data_capture_prohibition,message,metadata_metada_id,time_period_time_period_id,authority_authority_oid,code_period_type_period_type)
VALUES(country_id,name,vol_id,code_zone_type_type_code,region,data_capture_prohibition,message,metadata_id,time_period_id,authority_oid,type_deriod)
RETURNING uas_zone_id

2.Insertion of time period
#for each time_period:
    make 1 the bool_days(bool_M,bool_T,bool_W,bool_Th,bool_F,bool_Sa,bool_Su) that appear on the vector days of the timeperiod teh others 0.
    INSERT INTO time_period(start_date_time, end_date_time, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
    VALUES(start_date, end_date,bool_M,bool_T,bool_W,bool_Th,bool_F,bool_Sa,bool_Su) RETURNING time_period_id;

    INSERT INTO uas_time_period(time_period_time_period_id,uas_zone_version_uas_zone_id)
    VALUES(time_period,uas_zone_id);

    #for each daily_period in vec [time_start,time_end]
    INSERT INTO sloot_time(start_time, end_time,time_period_time_period_id)
    VALUES(time_start,time_end,time_period_id);

6.Insertion of restrictions into the different tables uas_ to suport relation n to n:
#for each volume_limitation in the vector:
INSERT INTO volume_limitation(volume_limitation_definitions_limitation_definition_id, uas_zone_version_uas_zone_id)
VALUES(volume_limitation, uas_zone_id);

#for each value in the code_uspace_class_type:
INSERT INTO uas_uspace_class_type(code_uspace_class_type_u_space_code,uas_zone_version_uas_zone_id)
VALUES(code_uspace_class_type, uas_zone_id);

#for each value in the vector code_zone_reason:
INSERT INTO uas_code_zone_reason(code_zone_reason_code_zone_reason,uas_zone_version_uas_zone_id)
VALUE(code_zone_reason,uas_zone_id)

#for each value in the vector code_restriction_type:
    INSERT INTO uas_code_restriction_type(code_restriction_type_restriction_code,uas_version_uas_zone_id)
    VALUES(code_restriction_type,uas_zone_id) RETURNING uas_restriction_code;
    #if uas_code_restriction_type==3:
        #for each value in vector condition_expression_type 
        #if condition_expression_type==3: 
            INSERT INTO uas_condition_expression_type(condition_expression_type_restriction_code, uas_code_restrcition_type_uas_retriction_code, uas_zone_version_uas_zone_id,upperlimitnorestriction)
            VALUES(condition_expression_type,uas_code_restriction_code,uas_zone_id,upperlimitnorestriction) RETURNING uas_restriction_code;
        #else:
            INSERT INTO uas_condition_expression_type(condition_expression_type_restriction_code, uas_code_restrcition_type_uas_retriction_code, uas_zone_version_uas_zone_id)
            VALUES(condition_expression_type,uas_code_restriction_code,uas_zone_id) RETURNING uas_restriction_code;
        #if condition_expression_type==4: 
            #for each value in vector [operator_id,drone_id]:
            INSERT INTO list_allowed_operators_drones(operator_id,drone_id,uas_restriction_code,uas_zone_id)


7.Creation of jsons and insertion on uas_zone_version:
CALL json_restriction(uas_zone_id)
CALL json_time_period(uas_zone_id)
CALL json_uspace(uas_zone_id)
CALL json_volume_limitation(uas_zone_id)
CALL json_zone_reason(uas_zone_id)

8. Refresh the view in which the feature is include:
REFRESH MATERIALIZED VIEW layer_name;

-------------------------------------------UPDATE FEATURE---------------------------------------

The idea is to update the rows of the tables affected. 
1. Get the airspace_volume id.
2. Update the row affected


