View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Validating and transformation processors for the Continua HRN transaction.
41   * 
42   * @author Dmytro Rud
43   * @author Stefan Ivanov
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          // ebXML validation
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          // transform ebXML into simplified model, extract embedded documents, check document count
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          // Document content type enrichment: create byte array and String
68          Document document = request.getDocuments().get(0);
69          byte[] documentBytes = document.getContent(byte[].class);
70          String documentString = document.getContent(String.class);
71  
72          // PHMR-specific validation
73          if (validationEnabled) {
74              new CombinedXmlValidator().validate(documentString, new PhmrValidationProfile());
75          }
76      };
77  
78  
79      /**
80       * Converts the given byte array to a Source object.
81       */
82      private static Source getSource(byte[] bytes) {
83          return new StreamSource(new ByteArrayInputStream(bytes));
84      }
85  
86  
87      /**
88       * Returns a transformation & validation processor for Continua HRN request messages.
89       * The HRN request contained in the exchange will be translated into IPF simplified
90       * XDS message model and enriched with CDA document content.
91       */
92      public static Processor continuaHrnRequestTransformerAndValidator() {
93          return HRN_REQUEST_TRANSFORMER_AND_VALIDATOR;
94      }
95      
96      /**
97       * Returns a validating processor for Continua HRN response messages.
98       * Actually there is no differences to ITI-41 response message validation.
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 }