<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>deferment_liberation_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>Create job execution record</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>sort_by_recruit_id</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_by_recruit_id</from>
      <to>Group by</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error 2</from>
      <to>Abort 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Group by</from>
      <to>User defined Java expression</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>User defined Java expression</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Change job status on error 2</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>1408</xloc>
      <yloc>400</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>1200</xloc>
      <yloc>400</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>1408</xloc>
      <yloc>176</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>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>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>320</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>688</xloc>
      <yloc>176</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>1408</xloc>
      <yloc>320</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>688</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>postgres.decision-document-service</connection>
    <execute_each_row>N</execute_each_row>
    <limit/>
    <sql>SELECT
    dd.id AS deferment_id,
    dd.recruit_id,
    dd.base_deferment,
    dd.created_at,
    dd.updated_at,
    dd.postponement_date,
    dd.hidden,
    CASE ddt.code
        WHEN '1' THEN 'deferment'
        WHEN '2' THEN 'liberation'
        END AS type,
--     'fact' AS permission_fact,
    dd.fact_provision AS has_deferment_raw,
    -- true только если тип "освобождение" и факт предоставления = 1
    case when (ddt.code = '2' AND dd.fact_provision = '1') then 1
        else 0 end AS has_liberation_raw
FROM public.deferment_decision dd
         JOIN public.recruit r
              ON r.id = dd.recruit_id
                  AND (r.current_recruitment = '${IDM_ID}'
                      OR (r.current_recruitment IS NULL AND r.target_recruitment = '${IDM_ID}'))
         JOIN public.deferment_decision_type ddt
              ON dd.type_id = ddt.id
WHERE ddt.code IN ('1', '2');</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</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>deferment_id</column_name>
        <stream_name>deferment_id</stream_name>
      </field>
      <field>
        <column_name>base_deferment</column_name>
        <stream_name>base_deferment</stream_name>
      </field>
      <field>
        <column_name>created_at</column_name>
        <stream_name>created_at</stream_name>
      </field>
      <field>
        <column_name>updated_at</column_name>
        <stream_name>updated_at</stream_name>
      </field>
      <field>
        <column_name>postponement_date</column_name>
        <stream_name>postponement_date</stream_name>
      </field>
      <field>
        <column_name>hidden</column_name>
        <stream_name>hidden</stream_name>
      </field>
      <field>
        <column_name>type</column_name>
        <stream_name>type</stream_name>
      </field>
      <field>
        <column_name>permission_fact</column_name>
        <stream_name>permission_fact</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_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>Y</specify_fields>
    <table>deferment_liberation</table>
    <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>880</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</name>
    <type>Update</type>
    <description/>
    <distribute>N</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>has_liberation</name>
        <rename>has_liberation</rename>
      </value>
      <value>
        <name>has_deferment</name>
        <rename>has_deferment</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform>
    <name>sort_by_recruit_id</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>688</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Group by</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_deferment_raw</aggregate>
        <subject/>
        <type>MAX</type>
        <valuefield/>
      </field>
      <field>
        <aggregate>has_liberation_raw</aggregate>
        <subject/>
        <type>MAX</type>
        <valuefield/>
      </field>
    </fields>
    <give_back_row>N</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <linenr_fieldname/>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>816</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform>
    <name>User defined Java expression</name>
    <type>Janino</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <formula>
      <field_name>has_deferment</field_name>
      <formula_string>has_deferment_raw == 1</formula_string>
      <value_type>Boolean</value_type>
      <value_length>-1</value_length>
      <value_precision>-1</value_precision>
      <replace_field/>
    </formula>
    <formula>
      <field_name>has_liberation</field_name>
      <formula_string>has_liberation_raw == 1</formula_string>
      <value_type>Boolean</value_type>
      <value_length>-1</value_length>
      <value_precision>-1</value_precision>
      <replace_field/>
    </formula>
    <attributes/>
    <GUI>
      <xloc>976</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <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>1520</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on error 2</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>1312</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Table output</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>Update</source_transform>
      <target_transform>Change job status on error 2</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>
