<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>drivers_licence_flow_repeat</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <pipeline_status>0</pipeline_status>
    <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/08 13:25:52.319</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/08 13:25:52.319</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>driver_license_failure_job</from>
      <to>Abort 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>driver_lisence_job_create</from>
      <to>drivers_licence_input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream 2</from>
      <to>driver_licence_success_job</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows 2</from>
      <to>driver_licence_success_job</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream </from>
      <to>Filter rows 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>drivers_licence_input</from>
      <to>Detect empty stream 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>drivers_licence_input</from>
      <to>driver_licence_group_by_recruit_id</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_drivers_licence_flags</from>
      <to>driver_license_failure_job</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>drivers_licence_input</from>
      <to>Insert / update 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update 2</from>
      <to>Identify last row in a stream </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update 2</from>
      <to>driver_license_failure_job</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>driver_licence_group_by_recruit_id</from>
      <to>update_drivers_licence_flags</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort 2</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>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1712</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream 2</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>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter rows 2</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>driver_licence_success_job</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1200</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream </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>1200</xloc>
      <yloc>624</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update 2</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>N</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>source_id</field>
        <name>source_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>drivers_licence</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>status</name>
        <rename>status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>licence_series</name>
        <rename>licence_series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>licence_number</name>
        <rename>licence_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>categories</name>
        <rename>categories</rename>
        <update>Y</update>
      </value>
      <value>
        <name>start_date</name>
        <rename>start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>close_date</name>
        <rename>close_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>replacement_date</name>
        <rename>replacement_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>return_date</name>
        <rename>return_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>tractor_driver</name>
        <rename>tractor_driver</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source_update_date</name>
        <rename>source_update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source_id</name>
        <rename>source_id</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1200</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>driver_licence_group_by_recruit_id</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_driver_license</aggregate>
        <subject>is_has_driver_license</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_tractor_license</aggregate>
        <subject>is_has_tractor_license</subject>
        <type>MAX</type>
      </field>
    </fields>
    <give_back_row>N</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>1024</yloc>
    </GUI>
  </transform>
  <transform>
    <name>driver_licence_success_job</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>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <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>
    <attributes/>
    <GUI>
      <xloc>896</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>driver_license_failure_job</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>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <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>
    <attributes/>
    <GUI>
      <xloc>1456</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>driver_lisence_job_create</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>
    <execute_each_row>N</execute_each_row>
    <quoteString>N</quoteString>
    <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}'
)
ON CONFLICT (job_name, recruitment_id)
DO UPDATE SET
    status = 'PROCESSING',
    execution_datetime = DEFAULT,
    error_description = NULL;</sql>
    <attributes/>
    <GUI>
      <xloc>368</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>drivers_licence_input</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</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
    base AS (
        SELECT
            ri.recruit_id,
            ri.info
        FROM ervu_dashboard.recruits_info ri
                 JOIN ervu_dashboard.citizen r ON r.recruit_id = ri.recruit_id
       WHERE ('${IDM_ID}' != '' AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}')
          AND (
            jsonb_typeof(ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->'svedKat') = 'array'
                OR (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM') IS NOT NULL
                OR (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM') IS NOT NULL
            )
    ),
    all_data AS (
        -- Водительские удостоверения
        SELECT
            ri.recruit_id,
            (ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->> 'id') as source_id,
            to_date((ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->>'dataSved'), 'YYYY-MM-DD') AS source_update_date,
            (ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->>'kodStatus') AS status,
            (ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->>'serNomDok') AS licence_series,
            NULL AS licence_number,
            array_to_string(array(
                                    SELECT kat->>'naimKatTS'
                                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->'svedKat') AS kat
                            ), ',') AS categories,
            to_date((ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->>'dataVydDok'), 'YYYY-MM-DD') AS start_date,
            to_date((ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->>'srokDeistvDok'), 'YYYY-MM-DD') AS close_date,
            NULL AS replacement_date,
            NULL AS return_date,
            false AS tractor_driver
        FROM base ri
        WHERE jsonb_typeof(ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->'svedKat') = 'array'

        UNION ALL

        -- Действительные удостоверения тракториста
        SELECT
            ri.recruit_id,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->> 'id') as source_id,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataSved'), 'YYYY-MM-DD') AS source_update_date,
            '1' AS status,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'serUTM') AS licence_series,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'nomUTM') AS licence_number,
            (SELECT array_to_string(ARRAY(
                                            SELECT jsonb_array_elements_text(ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->'extend'->'kodKatUTM')
                                    ), ',')) AS categories,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataVydUTM'), 'YYYY-MM-DD') AS start_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataOkonchUTM'), 'YYYY-MM-DD') AS close_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataZameny'), 'YYYY-MM-DD') AS replacement_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataVozvr'), 'YYYY-MM-DD') AS return_date,
            true AS tractor_driver
        FROM base ri
        WHERE (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM') IS NOT NULL

        UNION ALL

        -- Просроченные удостоверения тракториста
        SELECT
            ri.recruit_id,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->> 'id') as source_id,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'dataSved'), 'YYYY-MM-DD') AS source_update_date,
            '0' AS status,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'serUTM') AS licence_series,
            (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'nomUTM') AS licence_number,
            (SELECT array_to_string(ARRAY(
                                            SELECT jsonb_array_elements_text(ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->'extend'->'kodKatUTM')
                                    ), ',')) AS categories,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'dataVydUTM'), 'YYYY-MM-DD') AS start_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'dataOkonchUTM'), 'YYYY-MM-DD') AS close_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'dataZameny'), 'YYYY-MM-DD') AS replacement_date,
            to_date((ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM'->>'dataVozvr'), 'YYYY-MM-DD') AS return_date,
            true AS tractor_driver
        FROM base ri
        WHERE (ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'predUTM') IS NOT NULL
    )
SELECT
    *,
    CASE
        WHEN tractor_driver = false AND status = '1' THEN true
        ELSE false
        END AS is_has_driver_license,
    CASE
        WHEN tractor_driver = true AND status = '1' THEN true
        ELSE false
        END AS is_has_tractor_license
FROM all_data
ORDER BY recruit_id </sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>784</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_drivers_licence_flags</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>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>tractor_license</name>
        <rename>has_tractor_license</rename>
      </value>
      <value>
        <name>has_driver_license</name>
        <rename>has_driver_license</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1456</xloc>
      <yloc>1024</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Insert / update 2</source_transform>
      <target_transform>driver_license_failure_job</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_drivers_licence_flags</source_transform>
      <target_transform>driver_license_failure_job</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>
