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.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   * Transforms between an {@link Author} instance and its representation in ebXML.
33   * @author Jens Riemschneider
34   */
35  public class AuthorTransformer {
36      private final EbXMLFactory factory;
37      
38      /**
39       * Constructs the transformer
40       * @param ebXMLFactory
41       *          factory for version independent ebXML objects. 
42       */
43      public AuthorTransformer(EbXMLFactory ebXMLFactory) {
44          notNull(ebXMLFactory, "ebXMLFactory cannot be null");
45          factory = ebXMLFactory;
46      }
47  
48      /**
49       * Transforms an {@link Author} to a {@link EbXMLClassification}.
50       * @param author
51       *          the author. Can be <code>null</code>.
52       * @param objectLibrary
53       *          the object library to use.
54       * @return the classification. <code>null</code> if the input was <code>null</code>.
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       * Transforms an a {@link EbXMLClassification} to {@link Author}. 
81       * @param classification
82       *          the classification. Can be <code>null</code>.
83       * @return the author. <code>null</code> if the input was <code>null</code>.
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      * Extracts slots with the given name from the given classification,
109      * transforms them into HL7-based XDS object model instances
110      * of the given type and inserts into the given collection.
111      *
112      * @param sourceClassification
113      *      source classification.
114      * @param sourceSlotName
115      *      slot name in the source classification.
116      * @param targetCollection
117      *      target collection of HL7-based object model instances.
118      * @param targetClass
119      *      target class of the HL7-based object model.
120      * @param <T>
121      *      target class of the HL7-based object model.
122      * @param <C>
123      *      composite HAPI type wrapped by {@link T}.
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      * Renders HL7-based XDS object model instances contained in the given
142      * collection and stores them into the given classification
143      * as slot values with the given name.
144      *
145      * @param sourceCollection
146      *      source collection of HL7-based object model instances.
147      * @param targetClassification
148      *      target classification.
149      * @param targetSlotName
150      *      slot number in the target classification.
151      * @param <T>
152      *      source class of the HL7-based object model.
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 }