001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 University Health Network 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 */ 022import static org.apache.commons.lang3.StringUtils.isNotBlank; 023 024import java.util.List; 025 026import org.hl7.fhir.instance.model.api.IBase; 027import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; 028import org.hl7.fhir.instance.model.api.IBaseResource; 029import org.hl7.fhir.instance.model.api.IPrimitiveType; 030 031import ca.uhn.fhir.context.BaseRuntimeChildDefinition; 032import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition; 033import ca.uhn.fhir.context.BaseRuntimeElementDefinition; 034import ca.uhn.fhir.context.FhirContext; 035import ca.uhn.fhir.context.FhirVersionEnum; 036import ca.uhn.fhir.context.RuntimeResourceDefinition; 037import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 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 * 047 * @param theCtx 048 * The fhir context 049 * @param theOperationOutcome 050 * The OO resource to add to 051 * @param theSeverity 052 * The severity (fatal | error | warning | information) 053 * @param theDetails 054 * The details string 055 * @param theCode 056 */ 057 public static void addIssue(FhirContext theCtx, IBaseOperationOutcome theOperationOutcome, String theSeverity, String theDetails, String theLocation, String theCode) { 058 IBase issue = createIssue(theCtx, theOperationOutcome); 059 populateDetails(theCtx, issue, theSeverity, theDetails, theLocation, theCode); 060 } 061 062 private static IBase createIssue(FhirContext theCtx, IBaseResource theOutcome) { 063 RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); 064 BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); 065 BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) issueChild.getChildByName("issue"); 066 067 IBase issue = issueElement.newInstance(); 068 issueChild.getMutator().addValue(theOutcome, issue); 069 return issue; 070 } 071 072 public static String getFirstIssueDetails(FhirContext theCtx, IBaseOperationOutcome theOutcome) { 073 return getFirstIssueStringPart(theCtx, theOutcome, "diagnostics"); 074 } 075 076 public static String getFirstIssueLocation(FhirContext theCtx, IBaseOperationOutcome theOutcome) { 077 return getFirstIssueStringPart(theCtx, theOutcome, "location"); 078 } 079 080 private static String getFirstIssueStringPart(FhirContext theCtx, IBaseOperationOutcome theOutcome, String name) { 081 if (theOutcome == null) { 082 return null; 083 } 084 085 RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); 086 BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); 087 088 List<IBase> issues = issueChild.getAccessor().getValues(theOutcome); 089 if (issues.isEmpty()) { 090 return null; 091 } 092 093 IBase issue = issues.get(0); 094 BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(issue.getClass()); 095 BaseRuntimeChildDefinition detailsChild = issueElement.getChildByName(name); 096 097 List<IBase> details = detailsChild.getAccessor().getValues(issue); 098 if (details.isEmpty()) { 099 return null; 100 } 101 return ((IPrimitiveType<?>) details.get(0)).getValueAsString(); 102 } 103 104 /** 105 * Returns true if the given OperationOutcome has 1 or more Operation.issue repetitions 106 */ 107 public static boolean hasIssues(FhirContext theCtx, IBaseOperationOutcome theOutcome) { 108 if (theOutcome == null) { 109 return false; 110 } 111 RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); 112 BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); 113 return issueChild.getAccessor().getValues(theOutcome).size() > 0; 114 } 115 116 public static IBaseOperationOutcome newInstance(FhirContext theCtx) { 117 RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition("OperationOutcome"); 118 try { 119 return (IBaseOperationOutcome) ooDef.getImplementingClass().newInstance(); 120 } catch (InstantiationException e) { 121 throw new InternalErrorException("Unable to instantiate OperationOutcome", e); 122 } catch (IllegalAccessException e) { 123 throw new InternalErrorException("Unable to instantiate OperationOutcome", e); 124 } 125 } 126 127 private static void populateDetails(FhirContext theCtx, IBase theIssue, String theSeverity, String theDetails, String theLocation, String theCode) { 128 BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(theIssue.getClass()); 129 BaseRuntimeChildDefinition detailsChild; 130 detailsChild = issueElement.getChildByName("diagnostics"); 131 132 BaseRuntimeChildDefinition codeChild = issueElement.getChildByName("code"); 133 IPrimitiveType<?> codeElem = (IPrimitiveType<?>) codeChild.getChildByName("code").newInstance(codeChild.getInstanceConstructorArguments()); 134 codeElem.setValueAsString(theCode); 135 codeChild.getMutator().addValue(theIssue, codeElem); 136 137 BaseRuntimeElementDefinition<?> stringDef = detailsChild.getChildByName(detailsChild.getElementName()); 138 BaseRuntimeChildDefinition severityChild = issueElement.getChildByName("severity"); 139 BaseRuntimeChildDefinition locationChild = issueElement.getChildByName("location"); 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 if (isNotBlank(theLocation)) { 150 IPrimitiveType<?> locationElem = (IPrimitiveType<?>) locationChild.getChildByName("location").newInstance(locationChild.getInstanceConstructorArguments()); 151 locationElem.setValueAsString(theLocation); 152 locationChild.getMutator().addValue(theIssue, locationElem); 153 } 154 } 155 156}