001/*
002 The contents of this file are subject to the Mozilla Public License Version 1.1
003 (the "License"); you may not use this file except in compliance with the License.
004 You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 Software distributed under the License is distributed on an "AS IS" basis,
006 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 specific language governing rights and limitations under the License.
008
009 The Original Code is "ChoiceElementsRespectedRule.java ".  Description:
010 "Message rule enforcing that one choice element is present"
011
012 The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 2013.  All Rights Reserved.
014
015 Contributor(s): ______________________________________.
016
017 Alternatively, the contents of this file may be used under the terms of the
018 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
019 applicable instead of those above.  If you wish to allow use of your version of this
020 file only under the terms of the GPL and not to allow others to use your version
021 of this file under the MPL, indicate your decision by deleting  the provisions above
022 and replace  them with the notice and other provisions required by the GPL License.
023 If you do not delete the provisions above, a recipient may use your version of
024 this file under either the MPL or the GPL.
025 */
026
027package ca.uhn.hl7v2.validation.builder.support;
028
029import java.util.ArrayList;
030import java.util.HashSet;
031import java.util.List;
032import java.util.Set;
033
034import ca.uhn.hl7v2.HL7Exception;
035import ca.uhn.hl7v2.model.Group;
036import ca.uhn.hl7v2.model.Message;
037import ca.uhn.hl7v2.model.Structure;
038import ca.uhn.hl7v2.validation.MessageRule;
039import ca.uhn.hl7v2.validation.ValidationException;
040import ca.uhn.hl7v2.validation.impl.AbstractMessageRule;
041
042/**
043 * Message rule which enforces that only one "choice element" within any groups
044 * in the message must only have content within one of the possible choices.
045 */
046public class ChoiceElementsRespectedRule extends AbstractMessageRule {
047
048        /** Singleton instance */
049        public static final MessageRule CHOICE_ELEMENTS_RESPECTED = new ChoiceElementsRespectedRule();
050
051        public ValidationException[] apply(Message theValue) {
052                List<ValidationException> exceptions = new ArrayList<ValidationException>();
053                apply(theValue, exceptions);
054                return exceptions.toArray(new ValidationException[exceptions.size()]);
055        }
056
057        private void apply(Group theStructure, List<ValidationException> theExceptions) {
058                Set<String> choicesWithContent = null;
059
060                for (String nextName : theStructure.getNames()) {
061                        try {
062                                boolean nextIsChoiceElement = theStructure.isChoiceElement(nextName);
063                                for (Structure nextStruct : theStructure.getAll(nextName)) {
064
065                                        if (nextIsChoiceElement && !nextStruct.isEmpty()) {
066                                                if (choicesWithContent == null) {
067                                                        choicesWithContent = new HashSet<String>();
068                                                }
069                                                choicesWithContent.add(nextName);
070                                        }
071
072                                        if (nextStruct instanceof Group) {
073                                                apply((Group) nextStruct, theExceptions);
074                                        }
075                                }
076                        } catch (HL7Exception e) {
077                                throw new Error("Failed to find " + nextName + " in structure. This is probably a HAPI bug.");
078                        }
079                }
080
081                if (choicesWithContent != null && choicesWithContent.size() > 1) {
082                        theExceptions.add(new ValidationException("Structure '" + theStructure.getName() + "' must have content only in one of the following choices: " + choicesWithContent.toString()));
083                }
084
085        }
086
087}