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.EbXMLInternationalString;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.LocalizedString;
20  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.InternationalStringType;
21  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.LocalizedStringType;
22  
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.stream.Collectors;
26  
27  /**
28   * Encapsulation of {@link InternationalStringType}.
29   * @author Jens Riemschneider
30   */
31  public class EbXMLInternationalString30 implements EbXMLInternationalString {
32      private final InternationalStringType international;
33  
34      /**
35       * Constructs the string by wrapping the given ebXML 3.0 object.
36       * @param international
37       *          the string to wrap. Can be <code>null</code>.
38       */
39      public EbXMLInternationalString30(InternationalStringType international) {
40          this.international = international;
41      }
42  
43      /**
44       * Constructs the string by wrapping the given ebXML 3.0 object.
45       * @param localized
46       *          the string to wrap. Can be <code>null</code>.
47       */
48      public EbXMLInternationalString30(LocalizedString localized) {
49          if (localized != null) {
50              LocalizedStringType localizedEbRS30 = EbXMLFactory30.RIM_FACTORY.createLocalizedStringType();
51              localizedEbRS30.setCharset(localized.getCharset());
52              localizedEbRS30.setLang(localized.getLang());
53              localizedEbRS30.setValue(localized.getValue());
54  
55              international = EbXMLFactory30.RIM_FACTORY.createInternationalStringType();
56              international.getLocalizedString().add(localizedEbRS30);
57          }
58          else {
59              international = null;
60          }
61      }
62  
63      @Override
64      public LocalizedString getLocalizedString(int idx) {
65          if (international == null) {
66              return null;
67          }
68          
69          List<LocalizedStringType> localizedList = international.getLocalizedString();
70          if (idx >= localizedList.size() || idx < 0) {
71              return null;
72          }
73          
74          LocalizedStringType localizedEbRS30 = localizedList.get(idx);
75          if (localizedEbRS30 == null) {
76              return null;
77          }
78          
79          return createLocalizedString(localizedEbRS30);
80      }
81  
82      @Override
83      public List<LocalizedString> getLocalizedStrings() {
84          if (international == null) {
85              return Collections.emptyList(); 
86          }
87          
88          List<LocalizedStringType> list = international.getLocalizedString();
89          return list.stream()
90                  .map(this::createLocalizedString)
91                  .collect(Collectors.toList());
92      }
93  
94      @Override
95      public LocalizedString getSingleLocalizedString() {
96          if (international == null) {
97              return null;
98          }
99          
100         List<LocalizedStringType> locals = international.getLocalizedString();
101         if (locals == null || locals.size() == 0) {
102             return null;
103         }
104         
105         return createLocalizedString(locals.get(0));
106     }
107 
108     private LocalizedString createLocalizedString(LocalizedStringType localizedEbRS30) {
109         LocalizedString localized = new LocalizedString();
110         localized.setCharset(localizedEbRS30.getCharset());
111         localized.setLang(localizedEbRS30.getLang());
112         localized.setValue(localizedEbRS30.getValue());
113         return localized;
114     }
115 
116     /**
117      * @return the ebXML 3.0 object being wrapped by this object.
118      */
119     InternationalStringType getInternal() {
120         return international;
121     }
122 }