| 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.builder; |
| 27 | |
|
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.Arrays; |
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.Collections; |
| 32 | |
import java.util.HashSet; |
| 33 | |
import java.util.List; |
| 34 | |
import java.util.Set; |
| 35 | |
|
| 36 | |
import ca.uhn.hl7v2.HL7Exception; |
| 37 | |
import ca.uhn.hl7v2.Version; |
| 38 | |
import ca.uhn.hl7v2.model.Message; |
| 39 | |
import ca.uhn.hl7v2.util.Terser; |
| 40 | |
import ca.uhn.hl7v2.validation.MessageRule; |
| 41 | |
import ca.uhn.hl7v2.validation.PrimitiveTypeRule; |
| 42 | |
import ca.uhn.hl7v2.validation.Rule; |
| 43 | |
import ca.uhn.hl7v2.Severity; |
| 44 | |
import ca.uhn.hl7v2.validation.ValidationException; |
| 45 | |
import ca.uhn.hl7v2.validation.impl.AbstractMessageRule; |
| 46 | |
import ca.uhn.hl7v2.validation.impl.RuleBinding; |
| 47 | |
import ca.uhn.hl7v2.validation.impl.RuleSupport; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
@SuppressWarnings("serial") |
| 58 | 21720 | public class RuleTypeBuilder<S extends RuleTypeBuilder<S, T>, T extends Rule<?>> extends |
| 59 | |
BuilderSupport { |
| 60 | |
|
| 61 | 104630 | private List<RuleBinding<? extends Rule<?>>> rules = new ArrayList<RuleBinding<? extends Rule<?>>>(); |
| 62 | |
private Set<Version> versions; |
| 63 | |
private String description; |
| 64 | |
private String sectionReference; |
| 65 | 104630 | private boolean active = true; |
| 66 | 104630 | private Severity severity = Severity.ERROR; |
| 67 | |
|
| 68 | |
protected RuleTypeBuilder() { |
| 69 | 5875 | super(); |
| 70 | 5875 | } |
| 71 | |
|
| 72 | |
protected RuleTypeBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions) { |
| 73 | 76555 | super(); |
| 74 | 76555 | if (versions.size() == 0) |
| 75 | 0 | throw new IllegalArgumentException("Must specify a version"); |
| 76 | 76555 | this.rules = rules; |
| 77 | 76555 | this.versions = versions; |
| 78 | 76555 | } |
| 79 | |
|
| 80 | |
protected RuleTypeBuilder(List<RuleBinding<? extends Rule<?>>> rules, Version... versions) { |
| 81 | 22200 | super(); |
| 82 | 22200 | if (versions.length == 0) |
| 83 | 0 | throw new IllegalArgumentException("Must specify a version"); |
| 84 | 22200 | this.rules = rules; |
| 85 | 22200 | this.versions = new HashSet<Version>(Arrays.asList(versions)); |
| 86 | 22200 | } |
| 87 | |
|
| 88 | |
@SuppressWarnings("unchecked") |
| 89 | |
protected S instance() { |
| 90 | 103595 | return (S) this; |
| 91 | |
} |
| 92 | |
|
| 93 | |
protected List<RuleBinding<? extends Rule<?>>> getRules() { |
| 94 | 33755 | return rules; |
| 95 | |
} |
| 96 | |
|
| 97 | |
protected T prepareRule(T rule) { |
| 98 | 76445 | if (rule instanceof RuleSupport) { |
| 99 | 76445 | RuleSupport<?> rs = (RuleSupport<?>) rule; |
| 100 | 76445 | if (description != null) rs.setDescription(description); |
| 101 | 76445 | if (sectionReference != null) rs.setSectionReference(sectionReference); |
| 102 | 76445 | rs.setSeverity(severity); |
| 103 | |
} |
| 104 | 76445 | return rule; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public S description(String description) { |
| 114 | 0 | this.description = description; |
| 115 | 0 | return instance(); |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public S refersToSection(String sectionReference) { |
| 125 | 27075 | this.sectionReference = sectionReference; |
| 126 | 27075 | return instance(); |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public S severity(Severity severity) { |
| 136 | 0 | this.severity = severity; |
| 137 | 0 | return instance(); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public S active(boolean active) { |
| 147 | 0 | this.active = active; |
| 148 | 0 | return instance(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public S test(T rule) { |
| 158 | 76520 | addRuleBindings(rule); |
| 159 | 76520 | return instance(); |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public PrimitiveRuleBuilder primitive(String... type) { |
| 169 | 65600 | if (type.length == 0) { |
| 170 | 0 | throw new IllegalArgumentException("Must specify a type"); |
| 171 | |
} |
| 172 | 65600 | return new PrimitiveRuleBuilder(rules, versions, new HashSet<String>(Arrays.asList(type))); |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public MessageRuleBuilder message(String eventType, String... triggerEvents) { |
| 183 | 70 | return new MessageRuleBuilder(rules, versions, eventType, triggerEvents); |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public MessageExpressionBuilder message() { |
| 193 | 10860 | return new MessageExpressionBuilder(); |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public EncodingRuleBuilder encoding(String encoding) { |
| 203 | 25 | return new EncodingRuleBuilder(rules, versions, encoding); |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
protected void addRuleBindings(T rule) { |
| 212 | 76520 | if (Version.allVersions(versions)) { |
| 213 | |
|
| 214 | 65595 | rules.addAll(getRuleBindings(rule, "*")); |
| 215 | |
} else { |
| 216 | 10925 | for (Version version : versions) { |
| 217 | 65075 | rules.addAll(getRuleBindings(rule, version.getVersion())); |
| 218 | 65075 | } |
| 219 | |
} |
| 220 | 76520 | } |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
@SuppressWarnings("unchecked") |
| 231 | |
protected Collection<RuleBinding<T>> getRuleBindings(T rule, String version) { |
| 232 | 5 | return (Collection<RuleBinding<T>>) Collections.EMPTY_LIST; |
| 233 | |
} |
| 234 | |
|
| 235 | |
protected Collection<RuleBinding<T>> activate(Collection<RuleBinding<T>> bindings) { |
| 236 | 130670 | for (RuleBinding<T> ruleBinding : bindings) { |
| 237 | 174300 | ruleBinding.setActive(active); |
| 238 | 174300 | } |
| 239 | 130670 | return bindings; |
| 240 | |
} |
| 241 | |
|
| 242 | |
|
| 243 | |
Set<Version> getVersions() { |
| 244 | 120 | return versions; |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | 10860 | public class MessageExpressionBuilder { |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public MessageRuleBuilder all() { |
| 258 | 10860 | return new MessageRuleBuilder(rules, versions, "*", "*"); |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public MessageRuleBuilder allOfEventType(String eventType) { |
| 268 | 0 | return new MessageRuleBuilder(rules, versions, eventType, "*"); |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
public MessageRuleBuilder rejectOtherThan(final String... triggerEvents) { |
| 279 | 15 | final Set<String> triggers = new HashSet<String>(Arrays.asList(triggerEvents)); |
| 280 | 15 | return all().test(new AbstractMessageRule() { |
| 281 | |
public ValidationException[] apply(Message message) { |
| 282 | |
try { |
| 283 | 15 | Terser t = new Terser(message); |
| 284 | 15 | String eventType = t.get("MSH-9-1"); |
| 285 | 15 | String triggerEvent = t.get("MSH-9-2"); |
| 286 | 30 | return triggers.contains(triggerEvent) ? |
| 287 | 5 | passed() : |
| 288 | 10 | failed(eventType + "^" + triggerEvent + " is not accepted"); |
| 289 | 0 | } catch (HL7Exception e) { |
| 290 | 0 | return failed(e); |
| 291 | |
} |
| 292 | |
} |
| 293 | |
}); |
| 294 | |
|
| 295 | |
} |
| 296 | |
|
| 297 | |
} |
| 298 | |
} |