001package ca.uhn.fhir.jpa.batch.job.model;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.model.api.IModelJson;
026import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
027import ca.uhn.fhir.util.JsonUtil;
028import com.fasterxml.jackson.annotation.JsonProperty;
029import com.fasterxml.jackson.core.JsonProcessingException;
030import com.fasterxml.jackson.databind.ObjectMapper;
031
032import java.util.ArrayList;
033import java.util.List;
034
035/**
036 * Serialize a list of URLs and partition ids so Spring Batch can store it as a String
037 */
038public class RequestListJson implements IModelJson {
039        static final ObjectMapper ourObjectMapper = new ObjectMapper();
040
041        @JsonProperty("partitionedUrls")
042        private List<PartitionedUrl> myPartitionedUrls;
043
044        public static RequestListJson fromUrlStringsAndRequestPartitionIds(List<String> theUrls, List<RequestPartitionId> theRequestPartitionIds) {
045                assert theUrls.size() == theRequestPartitionIds.size();
046
047                RequestListJson retval = new RequestListJson();
048                List<PartitionedUrl> partitionedUrls = new ArrayList<>();
049                for (int i = 0; i < theUrls.size(); ++i) {
050                        partitionedUrls.add(new PartitionedUrl(theUrls.get(i), theRequestPartitionIds.get(i)));
051                }
052                retval.setPartitionedUrls(partitionedUrls);
053                return retval;
054        }
055
056        public static RequestListJson fromJson(String theJson) {
057                try {
058                        return ourObjectMapper.readValue(theJson, RequestListJson.class);
059                } catch (JsonProcessingException e) {
060                        throw new InternalErrorException(Msg.code(1283) + "Failed to decode " + RequestListJson.class);
061                }
062        }
063
064        public String toJson() {
065                return JsonUtil.serializeOrInvalidRequest(this);
066        }
067
068        @Override
069        public String toString() {
070                return "RequestListJson{" +
071                        "myPartitionedUrls=" + myPartitionedUrls +
072                        '}';
073        }
074
075        public List<PartitionedUrl> getPartitionedUrls() {
076                return myPartitionedUrls;
077        }
078
079        public void setPartitionedUrls(List<PartitionedUrl> thePartitionedUrls) {
080                myPartitionedUrls = thePartitionedUrls;
081        }
082}