001package org.hl7.fhir.r4.utils;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032import org.hl7.fhir.r4.model.CodeableConcept;
033import org.hl7.fhir.r4.model.IntegerType;
034import org.hl7.fhir.r4.model.OperationOutcome;
035import org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity;
036import org.hl7.fhir.r4.model.OperationOutcome.IssueType;
037import org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent;
038import org.hl7.fhir.utilities.validation.ValidationMessage;
039
040public class OperationOutcomeUtilities {
041
042  public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
043    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
044    issue.setCode(convert(message.getType()));
045
046    if (message.getLocation() != null) {
047      // message location has a fhirPath in it. We need to populate the expression
048      issue.addExpression(message.getLocation());
049    }
050    // pass through line/col if they're present
051    if (message.getLine() != 0)
052      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_LINE).setValue(new IntegerType(message.getLine()));
053    if (message.getCol() != 0)
054      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_COL).setValue(new IntegerType(message.getCol()));
055    issue.setSeverity(convert(message.getLevel()));
056    CodeableConcept c = new CodeableConcept();
057    c.setText(message.getMessage());
058    issue.setDetails(c);
059    if (message.getSource() != null) {
060      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
061    }
062    return issue;
063  }
064
065  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
066    switch (level) {
067    case FATAL:
068      return IssueSeverity.FATAL;
069    case ERROR:
070      return IssueSeverity.ERROR;
071    case WARNING:
072      return IssueSeverity.WARNING;
073    case INFORMATION:
074      return IssueSeverity.INFORMATION;
075    case NULL:
076      return IssueSeverity.NULL;
077    }
078    return IssueSeverity.NULL;
079  }
080
081  private static IssueType convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType type) {
082    switch (type) {
083    case INVALID:
084    case STRUCTURE:
085      return IssueType.STRUCTURE;
086    case REQUIRED:
087      return IssueType.REQUIRED;
088    case VALUE:
089      return IssueType.VALUE;
090    case INVARIANT:
091      return IssueType.INVARIANT;
092    case SECURITY:
093      return IssueType.SECURITY;
094    case LOGIN:
095      return IssueType.LOGIN;
096    case UNKNOWN:
097      return IssueType.UNKNOWN;
098    case EXPIRED:
099      return IssueType.EXPIRED;
100    case FORBIDDEN:
101      return IssueType.FORBIDDEN;
102    case SUPPRESSED:
103      return IssueType.SUPPRESSED;
104    case PROCESSING:
105      return IssueType.PROCESSING;
106    case NOTSUPPORTED:
107      return IssueType.NOTSUPPORTED;
108    case DUPLICATE:
109      return IssueType.DUPLICATE;
110    case NOTFOUND:
111      return IssueType.NOTFOUND;
112    case TOOLONG:
113      return IssueType.TOOLONG;
114    case CODEINVALID:
115      return IssueType.CODEINVALID;
116    case EXTENSION:
117      return IssueType.EXTENSION;
118    case TOOCOSTLY:
119      return IssueType.TOOCOSTLY;
120    case BUSINESSRULE:
121      return IssueType.BUSINESSRULE;
122    case CONFLICT:
123      return IssueType.CONFLICT;
124    case INCOMPLETE:
125      return IssueType.INCOMPLETE;
126    case TRANSIENT:
127      return IssueType.TRANSIENT;
128    case LOCKERROR:
129      return IssueType.LOCKERROR;
130    case NOSTORE:
131      return IssueType.NOSTORE;
132    case EXCEPTION:
133      return IssueType.EXCEPTION;
134    case TIMEOUT:
135      return IssueType.TIMEOUT;
136    case THROTTLED:
137      return IssueType.THROTTLED;
138    case INFORMATIONAL:
139      return IssueType.INFORMATIONAL;
140    case NULL:
141      return IssueType.NULL;
142    }
143    return IssueType.NULL;
144  }
145}