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 org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlotList;
20  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.SlotType1;
21  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ValueListType;
22  
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.stream.Collectors;
26  
27  import static org.apache.commons.lang3.Validate.notNull;
28  
29  /**
30   * Represents a list of slots.
31   * @author Jens Riemschneider
32   */
33  public class EbXMLSlotList30 implements EbXMLSlotList {
34      private final List<SlotType1> slotListObj;
35      
36      /**
37       * Constructs the slot list by wrapping the given ebXML 3.0 object.
38       * @param slotListObj
39       *          the object to wrap.
40       */
41      public EbXMLSlotList30(List<SlotType1> slotListObj) {
42          notNull(slotListObj, "slotListObj cannot be null");
43          this.slotListObj = slotListObj;
44      }
45      
46      @Override
47      public void addSlot(String slotName, String... slotValues) {
48          if (slotValues == null || slotValues.length == 0) {
49              return;
50          }
51  
52          ValueListType valueList = EbXMLFactory30.RIM_FACTORY.createValueListType();
53          List<String> values = valueList.getValue();
54          for (String slotValue : slotValues) {
55              if (slotValue != null) {
56                  values.add(slotValue);
57              }
58          }
59          
60          SlotType1 slotEbXML = EbXMLFactory30.RIM_FACTORY.createSlotType1();
61          slotEbXML.setName(slotName);
62          slotEbXML.setValueList(valueList);        
63          EbXMLSlot30 slot = new EbXMLSlot30(slotEbXML);
64          
65          if (!slot.getValueList().isEmpty()) {
66              slotListObj.add(slotEbXML);
67          }
68      }
69  
70      @Override
71      public List<String> getSlotValues(String slotName) {
72          notNull(slotName, "slotName cannot be null");
73          for (SlotType1 slot30 : slotListObj) {
74              if (slotName.equals(slot30.getName())) {
75                  return slot30.getValueList().getValue();
76              }
77          }
78          
79          return Collections.emptyList();
80      }
81  
82      @Override
83      public String getSingleSlotValue(String slotName) {
84          List<String> slotValues = getSlotValues(slotName);
85          return slotValues.size() > 0 ? slotValues.get(0) : null;
86      }
87  
88      @Override
89      public List<EbXMLSlot> getSlots() {
90          return slotListObj.stream()
91                  .map(EbXMLSlot30::new)
92                  .collect(Collectors.toList());
93      }
94  
95      @Override
96      public List<EbXMLSlot> getSlots(String slotName) {
97          notNull(slotName, "slotName cannot be null");
98          return slotListObj.stream()
99                  .filter(slot30 -> slotName.equals(slot30.getName()))
100                 .map(EbXMLSlot30::new)
101                 .collect(Collectors.toList());
102     }
103 }