001package ca.uhn.fhir.jpa.searchparam.nickname;
002
003/*-
004 * #%L
005 * HAPI FHIR Search Parameters
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.springframework.core.io.ClassPathResource;
024import org.springframework.core.io.Resource;
025
026import javax.annotation.Nonnull;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.InputStreamReader;
030import java.io.Reader;
031import java.util.Collection;
032import java.util.HashSet;
033import java.util.List;
034import java.util.Set;
035
036public class NicknameSvc {
037        private final NicknameMap myNicknameMap = new NicknameMap();
038
039        public NicknameSvc() throws IOException {
040                Resource nicknameCsvResource = new ClassPathResource("/nickname/names.csv");
041                try (InputStream inputStream = nicknameCsvResource.getInputStream()) {
042                        try (Reader reader = new InputStreamReader(inputStream)) {
043                                myNicknameMap.load(reader);
044                        }
045                }
046        }
047
048        public int size() {
049                return myNicknameMap.size();
050        }
051
052        public Collection<String> getEquivalentNames(String theName) {
053                Set<String> retval = new HashSet<>(getNicknamesFromFormalName(theName));
054
055                if (retval.isEmpty()) {
056                        List<String> formalNames = getFormalNamesFromNickname(theName);
057                        retval.addAll(formalNames);
058                        for (String formalName : formalNames) {
059                                retval.addAll(getNicknamesFromFormalName(formalName));
060                        }
061                }
062                retval.add(theName);
063                return retval;
064        }
065
066        @Nonnull
067        List<String> getNicknamesFromFormalName(String theName) {
068                return myNicknameMap.getNicknamesFromFormalName(theName);
069        }
070
071        @Nonnull
072        List<String> getFormalNamesFromNickname(String theNickname) {
073                return myNicknameMap.getFormalNamesFromNickname(theNickname);
074        }
075}