1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.continua.hrn;
17
18 import org.apache.camel.Processor;
19 import org.openehealth.ipf.commons.core.modules.api.ValidationException;
20 import org.openehealth.ipf.commons.ihe.xds.CONTINUA_HRN;
21 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLProvideAndRegisterDocumentSetRequest30;
22 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
23 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Document;
24 import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
25 import org.openehealth.ipf.commons.ihe.xds.core.validate.requests.ProvideAndRegisterDocumentSetRequestValidator;
26 import org.openehealth.ipf.commons.xml.CombinedXmlValidationProfile;
27 import org.openehealth.ipf.commons.xml.CombinedXmlValidator;
28 import org.openehealth.ipf.modules.cda.CDAR2Constants;
29 import org.openehealth.ipf.platform.camel.ihe.xds.XdsCamelValidators;
30
31 import javax.xml.transform.Source;
32 import javax.xml.transform.stream.StreamSource;
33 import java.io.ByteArrayInputStream;
34 import java.util.Collections;
35 import java.util.Map;
36
37 import static org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter.validationEnabled;
38
39
40
41
42
43
44
45 abstract public class ContinuaHrnCamelProcessors {
46
47 private static final Processor HRN_REQUEST_TRANSFORMER_AND_VALIDATOR = exchange -> {
48 boolean validationEnabled = validationEnabled(exchange);
49
50
51 if (validationEnabled) {
52 EbXMLProvideAndRegisterDocumentSetRequest30 message =
53 new EbXMLProvideAndRegisterDocumentSetRequest30(exchange.getIn().getBody(ProvideAndRegisterDocumentSetRequestType.class));
54 new ProvideAndRegisterDocumentSetRequestValidator().validate(message, CONTINUA_HRN.Interactions.ITI_41);
55 }
56
57
58 ProvideAndRegisterDocumentSet request = exchange.getIn().getBody(ProvideAndRegisterDocumentSet.class);
59 exchange.getIn().setBody(request);
60
61 if (validationEnabled) {
62 if (request.getDocuments().size() != 1) {
63 throw new ValidationException("exactly one document must be provided in the HRN request");
64 }
65 }
66
67
68 Document document = request.getDocuments().get(0);
69 byte[] documentBytes = document.getContent(byte[].class);
70 String documentString = document.getContent(String.class);
71
72
73 if (validationEnabled) {
74 new CombinedXmlValidator().validate(documentString, new PhmrValidationProfile());
75 }
76 };
77
78
79
80
81
82 private static Source getSource(byte[] bytes) {
83 return new StreamSource(new ByteArrayInputStream(bytes));
84 }
85
86
87
88
89
90
91
92 public static Processor continuaHrnRequestTransformerAndValidator() {
93 return HRN_REQUEST_TRANSFORMER_AND_VALIDATOR;
94 }
95
96
97
98
99
100 public static Processor continuaHrnResponseValidator() {
101 return XdsCamelValidators.iti41ResponseValidator();
102 }
103
104
105
106 private static class PhmrValidationProfile implements CombinedXmlValidationProfile {
107 @Override
108 public boolean isValidRootElement(String rootElementName) {
109 return "ClinicalDocument".equals(rootElementName);
110 }
111
112 @Override
113 public String getXsdPath(String rootElementName) {
114 return CDAR2Constants.CDAR2_SCHEMA;
115 }
116
117 @Override
118 public String getSchematronPath(String rootElementName) {
119 return CDAR2Constants.CDA_PHMR_SCHEMATRON_RULES;
120 }
121
122 @Override
123 public Map<String, Object> getCustomSchematronParameters(String rootElementName) {
124 return Collections.<String, Object>singletonMap("phase", "errors");
125 }
126 }
127 }