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