View Javadoc
1   /*
2    * Copyright 2009 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.commons.ihe.hl7v2.definitions;
17  
18  import java.util.Collections;
19  import java.util.Map;
20  
21  import ca.uhn.hl7v2.Version;
22  import ca.uhn.hl7v2.parser.Parser;
23  import ca.uhn.hl7v2.parser.PipeParser;
24  import org.openehealth.ipf.modules.hl7.parser.CustomModelClassFactory;
25  
26  /**
27   * Utilities to work with custom HAPI class definitions.
28   *
29   * @author Dmytro Rud
30   */
31  public final class CustomModelClassUtils {
32  
33      private static final String CUSTOM_EVENT_MAP_DIRECTORY = "org/openehealth/ipf/commons/ihe/hl7v2/";
34  
35      private CustomModelClassUtils() {
36          throw new IllegalStateException("Utility class, cannot instantiate");
37      }
38  
39      /**
40       * Creates a model class factory for the given transaction and HL7 version.
41       */
42      public static CustomModelClassFactory createFactory(String transaction, String version) {
43          String packageName = String.format("%s.%s.%s",
44                  CustomModelClassUtils.class.getPackage().getName(),
45                  transaction,
46                  Version.versionOf(version).getPackageVersion());
47          Map<String, String[]> map = Collections.singletonMap(version, new String[]{packageName});
48          CustomModelClassFactory cmcf = new CustomModelClassFactory(map);
49          cmcf.setEventMapDirectory(CUSTOM_EVENT_MAP_DIRECTORY);
50          return cmcf;
51      }
52  
53      /**
54       * Creates a parser for the given transaction and HL7 version.
55       *
56       * @deprecated the correct parser should be obtained from the {@link ca.uhn.hl7v2.HapiContext}
57       * that has been configured with the correct {@link ca.uhn.hl7v2.parser.ModelClassFactory}.
58       */
59      public static Parser createParser(String transaction, String version) {
60          return new PipeParser(createFactory(transaction, version));
61      }
62  
63  }