View Javadoc
1   /*
2    * Copyright 2009-2011 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 org.openehealth.ipf.commons.ihe.xds.core.ExtraMetadataHolder;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
23  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Hl7v2Based;
24  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
25  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary;
26  import org.openehealth.ipf.commons.ihe.xds.core.metadata.XDSMetaClass;
27  
28  /**
29   * Base class for transformers of {@link XDSMetaClass} and ebXML representations.
30   * @param <E>
31   *          the ebXML type.
32   * @param <C>
33   *          the {@link XDSMetaClass} type.
34   * @author Jens Riemschneider
35   */
36  public abstract class XDSMetaClassTransformer<E extends EbXMLRegistryObject, C extends XDSMetaClass> {
37      private final String patientIdExternalId;
38      private final String patientIdLocalizedString;
39      private final String uniqueIdExternalId;
40      private final String uniqueIdLocalizedString;
41  
42      /**
43       * Constructs the transformer using various constants from {@link Vocabulary}.
44       * @param patientIdExternalId
45       *          the external ID of the patient ID.
46       * @param patientIdLocalizedString
47       *          the localized string of the patient ID.
48       * @param uniqueIdExternalId
49       *          the external ID of the unique ID.
50       * @param uniqueIdLocalizedString
51       *          the localized string of the unique ID.
52       */
53      protected XDSMetaClassTransformer(
54              String patientIdExternalId,
55              String patientIdLocalizedString, 
56              String uniqueIdExternalId,
57              String uniqueIdLocalizedString) {
58          
59          this.patientIdExternalId = patientIdExternalId;
60          this.patientIdLocalizedString = patientIdLocalizedString;
61          this.uniqueIdExternalId = uniqueIdExternalId;
62          this.uniqueIdLocalizedString = uniqueIdLocalizedString;
63      }
64  
65      /**
66       * Transforms the given {@link XDSMetaClass} into its ebXML representation.
67       * @param metaData
68       *          the meta data to transform. Can be <code>null</code>.
69       * @param objectLibrary
70       *          the object library.
71       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
72       */
73      public E toEbXML(C metaData, EbXMLObjectLibrary objectLibrary) {
74          notNull(objectLibrary, "objectLibrary cannot be null");
75          
76          if (metaData == null) {
77              return null;
78          }
79          
80          E ebXML = createEbXMLInstance(metaData.getEntryUuid(), objectLibrary);
81          
82          addAttributes(metaData, ebXML, objectLibrary);        
83          addClassifications(metaData, ebXML, objectLibrary);
84          addSlots(metaData, ebXML, objectLibrary);
85          addExternalIdentifiers(metaData, ebXML, objectLibrary);
86  
87          if (ebXML instanceof ExtraMetadataHolder) {
88              ((ExtraMetadataHolder) ebXML).setExtraMetadata(metaData.getExtraMetadata());
89          }
90  
91          return ebXML;
92      }
93  
94      /**
95       * Transforms an ebXML representation into its {@link XDSMetaClass}.
96       * @param ebXML
97       *          the ebXML representation. Can be <code>null</code>.
98       * @return the meta data. <code>null</code> if the input was <code>null</code>.
99       */
100     public C fromEbXML(E ebXML) {
101         if (ebXML == null) {
102             return null;
103         }
104                 
105         C metaData = createMetaClassInstance();
106         
107         addAttributesFromEbXML(metaData, ebXML);        
108         addClassificationsFromEbXML(metaData, ebXML);
109         addSlotsFromEbXML(metaData, ebXML);
110         addExternalIdentifiersFromEbXML(metaData, ebXML);
111 
112         if (ebXML instanceof ExtraMetadataHolder) {
113             metaData.setExtraMetadata(((ExtraMetadataHolder) ebXML).getExtraMetadata());
114         }
115 
116         return metaData;
117     }
118     
119     /**
120      * Called by the base class to create a new instance of the ebXML type.
121      * @param id
122      *          the id of the object to create.
123      * @param objectLibrary
124      *          the object library. 
125      * @return a new instance of the ebXML type.
126      */
127     protected abstract E createEbXMLInstance(String id, EbXMLObjectLibrary objectLibrary);
128 
129     /**
130      * Called by the base class to create a new instance of the {@link XDSMetaClass}. 
131      * @return a new instance of the meta data type.
132      */
133     protected abstract C createMetaClassInstance();
134 
135     /**
136      * Called by the base class to add attributes to the ebXML instance.
137      * @param metaData
138      *          the meta data instance containing the attributes. 
139      * @param ebXML
140      *          the ebXML instance receiving the attributes.
141      * @param objectLibrary 
142      *          the object library.
143      */
144     protected void addAttributes(C metaData, E ebXML, EbXMLObjectLibrary objectLibrary) {
145         ebXML.setDescription(metaData.getComments());
146         ebXML.setName(metaData.getTitle());
147         ebXML.setLid(metaData.getLogicalUuid());
148         ebXML.setVersionInfo(metaData.getVersion());
149     }
150 
151     /**
152      * Called by the base class to add attributes to the meta data.
153      * @param metaData
154      *          the meta data instance receiving the attributes.
155      * @param ebXML
156      *          the ebXML instance containing the attributes.
157      */
158     protected void addAttributesFromEbXML(C metaData, E ebXML) {
159         metaData.setComments(ebXML.getDescription());
160         metaData.setTitle(ebXML.getName());
161         metaData.setEntryUuid(ebXML.getId());
162         metaData.setLogicalUuid(ebXML.getLid());
163         metaData.setVersion(ebXML.getVersionInfo());
164     }
165 
166     /**
167      * Called by the base class to add slots to the ebXML instance.
168      * @param metaData
169      *          the meta data instance containing the slots. 
170      * @param ebXML
171      *          the ebXML instance receiving the slots.
172      * @param objectLibrary 
173      *          the object library.
174      */
175     protected void addSlots(C metaData, E ebXML, EbXMLObjectLibrary objectLibrary) {}
176 
177     /**
178      * Called by the base class to add slots to the meta data.
179      * @param metaData
180      *          the meta data instance receiving the slots. 
181      * @param ebXML
182      *          the ebXML instance containing the slots.
183      */
184     protected void addSlotsFromEbXML(C metaData, E ebXML) {}
185 
186     /**
187      * Called by the base class to add classifications to the ebXML instance.
188      * @param metaData
189      *          the meta data instance containing the classifications. 
190      * @param ebXML
191      *          the ebXML instance receiving the classifications.
192      * @param objectLibrary 
193      *          the object library.
194      */
195     protected void addClassifications(C metaData, E ebXML, EbXMLObjectLibrary objectLibrary) {}
196 
197     /**
198      * Called by the base class to add classifications to the meta data.
199      * @param metaData
200      *          the meta data instance receiving the classifications. 
201      * @param ebXML
202      *          the ebXML instance containing the classifications.
203      */
204     protected void addClassificationsFromEbXML(C metaData, E ebXML) {}
205 
206     /**
207      * Called by the base class to add external identifiers to the ebXML instance.
208      * @param metaData
209      *          the meta data instance containing the external identifiers. 
210      * @param ebXML
211      *          the ebXML instance receiving the external identifiers.
212      * @param objectLibrary 
213      *          the object library.
214      */
215     protected void addExternalIdentifiers(C metaData, E ebXML, EbXMLObjectLibrary objectLibrary) {
216         String patientID = Hl7v2Based.render(metaData.getPatientId());
217         ebXML.addExternalIdentifier(patientID, patientIdExternalId, patientIdLocalizedString);        
218         ebXML.addExternalIdentifier(metaData.getUniqueId(), uniqueIdExternalId, uniqueIdLocalizedString);
219     }
220 
221     /**
222      * Called by the base class to add external identifiers to the meta data.
223      * @param metaData
224      *          the meta data instance receiving the external identifiers. 
225      * @param ebXML
226      *          the ebXML instance containing the external identifiers.
227      */
228     protected void addExternalIdentifiersFromEbXML(C metaData, E ebXML) {
229         String patientID = ebXML.getExternalIdentifierValue(patientIdExternalId);
230         metaData.setPatientId(Hl7v2Based.parse(patientID, Identifiable.class));
231         metaData.setUniqueId(ebXML.getExternalIdentifierValue(uniqueIdExternalId));
232     }
233 }