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.modules.hl7.model;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import org.openehealth.ipf.modules.hl7.HL7v2Exception;
22  
23  import ca.uhn.hl7v2.HL7Exception;
24  import ca.uhn.hl7v2.model.Structure;
25  import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
26  import ca.uhn.hl7v2.parser.ModelClassFactory;
27  
28  /**
29   * Convenience subclass of {@link ca.uhn.hl7v2.model.AbstractMessage} used for custom message structures
30   * as e.g. defined by IHE.
31   *
32   * @author Christian Ohr
33   */
34  @SuppressWarnings("serial")
35  public abstract class AbstractMessage extends ca.uhn.hl7v2.model.AbstractMessage {
36  
37  	protected enum Cardinality {
38          REQUIRED, OPTIONAL, REQUIRED_REPEATING, OPTIONAL_REPEATING
39      }
40  
41      public AbstractMessage() {
42          super(new DefaultModelClassFactory());
43          init();
44      }
45  
46      public AbstractMessage(ModelClassFactory factory) {
47          super(factory);
48          init();
49      }
50  
51      private void init() {
52          try {
53              for (Map.Entry<Class<? extends Structure>, Cardinality> structure : structures(
54                      new LinkedHashMap<>())
55                      .entrySet()) {
56                  switch (structure.getValue()) {
57                  case REQUIRED:
58                      add(structure.getKey(), true, false);
59                      break;
60                  case REQUIRED_REPEATING:
61                      add(structure.getKey(), true, true);
62                      break;
63                  case OPTIONAL:
64                      add(structure.getKey(), false, false);
65                      break;
66                  case OPTIONAL_REPEATING:
67                      add(structure.getKey(), false, true);
68                      break;
69                  default:
70                      // does not happen
71                      break;
72                  }
73  
74              }
75          } catch (HL7Exception e) {
76              throw new HL7v2Exception(e);
77          }
78      }
79  
80  
81      /**
82       * @deprecated use {@link ca.uhn.hl7v2.model.AbstractMessage#getTyped(String, Class)}
83       */
84      protected <T extends Structure> T get(Class<T> structureClass) {
85          return super.getTyped(structureClass.getSimpleName(), structureClass);
86      }
87  
88      /**
89       * @deprecated use {@link ca.uhn.hl7v2.model.AbstractMessage#getTyped(String, Class)}
90       */
91      protected <T extends Structure> T get(String structure, Class<T> structureClass) {
92          return super.getTyped(structure, structureClass);
93      }
94  
95      /**
96       * @deprecated use {@link ca.uhn.hl7v2.model.AbstractMessage#getTyped(String, int, Class)}
97       */
98      protected <T extends Structure> T get(Class<T> structureClass, int rep) {
99          return super.getTyped(structureClass.getSimpleName(), rep, structureClass);
100     }
101 
102     /**
103      * @deprecated use {@link ca.uhn.hl7v2.model.AbstractMessage#getTyped(String, int, Class)}
104      */
105     protected <T extends Structure> T get(String structure, Class<T> structureClass, int rep) {
106     	return super.getTyped(structure, rep, structureClass);
107     }
108 
109     /**
110      * @deprecated use {@link ca.uhn.hl7v2.model.AbstractGroup#getReps(String)}
111      */
112     protected <T extends Structure> int getReps(Class<T> structureClass) {
113         return super.getReps(structureClass.getSimpleName());
114     }
115 
116     /**
117      * @deprecated use {@link ca.uhn.hl7v2.model.AbstractGroup#getReps(String)}
118      */
119     protected <T extends Structure> int getReps(String structure, Class<T> structureClass) {
120         return super.getReps(structure);
121     }
122 
123     abstract protected Map<Class<? extends Structure>, Cardinality> structures(
124             Map<Class<? extends Structure>, Cardinality> structures);
125 
126 }