001package ca.uhn.fhir.mdm.api.params;
002
003/*-
004 * #%L
005 * HAPI FHIR - Master Data Management
006 * %%
007 * Copyright (C) 2014 - 2024 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 ca.uhn.fhir.mdm.provider.MdmControllerUtil;
024import ca.uhn.fhir.rest.server.provider.ProviderConstants;
025import jakarta.annotation.Nonnull;
026import jakarta.annotation.Nullable;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.hl7.fhir.instance.model.api.IIdType;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Objects;
033import java.util.stream.Collectors;
034
035public class MdmHistorySearchParameters {
036        private List<IIdType> myGoldenResourceIds = new ArrayList<>();
037        private List<IIdType> mySourceIds = new ArrayList<>();
038
039        public MdmHistorySearchParameters() {}
040
041        public List<IIdType> getGoldenResourceIds() {
042                return myGoldenResourceIds;
043        }
044
045        public List<IIdType> getSourceIds() {
046                return mySourceIds;
047        }
048
049        public MdmHistorySearchParameters setGoldenResourceIds(List<String> theGoldenResourceIds) {
050                myGoldenResourceIds = extractId(theGoldenResourceIds);
051                return this;
052        }
053
054        public MdmHistorySearchParameters setSourceIds(List<String> theSourceIds) {
055                mySourceIds = extractId(theSourceIds);
056                return this;
057        }
058
059        @Override
060        public boolean equals(Object theO) {
061                if (this == theO) return true;
062                if (theO == null || getClass() != theO.getClass()) return false;
063                final MdmHistorySearchParameters that = (MdmHistorySearchParameters) theO;
064                return Objects.equals(myGoldenResourceIds, that.myGoldenResourceIds)
065                                && Objects.equals(mySourceIds, that.mySourceIds);
066        }
067
068        @Override
069        public int hashCode() {
070                return Objects.hash(myGoldenResourceIds, mySourceIds);
071        }
072
073        @Override
074        public String toString() {
075                return new ToStringBuilder(this)
076                                .append("myMdmGoldenResourceIds", myGoldenResourceIds)
077                                .append("myMdmTargetResourceIds", mySourceIds)
078                                .toString();
079        }
080
081        @Nonnull
082        private static List<IIdType> extractId(List<String> theTheGoldenResourceIds) {
083                return theTheGoldenResourceIds.stream()
084                                .map(MdmHistorySearchParameters::extractId)
085                                .collect(Collectors.toUnmodifiableList());
086        }
087
088        @Nullable
089        private static IIdType extractId(String theTheGoldenResourceId) {
090                return MdmControllerUtil.extractGoldenResourceIdDtOrNull(
091                                ProviderConstants.MDM_QUERY_LINKS_GOLDEN_RESOURCE_ID, theTheGoldenResourceId);
092        }
093
094        public enum SearchOperatorEnum {
095                /**
096                 * Used to indicate we should perform an OR search between all IDs provided
097                 * ie. links only need at least 1 of the IDs provided in the search parameters
098                 */
099                OR,
100                /**
101                 * Used to indicate we should perform an AND search between all IDs provided
102                 * ie. links must contain all IDs provided in the search parameters
103                 */
104                AND
105        }
106}