001/*- 002 * #%L 003 * HAPI FHIR - Master Data Management 004 * %% 005 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.mdm.api.params; 021 022import ca.uhn.fhir.i18n.Msg; 023import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum; 024import ca.uhn.fhir.mdm.api.MdmMatchResultEnum; 025import ca.uhn.fhir.mdm.api.paging.MdmPageRequest; 026import ca.uhn.fhir.mdm.provider.MdmControllerUtil; 027import ca.uhn.fhir.rest.api.SortOrderEnum; 028import ca.uhn.fhir.rest.api.SortSpec; 029import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 030import ca.uhn.fhir.rest.server.provider.ProviderConstants; 031import jakarta.annotation.Nullable; 032import org.apache.commons.lang3.builder.ToStringBuilder; 033import org.hl7.fhir.instance.model.api.IIdType; 034 035import java.util.ArrayList; 036import java.util.List; 037import java.util.Set; 038 039import static org.hibernate.internal.util.StringHelper.isBlank; 040 041public class MdmQuerySearchParameters { 042 043 public static final String GOLDEN_RESOURCE_PID_NAME = "myGoldenResourcePid"; 044 public static final String SOURCE_PID_NAME = "mySourcePid"; 045 public static final String MATCH_RESULT_NAME = "myMatchResult"; 046 public static final String LINK_SOURCE_NAME = "myLinkSource"; 047 public static final String PARTITION_ID_NAME = "myPartitionId"; 048 public static final String GOLDEN_RESOURCE_NAME = "myGoldenResource"; 049 public static final String RESOURCE_TYPE_NAME = "myResourceType"; 050 public static final String CREATED_NAME = "myCreated"; 051 public static final String UPDATED_NAME = "myUpdated"; 052 public static final String SCORE_NAME = "myScore"; 053 054 public static final Set<String> ourValidSortParameters = Set.of( 055 GOLDEN_RESOURCE_PID_NAME, 056 SOURCE_PID_NAME, 057 MATCH_RESULT_NAME, 058 LINK_SOURCE_NAME, 059 PARTITION_ID_NAME, 060 GOLDEN_RESOURCE_NAME, 061 RESOURCE_TYPE_NAME, 062 CREATED_NAME, 063 UPDATED_NAME, 064 SCORE_NAME); 065 066 private IIdType myGoldenResourceId; 067 private IIdType mySourceId; 068 private MdmMatchResultEnum myMatchResult; 069 private MdmLinkSourceEnum myLinkSource; 070 private MdmPageRequest myPageRequest; 071 private List<Integer> myPartitionIds; 072 private String myResourceType; 073 private final List<SortSpec> mySort = new ArrayList<>(); 074 075 @Deprecated(since = "2023.02.R01", forRemoval = true) 076 public MdmQuerySearchParameters() {} 077 078 public MdmQuerySearchParameters(MdmPageRequest thePageRequest) { 079 setPageRequest(thePageRequest); 080 } 081 082 @Deprecated(since = "2023.02.R01", forRemoval = true) 083 public MdmQuerySearchParameters( 084 @Nullable String theGoldenResourceId, 085 @Nullable String theSourceId, 086 @Nullable String theMatchResult, 087 @Nullable String theLinkSource, 088 MdmPageRequest thePageRequest, 089 @Nullable List<Integer> thePartitionIds, 090 @Nullable String theResourceType) { 091 setGoldenResourceId(theGoldenResourceId); 092 setSourceId(theSourceId); 093 setLinkSource(theLinkSource); 094 setMatchResult(theMatchResult); 095 setPartitionIds(thePartitionIds); 096 setPageRequest(thePageRequest); 097 setResourceType(theResourceType); 098 } 099 100 @Deprecated(since = "2023.02.R01", forRemoval = true) 101 public MdmQuerySearchParameters( 102 @Nullable IIdType theGoldenResourceId, 103 @Nullable IIdType theSourceId, 104 @Nullable MdmMatchResultEnum theMatchResult, 105 @Nullable MdmLinkSourceEnum theLinkSource, 106 MdmPageRequest thePageRequest, 107 @Nullable List<Integer> thePartitionIds, 108 @Nullable String theResourceType) { 109 setGoldenResourceId(theGoldenResourceId); 110 setSourceId(theSourceId); 111 setLinkSource(theLinkSource); 112 setMatchResult(theMatchResult); 113 setPartitionIds(thePartitionIds); 114 setPageRequest(thePageRequest); 115 setResourceType(theResourceType); 116 } 117 118 public IIdType getGoldenResourceId() { 119 return myGoldenResourceId; 120 } 121 122 public MdmQuerySearchParameters setGoldenResourceId(IIdType theGoldenResourceId) { 123 myGoldenResourceId = theGoldenResourceId; 124 return this; 125 } 126 127 public MdmQuerySearchParameters setGoldenResourceId(String theGoldenResourceId) { 128 myGoldenResourceId = MdmControllerUtil.extractGoldenResourceIdDtOrNull( 129 ProviderConstants.MDM_QUERY_LINKS_GOLDEN_RESOURCE_ID, theGoldenResourceId); 130 return this; 131 } 132 133 public IIdType getSourceId() { 134 return mySourceId; 135 } 136 137 public MdmQuerySearchParameters setSourceId(IIdType theSourceId) { 138 mySourceId = theSourceId; 139 return this; 140 } 141 142 public MdmQuerySearchParameters setSourceId(String theSourceId) { 143 mySourceId = 144 MdmControllerUtil.extractSourceIdDtOrNull(ProviderConstants.MDM_QUERY_LINKS_RESOURCE_ID, theSourceId); 145 return this; 146 } 147 148 public MdmMatchResultEnum getMatchResult() { 149 return myMatchResult; 150 } 151 152 public MdmQuerySearchParameters setMatchResult(MdmMatchResultEnum theMatchResult) { 153 myMatchResult = theMatchResult; 154 return this; 155 } 156 157 public MdmQuerySearchParameters setMatchResult(String theMatchResult) { 158 myMatchResult = MdmControllerUtil.extractMatchResultOrNull(theMatchResult); 159 return this; 160 } 161 162 public MdmLinkSourceEnum getLinkSource() { 163 return myLinkSource; 164 } 165 166 public MdmQuerySearchParameters setLinkSource(MdmLinkSourceEnum theLinkSource) { 167 myLinkSource = theLinkSource; 168 return this; 169 } 170 171 public MdmQuerySearchParameters setLinkSource(String theLinkSource) { 172 myLinkSource = MdmControllerUtil.extractLinkSourceOrNull(theLinkSource); 173 return this; 174 } 175 176 public MdmPageRequest getPageRequest() { 177 return myPageRequest; 178 } 179 180 public void setPageRequest(MdmPageRequest thePageRequest) { 181 myPageRequest = thePageRequest; 182 } 183 184 public List<Integer> getPartitionIds() { 185 return myPartitionIds; 186 } 187 188 public MdmQuerySearchParameters setPartitionIds(List<Integer> thePartitionIds) { 189 myPartitionIds = thePartitionIds; 190 return this; 191 } 192 193 public String getResourceType() { 194 return myResourceType; 195 } 196 197 public MdmQuerySearchParameters setResourceType(String theResourceType) { 198 myResourceType = theResourceType; 199 return this; 200 } 201 202 public List<SortSpec> getSort() { 203 return mySort; 204 } 205 206 public MdmQuerySearchParameters setSort(String theSortString) { 207 if (isBlank(theSortString)) { 208 return this; 209 } 210 211 for (String param : theSortString.split(",")) { 212 String p = (param.startsWith("-") ? param.substring(1) : param).trim(); 213 if (!MdmQuerySearchParameters.ourValidSortParameters.contains(p)) { 214 throw new InvalidRequestException( 215 Msg.code(2233) + "Unrecognized sort parameter: " + p + ". Valid parameters are: " 216 + String.join(", ", MdmQuerySearchParameters.ourValidSortParameters)); 217 } 218 SortOrderEnum order = param.startsWith("-") ? SortOrderEnum.DESC : SortOrderEnum.ASC; 219 mySort.add(new SortSpec(p, order)); 220 } 221 return this; 222 } 223 224 @Override 225 public String toString() { 226 return new ToStringBuilder(this) 227 .append(GOLDEN_RESOURCE_PID_NAME, myGoldenResourceId) 228 .append(SOURCE_PID_NAME, mySourceId) 229 .append(MATCH_RESULT_NAME, myMatchResult) 230 .append(LINK_SOURCE_NAME, myLinkSource) 231 .append(PARTITION_ID_NAME, myPartitionIds) 232 .append(RESOURCE_TYPE_NAME, myResourceType) 233 .append("myPageRequest", myPageRequest) 234 .toString(); 235 } 236}