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