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.requests;
17
18 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
19 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
20 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
21 import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
22 import org.openehealth.ipf.commons.ihe.xds.core.requests.query.Query;
23 import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryReturnType;
24 import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryType;
25
26
27
28
29
30 public class QueryRegistryTransformer {
31 private final EbXMLFactory factory30 = new EbXMLFactory30();
32
33
34
35
36
37
38
39 public EbXMLAdhocQueryRequest toEbXML(QueryRegistry request) {
40 if (request == null) {
41 return null;
42 }
43
44 Query query = request.getQuery();
45 EbXMLAdhocQueryRequest ebXML = createAdhocQueryRequest();
46 query.accept(new ToEbXMLVisitor(ebXML));
47
48 ebXML.setReturnType(request.getReturnType().getCode());
49
50 return ebXML;
51 }
52
53
54
55
56
57
58
59 public QueryRegistry fromEbXML(EbXMLAdhocQueryRequest ebXML) {
60 if (ebXML == null) {
61 return null;
62 }
63
64 String id = ebXML.getId();
65 QueryType queryType = QueryType.valueOfId(id);
66 if (queryType == null) {
67 return null;
68 }
69
70 Query query = createQuery(queryType);
71 query.accept(new FromEbXMLVisitor(ebXML));
72
73 QueryRegistry queryRegistry = new QueryRegistry(query);
74 queryRegistry.setReturnType(QueryReturnType.valueOfCode(ebXML.getReturnType()));
75
76 return queryRegistry;
77 }
78
79 private Query createQuery(QueryType queryType) {
80 try {
81 return queryType.getType().newInstance();
82 } catch (InstantiationException | IllegalAccessException e) {
83 throw new IllegalStateException("Invalid query class for type: " + queryType, e);
84 }
85 }
86
87 private EbXMLAdhocQueryRequest createAdhocQueryRequest() {
88 return factory30.createAdhocQueryRequest();
89 }
90 }