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.support; |
27 | |
|
28 | |
import java.util.ArrayList; |
29 | |
import java.util.List; |
30 | |
import java.util.Set; |
31 | |
|
32 | |
import ca.uhn.hl7v2.HL7Exception; |
33 | |
import ca.uhn.hl7v2.model.Message; |
34 | |
import ca.uhn.hl7v2.model.Structure; |
35 | |
import ca.uhn.hl7v2.model.SuperStructure; |
36 | |
import ca.uhn.hl7v2.validation.MessageRule; |
37 | |
import ca.uhn.hl7v2.validation.ValidationException; |
38 | |
import ca.uhn.hl7v2.validation.impl.AbstractMessageRule; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 5003 | @SuppressWarnings("serial") |
47 | 5 | public class OnlyAllowableSegmentsInSuperstructureRule extends AbstractMessageRule { |
48 | |
|
49 | 5 | public static final MessageRule ONLY_ALLOWABLE_SEGMENTS = new OnlyAllowableSegmentsInSuperstructureRule(); |
50 | |
|
51 | |
public ValidationException[] apply(Message msg) { |
52 | 5003 | List<ValidationException> exceptions = new ArrayList<ValidationException>(); |
53 | |
|
54 | 5003 | if (msg instanceof SuperStructure) { |
55 | 30 | checkStructure((SuperStructure)msg, exceptions); |
56 | |
} |
57 | |
|
58 | 5003 | return exceptions.toArray(new ValidationException[exceptions.size()]); |
59 | |
} |
60 | |
|
61 | |
private void checkStructure(SuperStructure theMsg, List<ValidationException> theExceptions) { |
62 | 30 | String messageStructure = theMsg.getMessage().getName(); |
63 | 30 | Set<String> nonStandardNames = theMsg.getNonStandardNames(); |
64 | |
|
65 | |
FORNAME: |
66 | 1025 | for (String name : theMsg.getNames()) { |
67 | 995 | if (nonStandardNames != null && nonStandardNames.contains(name)) { |
68 | 5 | continue; |
69 | |
} |
70 | |
|
71 | |
try { |
72 | 990 | for (Structure rep : theMsg.getAll(name)) { |
73 | |
|
74 | 100 | if (!rep.isEmpty()) { |
75 | 100 | Set<String> structuresWhichChildAppliesTo = theMsg.getStructuresWhichChildAppliesTo(name); |
76 | 100 | if (!structuresWhichChildAppliesTo.contains(messageStructure)) { |
77 | 5 | String msgSimpleName = theMsg.getMessage().getClass().getSimpleName(); |
78 | 5 | theExceptions.add(new ValidationException("Message (superstructure " + msgSimpleName + ") of type " + messageStructure + " must not have content in " + name)); |
79 | |
} |
80 | 100 | continue FORNAME; |
81 | |
} |
82 | |
|
83 | |
} |
84 | 0 | } catch (HL7Exception e) { |
85 | |
|
86 | 0 | throw new Error("Can't get rep of structure " + name + ". This is probably a HAPI bug"); |
87 | 890 | } |
88 | |
|
89 | |
} |
90 | 30 | } |
91 | |
|
92 | |
} |