View Javadoc
1   /*
2    * Copyright 2016 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  
17  package org.openehealth.ipf.boot.hl7v2;
18  
19  import ca.uhn.hl7v2.DefaultHapiContext;
20  import ca.uhn.hl7v2.HapiContext;
21  import ca.uhn.hl7v2.conf.store.ClasspathProfileStore;
22  import ca.uhn.hl7v2.conf.store.ProfileStore;
23  import ca.uhn.hl7v2.parser.ParserConfiguration;
24  import ca.uhn.hl7v2.util.idgenerator.IDGenerator;
25  import ca.uhn.hl7v2.util.idgenerator.IpfHiLoIdGenerator;
26  import ca.uhn.hl7v2.util.idgenerator.NanoTimeGenerator;
27  import ca.uhn.hl7v2.util.idgenerator.UUIDGenerator;
28  import ca.uhn.hl7v2.validation.ValidationContext;
29  import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
30  import org.apache.camel.CamelContext;
31  import org.apache.camel.component.hl7.CustomHL7MLLPCodec;
32  import org.apache.camel.component.hl7.HL7MLLPCodec;
33  import org.openehealth.ipf.boot.atna.IpfAtnaAutoConfiguration;
34  import org.openehealth.ipf.commons.ihe.hl7v2.storage.InteractiveContinuationStorage;
35  import org.openehealth.ipf.commons.ihe.hl7v2.storage.UnsolicitedFragmentationStorage;
36  import org.openehealth.ipf.modules.hl7.parser.CustomModelClassFactory;
37  import org.openehealth.ipf.modules.hl7.parser.DefaultEscaping;
38  import org.openehealth.ipf.platform.camel.ihe.mllp.core.intercept.consumer.ConsumerDispatchingInterceptor;
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
41  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
42  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
43  import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
44  import org.springframework.boot.context.properties.ConfigurationProperties;
45  import org.springframework.boot.context.properties.EnableConfigurationProperties;
46  import org.springframework.cache.CacheManager;
47  import org.springframework.context.annotation.Bean;
48  import org.springframework.context.annotation.Configuration;
49  
50  import java.util.HashMap;
51  import java.util.Map;
52  
53  /**
54   * Configure a basic IPF setup, mostly configuring HL7v2 and Mapping stuff
55   */
56  @Configuration
57  @AutoConfigureAfter(IpfAtnaAutoConfiguration.class)
58  @EnableConfigurationProperties(IpfHl7v2ConfigurationProperties.class)
59  public class IpfHl7v2AutoConfiguration {
60  
61      private static final String IPF_HL7_DEFINITIONS_PREFIX = "org.openehealth.ipf.commons.ihe.hl7v2.definitions";
62  
63      @Autowired
64      private IpfHl7v2ConfigurationProperties properties;
65  
66      @Bean
67      @ConditionalOnMissingBean(HL7MLLPCodec.class)
68      HL7MLLPCodec hl7codec(IpfHl7v2ConfigurationProperties config) {
69          HL7MLLPCodec hl7MLLPCodec = new CustomHL7MLLPCodec();
70          if (config.getCharset() != null) {
71              hl7MLLPCodec.setCharset(config.getCharset());
72          }
73          hl7MLLPCodec.setConvertLFtoCR(config.isConvertLinefeed());
74          return hl7MLLPCodec;
75      }
76  
77      @Bean
78      @ConditionalOnMissingBean(CustomModelClassFactory.class)
79      public CustomModelClassFactory mllpModelClassFactory() {
80          Map<String, String[]> eventMap = new HashMap<>();
81          eventMap.put("2.3.1", new String[] {
82                  IPF_HL7_DEFINITIONS_PREFIX + "pix.v231"
83          });
84          eventMap.put("2.5", new String[] {
85                  IPF_HL7_DEFINITIONS_PREFIX + "pdq.v25",
86                  IPF_HL7_DEFINITIONS_PREFIX + "pix.v25"
87          });
88          CustomModelClassFactory modelClassFactory = new CustomModelClassFactory(eventMap);
89          modelClassFactory.setEventMapDirectory("org/openehealth/ipf/commons/ihe/hl7v2/");
90          return modelClassFactory;
91      }
92  
93      @Bean
94      @ConditionalOnMissingBean(ProfileStore.class)
95      public ProfileStore profileStore() {
96          return new ClasspathProfileStore("/org/openehealth/ipf/gazelle/validation/profile/v2");
97      }
98  
99      @Bean
100     @ConditionalOnMissingBean(ValidationContext.class)
101     public ValidationContext validationContext() {
102         return ValidationContextFactory.noValidation();
103     }
104 
105     @Bean
106     @ConditionalOnMissingBean(IDGenerator.class)
107     @ConditionalOnProperty(prefix = "ipf.hl7v2", name = "generator", havingValue = "file", matchIfMissing = true)
108     public IDGenerator fileGenerator() {
109         return new IpfHiLoIdGenerator(properties.getIdGenerator());
110     }
111 
112     @Bean
113     @ConditionalOnMissingBean(IDGenerator.class)
114     @ConditionalOnProperty(prefix = "ipf.hl7v2", name = "generator", havingValue = "uuid")
115     public IDGenerator uuidGenerator() {
116         return new UUIDGenerator();
117     }
118 
119     @Bean
120     @ConditionalOnMissingBean(IDGenerator.class)
121     @ConditionalOnProperty(prefix = "ipf.hl7v2", name = "generator", havingValue = "nano")
122     public IDGenerator nanoGenerator() {
123         return new NanoTimeGenerator();
124     }
125 
126     @ConfigurationProperties(prefix = "ipf.hl7v2.parser")
127     @Bean
128     public ParserConfiguration parserConfiguration() {
129         return new ParserConfiguration();
130     }
131 
132     @Bean
133     @ConditionalOnMissingBean(HapiContext.class)
134     public HapiContext hapiContext(CustomModelClassFactory modelClassFactory, ProfileStore profileStore,
135                                    ValidationContext validationContext, ParserConfiguration parserConfiguration,
136                                    IDGenerator idGenerator) {
137         HapiContext context = new DefaultHapiContext();
138         context.setModelClassFactory(modelClassFactory);
139         context.setValidationContext(validationContext);
140         context.setProfileStore(profileStore);
141         context.setParserConfiguration(parserConfiguration);
142         context.getParserConfiguration().setIdGenerator(idGenerator);
143         context.getParserConfiguration().setEscaping(DefaultEscaping.INSTANCE);
144         return context;
145     }
146 
147     // Provide bean for MLLP endpoint dispatching
148     @Bean
149     @ConditionalOnMissingBean(ConsumerDispatchingInterceptor.class)
150     public ConsumerDispatchingInterceptor mllpDispatcher(CamelContext camelContext) {
151         return new ConsumerDispatchingInterceptor(camelContext);
152     }
153 
154     // Provide "interactiveContinuationStorage" for HL7v2 paging
155     @Bean
156     @ConditionalOnMissingBean(InteractiveContinuationStorage.class)
157     @ConditionalOnSingleCandidate(CacheManager.class)
158     @ConditionalOnProperty("ipf.hl7v2.caching")
159     public InteractiveContinuationStorage interactiveContinuationStorage(CacheManager cacheManager) {
160         return new CachingInteractiveHl7v2ContinuationStorage(cacheManager);
161     }
162 
163     // Provide "unsolicitedFragmentationStorage" for HL7v2 fragmentation
164     @Bean
165     @ConditionalOnMissingBean(UnsolicitedFragmentationStorage.class)
166     @ConditionalOnSingleCandidate(CacheManager.class)
167     @ConditionalOnProperty("ipf.hl7v2.caching")
168     public UnsolicitedFragmentationStorage unsolicitedFragmentationStorage(CacheManager cacheManager) {
169         return new CachingUnsolicitedFragmentionStorage(cacheManager);
170     }
171 
172 }