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 org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
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.Association;
25  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationLabel;
26  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
27  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary;
28  
29  /**
30   * Transforms an {@link Association} to its ebXML representation.
31   * @author Jens Riemschneider
32   */
33  public class AssociationTransformer {
34      private final EbXMLFactory factory;
35      private final CodeTransformer codeTransformer;
36      private final StringToBoolTransformer stringToBoolTransformer;
37      
38      /**
39       * Constructs the transformer
40       * @param factory
41       *          factory for version independent ebXML objects. 
42       */
43      public AssociationTransformer(EbXMLFactory factory) {
44          notNull(factory, "factory cannot be null");
45          this.factory = factory;
46          codeTransformer = new CodeTransformer(factory);
47          stringToBoolTransformer = new StringToBoolTransformer();
48      }
49      
50      /**
51       * Transforms the given association to its ebXML representation.
52       * @param association
53       *          the association to transform. Can be <code>null</code>.
54       * @param objectLibrary 
55       *          the object library.
56       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
57       */
58      public EbXMLAssociation toEbXML(Association association, EbXMLObjectLibrary objectLibrary) {
59          notNull(objectLibrary, "objectLibrary cannot be null");
60          if (association == null) {
61              return null;
62          }
63          
64          EbXMLAssociation result = factory.createAssociation(association.getEntryUuid(), objectLibrary);
65          result.setAssociationType(association.getAssociationType());
66          result.setSource(association.getSourceUuid());
67          result.setTarget(association.getTargetUuid());
68  
69          String label = AssociationLabel.toOpcode(association.getLabel());
70          result.addSlot(Vocabulary.SLOT_NAME_SUBMISSION_SET_STATUS, label);
71  
72          String previousVersion = association.getPreviousVersion();
73          result.addSlot(Vocabulary.SLOT_NAME_PREVIOUS_VERSION, previousVersion);
74  
75          AvailabilityStatus originalStatus = association.getOriginalStatus();
76          result.addSlot(Vocabulary.SLOT_NAME_ORIGINAL_STATUS, AvailabilityStatus.toQueryOpcode(originalStatus));
77  
78          AvailabilityStatus newStatus = association.getNewStatus();
79          result.addSlot(Vocabulary.SLOT_NAME_NEW_STATUS, AvailabilityStatus.toQueryOpcode(newStatus));
80  
81          result.addSlot(Vocabulary.SLOT_NAME_ASSOCIATION_PROPAGATION,
82                                      stringToBoolTransformer.toEbXML(association.getAssociationPropagation()));
83  
84          EbXMLClassification contentType = codeTransformer.toEbXML(association.getDocCode(), objectLibrary);
85          result.addClassification(contentType, Vocabulary.ASSOCIATION_DOC_CODE_CLASS_SCHEME);
86  
87          result.setExtraMetadata(association.getExtraMetadata());
88          result.setStatus(association.getAvailabilityStatus());
89  
90          return result;
91      }
92      
93      /**
94       * Transforms the given ebXML representation into an {@link Association}. 
95       * @param association
96       *          the ebXML association. Can be <code>null</code>.
97       * @return the created {@link Association} instance. <code>null</code> if the input 
98       *          was <code>null</code>.
99       */
100     public Association fromEbXML(EbXMLAssociation association) {
101         if (association == null) {
102             return null;
103         }
104         Association result = new Association();
105         result.setAssociationType(association.getAssociationType());
106         result.setTargetUuid(association.getTarget());
107         result.setSourceUuid(association.getSource());
108         result.setEntryUuid(association.getId());
109 
110         String label = association.getSingleSlotValue(Vocabulary.SLOT_NAME_SUBMISSION_SET_STATUS);
111         result.setLabel(AssociationLabel.fromOpcode(label));
112 
113         String previousVersion = association.getSingleSlotValue(Vocabulary.SLOT_NAME_PREVIOUS_VERSION);
114         result.setPreviousVersion(previousVersion);
115 
116         String originalStatus = association.getSingleSlotValue(Vocabulary.SLOT_NAME_ORIGINAL_STATUS);
117         result.setOriginalStatus(AvailabilityStatus.valueOfOpcode(originalStatus));
118 
119         String newStatus = association.getSingleSlotValue(Vocabulary.SLOT_NAME_NEW_STATUS);
120         result.setNewStatus(AvailabilityStatus.valueOfOpcode(newStatus));
121 
122         String associationPropagation = association.getSingleSlotValue(Vocabulary.SLOT_NAME_ASSOCIATION_PROPAGATION);
123         result.setAssociationPropagation(stringToBoolTransformer.fromEbXML(associationPropagation));
124 
125         EbXMLClassification docCode = association.getSingleClassification(Vocabulary.ASSOCIATION_DOC_CODE_CLASS_SCHEME);
126         result.setDocCode(codeTransformer.fromEbXML(docCode));
127 
128         result.setExtraMetadata(association.getExtraMetadata());
129         result.setAvailabilityStatus(association.getStatus());
130 
131         return result;
132     }
133 }