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  
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
24  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
26  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
28  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSubmitObjectsRequest;
29  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
30  import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
31  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.AssociationTransformer;
32  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.DocumentEntryTransformer;
33  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.FolderTransformer;
34  import org.openehealth.ipf.commons.ihe.xds.core.transform.ebxml.SubmissionSetTransformer;
35  
36  /**
37   * Transforms between a {@link RegisterDocumentSet} and its ebXML representation.
38   * @author Jens Riemschneider
39   */
40  public class RegisterDocumentSetTransformer {
41      private final EbXMLFactory factory;    
42      private final SubmissionSetTransformer submissionSetTransformer;
43      private final DocumentEntryTransformer documentEntryTransformer;
44      private final FolderTransformer folderTransformer;
45      private final AssociationTransformer associationTransformer;
46      
47      /**
48       * Constructs the transformer
49       * @param factory
50       *          factory for version independent ebXML objects. 
51       */
52      public RegisterDocumentSetTransformer(EbXMLFactory factory) {
53          notNull(factory, "factory cannot be null");
54          this.factory = factory;
55          
56          submissionSetTransformer = new SubmissionSetTransformer(factory);
57          documentEntryTransformer = new DocumentEntryTransformer(factory);
58          folderTransformer = new FolderTransformer(factory);
59          associationTransformer = new AssociationTransformer(factory);
60      }
61  
62      /**
63       * Transforms the request into its ebXML representation.
64       * @param request
65       *          the request. Can be <code>null</code>.
66       * @return the ebXML representation. <code>null</code> if the input was <code>null</code>.
67       */
68      public EbXMLSubmitObjectsRequest toEbXML(RegisterDocumentSet request) {
69          notNull(request, "request cannot be null");
70                  
71          EbXMLSubmitObjectsRequest ebXML = factory.createSubmitObjectsRequest();
72          EbXMLObjectLibrary library = ebXML.getObjectLibrary();        
73          
74          for (DocumentEntry docEntry : request.getDocumentEntries()) {
75              ebXML.addExtrinsicObject(documentEntryTransformer.toEbXML(docEntry, library));
76          }
77          
78          for (Folder folder : request.getFolders()) {
79              ebXML.addRegistryPackage(folderTransformer.toEbXML(folder, library));
80              addClassification(ebXML, folder.getEntryUuid(), Vocabulary.FOLDER_CLASS_NODE, library);
81          }
82          
83          SubmissionSet submissionSet = request.getSubmissionSet();
84          ebXML.addRegistryPackage(submissionSetTransformer.toEbXML(submissionSet, library));
85          String entryUUID = submissionSet != null ? submissionSet.getEntryUuid() : null;
86          addClassification(ebXML, entryUUID, Vocabulary.SUBMISSION_SET_CLASS_NODE, library);
87          
88          for (Association association : request.getAssociations()) {
89              ebXML.addAssociation(associationTransformer.toEbXML(association, library));
90          }
91  
92          if (request.getTargetHomeCommunityId() != null) {
93              ebXML.addSlot(Vocabulary.SLOT_NAME_HOME_COMMUNITY_ID, request.getTargetHomeCommunityId());
94          }
95  
96          return ebXML;
97      }
98  
99      /**
100      * Transforms the ebXML representation into a request.
101      * @param ebXML
102      *          the ebXML representation. Can be <code>null</code>.
103      * @return the request. <code>null</code> if the input was <code>null</code>.
104      */
105     public RegisterDocumentSet fromEbXML(EbXMLSubmitObjectsRequest ebXML) {
106         notNull(ebXML, "ebXML cannot be null");
107         
108         RegisterDocumentSet request = new RegisterDocumentSet();        
109         
110         for (EbXMLExtrinsicObject extrinsic : ebXML.getExtrinsicObjects(DocumentEntryType.STABLE_OR_ON_DEMAND)) {
111             request.getDocumentEntries().add(documentEntryTransformer.fromEbXML(extrinsic));
112         }
113 
114         for (EbXMLRegistryPackage regPackage : ebXML.getRegistryPackages(Vocabulary.FOLDER_CLASS_NODE)) {
115             request.getFolders().add(folderTransformer.fromEbXML(regPackage));
116         }
117 
118         List<EbXMLRegistryPackage> regPackages = ebXML.getRegistryPackages(Vocabulary.SUBMISSION_SET_CLASS_NODE);
119         if (regPackages.size() > 0) {
120             request.setSubmissionSet(submissionSetTransformer.fromEbXML(regPackages.get(0)));
121         }
122         
123         for (EbXMLAssociation association : ebXML.getAssociations()) {
124             request.getAssociations().add(associationTransformer.fromEbXML(association));
125         }
126 
127         request.setTargetHomeCommunityId(ebXML.getSingleSlotValue(Vocabulary.SLOT_NAME_HOME_COMMUNITY_ID));
128 
129         return request;
130     }
131 
132     private void addClassification(EbXMLSubmitObjectsRequest ebXML, String classified, String node, EbXMLObjectLibrary library) {
133         EbXMLClassification classification = factory.createClassification(library);
134         classification.setClassifiedObject(classified);
135         classification.setClassificationNode(node);
136         classification.assignUniqueId();
137         ebXML.addClassification(classification);
138     }    
139 }