<Requests>
<!-- 002_2-ervu_subpoena_registry_delete_all_with_recruit   -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                BEGIN
                    DELETE FROM subpoena_history where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena_appearance where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena_send_info where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                
                    DELETE FROM notification_item where restriction_document_item_id in (
                        select id from restriction_document_item where restriction_document_create_id  in (
                        select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments})));
                    DELETE FROM notification_item where restriction_document_item_id in (
                        select id from restriction_document_item where restriction_document_cancel_id  in (
                        select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments})));    
                    
                    DELETE FROM restriction_document_item_history WHERE recruit_id in ${endpointArguments};
                    DELETE FROM restriction_document_item where restriction_document_create_id  in (
                        select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments}));
                    DELETE FROM restriction_document_item where restriction_document_cancel_id  in (
                        select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments}));
                    DELETE FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena where recruit_id in ${endpointArguments};
                    DELETE FROM recruits WHERE id in ${endpointArguments};
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>subpoena</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>
    
<!-- 005-ervu_decision_document-delete-recruit  -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                BEGIN
                    DELETE FROM decision where recruit_id in ${endpointArguments};
                    DELETE FROM foreign_decision where recruit_id in ${endpointArguments};
                    DELETE FROM infringement where recruit_id in ${endpointArguments};
                    DELETE FROM system_document where attachment_id in (SELECT id FROM attachment where recruit_id in ${endpointArguments}) ;
                    DELETE FROM attachment where recruit_id in ${endpointArguments};
                    DELETE FROM recruit where id in ${endpointArguments};
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>decision-document-service</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

<!-- 004-ervu_journal-delete-recruit -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    recruitIds uuid[] := ${endpointArguments};
                    importIds  uuid[] := ARRAY(SELECT import_id
                                            FROM import_results
                                            WHERE recruit_id = ANY (recruitIds));
                BEGIN
                    WITH ImportDeletes AS (DELETE FROM import_journal_start WHERE import_id = ANY (importIds) RETURNING journal_id)
                    DELETE
                    FROM journal
                    WHERE id IN (SELECT journal_id FROM ImportDeletes);
                
                    DELETE FROM import_journal_finish WHERE import_id = ANY (importIds);
                    DELETE FROM import_events_journal WHERE import_id = ANY (importIds);
                    DELETE FROM import_validation_errors WHERE import_id = ANY (importIds);
                    DELETE FROM import_results WHERE import_id = ANY (importIds);
                    DELETE FROM uploaded_files WHERE extra_info ->> 'importId' = ANY (importIds::text[]);
                    DELETE FROM object_history WHERE object_id = ANY (recruitIds);
                    DELETE FROM object_versions WHERE object_id = ANY (recruitIds);
                END
            $$; 
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>journal_dev</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

<!-- 008-ervu_geps_notifications-delete-recruit -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    var1 uuid[] := '{
            ${endpointArguments}
            }';
                BEGIN
                    DELETE FROM notifications where recruit_id = any (var1);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>geps_notifications</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

<!-- 007_2-ervu_incidents-delete-all_of_recruit  -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    var1 uuid[] := '{
            ${endpointArguments}
            }';
                BEGIN
                    DELETE
                    FROM incident_history
                    where incident_id in (select id from incident where recruit_id = any (var1));
                    DELETE FROM incident where recruit_id = any (var1);
                    DELETE FROM recruits where id = any (var1);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>incidents</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

<!-- 001-ervu_person_registry-delete-recruit -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    var1 uuid[] := '{
            ${endpointArguments}
            }';
                BEGIN
                    DELETE FROM recruits_info where recruit_id = any (var1);
            --     DELETE FROM recruit_archive where recruit_id = any (var1);
                    DELETE FROM recruit_xml_data where recruit_id = any (var1);
                    DELETE FROM recruits_history where recruit_id = any (var1);
                    DELETE FROM application where recruit_id = any (var1);
                    DELETE FROM department_history where recruit_id = any (var1);
                    DELETE FROM documents where recruit_id = any (var1);
                    DELETE FROM decision where recruit_id = any (var1);
                    DELETE FROM personal_documents where recruit_id = any (var1);
                    DELETE FROM recruit_private_file where recruit_id = any (var1);
                    DELETE FROM system_documents where recruit_id = any (var1);
                    DELETE FROM system_document_dto where recruit_id = any (var1);
                    DELETE FROM subpoena_dto where recruit_id = any (var1);
                    DELETE FROM attachments where recruit_id = any (var1);
                    DELETE FROM summoned_list where recruit_id = any (var1);
                    DELETE FROM recruits where id = any (var1);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>person_registry</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

<!-- 009_1-ervu_appeal_document-delete-appeal-with-recruit    -->
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    var1 uuid[] := '{
            ${endpointArguments}
            }';
                BEGIN		
                    DELETE FROM appeal_document where recruit_id = any (var1);
                    DELETE FROM recruit where id = any (var1);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>appeal-document-service</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>
</Requests>