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
032import org.hl7.fhir.dstu2.model.CodeableConcept;
033import org.hl7.fhir.dstu2.model.OperationOutcome;
034import org.hl7.fhir.dstu2.model.OperationOutcome.IssueSeverity;
035import org.hl7.fhir.dstu2.model.OperationOutcome.IssueType;
036import org.hl7.fhir.dstu2.model.OperationOutcome.OperationOutcomeIssueComponent;
037import org.hl7.fhir.dstu2.model.StringType;
038import org.hl7.fhir.utilities.Utilities;
039import org.hl7.fhir.utilities.validation.ValidationMessage;
040
041public class OperationOutcomeUtilities {
042
043  public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
044    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
045    issue.setCode(convert(message.getType()));
046    if (message.getLocation() != null) {
047      StringType s = new StringType();
048      s.setValue(Utilities.fhirPathToXPath(message.getLocation()) + (message.getLine() >= 0 && message.getCol() >= 0
049          ? " (line " + Integer.toString(message.getLine()) + ", col" + Integer.toString(message.getCol()) + ")"
050          : ""));
051      issue.getLocation().add(s);
052    }
053    issue.setSeverity(convert(message.getLevel()));
054    CodeableConcept c = new CodeableConcept();
055    c.setText(message.getMessage());
056    issue.setDetails(c);
057    if (message.getSource() != null) {
058      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
059    }
060    return issue;
061  }
062
063  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
064    switch (level) {
065    case FATAL:
066      return IssueSeverity.FATAL;
067    case ERROR:
068      return IssueSeverity.ERROR;
069    case WARNING:
070      return IssueSeverity.WARNING;
071    case INFORMATION:
072      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:
081      return IssueType.STRUCTURE;
082    case REQUIRED:
083      return IssueType.REQUIRED;
084    case VALUE:
085      return IssueType.VALUE;
086    case INVARIANT:
087      return IssueType.INVARIANT;
088    case SECURITY:
089      return IssueType.SECURITY;
090    case LOGIN:
091      return IssueType.LOGIN;
092    case UNKNOWN:
093      return IssueType.UNKNOWN;
094    case EXPIRED:
095      return IssueType.EXPIRED;
096    case FORBIDDEN:
097      return IssueType.FORBIDDEN;
098    case SUPPRESSED:
099      return IssueType.SUPPRESSED;
100    case PROCESSING:
101      return IssueType.PROCESSING;
102    case NOTSUPPORTED:
103      return IssueType.NOTSUPPORTED;
104    case DUPLICATE:
105      return IssueType.DUPLICATE;
106    case NOTFOUND:
107      return IssueType.NOTFOUND;
108    case TOOLONG:
109      return IssueType.TOOLONG;
110    case CODEINVALID:
111      return IssueType.CODEINVALID;
112    case EXTENSION:
113      return IssueType.EXTENSION;
114    case TOOCOSTLY:
115      return IssueType.TOOCOSTLY;
116    case BUSINESSRULE:
117      return IssueType.BUSINESSRULE;
118    case CONFLICT:
119      return IssueType.CONFLICT;
120    case INCOMPLETE:
121      return IssueType.INCOMPLETE;
122    case TRANSIENT:
123      return IssueType.TRANSIENT;
124    case LOCKERROR:
125      return IssueType.LOCKERROR;
126    case NOSTORE:
127      return IssueType.NOSTORE;
128    case EXCEPTION:
129      return IssueType.EXCEPTION;
130    case TIMEOUT:
131      return IssueType.TIMEOUT;
132    case THROTTLED:
133      return IssueType.THROTTLED;
134    case INFORMATIONAL:
135      return IssueType.INFORMATIONAL;
136    }
137    return IssueType.NULL;
138  }
139}