1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30;
17
18 import static org.apache.commons.lang3.Validate.noNullElements;
19 import static org.apache.commons.lang3.Validate.notNull;
20 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.*;
21 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.*;
22
23 import javax.xml.bind.JAXBElement;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29
30
31
32
33
34 public abstract class EbXMLObjectContainer30 implements EbXMLObjectContainer {
35 private final EbXMLObjectLibrary objectLibrary;
36
37 @Override
38 public EbXMLObjectLibrary getObjectLibrary() {
39 return objectLibrary;
40 }
41
42
43
44
45 protected void fillObjectLibrary() {
46 for (JAXBElement<? extends IdentifiableType> obj : getContents()) {
47 String id = obj.getValue().getId();
48 if (id != null) {
49 objectLibrary.put(id, obj.getValue());
50 }
51 }
52 }
53
54
55
56
57
58
59 EbXMLObjectContainer30(EbXMLObjectLibrary objectLibrary) {
60 notNull(objectLibrary, "objLibrary cannot be null");
61 this.objectLibrary = objectLibrary;
62 }
63
64 @Override
65 public void addAssociation(EbXMLAssociation association) {
66 if (association != null) {
67 AssociationType1 internal = ((EbXMLAssociation30)association).getInternal();
68 getContents().add(EbXMLFactory30.RIM_FACTORY.createAssociation(internal));
69 }
70 }
71
72 @Override
73 public void addExtrinsicObject(EbXMLExtrinsicObject extrinsic) {
74 if (extrinsic != null) {
75 ExtrinsicObjectType internal = ((EbXMLExtrinsicObject30)extrinsic).getInternal();
76 getContents().add(EbXMLFactory30.RIM_FACTORY.createExtrinsicObject(internal));
77 }
78 }
79
80 @Override
81 public void addRegistryPackage(EbXMLRegistryPackage regPackage) {
82 if (regPackage != null) {
83 RegistryPackageType internal = ((EbXMLRegistryPackage30)regPackage).getInternal();
84 getContents().add(EbXMLFactory30.RIM_FACTORY.createRegistryPackage(internal));
85 }
86 }
87
88 @Override
89 public List<EbXMLAssociation> getAssociations() {
90 List<EbXMLAssociation> results = new ArrayList<>();
91 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
92 AssociationType1 association = cast(identifiable, AssociationType1.class);
93 if (association != null) {
94 results.add(new EbXMLAssociation30(association, objectLibrary));
95 }
96 }
97
98 return results;
99 }
100
101 @Override
102 public List<EbXMLClassification> getClassifications() {
103 List<EbXMLClassification> results = new ArrayList<>();
104 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
105 ClassificationType classification = cast(identifiable, ClassificationType.class);
106 if (classification != null) {
107 results.add(new EbXMLClassification30(classification));
108 }
109 }
110
111 return results;
112 }
113
114 @Override
115 public List<EbXMLExtrinsicObject> getExtrinsicObjects(String... objectTypes) {
116 noNullElements(objectTypes, "objectTypes cannot be null or contain null elements");
117
118 List<EbXMLExtrinsicObject> results = new ArrayList<>();
119 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
120 ExtrinsicObjectType extrinsic = cast(identifiable, ExtrinsicObjectType.class);
121 if (extrinsic != null) {
122 for (String objectType : objectTypes) {
123 if (objectType.equals(extrinsic.getObjectType())) {
124 results.add(new EbXMLExtrinsicObject30(extrinsic, objectLibrary));
125 break;
126 }
127 }
128 }
129 }
130
131 return results;
132 }
133
134 @Override
135 public List<EbXMLExtrinsicObject> getExtrinsicObjects() {
136 List<EbXMLExtrinsicObject> results = new ArrayList<>();
137 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
138 ExtrinsicObjectType extrinsic = cast(identifiable, ExtrinsicObjectType.class);
139 if (extrinsic != null) {
140 results.add(new EbXMLExtrinsicObject30(extrinsic, objectLibrary));
141 }
142 }
143
144 return results;
145 }
146
147 @Override
148 public List<EbXMLRegistryPackage> getRegistryPackages(String classificationNode) {
149 notNull(classificationNode, "classificationNode cannot be null");
150
151 Set<String> acceptedIds = getAcceptedIds(classificationNode);
152
153 List<EbXMLRegistryPackage> results = new ArrayList<>();
154 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
155 RegistryPackageType regPackage = cast(identifiable, RegistryPackageType.class);
156 if (matchesFilter(regPackage, acceptedIds, classificationNode)) {
157 results.add(new EbXMLRegistryPackage30(regPackage, objectLibrary));
158 }
159 }
160
161 return results;
162 }
163
164 @Override
165 public List<EbXMLRegistryPackage> getRegistryPackages() {
166 List<EbXMLRegistryPackage> results = new ArrayList<>();
167 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
168 RegistryPackageType regPackage = cast(identifiable, RegistryPackageType.class);
169 if (regPackage != null) {
170 results.add(new EbXMLRegistryPackage30(regPackage, objectLibrary));
171 }
172 }
173
174 return results;
175 }
176
177 @Override
178 public void addClassification(EbXMLClassification classification) {
179 if (classification != null) {
180 ClassificationType internal = ((EbXMLClassification30)classification).getInternal();
181 getContents().add(EbXMLFactory30.RIM_FACTORY.createClassification(internal));
182 }
183 }
184
185 private boolean matchesFilter(RegistryPackageType regPackage, Set<String> acceptedIds, String classificationNode) {
186 return regPackage != null && (acceptedIds.contains(regPackage.getId()) || hasClassificationNode(regPackage, classificationNode));
187
188 }
189
190 private boolean hasClassificationNode(RegistryPackageType regPackage, String classificationNode) {
191 String id = regPackage.getId();
192 if (id == null) {
193 return false;
194 }
195
196 for (ClassificationType classification : regPackage.getClassification()) {
197 if (classificationNode.equals(classification.getClassificationNode())
198 && id.equals(classification.getClassifiedObject())) {
199 return true;
200 }
201 }
202 return false;
203 }
204
205 private Set<String> getAcceptedIds(String classificationNode) {
206 Set<String> acceptedIds = new HashSet<>();
207 for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
208 ClassificationType classification = cast(identifiable, ClassificationType.class);
209 if (classification != null && classificationNode.equals(classification.getClassificationNode())) {
210 acceptedIds.add(classification.getClassifiedObject());
211 }
212 }
213 return acceptedIds;
214 }
215
216
217
218
219
220
221
222
223
224
225
226 protected <T extends IdentifiableType> T cast(JAXBElement<? extends IdentifiableType> identifiable, Class<T> type) {
227 if ((identifiable.getDeclaredType() == type) || identifiable.getValue().getClass() == type) {
228 return type.cast(identifiable.getValue());
229 }
230 return null;
231 }
232
233
234
235
236 abstract List<JAXBElement<? extends IdentifiableType>> getContents();
237 }