View Javadoc
1   package ca.uhn.hl7v2.model;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.Map;
7   import java.util.Set;
8   
9   import ca.uhn.hl7v2.HL7Exception;
10  import ca.uhn.hl7v2.parser.ModelClassFactory;
11  import ca.uhn.hl7v2.util.StringUtil;
12  import ca.uhn.hl7v2.util.Terser;
13  
14  /**
15   * Base class for a {@link SuperStructure} message.
16   * 
17   * @see SuperStructure
18   * @see Message
19   */
20  public abstract class AbstractSuperMessage extends AbstractMessage implements SuperStructure {
21  
22  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(AbstractSuperMessage.class);
23  	private final Map<String, Set<String>> myChildNameToStructures = new HashMap<>();
24  
25  	private String myName;
26  
27  	/**
28  	 * Constructor
29  	 * 
30  	 * @param theFactory
31  	 *            The model class factory
32  	 */
33  	public AbstractSuperMessage(ModelClassFactory theFactory) {
34  		super(theFactory);
35  	}
36  
37  	protected void addSuperstructureApplication(String theChild, String theStructure) {
38  		StringUtil.validateNotEmpty(theChild);
39  		StringUtil.validateNotEmpty(theStructure);
40  
41  		if (!myChildNameToStructures.containsKey(theChild)) {
42  			myChildNameToStructures.put(theChild, new HashSet<>());
43  		}
44  		myChildNameToStructures.get(theChild).add(theStructure);
45  	}
46  
47  	/**
48  	 * Returns the name of this structure (e.g. "ADT_A01", or "ORU_R01"). Note
49  	 * that for super structures this value is explicitly set by the parser.
50  	 */
51  	public String getName() {
52  		if (StringUtil.isBlank(myName)) {
53  			String retVal = null;
54  			try {
55  				Terser t = new Terser(this);
56  				retVal = t.get("/MSH-9-3");
57  
58  				if (StringUtil.isBlank(retVal)) {
59  					String msh91 = t.get("/MSH-9-1");
60  					String msh92 = t.get("/MSH-9-2");
61  					if (StringUtil.isNotBlank(msh91) && StringUtil.isNotBlank(msh92)) {
62  						retVal = msh91 + "_" + msh92;
63  					}
64  				}
65  			} catch (HL7Exception e) {
66  				ourLog.debug("Failed to retrieve MSH-9", e);
67  			}
68  			return retVal;
69  		}
70  
71  		return myName;
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	public Set<String> getStructuresWhichChildAppliesTo(String theChildName) {
78  		return Collections.unmodifiableSet(myChildNameToStructures.get(theChildName));
79  	}
80  
81  	/**
82  	 * Provide the name that will be returned by {@link #getName()}
83  	 */
84  	public void setName(String theName) {
85  		myName = theName;
86  	}
87  
88  }