001package ca.uhn.hl7v2.model;
002
003import java.util.Collections;
004import java.util.HashMap;
005import java.util.HashSet;
006import java.util.Map;
007import java.util.Set;
008
009import ca.uhn.hl7v2.HL7Exception;
010import ca.uhn.hl7v2.parser.ModelClassFactory;
011import ca.uhn.hl7v2.util.StringUtil;
012import ca.uhn.hl7v2.util.Terser;
013
014/**
015 * Base class for a {@link SuperStructure} message.
016 * 
017 * @see SuperStructure
018 * @see Message
019 */
020public abstract class AbstractSuperMessage extends AbstractMessage implements SuperStructure {
021
022        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(AbstractSuperMessage.class);
023        private Map<String, Set<String>> myChildNameToStructures = new HashMap<String, Set<String>>();
024
025        private String myName;
026
027        /**
028         * Constructor
029         * 
030         * @param theFactory
031         *            The model class factory
032         */
033        public AbstractSuperMessage(ModelClassFactory theFactory) {
034                super(theFactory);
035        }
036
037        protected void addSuperstructureApplication(String theChild, String theStructure) {
038                StringUtil.validateNotEmpty(theChild);
039                StringUtil.validateNotEmpty(theStructure);
040
041                if (!myChildNameToStructures.containsKey(theChild)) {
042                        myChildNameToStructures.put(theChild, new HashSet<String>());
043                }
044                myChildNameToStructures.get(theChild).add(theStructure);
045        }
046
047        /**
048         * Returns the name of this structure (e.g. "ADT_A01", or "ORU_R01"). Note
049         * that for super structures this value is explicitly set by the parser.
050         */
051        public String getName() {
052                if (StringUtil.isBlank(myName)) {
053                        String retVal = null;
054                        try {
055                                Terser t = new Terser(this);
056                                retVal = t.get("/MSH-9-3");
057
058                                if (StringUtil.isBlank(retVal)) {
059                                        String msh91 = t.get("/MSH-9-1");
060                                        String msh92 = t.get("/MSH-9-2");
061                                        if (StringUtil.isNotBlank(msh91) && StringUtil.isNotBlank(msh92)) {
062                                                retVal = msh91 + "_" + msh92;
063                                        }
064                                }
065                        } catch (HL7Exception e) {
066                                ourLog.debug("Failed to retrieve MSH-9", e);
067                        }
068                        return retVal;
069                }
070
071                return myName;
072        }
073
074        /**
075         * {@inheritDoc}
076         */
077        public Set<String> getStructuresWhichChildAppliesTo(String theChildName) {
078                return Collections.unmodifiableSet(myChildNameToStructures.get(theChildName));
079        }
080
081        /**
082         * Provide the name that will be returned by {@link #getName()}
083         */
084        public void setName(String theName) {
085                myName = theName;
086        }
087
088}