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.ebxml;
17
18 import static org.apache.commons.lang3.Validate.notNull;
19
20 import ca.uhn.hl7v2.model.Composite;
21 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
22 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
23 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
24 import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
25
26 import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.*;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31
32
33
34
35 public class AuthorTransformer {
36 private final EbXMLFactory factory;
37
38
39
40
41
42
43 public AuthorTransformer(EbXMLFactory ebXMLFactory) {
44 notNull(ebXMLFactory, "ebXMLFactory cannot be null");
45 factory = ebXMLFactory;
46 }
47
48
49
50
51
52
53
54
55
56 public EbXMLClassification toEbXML(Author author, EbXMLObjectLibrary objectLibrary) {
57 notNull(objectLibrary, "objectLibrary cannot be null");
58
59 if (author == null) {
60 return null;
61 }
62
63 EbXMLClassification classification = factory.createClassification(objectLibrary);
64 classification.setNodeRepresentation("");
65
66 String hl7XCN = Hl7v2Based.render(author.getAuthorPerson());
67 if (hl7XCN != null) {
68 classification.addSlot(SLOT_NAME_AUTHOR_PERSON, hl7XCN);
69 }
70
71 transformToHl7Slots(author.getAuthorInstitution(), classification, SLOT_NAME_AUTHOR_INSTITUTION);
72 transformToHl7Slots(author.getAuthorRole(), classification, SLOT_NAME_AUTHOR_ROLE);
73 transformToHl7Slots(author.getAuthorSpecialty(), classification, SLOT_NAME_AUTHOR_SPECIALTY);
74 transformToHl7Slots(author.getAuthorTelecom(), classification, SLOT_NAME_AUTHOR_TELECOM);
75
76 return classification;
77 }
78
79
80
81
82
83
84
85 public Author fromEbXML(EbXMLClassification classification) {
86 if (classification == null) {
87 return null;
88 }
89
90 Author author = new Author();
91
92 List<String> persons = classification.getSlotValues(SLOT_NAME_AUTHOR_PERSON);
93 if (persons.size() > 0) {
94 Person person = Hl7v2Based.parse(persons.get(0), Person.class);
95 author.setAuthorPerson(person);
96 }
97
98 transformFromHl7Slots(classification, SLOT_NAME_AUTHOR_INSTITUTION, author.getAuthorInstitution(), Organization.class);
99 transformFromHl7Slots(classification, SLOT_NAME_AUTHOR_ROLE, author.getAuthorRole(), Identifiable.class);
100 transformFromHl7Slots(classification, SLOT_NAME_AUTHOR_SPECIALTY, author.getAuthorSpecialty(), Identifiable.class);
101 transformFromHl7Slots(classification, SLOT_NAME_AUTHOR_TELECOM, author.getAuthorTelecom(), Telecom.class);
102
103 return author;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 private static <C extends Composite, T extends Hl7v2Based<C>> void transformFromHl7Slots(
126 EbXMLClassification sourceClassification,
127 String sourceSlotName,
128 List<T> targetCollection,
129 Class<T> targetClass)
130 {
131 for (String source : sourceClassification.getSlotValues(sourceSlotName)) {
132 T target = Hl7v2Based.parse(source, targetClass);
133 if (target != null) {
134 targetCollection.add(target);
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 private static <T extends Hl7v2Based<?>> void transformToHl7Slots(
155 List<T> sourceCollection,
156 EbXMLClassification targetClassification,
157 String targetSlotName)
158 {
159 List<String> targetCollection = new ArrayList<>();
160 for (T source : sourceCollection) {
161 String target = Hl7v2Based.render(source);
162 if (source != null) {
163 targetCollection.add(target);
164 }
165 }
166
167 String[] array = new String[targetCollection.size()];
168 targetClassification.addSlot(targetSlotName, targetCollection.toArray(array));
169 }
170
171 }