<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>passport_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</from>
      <to>passport_input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>passport_insert_or_update</from>
      <to>passport_error_job_status</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>passport_success_job_status</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>passport_error_job_status</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>passport_input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>passport_input</from>
      <to>passport_insert_or_update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>passport_insert_or_update</from>
      <to>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>passport_success_job_status</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort</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>1008</xloc>
      <yloc>464</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Create job execution record</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>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>
    <attributes/>
    <GUI>
      <xloc>128</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>400</xloc>
      <yloc>16</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter rows</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>passport_success_job_status</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1248</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>960</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>passport_error_job_status</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 = 'DELTA_ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>464</yloc>
    </GUI>
  </transform>
  <transform>
    <name>passport_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>
    <sql>    SELECT
        ri.recruit_id,
        NULLIF(p.passport_data->>'nomDok', '') AS passport_number,
        NULLIF(p.passport_data->>'serDok', '') AS passport_series,
        NULLIF(p.passport_data->>'vydDok', '') AS organization_name,
        NULLIF(p.passport_data->>'kodVydDok', '') AS unit_code,
        TO_DATE(NULLIF(p.passport_data->>'dataDok', ''), 'YYYY-MM-DD') AS issue_date,
        CASE
            WHEN NULLIF(p.passport_data->>'kodStatus', '') = '1' THEN TRUE
            ELSE FALSE
        END AS actual
    FROM ervu_dashboard.recruits_info ri
    JOIN ervu_dashboard.citizen r
        ON r.recruit_id = ri.recruit_id
        AND '${IDM_ID}' != ''
        AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'
        AND r.update_date >= '${M_R_UP_DATE}'::timestamp
    JOIN LATERAL (
        SELECT passport_data
        FROM UNNEST(ARRAY[
            ri.info->'svedFL'->'svedBS'->'pasportRF'->'aktPasportRF',
            ri.info->'svedFL'->'svedBS'->'pasportRF'->'predPasportRF'
        ]) AS passport_data
        WHERE passport_data IS NOT NULL AND passport_data::text &lt;&gt; 'null'
    ) AS p ON TRUE
    WHERE LENGTH(REGEXP_REPLACE(p.passport_data->>'serDok', '[^0-9]', '', 'g')) = 4 
      AND LENGTH(REGEXP_REPLACE(p.passport_data->>'nomDok', '[^0-9]', '', 'g')) = 6




</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>400</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>passport_insert_or_update</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>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>actual</field>
        <name>actual</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>passport</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>passport_number</name>
        <rename>passport_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_series</name>
        <rename>passport_series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>organization_name</name>
        <rename>organization_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>unit_code</name>
        <rename>unit_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>issue_date</name>
        <rename>issue_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>actual</name>
        <rename>actual</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>passport_success_job_status</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 = 'DELTA_SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}'
and status = 'DELTA_PROCESSING';

</sql>
    <attributes/>
    <GUI>
      <xloc>1248</xloc>
      <yloc>16</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>passport_insert_or_update</source_transform>
      <target_transform>passport_error_job_status</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>
