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 static org.apache.commons.lang3.Validate.notNull;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
26  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRetrieveDocumentSetResponse;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType.DocumentResponse;
28  import org.openehealth.ipf.commons.ihe.xds.core.requests.DocumentReference;
29  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
30  import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
31  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
32  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryErrorList;
33  
34  /**
35   * The ebXML 3.0 version of the {@link EbXMLRetrieveDocumentSetResponse}.
36   * @author Jens Riemschneider
37   */
38  public class EbXMLRetrieveDocumentSetResponse30 implements EbXMLRetrieveDocumentSetResponse {
39      private final RetrieveDocumentSetResponseType response;
40  
41      /**
42       * Constructs a response by wrapping the given ebXML 3.0 object.
43       * @param response
44       *          the object to wrap.
45       */
46      public EbXMLRetrieveDocumentSetResponse30(RetrieveDocumentSetResponseType response) {
47          notNull(response, "response cannot be null");
48          this.response = response;
49      }
50      
51      @Override
52      public RetrieveDocumentSetResponseType getInternal() {
53          return response;
54      }
55  
56      @Override
57      public List<RetrievedDocument> getDocuments() {
58          List<RetrievedDocument> docs = new ArrayList<>();
59          for (DocumentResponse documentResponse : response.getDocumentResponse()) {
60              DocumentReference requestData = new DocumentReference();
61              requestData.setDocumentUniqueId(documentResponse.getDocumentUniqueId());
62              requestData.setHomeCommunityId(documentResponse.getHomeCommunityId());
63              requestData.setRepositoryUniqueId(documentResponse.getRepositoryUniqueId());
64  
65              RetrievedDocument doc = new RetrievedDocument();
66              doc.setDataHandler(documentResponse.getDocument());
67              doc.setRequestData(requestData);
68              doc.setNewRepositoryUniqueId(documentResponse.getNewRepositoryUniqueId());
69              doc.setNewDocumentUniqueId(documentResponse.getNewDocumentUniqueId());
70              if (documentResponse.getMimeType() != null) {
71                  doc.setMimeType(documentResponse.getMimeType());
72              } else if (documentResponse.getDocument() != null) {
73                  doc.setMimeType(documentResponse.getDocument().getContentType());
74              }
75  
76              docs.add(doc);
77          }
78          return docs;
79      }
80  
81      @Override
82      public void setDocuments(List<RetrievedDocument> documents) {
83          response.getDocumentResponse().clear();
84          if (documents != null) {
85              for (RetrievedDocument doc : documents) {
86                  DocumentResponse documentResponse = new DocumentResponse();
87                  documentResponse.setDocument(doc.getDataHandler());
88                  documentResponse.setNewRepositoryUniqueId(doc.getNewRepositoryUniqueId());
89                  documentResponse.setNewDocumentUniqueId(doc.getNewDocumentUniqueId());
90                  if (doc.getMimeType() != null) {
91                      documentResponse.setMimeType(doc.getMimeType());
92                  } else if (doc.getDataHandler() != null) {
93                      documentResponse.setMimeType(doc.getDataHandler().getContentType());
94                  }
95                  DocumentReference requestData = doc.getRequestData();
96                  if (requestData != null) {
97                      documentResponse.setDocumentUniqueId(requestData.getDocumentUniqueId());
98                      documentResponse.setHomeCommunityId(requestData.getHomeCommunityId());
99                      documentResponse.setRepositoryUniqueId(requestData.getRepositoryUniqueId());
100                 }
101                 response.getDocumentResponse().add(documentResponse);
102             }
103         }
104     }
105 
106     @Override
107     public void setStatus(Status status) {
108         if (response.getRegistryResponse() != null){
109             response.getRegistryResponse().setStatus(Status.getOpcode30(status));
110         }
111     }
112 
113     @Override
114     public Status getStatus() {
115         Status status = null;
116         if (response.getRegistryResponse() != null){
117             status = Status.valueOfOpcode(response.getRegistryResponse().getStatus());
118         }
119         return status;
120     }
121 
122     @Override
123     public List<EbXMLRegistryError> getErrors() {
124         RegistryErrorList list = response.getRegistryResponse().getRegistryErrorList();
125         if (list == null) {
126             return Collections.emptyList();
127         }
128 
129         return list.getRegistryError().stream()
130                 .map(EbXMLRegistryError30::new)
131                 .collect(Collectors.toList());
132     }
133 
134     @Override
135     public void setErrors(List<EbXMLRegistryError> errors) {
136         RegistryErrorList value = EbXMLFactory30.RS_FACTORY.createRegistryErrorList();
137         response.getRegistryResponse().setRegistryErrorList(value);
138         List<RegistryError> list = value.getRegistryError();
139         list.addAll(errors.stream()
140                 .map(error -> ((EbXMLRegistryError30) error).getInternal())
141                 .collect(Collectors.toList()));
142     }
143 }