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 org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
22  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Author;
23  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Recipient;
24  import org.openehealth.ipf.commons.ihe.xds.core.metadata.SubmissionSet;
25  
26  import java.util.List;
27  import java.util.stream.Collectors;
28  
29  import static org.apache.commons.lang3.Validate.notNull;
30  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Timestamp.toHL7;
31  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SLOT_NAME_INTENDED_RECIPIENT;
32  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SLOT_NAME_SUBMISSION_TIME;
33  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_AUTHOR_CLASS_SCHEME;
34  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_CONTENT_TYPE_CODE_CLASS_SCHEME;
35  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_LIMITED_METADATA_CLASS_SCHEME;
36  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_LOCALIZED_STRING_PATIENT_ID;
37  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_LOCALIZED_STRING_SOURCE_ID;
38  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_LOCALIZED_STRING_UNIQUE_ID;
39  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_PATIENT_ID_EXTERNAL_ID;
40  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_SOURCE_ID_EXTERNAL_ID;
41  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_UNIQUE_ID_EXTERNAL_ID;
42  
43  /**
44   * Transforms between a {@link SubmissionSet} and its ebXML representation.
45   * @author Jens Riemschneider
46   */
47  public class SubmissionSetTransformer extends XDSMetaClassTransformer<EbXMLRegistryPackage, SubmissionSet> {
48      private final EbXMLFactory factory;
49      
50      private final AuthorTransformer authorTransformer;
51      private final CodeTransformer codeTransformer;
52  
53      private final RecipientTransformer recipientTransformer = new RecipientTransformer();
54  
55      /**
56       * Constructs the transformer
57       * @param factory
58       *          factory for version independent ebXML objects. 
59       */
60      public SubmissionSetTransformer(EbXMLFactory factory) {
61          super(SUBMISSION_SET_PATIENT_ID_EXTERNAL_ID,
62                  SUBMISSION_SET_LOCALIZED_STRING_PATIENT_ID,
63                  SUBMISSION_SET_UNIQUE_ID_EXTERNAL_ID,
64                  SUBMISSION_SET_LOCALIZED_STRING_UNIQUE_ID);
65          
66          notNull(factory, "factory cannot be null");
67  
68          this.factory = factory;
69          authorTransformer = new AuthorTransformer(factory);
70          codeTransformer = new CodeTransformer(factory);
71      }
72  
73      @Override
74      protected EbXMLRegistryPackage createEbXMLInstance(String id, EbXMLObjectLibrary objectLibrary) {
75          return factory.createRegistryPackage(id, objectLibrary);
76      }
77  
78      @Override
79      protected SubmissionSet createMetaClassInstance() {
80          return new SubmissionSet();
81      }
82      
83      @Override
84      protected void addAttributes(SubmissionSet metaData, EbXMLRegistryPackage ebXML, EbXMLObjectLibrary objectLibrary) {
85          super.addAttributes(metaData, ebXML, objectLibrary);
86          ebXML.setStatus(metaData.getAvailabilityStatus());                
87          ebXML.setHome(metaData.getHomeCommunityId());
88      }
89      
90      @Override
91      protected void addAttributesFromEbXML(SubmissionSet metaData, EbXMLRegistryPackage ebXML) {
92          super.addAttributesFromEbXML(metaData, ebXML);
93          metaData.setAvailabilityStatus(ebXML.getStatus());        
94          metaData.setHomeCommunityId(ebXML.getHome());
95      }
96  
97      @Override
98      protected void addSlots(SubmissionSet metaData, EbXMLRegistryPackage ebXML, EbXMLObjectLibrary objectLibrary) {
99          super.addSlots(metaData, ebXML, objectLibrary);
100         
101         String[] slotValues = metaData.getIntendedRecipients().stream()
102                 .map(recipientTransformer::toEbXML)
103                 .toArray(String[]::new);
104         ebXML.addSlot(SLOT_NAME_INTENDED_RECIPIENT, slotValues);
105         ebXML.addSlot(SLOT_NAME_SUBMISSION_TIME, toHL7(metaData.getSubmissionTime()));
106     }
107     
108     @Override
109     protected void addSlotsFromEbXML(SubmissionSet metaData, EbXMLRegistryPackage ebXML) {
110         super.addSlotsFromEbXML(metaData, ebXML);
111         
112         List<Recipient> recipients = metaData.getIntendedRecipients();
113         recipients.addAll(ebXML.getSlotValues(SLOT_NAME_INTENDED_RECIPIENT).stream()
114                 .map(recipientTransformer::fromEbXML)
115                 .collect(Collectors.toList()));
116 
117         metaData.setSubmissionTime(ebXML.getSingleSlotValue(SLOT_NAME_SUBMISSION_TIME));
118     }
119     
120     @Override
121     protected void addClassificationsFromEbXML(SubmissionSet set, EbXMLRegistryPackage regPackage) {
122         super.addClassificationsFromEbXML(set, regPackage);
123         
124         for (EbXMLClassification author : regPackage.getClassifications(SUBMISSION_SET_AUTHOR_CLASS_SCHEME)) {
125             set.getAuthors().add(authorTransformer.fromEbXML(author));
126         }
127 
128         EbXMLClassification contentType = regPackage.getSingleClassification(SUBMISSION_SET_CONTENT_TYPE_CODE_CLASS_SCHEME);
129         set.setContentTypeCode(codeTransformer.fromEbXML(contentType));
130 
131         List<EbXMLClassification> limitedMetadata = regPackage.getClassifications(SUBMISSION_SET_LIMITED_METADATA_CLASS_SCHEME);
132         set.setLimitedMetadata(! limitedMetadata.isEmpty());
133     }
134     
135     @Override
136     protected void addClassifications(SubmissionSet set, EbXMLRegistryPackage regPackage, EbXMLObjectLibrary objectLibrary) {
137         super.addClassifications(set, regPackage, objectLibrary);
138         
139         for (Author author : set.getAuthors()) {
140             EbXMLClassification authorClasification = authorTransformer.toEbXML(author, objectLibrary);
141             regPackage.addClassification(authorClasification, SUBMISSION_SET_AUTHOR_CLASS_SCHEME);
142         }
143 
144         EbXMLClassification contentType = codeTransformer.toEbXML(set.getContentTypeCode(), objectLibrary);
145         regPackage.addClassification(contentType, SUBMISSION_SET_CONTENT_TYPE_CODE_CLASS_SCHEME);
146 
147         if (set.isLimitedMetadata()) {
148             EbXMLClassification classification = factory.createClassification(objectLibrary);
149             classification.setClassificationScheme(SUBMISSION_SET_LIMITED_METADATA_CLASS_SCHEME);
150             regPackage.addClassification(classification, SUBMISSION_SET_LIMITED_METADATA_CLASS_SCHEME);
151         }
152     }
153     
154     @Override
155     protected void addExternalIdentifiers(SubmissionSet metaData, EbXMLRegistryPackage ebXML, EbXMLObjectLibrary objectLibrary) {
156         super.addExternalIdentifiers(metaData, ebXML, objectLibrary);
157 
158         ebXML.addExternalIdentifier(metaData.getSourceId(), 
159                 SUBMISSION_SET_SOURCE_ID_EXTERNAL_ID,
160                 SUBMISSION_SET_LOCALIZED_STRING_SOURCE_ID);
161     }
162     
163     @Override
164     protected void addExternalIdentifiersFromEbXML(SubmissionSet metaData, EbXMLRegistryPackage ebXML) {
165         super.addExternalIdentifiersFromEbXML(metaData, ebXML);
166 
167         String sourceID = ebXML.getExternalIdentifierValue(SUBMISSION_SET_SOURCE_ID_EXTERNAL_ID);
168         metaData.setSourceId(sourceID);
169     }
170 }