<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>work_activity_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>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Create job execution record</from>
      <to>citizen_work_activity_input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter_null_parental_leave_dates</from>
      <to>Block until transforms finish</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Block until transforms finish</from>
      <to>parental_leave_upsert</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>citizen_work_activity_input</from>
      <to>Filter_null_parental_leave_dates</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>citizen_work_activity_upsert</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>parental_leave_upsert</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>citizen_work_activity_input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>citizen_work_activity_upsert</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>citizen_work_activity_input</from>
      <to>Sort rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Sort rows</from>
      <to>citizen_work_activity_upsert</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>1552</xloc>
      <yloc>720</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Block until transforms finish</name>
    <type>BlockUntilTransformsFinish</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <transforms>
      <transform>
        <name>citizen_work_activity_upsert</name>
      </transform>
    </transforms>
    <attributes/>
    <GUI>
      <xloc>896</xloc>
      <yloc>720</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 = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1344</xloc>
      <yloc>720</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 = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1344</xloc>
      <yloc>336</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 = 'PROCESSING',
    execution_datetime = current_timestamp,
    error_description = NULL
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <attributes/>
    <GUI>
      <xloc>432</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>640</xloc>
      <yloc>336</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>Change job status on success</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1344</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter_null_parental_leave_dates</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>IS NOT NULL</function>
        <leftvalue>parental_leave_start_date</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
      </condition>
    </compare>
    <send_false_to>Change job status on success</send_false_to>
    <send_true_to>Block until transforms finish</send_true_to>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>720</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>out</prefix>
    <sort_size>100000</sort_size>
    <free_memory/>
    <compress>N</compress>
    <compress_variable/>
    <unique_rows>Y</unique_rows>
    <fields>
      <field>
        <name>source_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>896</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>citizen_work_activity_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 filtered_data AS (SELECT ri.recruit_id,
                             ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' as trud_arr,
				 ctid
                      FROM ervu_dashboard.recruits_info ri
                      WHERE jsonb_typeof(ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat') = '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,
       trud_elem ->> 'id'                                     AS source_id,
       trud_elem -> 'svedOrg' ->> 'naimOrg'                   AS employer_name,
       trud_elem ->> 'trudFunkcziya'                          AS position,
       to_date(trud_elem ->> 'dataMeropr', 'YYYY-MM-DD')      AS personnel_event_date,
       trud_elem ->> 'naimTipKadrMeropr'                      AS personnel_event_type,
       to_date(trud_elem ->> 'dataSved', 'YYYY-MM-DD')        AS source_update_date,
       coalesce((trud_elem ->> 'prAktMestRab') = '1', false)  AS actual_employer,
       to_date(uhod_elem ->> 'dataNachUhodReb', 'YYYY-MM-DD') AS parental_leave_start_date,
       to_date(uhod_elem ->> 'dataKonUhodReb', 'YYYY-MM-DD')  AS parental_leave_end_date,
	fd.ctid = (select max(ctid) from filtered_data) as last_row
FROM filtered_data fd
         CROSS JOIN LATERAL jsonb_array_elements(trud_arr) AS trud_elem
         LEFT JOIN LATERAL jsonb_array_elements(
                CASE
                    WHEN jsonb_typeof(trud_elem -> 'svedUhodReb') = 'array'
                        THEN trud_elem -> 'svedUhodReb'
                    ELSE '[]'::jsonb
                    END) AS uhod_elem
                           ON true;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>citizen_work_activity_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>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>work_activity</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>employer_name</name>
        <rename>employer_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>position</name>
        <rename>position</rename>
        <update>Y</update>
      </value>
      <value>
        <name>personnel_event_date</name>
        <rename>personnel_event_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>personnel_event_type</name>
        <rename>personnel_event_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source_update_date</name>
        <rename>source_update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>actual_employer</name>
        <rename>actual_employer</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1136</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>parental_leave_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>citizen_work_activity_source_id</field>
        <name>source_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>educational_leave</table>
      <value>
        <name>start_date</name>
        <rename>parental_leave_start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>end_date</name>
        <rename>parental_leave_end_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>educational_institution_learning_source_id</name>
        <rename>source_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>reason</name>
        <rename>academ_reason</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1136</xloc>
      <yloc>720</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>citizen_work_activity_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>
    <error>
      <source_transform>parental_leave_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>
