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.ebxml.ebxml30;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.activation.DataHandler;
26  import javax.xml.bind.JAXBElement;
27  
28  import lombok.experimental.Delegate;
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.ebxml30.ProvideAndRegisterDocumentSetRequestType.Document;
32  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
33  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.IdentifiableType;
34  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryObjectListType;
35  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotListType;
36  
37  /**
38   * Encapsulation of {@link ProvideAndRegisterDocumentSetRequestType}
39   * @author Jens Riemschneider
40   */
41  public class EbXMLProvideAndRegisterDocumentSetRequest30 extends EbXMLObjectContainer30 implements EbXMLProvideAndRegisterDocumentSetRequest {
42      private final ProvideAndRegisterDocumentSetRequestType request;
43  
44      /**
45       * Constructs a request by wrapping the given ebXML 3.0 object.
46       * @param request
47       *          the object to wrap.
48       * @param objectLibrary
49       *          the object library to use.
50       */
51      public EbXMLProvideAndRegisterDocumentSetRequest30(ProvideAndRegisterDocumentSetRequestType request, EbXMLObjectLibrary objectLibrary) {
52          super(objectLibrary);
53          notNull(request, "request cannot be null");
54          this.request = request;
55      }
56  
57      /**
58       * Constructs a request by wrapping the given ebXML 3.0 object using a new object library.
59       * @param request
60       *          the object to wrap.
61       */
62      public EbXMLProvideAndRegisterDocumentSetRequest30(ProvideAndRegisterDocumentSetRequestType request) {
63          this(request, new EbXMLObjectLibrary());
64          fillObjectLibrary();
65      }
66  
67      @Override
68      public void addDocument(String id, DataHandler dataHandler) {
69          if (dataHandler != null && id != null) {
70              List<Document> documents = request.getDocument();
71              Document document = new Document();
72              document.setId(id);
73              document.setValue(dataHandler);
74              documents.add(document);
75          }
76      }
77  
78      @Override
79      public void removeDocument(String id) {
80          for (Document doc : request.getDocument()) {
81              if (doc.getId().equals(id)) {
82                  request.getDocument().remove(doc);
83                  return;
84              }
85          }
86      }
87  
88      @Override
89      public Map<String, DataHandler> getDocuments() {
90          Map<String, DataHandler> map = new HashMap<>();
91          for (Document document : request.getDocument()) {
92              map.put(document.getId(), document.getValue());
93          }
94          return map;
95      }
96  
97      @Override
98      public ProvideAndRegisterDocumentSetRequestType getInternal() {
99          return request;
100     }
101 
102     @Override
103     List<JAXBElement<? extends IdentifiableType>> getContents() {
104         SubmitObjectsRequest submitObjectsRequest = request.getSubmitObjectsRequest();
105         if (submitObjectsRequest == null) {
106             return Collections.emptyList();
107         }
108         RegistryObjectListType list = submitObjectsRequest.getRegistryObjectList();
109         if (list == null) {
110             return Collections.emptyList();
111         }
112         return list.getIdentifiable();
113     }
114 
115     /**
116      * Implements the {@link org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlotList} interface
117      * by delegating the calls to a "proper" slot list.
118      */
119     @Delegate
120     private EbXMLSlotList30 getSlotList() {
121         SubmitObjectsRequest submitObjectsRequest = request.getSubmitObjectsRequest();
122         if (submitObjectsRequest.getRequestSlotList() == null) {
123             submitObjectsRequest.setRequestSlotList(new SlotListType());
124         }
125         return new EbXMLSlotList30(submitObjectsRequest.getRequestSlotList().getSlot());
126     }
127 }