001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
024import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
025import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
026import ca.uhn.fhir.context.FhirContext;
027import ca.uhn.fhir.context.RuntimeResourceDefinition;
028import ca.uhn.fhir.i18n.Msg;
029import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
030import org.hl7.fhir.instance.model.api.IBase;
031import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
032import org.hl7.fhir.instance.model.api.IBaseResource;
033import org.hl7.fhir.instance.model.api.IPrimitiveType;
034
035import java.util.List;
036
037import static org.apache.commons.lang3.StringUtils.isNotBlank;
038
039/**
040 * Utilities for dealing with OperationOutcome resources across various model versions
041 */
042public class OperationOutcomeUtil {
043
044        /**
045         * Add an issue to an OperationOutcome
046         *  @param theCtx              The fhir context
047         * @param theOperationOutcome The OO resource to add to
048         * @param theSeverity         The severity (fatal | error | warning | information)
049         * @param theDetails          The details string
050         * @param theCode
051         * @return Returns the newly added issue
052         */
053        public static IBase addIssue(FhirContext theCtx, IBaseOperationOutcome theOperationOutcome, String theSeverity, String theDetails, String theLocation, String theCode) {
054                IBase issue = createIssue(theCtx, theOperationOutcome);
055                populateDetails(theCtx, issue, theSeverity, theDetails, theLocation, theCode);
056                return issue;
057        }
058
059        private static IBase createIssue(FhirContext theCtx, IBaseResource theOutcome) {
060                RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome);
061                BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue");
062                BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) issueChild.getChildByName("issue");
063
064                IBase issue = issueElement.newInstance();
065                issueChild.getMutator().addValue(theOutcome, issue);
066                return issue;
067        }
068
069        public static String getFirstIssueDetails(FhirContext theCtx, IBaseOperationOutcome theOutcome) {
070                return getFirstIssueStringPart(theCtx, theOutcome, "diagnostics");
071        }
072
073        public static String getFirstIssueLocation(FhirContext theCtx, IBaseOperationOutcome theOutcome) {
074                return getFirstIssueStringPart(theCtx, theOutcome, "location");
075        }
076
077        private static String getFirstIssueStringPart(FhirContext theCtx, IBaseOperationOutcome theOutcome, String name) {
078                if (theOutcome == null) {
079                        return null;
080                }
081
082                RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome);
083                BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue");
084
085                List<IBase> issues = issueChild.getAccessor().getValues(theOutcome);
086                if (issues.isEmpty()) {
087                        return null;
088                }
089
090                IBase issue = issues.get(0);
091                BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(issue.getClass());
092                BaseRuntimeChildDefinition detailsChild = issueElement.getChildByName(name);
093
094                List<IBase> details = detailsChild.getAccessor().getValues(issue);
095                if (details.isEmpty()) {
096                        return null;
097                }
098                return ((IPrimitiveType<?>) details.get(0)).getValueAsString();
099        }
100
101        /**
102         * Returns true if the given OperationOutcome has 1 or more Operation.issue repetitions
103         */
104        public static boolean hasIssues(FhirContext theCtx, IBaseOperationOutcome theOutcome) {
105                if (theOutcome == null) {
106                        return false;
107                }
108                return getIssueCount(theCtx, theOutcome) > 0;
109        }
110
111        public static int getIssueCount(FhirContext theCtx, IBaseOperationOutcome theOutcome) {
112                RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome);
113                BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue");
114                return issueChild.getAccessor().getValues(theOutcome).size();
115        }
116
117        public static IBaseOperationOutcome newInstance(FhirContext theCtx) {
118                RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition("OperationOutcome");
119                try {
120                        return (IBaseOperationOutcome) ooDef.getImplementingClass().newInstance();
121                } catch (InstantiationException e) {
122                        throw new InternalErrorException(Msg.code(1803) + "Unable to instantiate OperationOutcome", e);
123                } catch (IllegalAccessException e) {
124                        throw new InternalErrorException(Msg.code(1804) + "Unable to instantiate OperationOutcome", e);
125                }
126        }
127
128        private static void populateDetails(FhirContext theCtx, IBase theIssue, String theSeverity, String theDetails, String theLocation, String theCode) {
129                BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(theIssue.getClass());
130                BaseRuntimeChildDefinition detailsChild;
131                detailsChild = issueElement.getChildByName("diagnostics");
132
133                BaseRuntimeChildDefinition codeChild = issueElement.getChildByName("code");
134                IPrimitiveType<?> codeElem = (IPrimitiveType<?>) codeChild.getChildByName("code").newInstance(codeChild.getInstanceConstructorArguments());
135                codeElem.setValueAsString(theCode);
136                codeChild.getMutator().addValue(theIssue, codeElem);
137
138                BaseRuntimeElementDefinition<?> stringDef = detailsChild.getChildByName(detailsChild.getElementName());
139                BaseRuntimeChildDefinition severityChild = issueElement.getChildByName("severity");
140
141                IPrimitiveType<?> severityElem = (IPrimitiveType<?>) severityChild.getChildByName("severity").newInstance(severityChild.getInstanceConstructorArguments());
142                severityElem.setValueAsString(theSeverity);
143                severityChild.getMutator().addValue(theIssue, severityElem);
144
145                IPrimitiveType<?> string = (IPrimitiveType<?>) stringDef.newInstance();
146                string.setValueAsString(theDetails);
147                detailsChild.getMutator().setValue(theIssue, string);
148
149                addLocationToIssue(theCtx, theIssue, theLocation);
150        }
151
152        public static void addLocationToIssue(FhirContext theContext, IBase theIssue, String theLocation) {
153                if (isNotBlank(theLocation)) {
154                        BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theContext.getElementDefinition(theIssue.getClass());
155                        BaseRuntimeChildDefinition locationChild = issueElement.getChildByName("location");
156                        IPrimitiveType<?> locationElem = (IPrimitiveType<?>) locationChild.getChildByName("location").newInstance(locationChild.getInstanceConstructorArguments());
157                        locationElem.setValueAsString(theLocation);
158                        locationChild.getMutator().addValue(theIssue, locationElem);
159                }
160        }
161}