View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Transforms between a {@link QueryRegistry} and an {@link EbXMLAdhocQueryRequest}. 
28   * @author Jens Riemschneider
29   */
30  public class QueryRegistryTransformer {
31      private final EbXMLFactory factory30 = new EbXMLFactory30();
32      
33      /**
34       * Transforms the request into its ebXML representation.
35       * @param request
36       *          the request. Can be <code>null</code>.
37       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
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       * Transforms the ebXML representation into a request.
55       * @param ebXML
56       *          the ebXML representation. Can be <code>null</code>.
57       * @return the request. <code>null</code> if the input was <code>null</code>.
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  }