1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33 public class ErrorInfoListTransformer {
34 private final EbXMLFactory factory;
35
36
37
38
39
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 }