001package ca.cdr.api.pub.hl7v2.model;
002
003/*-
004 * #%L
005 * Smile CDR - CDR
006 * %%
007 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
008 * %%
009 * All rights reserved.
010 * #L%
011 */
012
013import ca.cdr.api.pub.hl7v2.IHl7V2MessageMapper;
014import ca.uhn.hl7v2.model.Message;
015
016import java.util.ArrayList;
017import java.util.Collections;
018import java.util.List;
019
020/**
021 * Holds the result of an HL7v2 message runtime mapping
022 *
023 * @see IHl7V2MessageMapper#map(Message, MappingContext)
024 */
025public class MappingResult {
026
027        private boolean myDoNotProcess;
028        private List<MappingMessage> myMappingMessages = new ArrayList<>();
029        private Message myMessage;
030
031        /**
032         * Constructor
033         */
034        public MappingResult() {
035                super();
036        }
037
038        /**
039         * Constructor
040         */
041        public MappingResult(Message theMessage) {
042                setMessage(theMessage);
043        }
044
045        /**
046         * Add a message relating to the mapping process
047         *
048         * @param theMappingMessage The message. Note that if the level is {@link MappingMessage.MessageLevel#ERROR} the {@link #isDoNotProcess() do not process} flag will be automatically set.
049         */
050        public void addMappingMessage(MappingMessage theMappingMessage) {
051                myMappingMessages.add(theMappingMessage);
052                if (theMappingMessage.getLevel().ordinal() >= MappingMessage.MessageLevel.ERROR.ordinal()) {
053                        myDoNotProcess = true;
054                }
055        }
056
057        /**
058         * @return Returns all mapping messages added to this result
059         */
060        public List<MappingMessage> getMappingMessages() {
061                return Collections.unmodifiableList(myMappingMessages);
062        }
063
064        /**
065         * Returns the resulting message
066         */
067        public Message getMessage() {
068                return myMessage;
069        }
070
071        /**
072         * Sets the resulting message
073         *
074         * @return Returns a reference to <code>this</code> for easy method chaining
075         */
076        public MappingResult setMessage(Message theMessage) {
077                myMessage = theMessage;
078                return this;
079        }
080
081        /**
082         * If set to {@literal true} (default is {@literal false}) the given message
083         * will not be processed by the system
084         */
085        public boolean isDoNotProcess() {
086                return myDoNotProcess;
087        }
088
089        /**
090         * If set to {@literal false} (default is {@literal true}) the given message
091         * will not be processed by the system
092         */
093        public boolean isDoProcess() {
094                return !isDoNotProcess();
095        }
096
097        /**
098         * If set to {@literal true} (default is {@literal false}) the given message
099         * will not be processed by the system
100         *
101         * @return Returns a reference to <code>this</code> for easy method chaining
102         */
103        public MappingResult setDoNotProcess(boolean theDoNotProcess) {
104                myDoNotProcess = theDoNotProcess;
105                return this;
106        }
107
108        /**
109         * Add mapping messages to the current list
110         */
111        public void addMappingMessages(List<MappingMessage> theMappingMessages) {
112                myMappingMessages.addAll(theMappingMessages);
113        }
114}