001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 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        private IPartitionLookupSvc myPartitionConfigSvc;
038
039        public RequestPartitionHelperSvc() {}
040
041        @Override
042        public RequestPartitionId validateAndNormalizePartitionIds(RequestPartitionId theRequestPartitionId) {
043                List<String> names = null;
044                List<Integer> partitionIds = null;
045                for (int i = 0; i < theRequestPartitionId.getPartitionIds().size(); i++) {
046
047                        PartitionEntity partition;
048                        Integer id = theRequestPartitionId.getPartitionIds().get(i);
049                        if (id == null) {
050                                partition = null;
051                                if (myPartitionSettings.getDefaultPartitionId() != null) {
052                                        if (partitionIds == null) {
053                                                partitionIds = new ArrayList<>(theRequestPartitionId.getPartitionIds());
054                                        }
055                                        partitionIds.set(i, myPartitionSettings.getDefaultPartitionId());
056                                }
057                        } else {
058                                try {
059                                        partition = myPartitionConfigSvc.getPartitionById(id);
060                                } catch (IllegalArgumentException e) {
061                                        String msg = myFhirContext
062                                                        .getLocalizer()
063                                                        .getMessage(
064                                                                        BaseRequestPartitionHelperSvc.class,
065                                                                        "unknownPartitionId",
066                                                                        theRequestPartitionId.getPartitionIds().get(i));
067                                        throw new ResourceNotFoundException(Msg.code(1316) + msg);
068                                }
069                        }
070
071                        if (theRequestPartitionId.hasPartitionNames()) {
072                                if (partition == null) {
073                                        Validate.isTrue(
074                                                        theRequestPartitionId.getPartitionIds().get(i) == null,
075                                                        "Partition %s must not have an ID",
076                                                        JpaConstants.DEFAULT_PARTITION_NAME);
077                                } else {
078                                        Validate.isTrue(
079                                                        Objects.equals(
080                                                                        theRequestPartitionId.getPartitionNames().get(i), partition.getName()),
081                                                        "Partition name %s does not match ID %s",
082                                                        theRequestPartitionId.getPartitionNames().get(i),
083                                                        theRequestPartitionId.getPartitionIds().get(i));
084                                }
085                        } else {
086                                if (names == null) {
087                                        names = new ArrayList<>();
088                                }
089                                if (partition != null) {
090                                        names.add(partition.getName());
091                                } else {
092                                        names.add(null);
093                                }
094                        }
095                }
096
097                if (names != null) {
098                        List<Integer> partitionIdsToUse = theRequestPartitionId.getPartitionIds();
099                        if (partitionIds != null) {
100                                partitionIdsToUse = partitionIds;
101                        }
102                        return RequestPartitionId.forPartitionIdsAndNames(
103                                        names, partitionIdsToUse, theRequestPartitionId.getPartitionDate());
104                }
105
106                return theRequestPartitionId;
107        }
108
109        @Override
110        public RequestPartitionId validateAndNormalizePartitionNames(RequestPartitionId theRequestPartitionId) {
111                List<Integer> ids = null;
112                for (int i = 0; i < theRequestPartitionId.getPartitionNames().size(); i++) {
113                        String partitionName = theRequestPartitionId.getPartitionNames().get(i);
114
115                        PartitionEntity partition = null;
116                        if (partitionName != null) {
117                                try {
118                                        partition = myPartitionConfigSvc.getPartitionByName(partitionName);
119                                } catch (IllegalArgumentException e) {
120                                        String msg = myFhirContext
121                                                        .getLocalizer()
122                                                        .getMessage(
123                                                                        BaseRequestPartitionHelperSvc.class,
124                                                                        "unknownPartitionName",
125                                                                        theRequestPartitionId.getPartitionNames().get(i));
126                                        throw new ResourceNotFoundException(Msg.code(1317) + msg);
127                                }
128                        }
129
130                        if (theRequestPartitionId.hasPartitionIds()) {
131                                Integer partitionId = theRequestPartitionId.getPartitionIds().get(i);
132                                if (partition == null) {
133                                        Validate.isTrue(
134                                                        partitionId == null || partitionId.equals(myPartitionSettings.getDefaultPartitionId()),
135                                                        "Partition %s must not have an ID",
136                                                        JpaConstants.DEFAULT_PARTITION_NAME);
137                                } else {
138                                        Validate.isTrue(
139                                                        Objects.equals(partitionId, partition.getId()),
140                                                        "Partition ID %s does not match name %s",
141                                                        partitionId,
142                                                        theRequestPartitionId.getPartitionNames().get(i));
143                                }
144                        } else {
145                                if (ids == null) {
146                                        ids = new ArrayList<>();
147                                }
148                                if (partition != null) {
149                                        ids.add(partition.getId());
150                                } else {
151                                        ids.add(myPartitionSettings.getDefaultPartitionId());
152                                }
153                        }
154                }
155
156                if (ids != null) {
157                        return RequestPartitionId.forPartitionIdsAndNames(
158                                        theRequestPartitionId.getPartitionNames(), ids, theRequestPartitionId.getPartitionDate());
159                }
160
161                return theRequestPartitionId;
162        }
163}