View Javadoc
1   /*
2    * Copyright 2013 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.query;
17  
18  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
20  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryList;
21  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.StoredQuery;
22  
23  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
24  
25  import java.util.Map;
26  
27  /**
28   * Base transformations for all stored queries.
29   * @author Dmytro Rud
30   */
31  abstract class AbstractStoredQueryTransformer<T extends StoredQuery> {
32  
33      /**
34       * Transforms the query into its ebXML representation.
35       * <p>
36       * Does not perform any transformation if one of the parameters is <code>null</code>. 
37       * @param query
38       *          the query. Can be <code>null</code>.
39       * @param ebXML
40       *          the ebXML representation. Can be <code>null</code>.
41       */
42      public void toEbXML(T query, EbXMLAdhocQueryRequest ebXML) {
43          if (query == null || ebXML == null) {
44              return;
45          }
46  
47          ebXML.setId(query.getType().getId());
48          ebXML.setHome(query.getHomeCommunityId());
49  
50          QuerySlotHelper slotHelper = new QuerySlotHelper(ebXML);
51          for (Map.Entry<String, QueryList<String>> entry : query.getExtraParameters().entrySet()) {
52              slotHelper.fromStringList(entry.getKey(), entry.getValue());
53          }
54      }
55      
56  
57      /**
58       * Transforms the ebXML representation of a query into a query object.
59       * <p>
60       * Does not perform any transformation if one of the parameters is <code>null</code>. 
61       * @param query
62       *          the query. Can be <code>null</code>.
63       * @param ebXML
64       *          the ebXML representation. Can be <code>null</code>.
65       */
66      public void fromEbXML(T query, EbXMLAdhocQueryRequest ebXML) {
67          if (query == null || ebXML == null) {
68              return;
69          }
70  
71          query.setHomeCommunityId(ebXML.getHome());
72  
73          QuerySlotHelper slotHelper = new QuerySlotHelper(ebXML);
74          for (EbXMLSlot slot : ebXML.getSlots()) {
75              String slotName = slot.getName();
76              if ((QueryParameter.valueOfSlotName(slotName) == null) && (! query.getExtraParameters().containsKey(slotName))) {
77                  QueryList<String> queryList = slotHelper.toStringQueryList(slotName);
78                  if (queryList != null) {
79                      query.getExtraParameters().put(slotName, queryList);
80                  }
81              }
82          }
83      }
84  }