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.ebxml;
17
18 import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
19
20 /**
21 * Transforms between a {@link Recipient} and its ebXML representation.
22 * <p>
23 * The ebXML representation is a simple String used as a slot value.
24 * This class is independent of the ebXML version being used.
25 * @author Jens Riemschneider
26 */
27 public class RecipientTransformer {
28
29 /**
30 * Transforms a recipient into its ebXML representation (a slot value).
31 * @param recipient
32 * the recipient to transform. Can be <code>null</code>.
33 * @return the slot value. <code>null</code> if the input was <code>null</code>.
34 */
35 public String toEbXML(Recipient recipient) {
36 if (recipient == null) {
37 return null;
38 }
39
40 String person = Hl7v2Based.render(recipient.getPerson());
41 String organization = Hl7v2Based.render(recipient.getOrganization());
42 String telecom = Hl7v2Based.render(recipient.getTelecom());
43
44 if ((person == null) && (organization == null) && (telecom == null)) {
45 return null;
46 }
47
48 StringBuilder sb = new StringBuilder();
49 if (organization != null) {
50 sb.append(organization);
51 }
52 if ((person != null) || (telecom != null)) {
53 sb.append('|');
54 if (person != null) {
55 sb.append(person);
56 }
57 if (telecom != null) {
58 sb.append('|').append(telecom);
59 }
60 }
61
62 return sb.toString();
63 }
64
65 /**
66 * Transforms an ebXML representation (a slot value) into a {@link Recipient}.
67 * @param slotValue
68 * the slot value. Can be <code>null</code>.
69 * @return the recipient. <code>null</code> if the input was <code>null</code>.
70 */
71 public Recipient fromEbXML(String slotValue) {
72 if (slotValue == null || slotValue.isEmpty()) {
73 return null;
74 }
75
76 Recipient recipient = new Recipient();
77
78 String[] parts = slotValue.split("\\|");
79 recipient.setOrganization(Hl7v2Based.parse(parts[0], Organization.class));
80 if (parts.length > 1) {
81 recipient.setPerson(Hl7v2Based.parse(parts[1], Person.class));
82 }
83 if (parts.length > 2) {
84 recipient.setTelecom(Hl7v2Based.parse(parts[2], Telecom.class));
85 }
86
87 return recipient;
88 }
89 }