001package org.hl7.fhir.r5.utils;
002
003import java.util.List;
004
005/*
006  Copyright (c) 2011+, HL7, Inc.
007  All rights reserved.
008  
009  Redistribution and use in source and binary forms, with or without modification, 
010  are permitted provided that the following conditions are met:
011    
012   * Redistributions of source code must retain the above copyright notice, this 
013     list of conditions and the following disclaimer.
014   * Redistributions in binary form must reproduce the above copyright notice, 
015     this list of conditions and the following disclaimer in the documentation 
016     and/or other materials provided with the distribution.
017   * Neither the name of HL7 nor the names of its contributors may be used to 
018     endorse or promote products derived from this software without specific 
019     prior written permission.
020  
021  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
022  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
023  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
024  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
025  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
026  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
027  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
028  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
029  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
030  POSSIBILITY OF SUCH DAMAGE.
031  
032 */
033
034
035
036import org.hl7.fhir.r5.model.CodeableConcept;
037import org.hl7.fhir.r5.model.IntegerType;
038import org.hl7.fhir.r5.model.OperationOutcome;
039import org.hl7.fhir.r5.model.OperationOutcome.IssueSeverity;
040import org.hl7.fhir.r5.model.OperationOutcome.IssueType;
041import org.hl7.fhir.r5.model.OperationOutcome.OperationOutcomeIssueComponent;
042import org.hl7.fhir.utilities.validation.ValidationMessage;
043
044public class OperationOutcomeUtilities {
045
046
047  public static OperationOutcomeIssueComponent convertToIssue(ValidationMessage message, OperationOutcome op) {
048    OperationOutcomeIssueComponent issue = new OperationOutcome.OperationOutcomeIssueComponent();
049    issue.setUserData("source.vm", message);   
050    issue.setCode(convert(message.getType()));
051    
052    if (message.getLocation() != null) {
053      // message location has a fhirPath in it. We need to populate the expression
054      issue.addExpression(message.getLocation());
055    }
056    // pass through line/col if they're present
057    if (message.getLine() >= 0)
058      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_LINE).setValue(new IntegerType(message.getLine()));
059    if (message.getCol() >= 0)
060      issue.addExtension().setUrl(ToolingExtensions.EXT_ISSUE_COL).setValue(new IntegerType(message.getCol()));
061    issue.setSeverity(convert(message.getLevel()));
062    CodeableConcept c = new CodeableConcept();
063    c.setText(message.getMessage());
064    issue.setDetails(c);
065    if (message.getSource() != null) {
066      issue.getExtension().add(ToolingExtensions.makeIssueSource(message.getSource()));
067    }
068    issue.setUserData("source.msg", message);
069    return issue;
070  }
071
072  private static IssueSeverity convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity level) {
073    switch (level) {
074    case FATAL : return IssueSeverity.FATAL;
075    case ERROR : return IssueSeverity.ERROR;
076    case WARNING : return IssueSeverity.WARNING;
077    case INFORMATION : return IssueSeverity.INFORMATION;
078         case NULL : return IssueSeverity.NULL;
079    }
080    return IssueSeverity.NULL;
081  }
082
083  private static IssueType convert(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType type) {
084    switch (type) {
085    case INVALID: 
086    case STRUCTURE: return IssueType.STRUCTURE;
087    case REQUIRED: return IssueType.REQUIRED;
088    case VALUE: return IssueType.VALUE;
089    case INVARIANT: return IssueType.INVARIANT;
090    case SECURITY: return IssueType.SECURITY;
091    case LOGIN: return IssueType.LOGIN;
092    case UNKNOWN: return IssueType.UNKNOWN;
093    case EXPIRED: return IssueType.EXPIRED;
094    case FORBIDDEN: return IssueType.FORBIDDEN;
095    case SUPPRESSED: return IssueType.SUPPRESSED;
096    case PROCESSING: return IssueType.PROCESSING;
097    case NOTSUPPORTED: return IssueType.NOTSUPPORTED;
098    case DUPLICATE: return IssueType.DUPLICATE;
099    case NOTFOUND: return IssueType.NOTFOUND;
100    case TOOLONG: return IssueType.TOOLONG;
101    case CODEINVALID: return IssueType.CODEINVALID;
102    case EXTENSION: return IssueType.EXTENSION;
103    case TOOCOSTLY: return IssueType.TOOCOSTLY;
104    case BUSINESSRULE: return IssueType.BUSINESSRULE;
105    case CONFLICT: return IssueType.CONFLICT;
106    case INCOMPLETE: return IssueType.INCOMPLETE;
107    case TRANSIENT: return IssueType.TRANSIENT;
108    case LOCKERROR: return IssueType.LOCKERROR;
109    case NOSTORE: return IssueType.NOSTORE;
110    case EXCEPTION: return IssueType.EXCEPTION;
111    case TIMEOUT: return IssueType.TIMEOUT;
112    case THROTTLED: return IssueType.THROTTLED;
113    case INFORMATIONAL: return IssueType.INFORMATIONAL;
114         case NULL: return IssueType.NULL;
115    }
116    return IssueType.NULL;
117  }
118
119  public static OperationOutcome createOutcome(List<ValidationMessage> messages) {
120    OperationOutcome res = new OperationOutcome();
121    for (ValidationMessage vm : messages) {
122      res.addIssue(convertToIssue(vm, res));
123    }
124    return res;
125  }
126}