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.EbXMLRegistryResponse;
22  import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
23  
24  /**
25   * Transforms between {@link Response} and the ebXML representation.
26   * @author Jens Riemschneider
27   */
28  public class ResponseTransformer {    
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 ResponseTransformer(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 Response} to a {@link EbXMLRegistryResponse}.
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 EbXMLRegistryResponse toEbXML(Response response) {
50          notNull(response, "response cannot be null");
51          
52          EbXMLRegistryResponse ebXML = factory.createRegistryResponse();
53          
54          ebXML.setStatus(response.getStatus());
55          if (!response.getErrors().isEmpty()) {
56              ebXML.setErrors(errorInfoListTransformer.toEbXML(response.getErrors()));
57          }
58          
59          return ebXML;
60      }
61      
62      /**
63       * Transforms a {@link EbXMLRegistryResponse} to a {@link Response}.
64       * @param ebXML
65       *          the ebXML representation. Can be <code>null</code>.
66       * @return the response. <code>null</code> if the input was <code>null</code>.
67       */
68      public Response fromEbXML(EbXMLRegistryResponse ebXML) {
69          notNull(ebXML, "ebXML cannot be null");
70          
71          Response response = new Response();
72          
73          response.setStatus(ebXML.getStatus());
74          if (!ebXML.getErrors().isEmpty()) {
75              response.setErrors(errorInfoListTransformer.fromEbXML(ebXML.getErrors()));
76          }
77          
78          return response;
79      }
80  }