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  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
23  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
24  
25  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.*;
26  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Timestamp.toHL7;
27  
28  import org.openehealth.ipf.commons.ihe.xds.core.transform.hl7.PatientInfoTransformer;
29  
30  import java.util.List;
31  import java.util.stream.Collectors;
32  
33  /**
34   * Transforms between a {@link DocumentEntry} and its ebXML representation.
35   * @author Jens Riemschneider
36   */
37  public class DocumentEntryTransformer extends XDSMetaClassTransformer<EbXMLExtrinsicObject, DocumentEntry> {
38      private final EbXMLFactory factory;
39      
40      private final AuthorTransformer authorTransformer;
41      private final CodeTransformer codeTransformer;
42      
43      private final PatientInfoTransformer patientInfoTransformer = new PatientInfoTransformer();
44  
45      /**
46       * Constructs the transformer
47       * @param factory
48       *          factory for version independent ebXML objects. 
49       */
50      public DocumentEntryTransformer(EbXMLFactory factory) {
51          super(DOC_ENTRY_PATIENT_ID_EXTERNAL_ID, 
52                  DOC_ENTRY_LOCALIZED_STRING_PATIENT_ID, 
53                  DOC_ENTRY_UNIQUE_ID_EXTERNAL_ID,
54                  DOC_ENTRY_LOCALIZED_STRING_UNIQUE_ID);
55          
56          notNull(factory, "factory cannot be null");
57  
58          this.factory = factory;
59          authorTransformer = new AuthorTransformer(factory);
60          codeTransformer = new CodeTransformer(factory);
61      }
62      
63      @Override
64      protected EbXMLExtrinsicObject createEbXMLInstance(String id, EbXMLObjectLibrary objectLibrary) {
65          return factory.createExtrinsic(id, objectLibrary);
66      }
67      
68      @Override
69      protected DocumentEntry createMetaClassInstance() {
70          return new DocumentEntry();
71      }
72  
73      @Override
74      protected void addAttributesFromEbXML(DocumentEntry docEntry, EbXMLExtrinsicObject extrinsic) {
75          super.addAttributesFromEbXML(docEntry, extrinsic);
76          docEntry.setAvailabilityStatus(extrinsic.getStatus());        
77          docEntry.setMimeType(extrinsic.getMimeType());
78          docEntry.setType(DocumentEntryType.valueOfUuid(extrinsic.getObjectType()));
79          docEntry.setHomeCommunityId(extrinsic.getHome());
80      }
81  
82      @Override
83      protected void addAttributes(DocumentEntry metaData, EbXMLExtrinsicObject ebXML, EbXMLObjectLibrary objectLibrary) {
84          super.addAttributes(metaData, ebXML, objectLibrary);
85          ebXML.setStatus(metaData.getAvailabilityStatus());                
86          ebXML.setMimeType(metaData.getMimeType());
87          ebXML.setObjectType(DocumentEntryType.toUuid(metaData.getType()));
88          ebXML.setHome(metaData.getHomeCommunityId());
89      }
90  
91      @Override
92      protected void addSlotsFromEbXML(DocumentEntry docEntry, EbXMLExtrinsicObject extrinsic) {
93          super.addSlotsFromEbXML(docEntry, extrinsic);
94          
95          docEntry.setCreationTime(extrinsic.getSingleSlotValue(SLOT_NAME_CREATION_TIME));
96          docEntry.setHash(extrinsic.getSingleSlotValue(SLOT_NAME_HASH));
97          docEntry.setLanguageCode(extrinsic.getSingleSlotValue(SLOT_NAME_LANGUAGE_CODE));
98          docEntry.setServiceStartTime(extrinsic.getSingleSlotValue(SLOT_NAME_SERVICE_START_TIME));
99          docEntry.setServiceStopTime(extrinsic.getSingleSlotValue(SLOT_NAME_SERVICE_STOP_TIME));
100         docEntry.setRepositoryUniqueId(extrinsic.getSingleSlotValue(SLOT_NAME_REPOSITORY_UNIQUE_ID));
101         docEntry.setUri(extrinsic.getSingleSlotValue(SLOT_NAME_URI));
102         docEntry.setDocumentAvailability(DocumentAvailability.valueOfOpcode(
103                 extrinsic.getSingleSlotValue(SLOT_NAME_DOCUMENT_AVAILABILITY)));
104         
105         String size = extrinsic.getSingleSlotValue(SLOT_NAME_SIZE);
106         docEntry.setSize(size != null ? Long.parseLong(size) : null);
107         
108         String hl7LegalAuthenticator = extrinsic.getSingleSlotValue(SLOT_NAME_LEGAL_AUTHENTICATOR);
109         docEntry.setLegalAuthenticator(Hl7v2Based.parse(hl7LegalAuthenticator, Person.class));
110         
111         String sourcePatient = extrinsic.getSingleSlotValue(SLOT_NAME_SOURCE_PATIENT_ID);
112         docEntry.setSourcePatientId(Hl7v2Based.parse(sourcePatient, Identifiable.class));
113         
114         List<String> slotValues = extrinsic.getSlotValues(SLOT_NAME_SOURCE_PATIENT_INFO);        
115         docEntry.setSourcePatientInfo(patientInfoTransformer.fromHL7(slotValues));
116 
117         for (String referenceIdValue : extrinsic.getSlotValues(SLOT_NAME_REFERENCE_ID_LIST)) {
118             docEntry.getReferenceIdList().add(Hl7v2Based.parse(referenceIdValue, ReferenceId.class));
119         }
120     }
121     
122     @Override
123     protected void addSlots(DocumentEntry docEntry, EbXMLExtrinsicObject extrinsic, EbXMLObjectLibrary objectLibrary) {
124         super.addSlots(docEntry, extrinsic, objectLibrary);
125         
126         extrinsic.addSlot(SLOT_NAME_CREATION_TIME, toHL7(docEntry.getCreationTime()));
127         extrinsic.addSlot(SLOT_NAME_HASH, docEntry.getHash());
128         extrinsic.addSlot(SLOT_NAME_LANGUAGE_CODE, docEntry.getLanguageCode());
129         extrinsic.addSlot(SLOT_NAME_SERVICE_START_TIME, toHL7(docEntry.getServiceStartTime()));
130         extrinsic.addSlot(SLOT_NAME_SERVICE_STOP_TIME, toHL7(docEntry.getServiceStopTime()));
131         extrinsic.addSlot(SLOT_NAME_REPOSITORY_UNIQUE_ID, docEntry.getRepositoryUniqueId());
132         extrinsic.addSlot(SLOT_NAME_URI, docEntry.getUri());
133         extrinsic.addSlot(SLOT_NAME_DOCUMENT_AVAILABILITY,
134                 DocumentAvailability.toFullQualifiedOpcode(docEntry.getDocumentAvailability()));
135         
136         Long size = docEntry.getSize();
137         extrinsic.addSlot(SLOT_NAME_SIZE, size != null ? size.toString() : null);
138         
139         String hl7LegalAuthenticator = Hl7v2Based.render(docEntry.getLegalAuthenticator());
140         extrinsic.addSlot(SLOT_NAME_LEGAL_AUTHENTICATOR, hl7LegalAuthenticator);
141         
142         String sourcePatient = Hl7v2Based.render(docEntry.getSourcePatientId());
143         extrinsic.addSlot(SLOT_NAME_SOURCE_PATIENT_ID, sourcePatient);
144         
145         List<String> slotValues = patientInfoTransformer.toHL7(docEntry.getSourcePatientInfo());
146         extrinsic.addSlot(SLOT_NAME_SOURCE_PATIENT_INFO, slotValues.toArray(new String[slotValues.size()]));
147 
148         if (! docEntry.getReferenceIdList().isEmpty()) {
149             String[] referenceIdValues = new String[docEntry.getReferenceIdList().size()];
150             for (int i = 0; i < docEntry.getReferenceIdList().size(); ++i) {
151                 referenceIdValues[i] = Hl7v2Based.render(docEntry.getReferenceIdList().get(i));
152             }
153             extrinsic.addSlot(SLOT_NAME_REFERENCE_ID_LIST, referenceIdValues);
154         }
155     }
156 
157     @Override
158     protected void addClassificationsFromEbXML(DocumentEntry docEntry, EbXMLExtrinsicObject extrinsic) {
159         super.addClassificationsFromEbXML(docEntry, extrinsic);
160 
161         for (EbXMLClassification author : extrinsic.getClassifications(DOC_ENTRY_AUTHOR_CLASS_SCHEME)) {
162             docEntry.getAuthors().add(authorTransformer.fromEbXML(author));
163         }
164         
165         EbXMLClassification classCode = extrinsic.getSingleClassification(DOC_ENTRY_CLASS_CODE_CLASS_SCHEME);
166         docEntry.setClassCode(codeTransformer.fromEbXML(classCode));
167 
168         EbXMLClassification formatCode = extrinsic.getSingleClassification(DOC_ENTRY_FORMAT_CODE_CLASS_SCHEME);
169         docEntry.setFormatCode(codeTransformer.fromEbXML(formatCode));
170 
171         EbXMLClassification healthcareFacility = extrinsic.getSingleClassification(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME);
172         docEntry.setHealthcareFacilityTypeCode(codeTransformer.fromEbXML(healthcareFacility));
173         
174         EbXMLClassification practiceSetting = extrinsic.getSingleClassification(DOC_ENTRY_PRACTICE_SETTING_CODE_CLASS_SCHEME);
175         docEntry.setPracticeSettingCode(codeTransformer.fromEbXML(practiceSetting));
176         
177         EbXMLClassification typeCode = extrinsic.getSingleClassification(DOC_ENTRY_TYPE_CODE_CLASS_SCHEME);
178         docEntry.setTypeCode(codeTransformer.fromEbXML(typeCode));
179         
180         List<Code> confidentialityCodes = docEntry.getConfidentialityCodes();
181         confidentialityCodes.addAll(extrinsic.getClassifications(DOC_ENTRY_CONFIDENTIALITY_CODE_CLASS_SCHEME).stream()
182                 .map(codeTransformer::fromEbXML)
183                 .collect(Collectors.toList()));
184 
185         List<Code> eventCodeList = docEntry.getEventCodeList();
186         eventCodeList.addAll(extrinsic.getClassifications(DOC_ENTRY_EVENT_CODE_CLASS_SCHEME).stream()
187                 .map(codeTransformer::fromEbXML)
188                 .collect(Collectors.toList()));
189 
190         List<EbXMLClassification> limitedMetadata = extrinsic.getClassifications(DOC_ENTRY_LIMITED_METADATA_CLASS_SCHEME);
191         docEntry.setLimitedMetadata(! limitedMetadata.isEmpty());
192     }
193 
194     @Override
195     protected void addClassifications(DocumentEntry docEntry, EbXMLExtrinsicObject extrinsic, EbXMLObjectLibrary objectLibrary) {
196         super.addClassifications(docEntry, extrinsic, objectLibrary);
197         
198         for (Author author : docEntry.getAuthors()) {
199             EbXMLClassification authorClasification = authorTransformer.toEbXML(author, objectLibrary);
200             extrinsic.addClassification(authorClasification, DOC_ENTRY_AUTHOR_CLASS_SCHEME);
201         }
202         
203         EbXMLClassification classCode = codeTransformer.toEbXML(docEntry.getClassCode(), objectLibrary);
204         extrinsic.addClassification(classCode, DOC_ENTRY_CLASS_CODE_CLASS_SCHEME);
205         
206         EbXMLClassification formatCode = codeTransformer.toEbXML(docEntry.getFormatCode(), objectLibrary);
207         extrinsic.addClassification(formatCode, DOC_ENTRY_FORMAT_CODE_CLASS_SCHEME);
208 
209         EbXMLClassification healthcareFacility = codeTransformer.toEbXML(docEntry.getHealthcareFacilityTypeCode(), objectLibrary);
210         extrinsic.addClassification(healthcareFacility, DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME);
211         
212         EbXMLClassification practiceSetting = codeTransformer.toEbXML(docEntry.getPracticeSettingCode(), objectLibrary);
213         extrinsic.addClassification(practiceSetting, DOC_ENTRY_PRACTICE_SETTING_CODE_CLASS_SCHEME);
214         
215         EbXMLClassification typeCode = codeTransformer.toEbXML(docEntry.getTypeCode(), objectLibrary);
216         extrinsic.addClassification(typeCode, DOC_ENTRY_TYPE_CODE_CLASS_SCHEME);
217         
218         for (Code confCode : docEntry.getConfidentialityCodes()) {
219             EbXMLClassification conf = codeTransformer.toEbXML(confCode, objectLibrary);
220             extrinsic.addClassification(conf, DOC_ENTRY_CONFIDENTIALITY_CODE_CLASS_SCHEME);
221         }
222         
223         for (Code eventCode : docEntry.getEventCodeList()) {
224             EbXMLClassification event = codeTransformer.toEbXML(eventCode, objectLibrary);
225             extrinsic.addClassification(event, DOC_ENTRY_EVENT_CODE_CLASS_SCHEME);
226         }
227 
228         if (docEntry.isLimitedMetadata()) {
229             EbXMLClassification classification = factory.createClassification(objectLibrary);
230             extrinsic.addClassification(classification, DOC_ENTRY_LIMITED_METADATA_CLASS_SCHEME);
231         }
232     }
233 }