<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>foreign_diploma_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/09/05 08:36:17.176</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/09/05 08:36:17.176</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Create job execution record</from>
      <to>foreign_diploma_input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>foreign_diploma_upsert</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Sort rows</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>foreign_diploma_input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>foreign_diploma_input</from>
      <to>foreign_diploma_upsert</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>foreign_diploma_input</from>
      <to>Sort rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Unique rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Unique rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>foreign_diploma_upsert</from>
      <to>Filter rows</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</abort_option>
    <always_log_rows>Y</always_log_rows>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1328</xloc>
      <yloc>688</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on error</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>1136</xloc>
      <yloc>688</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on success</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}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1136</xloc>
      <yloc>208</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 = current_timestamp,
    error_description = NULL
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <attributes/>
    <GUI>
      <xloc>352</xloc>
      <yloc>496</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>576</xloc>
      <yloc>208</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>Unique rows</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1136</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Sort rows</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>576</xloc>
      <yloc>688</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Unique rows</name>
    <type>Unique</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <count_rows>N</count_rows>
    <fields>
      <field>
        <case_insensitive>N</case_insensitive>
        <name>recruit_id</name>
      </field>
    </fields>
    <reject_duplicate_row>N</reject_duplicate_row>
    <attributes/>
    <GUI>
      <xloc>1136</xloc>
      <yloc>352</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</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>N</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_foreign_diploma</name>
        <rename>has_foreign_diploma</rename>
      </value>
    </lookup>
    <skip_lookup>Y</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>832</xloc>
      <yloc>688</yloc>
    </GUI>
  </transform>
  <transform>
    <name>foreign_diploma_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>WITH filteredData AS (SELECT ri.recruit_id,
                             ri.info -> 'svedFL' -> 'svedInObr' -> 'inObr' as diploma_arr,
				 ctid
                      FROM ervu_dashboard.recruits_info ri
                      WHERE jsonb_typeof(ri.info -> 'svedFL' -> 'svedInObr' -> 'inObr') = 'array'
                        AND (ri.current_recruitment_id = '${IDM_ID}' or (ri.current_recruitment_id is null and ri.target_recruitment_id = '${IDM_ID}'))
)
SELECT fd.recruit_id,
       diploma_elem ->> 'id'                               AS source_id,
       to_date(diploma_elem ->> 'dataSved', 'YYYY-MM-DD')  as source_update_date,
       diploma_elem ->> 'kodUrObr'                         as education_level_code,
       diploma_elem ->> 'naimUchZaved'                     as educational_institution_name,
       diploma_elem ->> 'naimInoDok'                       as foreign_doc_name,
       diploma_elem ->> 'serNomDok'                        as ser_num_doc,
       diploma_elem ->> 'regNomDok'                        as reg_num,
       diploma_elem ->> 'nomSved'                          as recognition_certificate_number,
       to_date(diploma_elem ->> 'dataPriz', 'YYYY-MM-DD')  as recognition_date,
       diploma_elem -> 'svedSpecz' ->> 'naimSpecz'         as speciality_name,
       diploma_elem -> 'svedSpeczMinobr' ->> 'kodSpecz'    as minobr_speciality_code,
       diploma_elem -> 'svedSpeczMinprosv' ->> 'kodSpecz'  as minprosv_speciality_code,
       diploma_elem ->> 'kodOKSM'                          as oksm_code,
       coalesce(diploma_elem ->> 'prVUSpecz' = '1', false) as vus_spec,
	true as has_foreign_diploma,
	fd.ctid = (select max(ctid) from filteredData) as last_row
FROM filteredData fd
         CROSS JOIN LATERAL jsonb_array_elements(diploma_arr) AS diploma_elem
where case
          when '${MAX_SOURCE_UPDATE_DATE}' &lt;&gt; ''
              then to_date(diploma_elem ->> 'dataSved', 'YYYY-MM-DD') >
                   to_date('MAX_SOURCE_UPDATE_DATE', 'YYYY-MM-DD')
          else true end;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>576</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>foreign_diploma_upsert</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>source_id</field>
        <name>source_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>foreign_diploma</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>source_id</name>
        <rename>source_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>source_update_date</name>
        <rename>source_update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>education_level_code</name>
        <rename>education_level_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>educational_institution_name</name>
        <rename>educational_institution_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>foreign_doc_name</name>
        <rename>foreign_doc_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>ser_num_doc</name>
        <rename>ser_num_doc</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reg_num</name>
        <rename>reg_num</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recognition_certificate_number</name>
        <rename>recognition_certificate_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recognition_date</name>
        <rename>recognition_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>speciality_name</name>
        <rename>speciality_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>minobr_speciality_code</name>
        <rename>minobr_speciality_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>minprosv_speciality_code</name>
        <rename>minprosv_speciality_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>oksm_code</name>
        <rename>oksm_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vus_spec</name>
        <rename>vus_spec</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>832</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Update</source_transform>
      <target_transform>Change job status on error</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename/>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>foreign_diploma_upsert</source_transform>
      <target_transform>Change job status on error</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>
