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.responses;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRetrieveDocumentSetResponse;
22  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocumentSet;
23  
24  /**
25   * Transforms between a {@link EbXMLRetrieveDocumentSetResponse} and its ebXML representation. 
26   * @author Jens Riemschneider
27   */
28  public class RetrieveDocumentSetResponseTransformer {
29      private final EbXMLFactory factory;
30      private final ErrorInfoListTransformer errorInfoListTransformer;
31  
32      /**
33       * Constructs the transformer.
34       * @param factory
35       *          the factory for ebXML objects.
36       */
37      public RetrieveDocumentSetResponseTransformer(EbXMLFactory factory) {
38          notNull(factory, "factory cannot be null");
39          this.factory = factory;
40          this.errorInfoListTransformer = new ErrorInfoListTransformer(factory);
41      }
42      
43      /**
44       * Transforms a {@link RetrievedDocumentSet} to a {@link EbXMLRetrieveDocumentSetResponse}.
45       * @param response
46       *          the response. Can be <code>null</code>.
47       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
48       */
49      public EbXMLRetrieveDocumentSetResponse toEbXML(RetrievedDocumentSet response) {
50          if (response == null) {
51              return null;
52          }
53          
54          EbXMLRetrieveDocumentSetResponse ebXML = factory.createRetrieveDocumentSetResponse();
55          if (!response.getErrors().isEmpty()) {
56              ebXML.setErrors(errorInfoListTransformer.toEbXML(response.getErrors()));
57          }
58          ebXML.setStatus(response.getStatus());
59          ebXML.setDocuments(response.getDocuments());        
60          return ebXML;
61      }
62      
63      /**
64       * Transforms a {@link EbXMLRetrieveDocumentSetResponse} to a {@link RetrievedDocumentSet}.
65       * @param ebXML
66       *          the ebXML representation. Can be <code>null</code>.
67       * @return the response. <code>null</code> if the input was <code>null</code>.
68       */
69      public RetrievedDocumentSet fromEbXML(EbXMLRetrieveDocumentSetResponse ebXML) {
70          if (ebXML == null) {
71              return null;
72          }
73              
74          RetrievedDocumentSet response = new RetrievedDocumentSet();
75          response.getDocuments().addAll(ebXML.getDocuments());
76          if (!ebXML.getErrors().isEmpty()) {
77              response.setErrors(errorInfoListTransformer.fromEbXML(ebXML.getErrors()));
78          }
79          response.setStatus(ebXML.getStatus());
80          return response;
81      }
82  }