<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>subpoena_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>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</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>Insert / update</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>Insert / update</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>Insert / update</from>
      <to>Filter rows</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>Update</from>
      <to>Change job status on error 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>User defined Java expression</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_by_recruit_id</from>
      <to>Group by</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>sort_by_recruit_id</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>1456</xloc>
      <yloc>800</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>1280</xloc>
      <yloc>800</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>1488</xloc>
      <yloc>560</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>400</xloc>
      <yloc>704</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>848</xloc>
      <yloc>560</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>1488</xloc>
      <yloc>704</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>848</xloc>
      <yloc>704</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update</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>id</field>
        <name>id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>subpoena</table>
      <value>
        <name>id</name>
        <rename>id</rename>
        <update>N</update>
      </value>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>send_date</name>
        <rename>send_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>series</name>
        <rename>series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>number</name>
        <rename>number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_id</name>
        <rename>reason_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name_responsible_user</name>
        <rename>full_name_responsible_user</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sig_info</name>
        <rename>sig_info</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_name</name>
        <rename>recruitment_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_address</name>
        <rename>recruitment_address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>visit_date</name>
        <rename>visit_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status_id</name>
        <rename>status_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>create_date</name>
        <rename>create_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status_change_date</name>
        <rename>status_change_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>subpoena_reason_type</name>
        <rename>subpoena_reason_type</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1056</xloc>
      <yloc>704</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.subpoena</connection>
    <execute_each_row>N</execute_each_row>
    <sql>SELECT
    s.id, --id повестки
    CASE
        WHEN (s.system_stages->>'subpoenaDirected')::text = 'true' THEN 1
        ELSE 0
        END AS has_sent_summons_raw, --факт направления повестки///Признак наличия направленной повестки
    CASE
        WHEN (s.system_stages->>'subpoenaAwarded') = 'true' THEN 1
        ELSE 0
        END AS has_served_summons_raw, --has_served_summons//Признак наличия врученной повестки
    CASE
        WHEN (s.system_stages->>'recruitAttendanceConfirm') = 'true' THEN 1
        ELSE 0
        END AS appeared_to_recruitment_center_raw, -- appeared_to_recruitment_center//Признак явки в военный комиссариат
    CASE
        WHEN ss.code = '5.1' THEN 1
        ELSE 0
        END AS not_appeared_by_valid_excuse_raw, -- not_appeared_by_valid_excuse//Признак неявки в военкомат по уважительной причине
    s.recruit_id,
    s.send_date, --дата направления повестки
    s.series, --серия повестки
    s.number, --новер повестки
    s.reason_id AS subpoena_reason, --причина вызова по повестке /// код передавать или сразу наименование?
    s.full_name_responsible_user, --фио военного комиссара(фио пользователя сформировавшео повестку)
    s.sig_info, --сертификат ключа проверки ЭП
    s.recruitment_name, --наименование ВК
    s.recruitment_address, --адрес, по которому необходимо явиться
    s.visit_date, --дата и время явки
    --, --способ направления // искать в subpoena_send_info
    --, --УН заказного почтового отправления // искать в subpoena_send_info
    s.status_id, --статус // ссылка на справочник subpoena_status/// AS subpoena_status
    s.create_date, --дата создания // для загрузки данных
    s.status_change_date,  --дата обновления // для загрузки данных (timestamp without time zone)
    sr.type AS subpoena_reason_type -- поля для карточки, раздел "Сведения о направлении повестки военного комиссариата"
FROM public.subpoena s
         join public.subpoena_status ss on s.status_id = ss.id
         join public.subpoena_reason sr on sr.id = s.reason_id
WHERE s.department_id = '${IDM_ID}'
    AND s.status_change_date >= '${M_S_UP_DATE}'::timestamp</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>608</xloc>
      <yloc>704</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>1760</xloc>
      <yloc>960</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>1536</xloc>
      <yloc>960</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_sent_summons</aggregate>
        <subject>has_sent_summons</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_served_summons</aggregate>
        <subject>has_served_summons</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>appeared_to_recruitment_center</aggregate>
        <subject>appeared_to_recruitment_center</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>not_appeared_by_valid_excuse</aggregate>
        <subject>not_appeared_by_valid_excuse</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>992</xloc>
      <yloc>960</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_sent_summons</name>
        <rename>has_sent_summons</rename>
      </value>
      <value>
        <name>has_served_summons</name>
        <rename>has_served_summons</rename>
      </value>
      <value>
        <name>appeared_to_recruitment_center</name>
        <rename>appeared_to_recruitment_center</rename>
      </value>
      <value>
        <name>not_appeared_by_valid_excuse</name>
        <rename>not_appeared_by_valid_excuse</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1360</xloc>
      <yloc>960</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_sent_summons</field_name>
      <formula_string>has_sent_summons_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_served_summons</field_name>
      <formula_string>has_served_summons_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>appeared_to_recruitment_center</field_name>
      <formula_string>appeared_to_recruitment_center_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>not_appeared_by_valid_excuse</field_name>
      <formula_string>not_appeared_by_valid_excuse_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>1184</xloc>
      <yloc>960</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>N</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>848</xloc>
      <yloc>960</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Insert / update</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>
