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