View Javadoc
1   /*
2    * Copyright 2011 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 org.apache.commons.lang3.Validate;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
21  import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
22  import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorInfo;
23  
24  import java.util.List;
25  import java.util.stream.Collectors;
26  
27  import static org.apache.commons.lang3.Validate.notNull;
28  
29  /**
30   * Transforms between lists of {@link ErrorInfo} objects and their ebXML representations.
31   * @author Dmytro Rud
32   */
33  public class ErrorInfoListTransformer {
34      private final EbXMLFactory factory;
35  
36      /**
37       * Constructs the transformer.
38       * @param factory
39       *          the factory for ebXML objects.
40       */
41      public ErrorInfoListTransformer(EbXMLFactory factory) {
42          Validate.notNull(factory, "ebXML factory must be not null");
43          this.factory = factory;
44      }
45  
46  
47      public List<EbXMLRegistryError> toEbXML(List<ErrorInfo> errorInfoList) {
48          notNull(errorInfoList, "error info list cannot be null");
49          return errorInfoList.stream()
50                  .map(this::toEbXML)
51                  .collect(Collectors.toList());
52      }
53  
54  
55      public List<ErrorInfo> fromEbXML(List<EbXMLRegistryError> registryErrorList) {
56          notNull(registryErrorList, "registry error list cannot be null");
57  
58          return registryErrorList.stream()
59                  .map(this::fromEbXML)
60                  .collect(Collectors.toList());
61      }
62  
63  
64      public EbXMLRegistryError toEbXML(ErrorInfo errorInfo) {
65          notNull(errorInfo, "error info cannot be null");
66  
67          EbXMLRegistryError regError = factory.createRegistryError();
68  
69          regError.setErrorCode((errorInfo.getErrorCode() == ErrorCode._USER_DEFINED)
70                  ? errorInfo.getCustomErrorCode()
71                  : ErrorCode.getOpcode(errorInfo.getErrorCode()));
72  
73          regError.setCodeContext(errorInfo.getCodeContext());
74          regError.setSeverity(errorInfo.getSeverity());
75          regError.setLocation(errorInfo.getLocation());
76          return regError;
77      }
78  
79  
80      public ErrorInfo fromEbXML(EbXMLRegistryError ebXML) {
81          ErrorInfo errorInfo = new ErrorInfo();
82  
83          ErrorCode errorCode = ErrorCode.valueOfOpcode(ebXML.getErrorCode());
84          errorInfo.setErrorCode(errorCode);
85          if (errorCode == ErrorCode._USER_DEFINED) {
86              errorInfo.setCustomErrorCode(ebXML.getErrorCode());
87          }
88  
89          errorInfo.setCodeContext(ebXML.getCodeContext());
90          errorInfo.setLocation(ebXML.getLocation());
91          errorInfo.setSeverity(ebXML.getSeverity());
92          return errorInfo;
93      }
94  
95  }