1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.transform.responses;
17
18 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
19 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
20 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
21 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
22 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
23 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLQueryResponse;
24 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
25 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Association;
26 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Document;
27 import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
28 import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntryType;
29 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Folder;
30 import org.openehealth.ipf.commons.ihe.xds.core.metadata.SubmissionSet;
31 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary;
32 import org.openehealth.ipf.commons.ihe.xds.core.responses.QueryResponse;
33 import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.AssociationTransformer;
34 import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.DocumentEntryTransformer;
35 import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.FolderTransformer;
36 import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.SubmissionSetTransformer;
37
38 import javax.activation.DataHandler;
39
40 import static org.apache.commons.lang3.Validate.notNull;
41
42
43
44
45
46
47 public class QueryResponseTransformer {
48 private final EbXMLFactory factory;
49 private final SubmissionSetTransformer submissionSetTransformer;
50 private final DocumentEntryTransformer documentEntryTransformer;
51 private final FolderTransformer folderTransformer;
52 private final AssociationTransformer associationTransformer;
53 private final ErrorInfoListTransformer errorInfoListTransformer;
54
55
56
57
58
59
60 public QueryResponseTransformer(EbXMLFactory factory) {
61 notNull(factory, "factory cannot be null");
62 this.factory = factory;
63
64 submissionSetTransformer = new SubmissionSetTransformer(factory);
65 documentEntryTransformer = new DocumentEntryTransformer(factory);
66 folderTransformer = new FolderTransformer(factory);
67 associationTransformer = new AssociationTransformer(factory);
68 errorInfoListTransformer = new ErrorInfoListTransformer(factory);
69 }
70
71
72
73
74
75
76
77 public EbXMLQueryResponse toEbXML(QueryResponse response) {
78 if (response == null) {
79 return null;
80 }
81
82 EbXMLObjectLibrary library = factory.createObjectLibrary();
83 EbXMLQueryResponse ebXML = factory.createAdhocQueryResponse(library, !response.getReferences().isEmpty());
84 ebXML.setStatus(response.getStatus());
85
86 if (!response.getErrors().isEmpty()) {
87 ebXML.setErrors(errorInfoListTransformer.toEbXML(response.getErrors()));
88 }
89
90 for (DocumentEntry docEntry : response.getDocumentEntries()) {
91 EbXMLExtrinsicObject extrinsic = documentEntryTransformer.toEbXML(docEntry, library);
92 for (Document document : response.getDocuments()) {
93 if ((document != null) && (document.getDocumentEntry() == docEntry)) {
94 extrinsic.setDataHandler(document.getContent(DataHandler.class));
95 break;
96 }
97 }
98 ebXML.addExtrinsicObject(extrinsic);
99 }
100
101 for (Folder folder : response.getFolders()) {
102 ebXML.addRegistryPackage(folderTransformer.toEbXML(folder, library));
103 addClassification(ebXML, folder.getEntryUuid(), Vocabulary.FOLDER_CLASS_NODE, library);
104 }
105
106 for (SubmissionSet set : response.getSubmissionSets()) {
107 ebXML.addRegistryPackage(submissionSetTransformer.toEbXML(set, library));
108 addClassification(ebXML, set.getEntryUuid(), Vocabulary.SUBMISSION_SET_CLASS_NODE, library);
109 }
110
111 for (Association association : response.getAssociations()) {
112 ebXML.addAssociation(associationTransformer.toEbXML(association, library));
113 }
114
115 response.getReferences().forEach(ebXML::addReference);
116
117 return ebXML;
118 }
119
120
121
122
123
124
125
126 public QueryResponse fromEbXML(EbXMLQueryResponse ebXML) {
127 if (ebXML == null) {
128 return null;
129 }
130
131 QueryResponse response = new QueryResponse();
132 response.setStatus(ebXML.getStatus());
133
134 if (!ebXML.getErrors().isEmpty()) {
135 response.setErrors(errorInfoListTransformer.fromEbXML(ebXML.getErrors()));
136 }
137
138 boolean foundNonObjRefs = false;
139
140 for (EbXMLExtrinsicObject extrinsic : ebXML.getExtrinsicObjects(DocumentEntryType.STABLE_OR_ON_DEMAND)) {
141 DocumentEntry documentEntry = documentEntryTransformer.fromEbXML(extrinsic);
142 response.getDocumentEntries().add(documentEntry);
143 if (extrinsic.getDataHandler() != null) {
144 response.getDocuments().add(new Document(documentEntry, extrinsic.getDataHandler()));
145 }
146 foundNonObjRefs = true;
147 }
148
149 for (EbXMLRegistryPackage regPackage : ebXML.getRegistryPackages(Vocabulary.FOLDER_CLASS_NODE)) {
150 response.getFolders().add(folderTransformer.fromEbXML(regPackage));
151 foundNonObjRefs = true;
152 }
153
154 for (EbXMLRegistryPackage regPackage : ebXML.getRegistryPackages(Vocabulary.SUBMISSION_SET_CLASS_NODE)) {
155 response.getSubmissionSets().add(submissionSetTransformer.fromEbXML(regPackage));
156 foundNonObjRefs = true;
157 }
158
159 for (EbXMLAssociation association : ebXML.getAssociations()) {
160 response.getAssociations().add(associationTransformer.fromEbXML(association));
161 foundNonObjRefs = true;
162 }
163
164 if (!foundNonObjRefs) {
165 EbXMLObjectLibrary standardLibrary = factory.createObjectLibrary();
166 ebXML.getReferences().stream()
167 .filter(ref -> standardLibrary.getById(ref.getId()) == null)
168 .forEach(ref -> response.getReferences().add(ref));
169 }
170
171 return response;
172 }
173
174 private void addClassification(EbXMLQueryResponse ebXML, String classified, String node, EbXMLObjectLibrary library) {
175 EbXMLClassification classification = factory.createClassification(library);
176 classification.setClassifiedObject(classified);
177 classification.setClassificationNode(node);
178 classification.assignUniqueId();
179 ebXML.addClassification(classification);
180 }
181 }