001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.validation;
021
022import org.apache.commons.lang3.builder.EqualsBuilder;
023import org.apache.commons.lang3.builder.HashCodeBuilder;
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.commons.lang3.builder.ToStringStyle;
026
027import java.util.ArrayList;
028import java.util.List;
029
030import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;
031
032public class SingleValidationMessage {
033
034        private Integer myLocationCol;
035        private Integer myLocationLine;
036        private String myLocationString;
037        private String myMessage;
038        private String myMessageId;
039        private ResultSeverityEnum mySeverity;
040        private List<String> mySliceMessages;
041
042        /**
043         * Constructor
044         */
045        public SingleValidationMessage() {
046                super();
047        }
048
049        /**
050         * Copy constructor
051         */
052        public SingleValidationMessage(SingleValidationMessage theMessage) {
053                this.myLocationCol = theMessage.myLocationCol;
054                this.myLocationLine = theMessage.myLocationLine;
055                this.myLocationString = theMessage.myLocationString;
056                this.myMessage = theMessage.myMessage;
057                this.myMessageId = theMessage.myMessageId;
058                this.mySeverity = theMessage.mySeverity;
059                this.mySliceMessages = theMessage.mySliceMessages == null ? null : new ArrayList<>(theMessage.mySliceMessages);
060        }
061
062        @Override
063        public boolean equals(Object obj) {
064                if (this == obj) {
065                        return true;
066                }
067                if (obj == null) {
068                        return false;
069                }
070                if (!(obj instanceof SingleValidationMessage)) {
071                        return false;
072                }
073                SingleValidationMessage other = (SingleValidationMessage) obj;
074                EqualsBuilder b = new EqualsBuilder();
075                b.append(myLocationCol, other.myLocationCol);
076                b.append(myLocationLine, other.myLocationLine);
077                b.append(myLocationString, other.myLocationString);
078                b.append(myMessage, other.myMessage);
079                b.append(mySeverity, other.mySeverity);
080                b.append(mySliceMessages, other.mySliceMessages);
081                return b.isEquals();
082        }
083
084        public Integer getLocationCol() {
085                return myLocationCol;
086        }
087
088        public Integer getLocationLine() {
089                return myLocationLine;
090        }
091
092        public String getLocationString() {
093                return myLocationString;
094        }
095
096        public String getMessage() {
097                return myMessage;
098        }
099
100        public String getMessageId() {
101                return myMessageId;
102        }
103
104        public ResultSeverityEnum getSeverity() {
105                return mySeverity;
106        }
107
108        @Override
109        public int hashCode() {
110                HashCodeBuilder b = new HashCodeBuilder();
111                b.append(myLocationCol);
112                b.append(myLocationCol);
113                b.append(myLocationString);
114                b.append(myMessage);
115                b.append(mySeverity);
116                b.append(mySliceMessages);
117                return b.toHashCode();
118        }
119
120        public void setLocationCol(Integer theLocationCol) {
121                myLocationCol = theLocationCol;
122        }
123
124        public void setLocationLine(Integer theLocationLine) {
125                myLocationLine = theLocationLine;
126        }
127
128        public void setLocationString(String theLocationString) {
129                myLocationString = theLocationString;
130        }
131
132        public void setMessage(String theMessage) {
133                myMessage = theMessage;
134        }
135
136        public void setMessageId(String messageId) {
137                myMessageId = messageId;
138        }
139
140        public void setSeverity(ResultSeverityEnum theSeverity) {
141                mySeverity = theSeverity;
142        }
143
144        @Override
145        public String toString() {
146                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
147                if (myLocationCol != null || myLocationLine != null) {
148                        b.append("col", myLocationCol);
149                        b.append("row", myLocationLine);
150                }
151                if (myLocationString != null) {
152                        b.append("locationString", myLocationString);
153                }
154                b.append("message", myMessage);
155                if (myMessageId != null) {
156                        b.append(myMessageId);
157                }
158                if (mySeverity != null) {
159                        b.append("severity", mySeverity.getCode());
160                }
161                if (mySliceMessages != null) {
162                        b.append("sliceMessages", mySliceMessages);
163                }
164                return b.toString();
165        }
166
167        public void setSliceMessages(List<String> theSliceMessages) {
168                mySliceMessages = theSliceMessages;
169        }
170
171        public List<String> getSliceMessages() {
172                return mySliceMessages;
173        }
174
175        public boolean hasSliceMessages() {
176                return isNotEmpty(mySliceMessages);
177        }
178}