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.EbXMLFactory;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
22  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
23  
24  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SLOT_NAME_CODING_SCHEME;
25  
26  import java.util.List;
27  
28  /**
29   * Transforms between {@link Code} and its ebXML representation.
30   * @author Jens Riemschneider
31   */
32  public class CodeTransformer {
33      private final EbXMLFactory factory;
34  
35      /**
36       * Constructs the transformer
37       * @param ebXMLFactory
38       *          factory for version independent ebXML objects. 
39       */
40      public CodeTransformer(EbXMLFactory ebXMLFactory) {
41          notNull(ebXMLFactory, "ebXMLFactory cannot be null");
42          factory = ebXMLFactory;
43      }
44  
45      /**
46       * Transforms a {@link Code} instance to a {@link EbXMLClassification}. 
47       * @param code
48       *          the code instance to transform. Can be <code>null</code>.
49       * @param objectLibrary 
50       *          the object library.
51       * @return the {@link EbXMLClassification}. <code>null</code> if the input 
52       *          was <code>null</code>.
53       */
54      public EbXMLClassification toEbXML(Code code, EbXMLObjectLibrary objectLibrary) {
55          if (code == null) {
56              return null;
57          }
58          
59          EbXMLClassification classification = factory.createClassification(objectLibrary);
60          classification.setNodeRepresentation(code.getCode());
61          classification.setName(code.getDisplayName());
62          
63          if (code.getSchemeName() != null) {
64              classification.addSlot(SLOT_NAME_CODING_SCHEME, code.getSchemeName());
65          }
66          
67          return classification;
68      }
69      
70      /**
71       * Transforms a {@link EbXMLClassification} to a {@link Code} instance. 
72       * @param classification
73       *          {@link EbXMLClassification}. Can be <code>null</code>.
74       * @return the code instance. <code>null</code> if the input was <code>null</code>.
75       */
76      public Code fromEbXML(EbXMLClassification classification) {
77          if (classification == null) {
78              return null;
79          }
80          
81          Code code = new Code();
82          code.setCode(classification.getNodeRepresentation());
83          code.setDisplayName(classification.getName());
84  
85          List<String> slotValues = classification.getSlotValues(SLOT_NAME_CODING_SCHEME);
86          if (slotValues.size() > 0) {
87              code.setSchemeName(slotValues.get(0));
88          }
89          
90          return code;
91      }
92  }