1 package ca.uhn.fhir.validation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import static org.apache.commons.lang3.StringUtils.isNotBlank;
24
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
29
30 import ca.uhn.fhir.context.FhirContext;
31 import ca.uhn.fhir.rest.api.Constants;
32 import ca.uhn.fhir.util.OperationOutcomeUtil;
33
34
35
36
37
38
39
40 public class ValidationResult {
41 private final FhirContext myCtx;
42 private final boolean myIsSuccessful;
43 private final List<SingleValidationMessage> myMessages;
44
45 public ValidationResult(FhirContext theCtx, List<SingleValidationMessage> theMessages) {
46 boolean successful = true;
47 myCtx = theCtx;
48 myMessages = theMessages;
49 for (SingleValidationMessage next : myMessages) {
50 next.getSeverity();
51 if (next.getSeverity() == null || next.getSeverity().ordinal() > ResultSeverityEnum.WARNING.ordinal()) {
52 successful = false;
53 }
54 }
55 myIsSuccessful = successful;
56 }
57
58 public List<SingleValidationMessage> getMessages() {
59 return Collections.unmodifiableList(myMessages);
60 }
61
62
63
64
65
66
67
68
69
70 public boolean isSuccessful() {
71 return myIsSuccessful;
72 }
73
74 private String toDescription() {
75 StringBuilder b = new StringBuilder(100);
76 if (myMessages.size() > 0) {
77 if (myMessages.get(0).getSeverity() != null) {
78 b.append(myMessages.get(0).getSeverity().name());
79 b.append(" - ");
80 }
81 b.append(myMessages.get(0).getMessage());
82 b.append(" - ");
83 b.append(myMessages.get(0).getLocationString());
84 } else {
85 b.append("No issues");
86 }
87 return b.toString();
88 }
89
90
91
92
93
94
95 @Deprecated
96 public IBaseOperationOutcome getOperationOutcome() {
97 return toOperationOutcome();
98 }
99
100
101
102
103 public IBaseOperationOutcome toOperationOutcome() {
104 IBaseOperationOutcome oo = (IBaseOperationOutcome) myCtx.getResourceDefinition("OperationOutcome").newInstance();
105 populateOperationOutcome(oo);
106 return oo;
107 }
108
109
110
111
112 public void populateOperationOutcome(IBaseOperationOutcome theOperationOutcome) {
113 for (SingleValidationMessage next : myMessages) {
114 String location;
115 if (isNotBlank(next.getLocationString())) {
116 location = next.getLocationString();
117 } else if (next.getLocationLine() != null || next.getLocationCol() != null) {
118 location = "Line[" + next.getLocationLine() + "] Col[" + next.getLocationCol() + "]";
119 } else {
120 location = null;
121 }
122 String severity = next.getSeverity() != null ? next.getSeverity().getCode() : null;
123 OperationOutcomeUtil.addIssue(myCtx, theOperationOutcome, severity, next.getMessage(), location, Constants.OO_INFOSTATUS_PROCESSING);
124 }
125
126 if (myMessages.isEmpty()) {
127 String message = myCtx.getLocalizer().getMessage(ValidationResult.class, "noIssuesDetected");
128 OperationOutcomeUtil.addIssue(myCtx, theOperationOutcome, "information", message, null, "informational");
129 }
130 }
131
132 @Override
133 public String toString() {
134 return "ValidationResult{" + "messageCount=" + myMessages.size() + ", isSuccessful=" + myIsSuccessful + ", description='" + toDescription() + '\'' + '}';
135 }
136 }