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.ebxml.ebxml30;
17  
18  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLQueryResponse;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
21  import org.openehealth.ipf.commons.ihe.xds.core.metadata.ObjectReference;
22  import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
23  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
24  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.IdentifiableType;
25  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ObjectRefType;
26  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryObjectListType;
27  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
28  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryErrorList;
29  
30  import javax.xml.bind.JAXBElement;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.stream.Collectors;
35  
36  import static org.apache.commons.lang3.Validate.notNull;
37  
38  /**
39   * Encapsulation of {@link AdhocQueryResponse}.
40   * @author Jens Riemschneider
41   */
42  public class EbXMLQueryResponse30 extends EbXMLObjectContainer30 implements EbXMLQueryResponse {
43      private final AdhocQueryResponse response;
44  
45      /**
46       * Constructs a query response by wrapping the given ebXML 3.0 object.
47       * @param response
48       *          the object to wrap.
49       * @param objectLibrary
50       *          the object library to use.
51       */
52      public EbXMLQueryResponse30(AdhocQueryResponse response, EbXMLObjectLibrary objectLibrary) {
53          super(objectLibrary);
54          notNull(response, "response cannot be null");
55          this.response = response;
56      }
57      
58      /**
59       * Constructs the response wrapper using a new {@link EbXMLObjectLibrary}.
60       * @param response
61       *          the object to wrap. 
62       */
63      public EbXMLQueryResponse30(AdhocQueryResponse response) {
64          this(response, new EbXMLObjectLibrary());
65          fillObjectLibrary();
66      }
67  
68      @Override
69      List<JAXBElement<? extends IdentifiableType>> getContents() {
70          RegistryObjectListType list = response.getRegistryObjectList();
71          if (list == null) {
72              return Collections.emptyList();
73          }
74          return list.getIdentifiable();
75      }
76  
77      @Override
78      public Status getStatus() {
79          return Status.valueOfOpcode(response.getStatus());
80      }
81  
82      @Override
83      public void setStatus(Status status) {
84          response.setStatus(Status.getOpcode30(status));
85      }
86  
87      @Override
88      public List<EbXMLRegistryError> getErrors() {
89          RegistryErrorList list = response.getRegistryErrorList();
90          if (list == null) {
91              return Collections.emptyList();
92          }
93          
94          return list.getRegistryError().stream()
95                  .map(EbXMLRegistryError30::new)
96                  .collect(Collectors.toList());
97      }
98  
99      @Override
100     public void setErrors(List<EbXMLRegistryError> errors) {
101         RegistryErrorList value = EbXMLFactory30.RS_FACTORY.createRegistryErrorList();
102         response.setRegistryErrorList(value);
103         List<RegistryError> list = value.getRegistryError();
104         for (EbXMLRegistryError error : errors) {
105             RegistryError regError = ((EbXMLRegistryError30) error).getInternal();
106             list.add(regError);
107         }
108     }
109 
110     @Override
111     public void addReference(ObjectReference ref) {
112         if (ref != null) {
113             ObjectRefType objectRef = EbXMLFactory30.RIM_FACTORY.createObjectRefType();
114             objectRef.setId(ref.getId());
115             objectRef.setHome(ref.getHome());
116             getContents().add(EbXMLFactory30.RIM_FACTORY.createObjectRef(objectRef));
117         }
118     }
119 
120     @Override
121     public List<ObjectReference> getReferences() {
122         List<ObjectReference> results = new ArrayList<>();
123         for (JAXBElement<? extends IdentifiableType> identifiable : getContents()) {
124             ObjectRefType objRefEbXML = cast(identifiable, ObjectRefType.class);            
125             if (objRefEbXML != null) {
126                 ObjectReference objRef = new ObjectReference();
127                 objRef.setId(objRefEbXML.getId());
128                 objRef.setHome(objRefEbXML.getHome());
129                 results.add(objRef);
130             }
131         }
132         
133         return results;
134     }
135 
136     @Override
137     public AdhocQueryResponse getInternal() {
138         return response;
139     }
140 }