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