CREATE UNIQUE INDEX unique_xml_tag_config_not_null_ns ON public.xml_tag_config (tag_name, namespace) WHERE namespace IS NOT NULL;
CREATE UNIQUE INDEX unique_xml_tag_config_null_ns ON public.xml_tag_config (tag_name) WHERE namespace IS NULL;

INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'), 'Envelope','http://www.w3.org/2003/05/soap-envelope','SOAP Envelope', NULL);
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Body','http://www.w3.org/2003/05/soap-envelope','SOAP Body', NULL);
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Header','http://www.w3.org/2003/05/soap-envelope','SOAP Header', NULL);
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ClinicalDocument','urn:hl7-org:v3','CDA', 'CDA');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'([A-Z]{4}_[A-Z]{2}[0-9]{6})([A-Z]{2}[0-9]{2})','urn:hl7-org:v3','HL7V3', 'HL7V3');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Subscribe','http://docs.oasis-open.org/wsn/b-2','DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'SubscribeResponse', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Unsubscribe', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UnsubscribeResponse', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Notify', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'CreatePullPoint', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'CreatePullPointResponse', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'DestroyPullPoint', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'DestroyPullPointResponse', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'GetMessages', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'GetMessagesResponse', 'http://docs.oasis-open.org/wsn/b-2', 'DSUB', 'DSUB');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ProvideAndRegisterDocumentSetRequest', 'urn:ihe:iti:xds-b:2007', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveDocumentSetRequest', 'urn:ihe:iti:xds-b:2007', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveDocumentSetResponse', 'urn:ihe:iti:xds-b:2007', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveImagingDocumentSetRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'CatalogContentRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'CatalogContentResponse', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ValidateContentRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ValidateContentResponse', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AcceptObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ApproveObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'DeprecateObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RelocateObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RemoveObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'SubmitObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UndeprecateObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UpdateObjectsRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AdhocQueryQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AdhocQueryRequest', 'urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AdhocQueryResponse', 'urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AssociationQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AuditableEventQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ClassificationNodeQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ClassificationQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ClassificationSchemeQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ExternalIdentifierQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ExternalLinkQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ExtrinsicObjectQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'FederationQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'NotificationQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'OrganizationQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'PersonQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RegistryPackageQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RegistryQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ServiceBindingQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'ServiceQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'SpecificationLinkQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'SubscriptionQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UserQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AdhocQuery', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'QueryExpression', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RegistryRequest', NULL, 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RegistryResponse', 'urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0', 'XDS', 'XDS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'XDW.WorkflowDocument', 'urn:ihe:iti:2011:xdw', 'XDW', 'XDW');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AuditMessage', NULL, 'AUDIT_MESSAGE', 'AUDIT_MESSAGE');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'batchRequest', 'urn:oasis:names:tc:DSML:2:0:core', 'HPD', 'HPD');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'batchResponse', NULL, 'HPD', 'HPD');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveValueSetResponse', NULL, 'SVS', 'SVS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveMultipleValueSetsResponse', NULL, 'SVS', 'SVS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveValueSetRequest', NULL, 'SVS', 'SVS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RetrieveMultipleValueSetsRequest', 'urn:ihe:iti:svs:2008', 'SVS', 'SVS');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'XACMLAuthzDecisionQuery', 'urn:oasis:names:tc:xacml:2.0:context:schema:os', 'CH:ADR', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'XACMLAuthzDecisionQuery', 'urn:oasis:names:tc:xacml:2.0:profile:saml2.0:v2:schema:protocol', 'CH:ADR', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'AddPolicyRequest', 'urn:e-health-suisse:2015:policy-administration', 'CH:PPQ-1', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UpdatePolicyRequest', 'urn:e-health-suisse:2015:policy-administration', 'CH:PPQ-1', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'EprPolicyRepositoryResponse', 'urn:e-health-suisse:2015:policy-administration', 'CH:PPQ-1', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'DeletePolicyRequest', 'urn:e-health-suisse:2015:policy-administration', 'CH:PPQ-1', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'UnknownPolicySetId', 'urn:e-health-suisse:2015:policy-administration', 'CH:PPQ-1', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'XACMLPolicyQuery', 'urn:oasis:names:tc:xacml:2.0:profile:saml2.0:v2:schema:protocol', 'CH:PPQ-2', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Response', 'urn:oasis:names:tc:SAML:2.0:protocol', 'XACML Response', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Fault', NULL, 'SOAP Fault', NULL);
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RequestSecurityToken', 'http://docs.oasis-open.org/ws-sx/ws-trust/200512', 'WSTrust', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'RequestSecurityTokenResponseCollection', 'http://docs.oasis-open.org/ws-sx/ws-trust/200512', 'WSTrust', 'XML');
INSERT INTO "xml_tag_config" (id, tag_name, namespace, doc_type, validation_type) VALUES (nextval('xml_tag_config_seq'),'Assertion', 'urn:oasis:names:tc:SAML:2.0:assertion', 'SAML', 'SAML');

INSERT INTO "mime_type_config" (id, mime_type, doc_type, validation_type) VALUES (nextval('mime_type_config_seq'), 'application/pdf', 'PDF', 'PDF');

INSERT INTO "content_analysis_config" (id, doc_type, validation_type, byte_pattern, string_pattern, unwanted_content) VALUES (nextval('content_analysis_config_seq'), 'DICOM', 'DICOM', '\x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004449434d', NULL, NULL);

INSERT INTO "content_analysis_config" (id, doc_type, validation_type, byte_pattern, string_pattern, unwanted_content) VALUES (nextval('content_analysis_config_seq'), 'HL7v2', 'HL7V2', NULL, NULL, NULL);
INSERT INTO "starts_with" (content_analysis_config_id, startswith) VALUES (currval('content_analysis_config_seq'), 'MSH');

INSERT INTO "content_analysis_config" (id, doc_type, validation_type, byte_pattern, string_pattern, unwanted_content) VALUES (nextval('content_analysis_config_seq'), 'Certificate', 'TLS', NULL, NULL, NULL);
INSERT INTO "starts_with" (content_analysis_config_id, startswith) VALUES (currval('content_analysis_config_seq'), '-----BEGIN CERTIFICATE-----');

UPDATE "analyzed_message_part" set validation_type = 'HL7V3' where validation_type like 'HL7v3';
UPDATE "analyzed_message_part" set validation_type = null where validation_type like 'UNKNOWN';
UPDATE "analyzed_message_part" set validation_type = null where validation_type like '';
UPDATE "analyzed_message_part" set validation_type = null where validation_type like 'XACML';
UPDATE "analyzed_message_part" set validation_type = 'AUDIT_MESSAGE' where validation_type like 'AuditMessage';
UPDATE "analyzed_message_part" set validation_type = 'WS_TRUST' where validation_type like 'AuditMessage';
UPDATE "analyzed_message_part" set validation_type = null where validation_type not like 'ATNA'
AND validation_type not like 'AUDIT_MESSAGE'
AND validation_type not like 'CDA'
AND validation_type not like 'DICOM'
AND validation_type not like 'DSUB'
AND validation_type not like 'FHIR'
AND validation_type not like 'HL7V2'
AND validation_type not like 'HL7V3'
AND validation_type not like 'HPD'
AND validation_type not like 'PDF'
AND validation_type not like 'SAML'
AND validation_type not like 'SVS'
AND validation_type not like 'TLS'
AND validation_type not like 'WS_TRUST'
AND validation_type not like 'XDS'
AND validation_type not like 'XDW'
AND validation_type not like 'XML'
AND validation_type is not null;

INSERT INTO cmn_application_preference (id, preference_name, preference_value, class_name, description) VALUES (nextval('cmn_application_preference_id_seq'), 'x_validator_definition_oid', '1.3.6.1.4.1.12559.11.1.2.1.4.3', 'java.lang.String', 'Cross validators root oid');

UPDATE xval_cross_validator SET oid=((SELECT preference_value from cmn_application_preference where preference_name LIKE 'root_oid')||(SELECT preference_value FROM cmn_application_preference WHERE preference_name LIKE 'x_validator_definition_oid_suffix'))|| id WHERE oid IS NOT NULL;

UPDATE xval_validator_input SET message_type = 'REQUEST' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%request%');
UPDATE xval_validator_input SET message_type = 'RESPONSE' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%response%');
UPDATE xval_validator_input SET message_type = 'ATTACHMENT' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%attachment%');
UPDATE xval_validator_input SET message_type = 'REQUEST' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%XDS Metadata%');
UPDATE xval_validator_input SET message_type = 'RESPONSE' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%CDA Transport%');
UPDATE xval_validator_input SET message_type = 'REQUEST' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%PHARM_PRESCRIPTION%');
UPDATE xval_validator_input SET message_type = 'RESPONSE' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%PHARM_DISPENSATION%');
UPDATE xval_validator_input SET message_type = 'REQUEST' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%CDA L1%');
UPDATE xval_validator_input SET message_type = 'RESPONSE' WHERE referenced_object_id IN (SELECT id FROM xval_referenced_object WHERE keyword ilike '%CDA L3%');




