-- SQL statements to register this dispositive rule in the QCS database.
-- This file should be executed against both qcs.db files in quality-check-software and qcs-admin projects
-- ECARS-2368: Add a ErrorCodeMapperRule mapping error code into Survival labels.

-- Step 1: Add validation message for this rule. NOT USED, DONE JUST FOR ROBUSTNESS
INSERT INTO validationMessage(id,code,type,reference,description) VALUES
(218,'D-SURV','DISPOSITIVE','Mapping error/warnings into Survival flags','Mapping error/warnings into Survival flags, used as input for the CRATE Survival study');

-- Step 2: Add rule definition
INSERT INTO ruledefinition(ID,NAME,DESCRIPTION,POSITIVE,CASESENSITIVITY,LOCATION,IMPLEMENTATION,VALIDATIONMESSAGE_ID,RULETYPE_ID) VALUES
(330,'Survival error mapper','Survival CRATE adapter',true,'INSENSITIVE','eu.europa.ec.jrc.qcs.engine.rule.crossfield.encr2020','ErrorCodeMapperRule',218,10);

-- Step 3: EXPERIMENTAL: TO BE CONFIRMED
-- We must make sure that the ErrorCodeMapperRule is executed after ALL others cross-field rules,
-- that is: this should be the latest rule of the RECORD cycle.
UPDATE protocolToRule SET priority = 9 WHERE ID IN ( 
SELECT id FROM protocolToRule WHERE protocol_id = 11 AND priority = 8
);

-- Step 4: EXPERIMENTAL: TO BE CONFIRMED
INSERT INTO ruleType(id, ordered, description, level, name, scope) VALUES
(10,1,'a rule producing and/or updating specific data',5,'DispositiveRule','RECORD');

-- Step 5: Add rule to ENCR 2020 protocol (protocol_id=11) with priority 9 (function rules level)
INSERT INTO protocolToRule(id,protocol_id,priority,block,ruleDefinition_id) VALUES
(376,11,9,0,330);

-- Step 6: Rule-to-Field Mappings: 
INSERT INTO ruletofield(id,protocol_id,ruledefinition_id,target_id,position,key,field_id) VALUES 
(1663,11,376,2,0,0,NULL);
