<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>restriction_document_item_flow_delta</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/08/11 13:22:56.048</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/11 13:22:56.048</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>create_job_execution_record_RDI</from>
      <to>table_input_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>detect_empty_stream_RDI</from>
      <to>change_job_status_on_success_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>filter_rows_RDI</from>
      <to>change_job_status_on_success_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>change_job_status_on_error_RDI</from>
      <to>abort_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>insert_or_update_RDI</from>
      <to>change_job_status_on_error_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_flags_RDI</from>
      <to>change_job_status_on_error_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>insert_or_update_RDI</from>
      <to>identify_last_row_in_a_stream_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>identify_last_row_in_a_stream_RDI</from>
      <to>filter_rows_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_RDI</from>
      <to>insert_or_update_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_RDI</from>
      <to>detect_empty_stream_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_RDI</from>
      <to>group_by_RDI</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>group_by_RDI</from>
      <to>update_flags_RDI</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>abort_RDI</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT_WITH_ERROR</abort_option>
    <always_log_rows>Y</always_log_rows>
    <message/>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1488</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_error_RDI</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>error_description</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <delete_field/>
    <execute_each_row>Y</execute_each_row>
    <insert_field/>
    <quoteString>N</quoteString>
    <read_field/>
    <replace_variables>Y</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_ERROR',
    error_description = ?
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_success_RDI</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <delete_field/>
    <execute_each_row>Y</execute_each_row>
    <insert_field/>
    <quoteString>N</quoteString>
    <read_field/>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_SUCCESS'
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1552</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform>
    <name>create_job_execution_record_RDI</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <delete_field/>
    <execute_each_row>N</execute_each_row>
    <insert_field/>
    <quoteString>N</quoteString>
    <read_field/>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution  
SET
    status = 'DELTA_PROCESSING',
    execution_datetime = DEFAULT,
    error_description = NULL
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>320</xloc>
      <yloc>576</yloc>
    </GUI>
  </transform>
  <transform>
    <name>detect_empty_stream_RDI</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform>
    <name>filter_rows_RDI</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>last_row</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_true_to>change_job_status_on_success_RDI</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1552</xloc>
      <yloc>576</yloc>
    </GUI>
  </transform>
  <transform>
    <name>group_by_RDI</name>
    <type>GroupBy</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <add_linenr>N</add_linenr>
    <all_rows>N</all_rows>
    <directory>${java.io.tmpdir}</directory>
    <fields>
      <field>
        <aggregate>has_active_temporary_measure</aggregate>
        <subject>has_active_temporary_measure</subject>
        <type>MAX</type>
        <valuefield/>
      </field>
    </fields>
    <give_back_row>N</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <linenr_fieldname/>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>identify_last_row_in_a_stream_RDI</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>1344</xloc>
      <yloc>576</yloc>
    </GUI>
  </transform>
  <transform>
    <name>insert_or_update_RDI</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>id</field>
        <name>id</name>
        <name2/>
      </key>
      <key>
        <condition>=</condition>
        <field>created_at</field>
        <name>created_at</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>restriction_document_item</table>
      <value>
        <name>id</name>
        <rename>id</rename>
        <update>N</update>
      </value>
      <value>
        <name>restriction_document_create_id</name>
        <rename>restriction_document_create_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>created_at</name>
        <rename>created_at</rename>
        <update>N</update>
      </value>
      <value>
        <name>updated_at</name>
        <rename>updated_at</rename>
        <update>Y</update>
      </value>
      <value>
        <name>restriction_id</name>
        <rename>restriction_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status_applied</name>
        <rename>status_applied</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status_text</name>
        <rename>status_text</rename>
        <update>Y</update>
      </value>
      <value>
        <name>applied_date</name>
        <rename>applied_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>cancel_date</name>
        <rename>cancel_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>extra_info</name>
        <rename>extra_info</rename>
        <update>Y</update>
      </value>
      <value>
        <name>applied_fact</name>
        <rename>applied_fact</rename>
        <update>Y</update>
      </value>
      <value>
        <name>restriction_document_cancel_id</name>
        <rename>restriction_document_cancel_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status_canceled</name>
        <rename>status_canceled</rename>
        <update>Y</update>
      </value>
      <value>
        <name>foiv_code</name>
        <rename>foiv_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>human_readable_id</name>
        <rename>human_readable_id</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>576</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_input_RDI</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>postgres.subpoena</connection>
    <execute_each_row>N</execute_each_row>
    <limit/>
    <sql>select
s.recruit_id,
CASE
  WHEN rdi.applied_fact is true THEN true
  ELSE false
END AS has_active_temporary_measure, -- has_active_temporary_measure//Признак наличия примененных временных мер
rdi.*
from public.restriction_document_item rdi
join public.restriction_document rd on 
rd.id = coalesce(rdi.restriction_document_create_id,rdi.restriction_document_cancel_id)
join public.subpoena s on
s.id = rd.subpoena_id
WHERE 
    '${IDM_ID}' != ''  -- Проверка на пустую строку
    AND rd.vk_id = '${IDM_ID}'
    AND rdi.updated_at >= '${MAX_UPDATE_DATE}'::timestamp
ORDER BY s.recruit_id</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>576</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_flags_RDI</name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <ignore_flag_field/>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </key>
      <key>
        <condition>&lt;></condition>
        <field>has_active_temporary_measure</field>
        <name>has_active_temporary_measure</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_active_temporary_measure</name>
        <rename>has_active_temporary_measure</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>960</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>insert_or_update_RDI</source_transform>
      <target_transform>change_job_status_on_error_RDI</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>update_flags_RDI</source_transform>
      <target_transform>change_job_status_on_error_RDI</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
