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.requests;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.activation.DataHandler;
24  
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
26  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
28  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
29  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
30  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLProvideAndRegisterDocumentSetRequest;
31  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
32  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
33  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
34  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.AssociationTransformer;
35  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.DocumentEntryTransformer;
36  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.FolderTransformer;
37  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.SubmissionSetTransformer;
38  
39  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.*;
40  
41  /**
42   * Transforms between a {@link ProvideAndRegisterDocumentSet} and its ebXML representation.
43   * @author Jens Riemschneider
44   */
45  public class ProvideAndRegisterDocumentSetTransformer {
46      private final EbXMLFactory factory;    
47      private final SubmissionSetTransformer submissionSetTransformer;
48      private final DocumentEntryTransformer documentEntryTransformer;
49      private final FolderTransformer folderTransformer;
50      private final AssociationTransformer associationTransformer;
51      
52      /**
53       * Constructs the transformer
54       * @param factory
55       *          factory for version independent ebXML objects. 
56       */
57      public ProvideAndRegisterDocumentSetTransformer(EbXMLFactory factory) {
58          notNull(factory, "factory cannot be null");
59          this.factory = factory;
60          
61          submissionSetTransformer = new SubmissionSetTransformer(factory);
62          documentEntryTransformer = new DocumentEntryTransformer(factory);
63          folderTransformer = new FolderTransformer(factory);
64          associationTransformer = new AssociationTransformer(factory);
65      }
66      
67      /**
68       * Transforms a request into its ebXML representation.
69       * @param request
70       *          the request. Can be <code>null</code>.
71       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
72       */
73      public EbXMLProvideAndRegisterDocumentSetRequest toEbXML(ProvideAndRegisterDocumentSet request) {
74          if (request == null) {
75              return null;
76          }
77          
78          EbXMLObjectLibrary library = factory.createObjectLibrary();        
79          EbXMLProvideAndRegisterDocumentSetRequest ebXML = factory.createProvideAndRegisterDocumentSetRequest(library);
80          
81          for (Document doc : request.getDocuments()) {
82              DocumentEntry docEntry = doc.getDocumentEntry();
83              if (docEntry != null) {
84                  ebXML.addExtrinsicObject(documentEntryTransformer.toEbXML(docEntry, library));
85                  ebXML.addDocument(docEntry.getEntryUuid(), doc.getContent(DataHandler.class));
86              }
87          }
88          
89          for (Folder folder : request.getFolders()) {
90              ebXML.addRegistryPackage(folderTransformer.toEbXML(folder, library));
91              addClassification(ebXML, folder.getEntryUuid(), FOLDER_CLASS_NODE, library);
92          }
93          
94          SubmissionSet submissionSet = request.getSubmissionSet();
95          ebXML.addRegistryPackage(submissionSetTransformer.toEbXML(submissionSet, library));
96          String entryUUID = submissionSet != null ? submissionSet.getEntryUuid() : null;
97          addClassification(ebXML, entryUUID, SUBMISSION_SET_CLASS_NODE, library);
98          
99          for (Association association : request.getAssociations()) {
100             ebXML.addAssociation(associationTransformer.toEbXML(association, library));
101         }
102 
103         if (request.getTargetHomeCommunityId() != null) {
104             ebXML.addSlot(SLOT_NAME_HOME_COMMUNITY_ID, request.getTargetHomeCommunityId());
105         }
106 
107         return ebXML;
108     }
109 
110     /**
111      * Transforms an ebXML representation or a request.
112      * @param ebXML
113      *          the ebXML representation. Can be <code>null</code>.
114      * @return the request. <code>null</code> if the input was <code>null</code>.
115      */
116     public ProvideAndRegisterDocumentSet fromEbXML(EbXMLProvideAndRegisterDocumentSetRequest ebXML) {
117         if (ebXML == null) {
118             return null;
119         }
120         
121         ProvideAndRegisterDocumentSet request = new ProvideAndRegisterDocumentSet();
122         
123         Map<String, DataHandler> documents = ebXML.getDocuments();
124         for (EbXMLExtrinsicObject extrinsic : ebXML.getExtrinsicObjects(DocumentEntryType.STABLE.getUuid())) {
125             DocumentEntry docEntry = documentEntryTransformer.fromEbXML(extrinsic);
126             if (docEntry != null) {
127                 Document document = new Document();
128                 document.setDocumentEntry(docEntry);
129                 if (docEntry.getEntryUuid() != null) {
130                     String id = docEntry.getEntryUuid();
131                     DataHandler data = documents.get(id);
132                     document.setContent(DataHandler.class, data);
133                 }
134                 request.getDocuments().add(document);
135             }
136         }
137 
138         for (EbXMLRegistryPackage regPackage : ebXML.getRegistryPackages(FOLDER_CLASS_NODE)) {
139             request.getFolders().add(folderTransformer.fromEbXML(regPackage));
140         }
141 
142         List<EbXMLRegistryPackage> regPackages = ebXML.getRegistryPackages(SUBMISSION_SET_CLASS_NODE);
143         if (regPackages.size() > 0) {
144             request.setSubmissionSet(submissionSetTransformer.fromEbXML(regPackages.get(0)));
145         }
146         
147         for (EbXMLAssociation association : ebXML.getAssociations()) {
148             request.getAssociations().add(associationTransformer.fromEbXML(association));
149         }
150 
151         request.setTargetHomeCommunityId(ebXML.getSingleSlotValue(SLOT_NAME_HOME_COMMUNITY_ID));
152 
153         return request;
154     }
155 
156     private void addClassification(EbXMLProvideAndRegisterDocumentSetRequest ebXML, String classified, String node, EbXMLObjectLibrary library) {
157         EbXMLClassification classification = factory.createClassification(library);
158         classification.setClassifiedObject(classified);
159         classification.setClassificationNode(node);
160         classification.assignUniqueId();
161         ebXML.addClassification(classification);
162     }    
163 }