<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>border_crossing_flow</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/05 14:31:22.799</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/05 14:31:22.799</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>сreate_job_execution_record_BC</from>
      <to>table_input_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>filter_last_row_BC</from>
      <to>change_job_status_on_success_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>detect_empty_stream_BC</from>
      <to>change_job_status_on_success_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>change_job_status_on_error_BC</from>
      <to>abort_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_output_BC</from>
      <to>change_job_status_on_error_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_citizen_BC</from>
      <to>change_job_status_on_error_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_by_recruit_id_BC</from>
      <to>update_citizen_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_output_BC</from>
      <to>filter_last_row_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify_last_row_BC</from>
      <to>detect_empty_stream_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify_last_row_BC</from>
      <to>sort_by_recruit_id_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify_last_row_BC</from>
      <to>table_output_BC</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_BC</from>
      <to>Identify_last_row_BC</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>abort_BC</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>1280</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_error_BC</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 = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1104</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_success_BC</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 = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1280</xloc>
      <yloc>176</yloc>
    </GUI>
  </transform>
  <transform>
    <name>сreate_job_execution_record_BC</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>INSERT INTO etl.job_execution (id, job_name, status, execution_datetime, error_description, recruitment_id)
VALUES (DEFAULT, '${JOB_NAME}', 'PROCESSING', DEFAULT, null, '${IDM_ID}');

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>detect_empty_stream_BC</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>176</yloc>
    </GUI>
  </transform>
  <transform>
    <name>filter_last_row_BC</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_BC</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1280</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_input_BC</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit/>
    <sql>WITH filteredData AS (SELECT ri.recruit_id,
                             ri.info->'svedFL'->'extend'->'svedPeresechGran' as border_cross_arr
                      FROM ervu_dashboard.recruits_info ri
                      WHERE jsonb_typeof(ri.info->'svedFL'->'extend'->'svedPeresechGran') = 'array'
                        AND coalesce(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'
)
SELECT to_date(border_cross_elem->>'dataVyezd' , 'YYYY-MM-DD') as date_crossing,
       to_date(border_cross_elem->>'dataVozvrashh' , 'YYYY-MM-DD') as return_date,
       true as border_crossed
FROM filteredData fd
         CROSS JOIN LATERAL jsonb_array_elements(border_cross_arr) AS border_cross_elem;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_output_BC</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>date_crossing</column_name>
        <stream_name>date_crossing</stream_name>
      </field>
      <field>
        <column_name>return_date</column_name>
        <stream_name>return_date</stream_name>
      </field>
    </fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_field/>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_field/>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>Y</specify_fields>
    <table>border_crossing</table>
    <tablename_field/>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>912</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_citizen_BC</name>
    <type>Update</type>
    <description/>
    <distribute>Y</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>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>border_crossed</name>
        <rename>border_crossed</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>912</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>sort_by_recruit_id_BC</name>
    <type>SortRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <directory>${java.io.tmpdir}</directory>
    <prefix>srt</prefix>
    <sort_size>100000</sort_size>
    <free_memory/>
    <compress>N</compress>
    <compress_variable/>
    <unique_rows>Y</unique_rows>
    <fields>
      <field>
        <name>recruit_id</name>
        <ascending>Y</ascending>
        <case_sensitive>N</case_sensitive>
        <collator_enabled>N</collator_enabled>
        <collator_strength>0</collator_strength>
        <presorted>N</presorted>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify_last_row_BC</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>result</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>table_output_BC</source_transform>
      <target_transform>change_job_status_on_error_BC</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_citizen_BC</source_transform>
      <target_transform>change_job_status_on_error_BC</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>
