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.provider;
021
022import ca.uhn.fhir.jpa.search.reindex.IInstanceReindexService;
023import ca.uhn.fhir.rest.annotation.IdParam;
024import ca.uhn.fhir.rest.annotation.Operation;
025import ca.uhn.fhir.rest.annotation.OperationParam;
026import ca.uhn.fhir.rest.api.server.RequestDetails;
027import ca.uhn.fhir.rest.server.provider.ProviderConstants;
028import jakarta.annotation.Nonnull;
029import org.apache.commons.lang3.Validate;
030import org.hl7.fhir.instance.model.api.IBaseParameters;
031import org.hl7.fhir.instance.model.api.IIdType;
032import org.hl7.fhir.instance.model.api.IPrimitiveType;
033
034import java.util.List;
035import java.util.Set;
036import java.util.stream.Collectors;
037
038public class InstanceReindexProvider {
039
040        private final IInstanceReindexService myInstanceReindexService;
041
042        /**
043         * Constructor
044         */
045        public InstanceReindexProvider(@Nonnull IInstanceReindexService theInstanceReindexService) {
046                Validate.notNull(theInstanceReindexService);
047                myInstanceReindexService = theInstanceReindexService;
048        }
049
050        @Operation(name = ProviderConstants.OPERATION_REINDEX_DRYRUN, idempotent = true, global = true)
051        public IBaseParameters reindexInstanceDryRun(
052                        @IdParam IIdType theId,
053                        @OperationParam(name = "code", typeName = "code", min = 0, max = OperationParam.MAX_UNLIMITED)
054                                        List<IPrimitiveType<String>> theCodes,
055                        RequestDetails theRequestDetails) {
056                Set<String> codes = null;
057                if (theCodes != null && theCodes.size() > 0) {
058                        codes = theCodes.stream().map(IPrimitiveType::getValueAsString).collect(Collectors.toSet());
059                }
060
061                return myInstanceReindexService.reindexDryRun(theRequestDetails, theId, codes);
062        }
063
064        @Operation(name = ProviderConstants.OPERATION_REINDEX, idempotent = false, global = true)
065        public IBaseParameters reindexInstance(@IdParam IIdType theId, RequestDetails theRequestDetails) {
066                return myInstanceReindexService.reindex(theRequestDetails, theId);
067        }
068}