View Javadoc
1   /*
2    * Copyright 2018 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  
17  package org.openehealth.ipf.commons.ihe.fhir.support.translation;
18  
19  import org.hl7.fhir.dstu3.model.NamingSystem;
20  import org.openehealth.ipf.commons.ihe.fhir.support.NamingSystemService;
21  import org.openehealth.ipf.commons.ihe.fhir.translation.AbstractUriMapper;
22  
23  import java.util.Optional;
24  
25  import static org.hl7.fhir.dstu3.model.NamingSystem.NamingSystemIdentifierType.*;
26  
27  
28  /**
29   * URI Mapper that is backed by a {@link NamingSystemService}. The mapping is done
30   * by search for a {@link NamingSystem}
31   * with a uniqueID of a certain type and returning a unique ID with a different type.
32   *
33   * @author Christian Ohr
34   * @since 3.4
35   *
36   */
37  public class NamingSystemUriMapper extends AbstractUriMapper {
38  
39      private final NamingSystemService namingSystemService;
40      private final String mappingId;
41  
42      public NamingSystemUriMapper(NamingSystemService namingSystemService, String mappingId) {
43          this.namingSystemService = namingSystemService;
44          this.mappingId = mappingId;
45      }
46  
47      @Override
48      protected Optional<String> mapOidToUri(String oid) {
49          return namingSystemService.findActiveNamingSystemByTypeAndValue(mappingId, OID, oid)
50                  .map(NamingSystemService.getValueOfType(URI));
51      }
52  
53      @Override
54      protected Optional<String> mapUriToOid(String uri) {
55          return namingSystemService.findActiveNamingSystemByTypeAndValue(mappingId, URI, uri)
56                  .map(NamingSystemService.getValueOfType(OID));
57      }
58  
59      @Override
60      protected Optional<String> mapUriToNamespace(String uri) {
61          return namingSystemService.findActiveNamingSystemByTypeAndValue(mappingId, URI, uri)
62                  .map(NamingSystemService.getValueOfType(OTHER));
63      }
64  
65      @Override
66      protected Optional<String> mapNamespaceToUri(String namespace) {
67          return namingSystemService.findActiveNamingSystemByTypeAndValue(mappingId, OTHER, namespace)
68                  .map(NamingSystemService.getValueOfType(URI));
69      }
70  }