001/*-
002 * #%L
003 * HAPI FHIR JPA Server - Batch2 Task Processor
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.batch2.jobs.chunk;
021
022import ca.uhn.fhir.interceptor.model.RequestPartitionId;
023import ca.uhn.fhir.jpa.api.svc.IIdHelperService;
024import ca.uhn.fhir.model.api.IModelJson;
025import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
026import com.fasterxml.jackson.annotation.JsonProperty;
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.Collections;
033import java.util.List;
034import java.util.stream.Collectors;
035
036public class ResourceIdListWorkChunkJson implements IModelJson {
037
038        @JsonProperty("requestPartitionId")
039        private RequestPartitionId myRequestPartitionId;
040
041        @JsonProperty("ids")
042        private List<TypedPidJson> myTypedPids;
043
044        /**
045         * Constructor
046         */
047        public ResourceIdListWorkChunkJson() {
048                super();
049        }
050
051        /**
052         * Constructor
053         */
054        public ResourceIdListWorkChunkJson(
055                        Collection<TypedPidJson> theTypedPids, RequestPartitionId theRequestPartitionId) {
056                this();
057                getTypedPids().addAll(theTypedPids);
058                myRequestPartitionId = theRequestPartitionId;
059        }
060
061        public RequestPartitionId getRequestPartitionId() {
062                return myRequestPartitionId;
063        }
064
065        private List<TypedPidJson> getTypedPids() {
066                if (myTypedPids == null) {
067                        myTypedPids = new ArrayList<>();
068                }
069                return myTypedPids;
070        }
071
072        @Override
073        public String toString() {
074                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
075                                .append("ids", myTypedPids)
076                                .append("requestPartitionId", myRequestPartitionId)
077                                .toString();
078        }
079
080        public <T extends IResourcePersistentId> List<T> getResourcePersistentIds(IIdHelperService<T> theIdHelperService) {
081                if (myTypedPids.isEmpty()) {
082                        return Collections.emptyList();
083                }
084
085                return myTypedPids.stream()
086                                .map(t -> {
087                                        T retval = theIdHelperService.newPidFromStringIdAndResourceName(t.getPid(), t.getResourceType());
088                                        return retval;
089                                })
090                                .collect(Collectors.toList());
091        }
092
093        public int size() {
094                return getTypedPids().size();
095        }
096
097        public void addTypedPid(String theResourceType, Long thePid) {
098                getTypedPids().add(new TypedPidJson(theResourceType, thePid.toString()));
099        }
100
101        public String getResourceType(int index) {
102                return getTypedPids().get(index).getResourceType();
103        }
104}