001package org.hl7.fhir.dstu3.utils;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu3
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
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
023
024import org.hl7.fhir.dstu3.model.CodeableConcept;
025import org.hl7.fhir.dstu3.model.OperationOutcome;
026import org.hl7.fhir.dstu3.model.OperationOutcome.IssueSeverity;
027import org.hl7.fhir.dstu3.model.OperationOutcome.IssueType;
028import org.hl7.fhir.dstu3.model.OperationOutcome.OperationOutcomeIssueComponent;
029import org.hl7.fhir.dstu3.model.StringType;
030import org.hl7.fhir.utilities.Utilities;
031import org.hl7.fhir.utilities.validation.ValidationMessage;
032
033public class OperationOutcomeUtilities {
034
035
036  public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
037    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
038    issue.setCode(convert(message.getType()));
039    if (message.getLocation() != null) {
040      // message location has a fhirPath in it. We need to populate the expression
041      issue.addExpression(message.getLocation());
042      // also, populate the XPath variant
043      StringType s = new StringType();
044      s.setValue(Utilities.fhirPathToXPath(message.getLocation())+(message.getLine()>= 0 && message.getCol() >= 0 ? " (line "+Integer.toString(message.getLine())+", col"+Integer.toString(message.getCol())+")" : "") );
045      issue.getLocation().add(s);
046    }
047    issue.setSeverity(convert(message.getLevel()));
048    CodeableConcept c = new CodeableConcept();
049    c.setText(message.getMessage());
050    issue.setDetails(c);
051    if (message.getSource() != null) {
052      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
053    }
054    return issue;
055  }
056
057  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
058    switch (level) {
059    case FATAL : return IssueSeverity.FATAL;
060    case ERROR : return IssueSeverity.ERROR;
061    case WARNING : return IssueSeverity.WARNING;
062    case INFORMATION : return IssueSeverity.INFORMATION;
063    }
064    return IssueSeverity.NULL;
065  }
066
067  private static IssueType convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType type) {
068    switch (type) {
069    case INVALID: 
070    case STRUCTURE: return IssueType.STRUCTURE;
071    case REQUIRED: return IssueType.REQUIRED;
072    case VALUE: return IssueType.VALUE;
073    case INVARIANT: return IssueType.INVARIANT;
074    case SECURITY: return IssueType.SECURITY;
075    case LOGIN: return IssueType.LOGIN;
076    case UNKNOWN: return IssueType.UNKNOWN;
077    case EXPIRED: return IssueType.EXPIRED;
078    case FORBIDDEN: return IssueType.FORBIDDEN;
079    case SUPPRESSED: return IssueType.SUPPRESSED;
080    case PROCESSING: return IssueType.PROCESSING;
081    case NOTSUPPORTED: return IssueType.NOTSUPPORTED;
082    case DUPLICATE: return IssueType.DUPLICATE;
083    case NOTFOUND: return IssueType.NOTFOUND;
084    case TOOLONG: return IssueType.TOOLONG;
085    case CODEINVALID: return IssueType.CODEINVALID;
086    case EXTENSION: return IssueType.EXTENSION;
087    case TOOCOSTLY: return IssueType.TOOCOSTLY;
088    case BUSINESSRULE: return IssueType.BUSINESSRULE;
089    case CONFLICT: return IssueType.CONFLICT;
090    case INCOMPLETE: return IssueType.INCOMPLETE;
091    case TRANSIENT: return IssueType.TRANSIENT;
092    case LOCKERROR: return IssueType.LOCKERROR;
093    case NOSTORE: return IssueType.NOSTORE;
094    case EXCEPTION: return IssueType.EXCEPTION;
095    case TIMEOUT: return IssueType.TIMEOUT;
096    case THROTTLED: return IssueType.THROTTLED;
097    case INFORMATIONAL: return IssueType.INFORMATIONAL;
098    }
099    return IssueType.NULL;
100  }
101}