001/*- 002 * #%L 003 * HAPI FHIR JPA Server 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.jpa.partition; 021 022import ca.uhn.fhir.i18n.Msg; 023import ca.uhn.fhir.interceptor.model.RequestPartitionId; 024import ca.uhn.fhir.jpa.entity.PartitionEntity; 025import ca.uhn.fhir.jpa.model.util.JpaConstants; 026import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; 027import org.apache.commons.lang3.Validate; 028import org.springframework.beans.factory.annotation.Autowired; 029 030import java.util.ArrayList; 031import java.util.List; 032import java.util.Objects; 033 034public class RequestPartitionHelperSvc extends BaseRequestPartitionHelperSvc { 035 036 @Autowired 037 IPartitionLookupSvc myPartitionConfigSvc; 038 039 public RequestPartitionHelperSvc() {} 040 041 @Override 042 public RequestPartitionId validateAndNormalizePartitionIds(RequestPartitionId theRequestPartitionId) { 043 List<String> names = null; 044 for (int i = 0; i < theRequestPartitionId.getPartitionIds().size(); i++) { 045 046 PartitionEntity partition; 047 Integer id = theRequestPartitionId.getPartitionIds().get(i); 048 if (id == null) { 049 partition = null; 050 } else { 051 try { 052 partition = myPartitionConfigSvc.getPartitionById(id); 053 } catch (IllegalArgumentException e) { 054 String msg = myFhirContext 055 .getLocalizer() 056 .getMessage( 057 BaseRequestPartitionHelperSvc.class, 058 "unknownPartitionId", 059 theRequestPartitionId.getPartitionIds().get(i)); 060 throw new ResourceNotFoundException(Msg.code(1316) + msg); 061 } 062 } 063 064 if (theRequestPartitionId.hasPartitionNames()) { 065 if (partition == null) { 066 Validate.isTrue( 067 theRequestPartitionId.getPartitionIds().get(i) == null, 068 "Partition %s must not have an ID", 069 JpaConstants.DEFAULT_PARTITION_NAME); 070 } else { 071 Validate.isTrue( 072 Objects.equals( 073 theRequestPartitionId.getPartitionNames().get(i), partition.getName()), 074 "Partition name %s does not match ID %s", 075 theRequestPartitionId.getPartitionNames().get(i), 076 theRequestPartitionId.getPartitionIds().get(i)); 077 } 078 } else { 079 if (names == null) { 080 names = new ArrayList<>(); 081 } 082 if (partition != null) { 083 names.add(partition.getName()); 084 } else { 085 names.add(null); 086 } 087 } 088 } 089 090 if (names != null) { 091 return RequestPartitionId.forPartitionIdsAndNames( 092 names, theRequestPartitionId.getPartitionIds(), theRequestPartitionId.getPartitionDate()); 093 } 094 095 return theRequestPartitionId; 096 } 097 098 @Override 099 public RequestPartitionId validateAndNormalizePartitionNames(RequestPartitionId theRequestPartitionId) { 100 List<Integer> ids = null; 101 for (int i = 0; i < theRequestPartitionId.getPartitionNames().size(); i++) { 102 103 PartitionEntity partition; 104 try { 105 partition = myPartitionConfigSvc.getPartitionByName( 106 theRequestPartitionId.getPartitionNames().get(i)); 107 } catch (IllegalArgumentException e) { 108 String msg = myFhirContext 109 .getLocalizer() 110 .getMessage( 111 BaseRequestPartitionHelperSvc.class, 112 "unknownPartitionName", 113 theRequestPartitionId.getPartitionNames().get(i)); 114 throw new ResourceNotFoundException(Msg.code(1317) + msg); 115 } 116 117 if (theRequestPartitionId.hasPartitionIds()) { 118 if (partition == null) { 119 Validate.isTrue( 120 theRequestPartitionId.getPartitionIds().get(i) == null, 121 "Partition %s must not have an ID", 122 JpaConstants.DEFAULT_PARTITION_NAME); 123 } else { 124 Validate.isTrue( 125 Objects.equals( 126 theRequestPartitionId.getPartitionIds().get(i), partition.getId()), 127 "Partition ID %s does not match name %s", 128 theRequestPartitionId.getPartitionIds().get(i), 129 theRequestPartitionId.getPartitionNames().get(i)); 130 } 131 } else { 132 if (ids == null) { 133 ids = new ArrayList<>(); 134 } 135 if (partition != null) { 136 ids.add(partition.getId()); 137 } else { 138 ids.add(null); 139 } 140 } 141 } 142 143 if (ids != null) { 144 return RequestPartitionId.forPartitionIdsAndNames( 145 theRequestPartitionId.getPartitionNames(), ids, theRequestPartitionId.getPartitionDate()); 146 } 147 148 return theRequestPartitionId; 149 } 150}