INSERT INTO tf_domain (id, keyword, description, name) VALUES (nextval('tf_domain_id_seq'), 'EPD', 'eHealthSuisse domain', 'Electronische Patient Dossier');

UPDATE app_configuration SET value = 'EPD' where variable = 'default_pdq_domain';

INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201305UV02', 'CH:PDQ - Patient Demographics Query', (select id from tf_actor where keyword = 'PDC'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'QUQI_IN000003UV01', 'CH:PDQ - Patient Demographics Query HL7V3 Continuation', (select id from tf_actor where keyword = 'PDC'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'QUQI_IN000003UV01_Cancel', 'CH:PDQ - Patient Demographics Query HL7V3 Cancellation', (select id from tf_actor where keyword = 'PDC'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201306UV02', 'CH:PDQ - Patient Demographics Query Response', (select id from tf_actor where keyword = 'PDS'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'MCCI_IN000002UV01', 'CH:PDQ - Accept Acknowledgement', (select id from tf_actor where keyword = 'PDS'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'MCCI_IN000002UV01', 'CH:PIX - Patient Identity Feed HL7V3 - Acknowledgement', (select id from tf_actor where keyword = 'PAT_IDENTITY_X_REF_MGR'), (select id from tf_transaction where keyword = 'ITI-44'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201309UV02', 'CH:PIX - PIX Query', (select id from tf_actor where keyword = 'PAT_IDENTITY_CONSUMER'), (select id from tf_transaction where keyword = 'ITI-45'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201302UV02', 'CH:PIX - PIX Update Notification', (select id from tf_actor where keyword = 'PAT_IDENTITY_X_REF_MGR'), (select id from tf_transaction where keyword = 'ITI-46'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'MCCI_IN000002UV01', 'CH:PIX - PIX Update Notification acknowledgement', (select id from tf_actor where keyword = 'PAT_IDENTITY_CONSUMER'), (select id from tf_transaction where keyword = 'ITI-46'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201301UV02', 'CH:PIX - Patient Identity Feed HL7V3 - Add Patient Record', (select id from tf_actor where keyword = 'PAT_IDENTITY_SRC'), (select id from tf_transaction where keyword = 'ITI-44'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201302UV02', 'CH:PIX - Patient Identity Feed HL7V3 - Patient Revise Record', (select id from tf_actor where keyword = 'PAT_IDENTITY_SRC'), (select id from tf_transaction where keyword = 'ITI-44'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201304UV02', 'CH:PIX - Patient Identity Feed HL7V3 - Patient Identity Merge', (select id from tf_actor where keyword = 'PAT_IDENTITY_SRC'), (select id from tf_transaction where keyword = 'ITI-44'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201310UV02', 'CH:PIX - PIX Query Response', (select id from tf_actor where keyword = 'PAT_IDENTITY_X_REF_MGR'), (select id from tf_transaction where keyword = 'ITI-45'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201305UV02', 'CH:XCPD - Cross Gateway Patient Discovery Request', (select id from tf_actor where keyword = 'INIT_GW'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201305UV02 (deferred)', 'CH:XCPD - Cross Gateway Patient Discovery Request (Deferred option)',  (select id from tf_actor where keyword = 'INIT_GW'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'PRPA_IN201306UV02', 'CH:XCPD - Cross Gateway Patient Discovery Response',  (select id from tf_actor where keyword = 'RESP_GW'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'MCCI_IN000002UV01', 'CH:XCPD - Accept Acknowledgement',  (select id from tf_actor where keyword = 'RESP_GW'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from tf_domain where keyword = 'EPD'), 1);
INSERT INTO validation_parameters (id, message_type, profile_oid, actor_id, transaction_id, domain_id, validator_type) VALUES (nextval('ort_validation_parameters_id_seq'), 'MCCI_IN000002UV01', 'CH:XCPD - Accept Acknowledgement',  (select id from tf_actor where keyword = 'INIT_GW'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from tf_domain where keyword = 'EPD'), 1);

INSERT INTO affinity_domain (id, keyword, label_to_display, profile) VALUES (nextval('affinity_domain_id_seq'), 'EPD', 'EPD', 'All');

INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-47'), (select id from affinity_domain where keyword = 'EPD'), 'PDQV3 Supplier', 'EPD_PDQV3_PDS');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from affinity_domain where keyword = 'EPD'), 'XCPD Responding Gateway', 'EPD_XCPD_RESP');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-56'), (select id from affinity_domain where keyword = 'EPD'), 'XCPD Responding Gateway - Health Data Locator option', 'EPD_XCPD_RESP_PLQ');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from affinity_domain where keyword = 'EPD'), 'XCPD Initiating Gateway - Deferred response option', 'EPD_XCPD_INIT_DEFERRED');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-55'), (select id from affinity_domain where keyword = 'EPD'), 'XCPD Responding Gateway - Deferred response option', 'EPD_XCPD_RESP_DEFERRED');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-45'), (select id from affinity_domain where keyword = 'EPD'), 'PIXV3 Manager', 'EPD_PIX_MGR_ITI45');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-44'), (select id from affinity_domain where keyword = 'EPD'), 'PIXV3 Manager', 'EPD_PIX_MGR_ITI44');
INSERT INTO usage_metadata (id, transaction_id, affinity_id, action, keyword) VALUES (nextval('usage_id_seq'), (select id from tf_transaction where keyword = 'ITI-46'), (select id from affinity_domain where keyword = 'EPD'), 'PIXV3 Consumer', 'EPD_PIX_CONS');

INSERT INTO pam_person (id, contact_role_code, first_name, identifier, last_name, relationship_code, second_name, patient_id)
  SELECT nextval('pam_person_id_seq'),
    'N', null, null, mother_maiden_name, 'MTH', null, id
  FROM pat_patient WHERE mother_maiden_name is not NULL and mother_maiden_name != '';

UPDATE pat_patient SET mother_maiden_name = NULL ;
UPDATE pat_patient SET alternate_mothers_maiden_name = NULL ;