
delete from tm_institution_system where system_id not in (select id from tm_system where is_tool=true); -- should keep tools systems
delete from tm_connectathon_participant;
delete from usr_messages_usr_users;
delete from tm_user_preferences;
delete from tm_test_instance_participants;
delete from tm_system_in_session_user;
delete from tm_object_instance_file;
delete from tm_test_instance_test_steps_instance;
delete from tm_test_steps_test_steps_data;
delete from tm_test_steps_instance;
delete from tm_object_instance_attribute;
delete from tm_object_instance_annotation;
delete from tm_object_instance;
delete from tm_dicom_scu_configuration;
delete from tm_dicom_scp_configuration;
delete from tm_hl7_responder_configuration;
delete from tm_hl7_initiator_configuration;
delete from tm_web_service_configuration;
delete from tm_syslog_configuration;
delete from tm_hl7_v3_initiator_configuration;
delete from tm_hl7_v3_responder_configuration;
delete from tm_configuration;
delete from tm_certificate;
delete from tm_host;
delete from tm_oid_system_assignment;
delete from tm_demonstration_system_in_session;
delete from tm_system_in_session  where system_id not in (select id from tm_system where is_tool=true); --delete all exept the tools
delete from tm_system_in_session where testing_session_id not in (select id from tm_testing_session where description = 'Defaut CA Testing Session');
delete from tm_sap_result_tr_comment;
delete from tm_system_aipo_result_for_a_testing_session;
delete from tm_system_actor_profiles where system_id not in  (select id from tm_system where is_tool=true);
delete from sys_dicom_documents_for_systems;
delete from sys_hl7_documents_for_systems;
delete from tm_system where is_tool=false;
delete from tm_test_instance_test_instance_path_to_log_file;
delete from tm_test_instance_test_status;
delete from tm_test_instance_tm_test_steps_data;
delete from tm_test_instance;
delete from tm_monitor_test;
delete from tm_monitor_in_session;
delete from tm_test_user_comment;
delete from tm_user_comment;
delete from tm_simulator;
delete from tm_invoice;
delete from usr_institution_address;
delete from usr_persons_functions;
delete from usr_person;
delete from usr_messages_usr_message_parameters ;
delete from usr_messages;
delete from usr_message_parameters;
delete from tm_test_instance_event;
delete from tm_free_ip_ports_for_proxy;
delete from tm_domains_in_testing_sessions;
delete from tm_profiles_in_testing_sessions;
delete from tm_test_types_sessions;
delete from tm_simulator_in_session;
delete from tm_generic_oids_ip_config_param_for_session;
delete from tm_generic_oids_institution_for_session;
delete from tm_free_ips_for_session;
delete from tm_oids_ip_config_param_for_session_for_hl7;
delete from tm_network_config_for_session;
delete from tm_proxy_configuration_for_session;
delete from tm_demonstrations_in_testing_sessions;
delete from tm_testing_session_admin;
delete from tm_test_types_sessions;
delete from tm_user_preferences;
delete from tm_testing_session where description != 'Defaut CA Testing Session';
update tm_system set owner_user_id = ( select id from usr_users where username = 'admin');
delete from usr_users where  username != 'admin'; --delete all users exept admin
delete from usr_institution where id not in (select institution_id from usr_users where username = 'admin');
delete from usr_address  where id not in (select address_id from tm_testing_session where description = 'Defaut CA Testing Session');
delete from tm_user_photo;
delete from tm_test_instance_path_to_log_file;
delete from sys_table_session;
delete from tm_demonstration;

update tm_user_preferences set selected_testing_session_id = (select id from tm_testing_session where description = 'Defaut CA Testing Session');

-- reactivate and unblock admin account
update usr_users set activated = true where username = 'admin';
update usr_users set blocked = false where username = 'admin';
