View Javadoc
1   /*
2    * Copyright 2014 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.commons.ihe.hl7v2.definitions;
18  
19  import ca.uhn.hl7v2.DefaultHapiContext;
20  import ca.uhn.hl7v2.HapiContext;
21  import ca.uhn.hl7v2.conf.store.ProfileStore;
22  import ca.uhn.hl7v2.conf.store.ProfileStoreFactory;
23  import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
24  import ca.uhn.hl7v2.parser.ModelClassFactory;
25  import ca.uhn.hl7v2.util.idgenerator.FileBasedHiLoGenerator;
26  import ca.uhn.hl7v2.util.idgenerator.IDGenerator;
27  import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
28  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationWithoutTNBuilder;
29  import ca.uhn.hl7v2.validation.impl.SimpleValidationExceptionHandler;
30  import org.openehealth.ipf.gazelle.validation.profile.HL7v2Transactions;
31  import org.openehealth.ipf.gazelle.validation.profile.store.GazelleProfileStore;
32  import org.openehealth.ipf.modules.hl7.parser.DefaultEscaping;
33  
34  /**
35   * This factory creates HapiContext instances that are required to derive {@link ca.uhn.hl7v2.parser.ModelClassFactory}
36   * and {@link ca.uhn.hl7v2.validation.ValidationContext} valid for the given
37   * {@link org.openehealth.ipf.gazelle.validation.profile.HL7v2Transactions HL7v2 transaction}.
38   * <p>
39   * By default, the HapiContext defines the transaction-specific conformance profile and profile store, and
40   * disables validation during parsing.
41   * </p>
42   */
43  public class HapiContextFactory {
44  
45      private static IDGenerator idGenerator = new FileBasedHiLoGenerator();
46  
47      /**
48       * Allows to globally set the {@link ca.uhn.hl7v2.util.idgenerator.IDGenerator} that generates
49       * IDs for new HL7 messages from new HapiContext instances. This does not affect the ID generation
50       * of already created HapiContexts nor does it do any cleanup of the previous generator.
51       *
52       * @param generator global ID generator
53       */
54      public static synchronized void setIdGenerator(IDGenerator generator) {
55          idGenerator = generator;
56      }
57  
58      /**
59       * Returns a default HapiContext
60       *
61       * @return default HapiContext
62       */
63      public static HapiContext createHapiContext() {
64          HapiContext context = new DefaultHapiContext();
65          context.getParserConfiguration().setEscaping(DefaultEscaping.INSTANCE);
66          return context;
67      }
68  
69      /**
70       * Returns a HapiContext for the provided model class factory
71       *
72       * @param modelClassFactory model clas factory
73       * @return HapiContext
74       */
75      public static HapiContext createHapiContext(ModelClassFactory modelClassFactory) {
76          return createHapiContext(
77                  modelClassFactory,
78                  new DefaultValidationWithoutTNBuilder(),
79                  ProfileStoreFactory.getProfileStore());
80      }
81  
82      /**
83       * Returns a HapiContext for the provided
84       * {@link org.openehealth.ipf.gazelle.validation.profile.HL7v2Transactions HL7v2 transaction}, using
85       * a {@link ca.uhn.hl7v2.parser.DefaultModelClassFactory default HL7 model}
86       *
87       * @param transactions profile enumeration
88       * @return HapiContext
89       */
90      public static HapiContext createHapiContext(HL7v2Transactions transactions) {
91          return createHapiContext(
92                  new DefaultModelClassFactory(),
93                  transactions);
94      }
95  
96  
97      /**
98       * Returns a HapiContext for the provided
99       * {@link org.openehealth.ipf.gazelle.validation.profile.HL7v2Transactions HL7v2 transaction}, using
100      * a custom HL7 model.
101      *
102      * @param modelClassFactory transaction-specific model-class factory
103      * @param transactions      profile enumeration
104      * @return HapiContext
105      */
106     public static HapiContext createHapiContext(ModelClassFactory modelClassFactory, HL7v2Transactions transactions) {
107         return createHapiContext(
108                 modelClassFactory,
109                 new ConformanceProfileBasedValidationBuilder(transactions),
110                 new GazelleProfileStore());
111     }
112 
113     /**
114      * @param modelClassFactory     transaction-specific model-class factory
115      * @param validationRuleBuilder validation rule builder
116      * @param profileStore          profile store
117      * @return HapiContext
118      */
119     public static HapiContext createHapiContext(ModelClassFactory modelClassFactory, ValidationRuleBuilder validationRuleBuilder, ProfileStore profileStore) {
120         HapiContext context = new DefaultHapiContext(modelClassFactory);
121         context.setProfileStore(profileStore);
122         context.setValidationRuleBuilder(validationRuleBuilder);
123         context.getParserConfiguration().setValidating(false);
124         context.getParserConfiguration().setIdGenerator(idGenerator);
125         context.getParserConfiguration().setEscaping(DefaultEscaping.INSTANCE);
126         context.setValidationExceptionHandlerFactory(new SimpleValidationExceptionHandler(context));
127         return context;
128     }
129 }