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
032
033
034import org.hl7.fhir.r4.model.CodeableConcept;
035import org.hl7.fhir.r4.model.IntegerType;
036import org.hl7.fhir.r4.model.OperationOutcome;
037import org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity;
038import org.hl7.fhir.r4.model.OperationOutcome.IssueType;
039import org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent;
040import org.hl7.fhir.r4.model.StringType;
041import org.hl7.fhir.utilities.Utilities;
042import org.hl7.fhir.utilities.validation.ValidationMessage;
043
044public class OperationOutcomeUtilities {
045
046
047  public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
048    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
049    issue.setCode(convert(message.getType()));
050    
051    if (message.getLocation() != null) {
052      // message location has a fhirPath in it. We need to populate the expression
053      issue.addExpression(message.getLocation());
054    }
055    // pass through line/col if they're present
056    if (message.getLine() != 0)
057      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_LINE).setValue(new IntegerType(message.getLine()));
058    if (message.getCol() != 0)
059      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_COL).setValue(new IntegerType(message.getCol()));
060    issue.setSeverity(convert(message.getLevel()));
061    CodeableConcept c = new CodeableConcept();
062    c.setText(message.getMessage());
063    issue.setDetails(c);
064    if (message.getSource() != null) {
065      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
066    }
067    return issue;
068  }
069
070  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
071    switch (level) {
072    case FATAL : return IssueSeverity.FATAL;
073    case ERROR : return IssueSeverity.ERROR;
074    case WARNING : return IssueSeverity.WARNING;
075    case INFORMATION : return IssueSeverity.INFORMATION;
076         case NULL : 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: return IssueType.STRUCTURE;
085    case REQUIRED: return IssueType.REQUIRED;
086    case VALUE: return IssueType.VALUE;
087    case INVARIANT: return IssueType.INVARIANT;
088    case SECURITY: return IssueType.SECURITY;
089    case LOGIN: return IssueType.LOGIN;
090    case UNKNOWN: return IssueType.UNKNOWN;
091    case EXPIRED: return IssueType.EXPIRED;
092    case FORBIDDEN: return IssueType.FORBIDDEN;
093    case SUPPRESSED: return IssueType.SUPPRESSED;
094    case PROCESSING: return IssueType.PROCESSING;
095    case NOTSUPPORTED: return IssueType.NOTSUPPORTED;
096    case DUPLICATE: return IssueType.DUPLICATE;
097    case NOTFOUND: return IssueType.NOTFOUND;
098    case TOOLONG: return IssueType.TOOLONG;
099    case CODEINVALID: return IssueType.CODEINVALID;
100    case EXTENSION: return IssueType.EXTENSION;
101    case TOOCOSTLY: return IssueType.TOOCOSTLY;
102    case BUSINESSRULE: return IssueType.BUSINESSRULE;
103    case CONFLICT: return IssueType.CONFLICT;
104    case INCOMPLETE: return IssueType.INCOMPLETE;
105    case TRANSIENT: return IssueType.TRANSIENT;
106    case LOCKERROR: return IssueType.LOCKERROR;
107    case NOSTORE: return IssueType.NOSTORE;
108    case EXCEPTION: return IssueType.EXCEPTION;
109    case TIMEOUT: return IssueType.TIMEOUT;
110    case THROTTLED: return IssueType.THROTTLED;
111    case INFORMATIONAL: return IssueType.INFORMATIONAL;
112         case NULL: return IssueType.NULL;
113    }
114    return IssueType.NULL;
115  }
116}