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