1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
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
54
55
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
69
70
71
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
112
113
114
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 }