1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package ca.uhn.hl7v2.validation.impl; |
27 | |
|
28 | |
import ca.uhn.hl7v2.ErrorCode; |
29 | |
import ca.uhn.hl7v2.Location; |
30 | |
import ca.uhn.hl7v2.Severity; |
31 | |
import ca.uhn.hl7v2.validation.Rule; |
32 | |
import ca.uhn.hl7v2.validation.ValidationException; |
33 | |
|
34 | |
@SuppressWarnings("serial") |
35 | 65760 | public abstract class RuleSupport<T> implements Rule<T> { |
36 | |
|
37 | 5 | private static final ValidationException[] PASSED = new ValidationException[0]; |
38 | 65760 | private Severity severity = Severity.ERROR; |
39 | |
private String description; |
40 | |
private String sectionReference; |
41 | 65760 | private ErrorCode errorCode = ErrorCode.APPLICATION_INTERNAL_ERROR; |
42 | |
|
43 | |
protected ValidationException[] result(boolean result, Object value) { |
44 | 119608 | return result(result, value, Location.UNKNOWN); |
45 | |
} |
46 | |
|
47 | |
protected ValidationException[] result(boolean result, Object value, Location location) { |
48 | 119608 | return result ? passed() : failedWithValue(value, location); |
49 | |
} |
50 | |
|
51 | |
protected ValidationException[] passed() { |
52 | 119028 | return PASSED; |
53 | |
} |
54 | |
|
55 | |
protected ValidationException[] failedWithValue(Object value, Location location) { |
56 | 610 | String description = getDescription(); |
57 | 610 | String msg = String.format(description, String.valueOf(value)); |
58 | 610 | return failed("Validation failed: " + msg, location); |
59 | |
} |
60 | |
|
61 | |
protected ValidationException[] failed(String msg) { |
62 | 15 | return failed(msg, Location.UNKNOWN); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
protected ValidationException[] failed(String msg, Location location) { |
67 | 625 | ValidationException ve = new ValidationException(msg, severity); |
68 | 625 | ve.setError(getErrorCode()); |
69 | 625 | ve.setLocation(location); |
70 | 625 | return new ValidationException[] { ve }; |
71 | |
} |
72 | |
|
73 | |
protected ValidationException[] failed(Exception e) { |
74 | 0 | return failed(e, Location.UNKNOWN); |
75 | |
} |
76 | |
|
77 | |
protected ValidationException[] failed(Exception e, Location location) { |
78 | 0 | if (e instanceof ValidationException) |
79 | 0 | return new ValidationException[] { (ValidationException) e }; |
80 | 0 | ValidationException ve = new ValidationException(e.getMessage(), e, severity); |
81 | 0 | ve.setError(getErrorCode()); |
82 | 0 | ve.setLocation(location); |
83 | 0 | return new ValidationException[] { ve }; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public String getSectionReference() { |
90 | 0 | return sectionReference; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public String getDescription() { |
97 | 590 | return description; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public Severity getSeverity() { |
104 | 0 | return severity; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public ErrorCode getErrorCode() { |
111 | 625 | return errorCode; |
112 | |
} |
113 | |
|
114 | |
public void setSeverity(Severity severity) { |
115 | 76445 | this.severity = severity; |
116 | 76445 | } |
117 | |
|
118 | |
public void setDescription(String description) { |
119 | 65615 | this.description = description; |
120 | 65615 | } |
121 | |
|
122 | |
public void setSectionReference(String sectionReference) { |
123 | 27095 | this.sectionReference = sectionReference; |
124 | 27095 | } |
125 | |
|
126 | |
public void setErrorCode(ErrorCode errorCode) { |
127 | 0 | this.errorCode = errorCode; |
128 | 0 | } |
129 | |
} |