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