<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>property_flow_repeat</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/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>Create job execution record</from>
      <to>property_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>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_insert_or_update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_input</from>
      <to>property_insert_or_update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_input</from>
      <to>property_group_by</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_group_by</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>property_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>
  </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>1744</xloc>
      <yloc>832</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>1328</xloc>
      <yloc>832</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>1744</xloc>
      <yloc>256</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}'
)
ON CONFLICT (job_name, recruitment_id)
DO UPDATE SET
    status = 'PROCESSING',
    execution_datetime = DEFAULT,
    error_description = NULL
</sql>
    <attributes/>
    <GUI>
      <xloc>288</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>768</xloc>
      <yloc>256</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>1744</xloc>
      <yloc>496</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>1552</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>property_insert_or_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>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </key>
      <key>
        <condition>=</condition>
        <field>source_id</field>
        <name>source_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>property</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>kind_right</name>
        <rename>kind_right</rename>
        <update>Y</update>
      </value>
      <value>
        <name>address</name>
        <rename>address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>property_code</name>
        <rename>property_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>issue_year</name>
        <rename>issue_year</rename>
        <update>Y</update>
      </value>
      <value>
        <name>brand_model</name>
        <rename>brand_model</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reg_plate</name>
        <rename>reg_plate</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vin</name>
        <rename>vin</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vehicle_category</name>
        <rename>vehicle_category</rename>
        <update>Y</update>
      </value>
      <value>
        <name>start_date</name>
        <rename>start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>close_date</name>
        <rename>close_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>type_property</name>
        <rename>type_property</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source_update_date</name>
        <rename>source_update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source_id</name>
        <rename>source_id</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1328</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>property_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>
    <limit/>
    <sql>WITH base AS (
    SELECT
        ri.recruit_id,
        ri.info,
        jsonb_typeof(ri.info -> 'svedFL' -> 'svedON' -> 'on') = 'array'      AS has_realty,
        jsonb_typeof(ri.info -> 'svedFL' -> 'svedNazTS' -> 'nazTS') = 'array' AS has_ground,
        jsonb_typeof(ri.info -> 'svedFL' -> 'svedVozTS' -> 'vozTS') = 'array' AS has_air,
        jsonb_typeof(ri.info -> 'svedFL' -> 'svedVodTS' -> 'vodTS') = 'array' AS has_water
    FROM ervu_dashboard.recruits_info ri
    WHERE
        '${IDM_ID}' != '' AND
        COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}' AND
        (jsonb_typeof(info -> 'svedFL' -> 'svedON' -> 'on') = 'array' OR
         jsonb_typeof(info -> 'svedFL' -> 'svedNazTS' -> 'nazTS') = 'array' OR
         jsonb_typeof(info -> 'svedFL' -> 'svedVozTS' -> 'vozTS') = 'array' OR
         jsonb_typeof(info -> 'svedFL' -> 'svedVodTS' -> 'vodTS') = 'array')
),

     prop AS (
         SELECT
             base.recruit_id,
             'realty' AS property_type,
             property_data.value AS property_data
         FROM base
                  JOIN LATERAL jsonb_array_elements(base.info -> 'svedFL' -> 'svedON' -> 'on') AS property_data ON true
         WHERE base.has_realty

         UNION ALL

         SELECT
             base.recruit_id,
             'ground_transportation' AS property_type,
             property_data.value AS property_data
         FROM base
                  JOIN LATERAL jsonb_array_elements(base.info -> 'svedFL' -> 'svedNazTS' -> 'nazTS') AS property_data ON true
         WHERE base.has_ground

         UNION ALL

         SELECT
             base.recruit_id,
             'air_vehicles' AS property_type,
             property_data.value AS property_data
         FROM base
                  JOIN LATERAL jsonb_array_elements(base.info -> 'svedFL' -> 'svedVozTS' -> 'vozTS') AS property_data ON true
         WHERE base.has_air

         UNION ALL

         SELECT
             base.recruit_id,
             'water_vehicles' AS property_type,
             property_data.value AS property_data
         FROM base
                  JOIN LATERAL jsonb_array_elements(base.info -> 'svedFL' -> 'svedVodTS' -> 'vodTS') AS property_data ON true
         WHERE base.has_water
     ),

     all_data AS (
         SELECT
             prop.recruit_id,
             to_date(property_data ->> 'dataSved', 'YYYY-MM-DD') AS source_update_date,
             property_data ->> 'naimVidPrav' AS kind_right,
             property_data -> 'adrObSob' ->> 'adrObSobTekst' AS address,
             property_data ->> 'kodVidPrav' AS property_code,
             NULL AS issue_year,
             NULL AS brand_model,
             NULL AS reg_plate,
             NULL AS vin,
             NULL AS vehicle_category,
             to_date(property_data ->> 'dataRegPrav', 'YYYY-MM-DD') AS start_date,
             to_date(property_data ->> 'dataPrekPrav', 'YYYY-MM-DD') AS close_date,
             'realty' AS type_property,
             true AS has_possibility_reg_residence,
             property_data ->> 'id' AS source_id
         FROM prop
         WHERE prop.property_type = 'realty'

         UNION ALL

         SELECT
             prop.recruit_id,
             to_date(property_data ->> 'dataSved', 'YYYY-MM-DD'),
             NULL AS kind_right,
             NULL AS address,
             property_data ->> 'vidTS',
             property_data ->> 'godVyp',
             property_data ->> 'markaTS',
             property_data ->> 'gosRegZn',
             property_data ->> 'idenNom',
             NULL AS vehicle_category,
             to_date(property_data ->> 'dataRegPrav', 'YYYY-MM-DD'),
             to_date(property_data ->> 'dataPrekPrav', 'YYYY-MM-DD'),
             'ground_transportation',
             false,
             property_data ->> 'id' AS source_id
         FROM prop
         WHERE prop.property_type = 'ground_transportation'

         UNION ALL

         SELECT
             prop.recruit_id,
             to_date(property_data ->> 'dataSved', 'YYYY-MM-DD'),
             NULL,
             NULL,
             property_data ->> 'kodVid',
             NULL,
             NULL,
             NULL,
             NULL,
             property_data ->> 'naznKat',
             to_date(property_data ->> 'dataRegPrav', 'YYYY-MM-DD'),
             to_date(property_data ->> 'dataPrekPrav', 'YYYY-MM-DD'),
             'air_vehicles',
             false,
             property_data ->> 'id' AS source_id
         FROM prop
         WHERE prop.property_type = 'air_vehicles'

         UNION ALL

         SELECT
             prop.recruit_id,
             to_date(property_data ->> 'dataSved', 'YYYY-MM-DD'),
             NULL,
             NULL,
             property_data ->> 'kodVid',
             NULL,
             NULL,
             NULL,
             NULL,
             property_data ->> 'naznKat',
             to_date(property_data ->> 'dataRegPrav', 'YYYY-MM-DD'),
             to_date(property_data ->> 'dataPrekPrav', 'YYYY-MM-DD'),
             'water_vehicles',
             false,
             property_data ->> 'id' AS source_id
         FROM prop
         WHERE prop.property_type = 'water_vehicles'
     )

SELECT
    recruit_id,
    source_update_date,
    kind_right,
    address,
    property_code,
    issue_year::int,
    brand_model,
    reg_plate,
    vin,
    vehicle_category,
    start_date,
    close_date,
    type_property,
    has_possibility_reg_residence AS has_possibility_reg_residence,
    source_id,
    type_property = 'realty'                AS has_property,
    type_property = 'ground_transportation' AS has_ground_transport,
    type_property = 'air_vehicles'          AS has_air_transport,
    type_property = 'water_vehicles'        AS has_water_transport
FROM all_data
ORDER BY recruit_id;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>768</xloc>
      <yloc>496</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_property</name>
        <rename>has_property</rename>
      </value>
      <value>
        <name>has_ground_transport</name>
        <rename>has_ground_transport</rename>
      </value>
      <value>
        <name>has_air_transport</name>
        <rename>has_air_transport</rename>
      </value>
      <value>
        <name>has_water_transport</name>
        <rename>has_water_transport</rename>
      </value>
      <value>
        <name>has_possibility_reg_residence</name>
        <rename>has_possibility_reg_residence</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>768</xloc>
      <yloc>832</yloc>
    </GUI>
  </transform>
  <transform>
    <name>property_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_property</aggregate>
        <subject>has_property</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_ground_transport</aggregate>
        <subject>has_ground_transport</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_air_transport</aggregate>
        <subject>has_air_transport</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_water_transport</aggregate>
        <subject>has_water_transport</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_possibility_reg_residence</aggregate>
        <subject>has_possibility_reg_residence</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>768</xloc>
      <yloc>704</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>property_insert_or_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</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>
