001package org.hl7.fhir.dstu2.utils;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu2
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.dstu2.model.CodeableConcept;
025import org.hl7.fhir.dstu2.model.OperationOutcome;
026import org.hl7.fhir.dstu2.model.OperationOutcome.IssueSeverity;
027import org.hl7.fhir.dstu2.model.OperationOutcome.IssueType;
028import org.hl7.fhir.dstu2.model.OperationOutcome.OperationOutcomeIssueComponent;
029import org.hl7.fhir.dstu2.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      StringType s = new StringType();
041      s.setValue(Utilities.fhirPathToXPath(message.getLocation())+(message.getLine()>= 0 && message.getCol() >= 0 ? " (line "+Integer.toString(message.getLine())+", col"+Integer.toString(message.getCol())+")" : "") );
042      issue.getLocation().add(s);
043    }
044    issue.setSeverity(convert(message.getLevel()));
045    CodeableConcept c = new CodeableConcept();
046    c.setText(message.getMessage());
047    issue.setDetails(c);
048    if (message.getSource() != null) {
049      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
050    }
051    return issue;
052  }
053
054  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
055    switch (level) {
056    case FATAL : return IssueSeverity.FATAL;
057    case ERROR : return IssueSeverity.ERROR;
058    case WARNING : return IssueSeverity.WARNING;
059    case INFORMATION : return IssueSeverity.INFORMATION;
060    }
061    return IssueSeverity.NULL;
062  }
063
064  private static IssueType convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType type) {
065    switch (type) {
066    case INVALID: 
067    case STRUCTURE: return IssueType.STRUCTURE;
068    case REQUIRED: return IssueType.REQUIRED;
069    case VALUE: return IssueType.VALUE;
070    case INVARIANT: return IssueType.INVARIANT;
071    case SECURITY: return IssueType.SECURITY;
072    case LOGIN: return IssueType.LOGIN;
073    case UNKNOWN: return IssueType.UNKNOWN;
074    case EXPIRED: return IssueType.EXPIRED;
075    case FORBIDDEN: return IssueType.FORBIDDEN;
076    case SUPPRESSED: return IssueType.SUPPRESSED;
077    case PROCESSING: return IssueType.PROCESSING;
078    case NOTSUPPORTED: return IssueType.NOTSUPPORTED;
079    case DUPLICATE: return IssueType.DUPLICATE;
080    case NOTFOUND: return IssueType.NOTFOUND;
081    case TOOLONG: return IssueType.TOOLONG;
082    case CODEINVALID: return IssueType.CODEINVALID;
083    case EXTENSION: return IssueType.EXTENSION;
084    case TOOCOSTLY: return IssueType.TOOCOSTLY;
085    case BUSINESSRULE: return IssueType.BUSINESSRULE;
086    case CONFLICT: return IssueType.CONFLICT;
087    case INCOMPLETE: return IssueType.INCOMPLETE;
088    case TRANSIENT: return IssueType.TRANSIENT;
089    case LOCKERROR: return IssueType.LOCKERROR;
090    case NOSTORE: return IssueType.NOSTORE;
091    case EXCEPTION: return IssueType.EXCEPTION;
092    case TIMEOUT: return IssueType.TIMEOUT;
093    case THROTTLED: return IssueType.THROTTLED;
094    case INFORMATIONAL: return IssueType.INFORMATIONAL;
095    }
096    return IssueType.NULL;
097  }
098}