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.packages;
021
022import ca.uhn.fhir.context.FhirVersionEnum;
023import jakarta.annotation.Nullable;
024import org.springframework.data.domain.PageRequest;
025
026import java.util.Objects;
027import java.util.Optional;
028import java.util.StringJoiner;
029
030/**
031 * Request parameters for {@link IHapiPackageCacheManager#findPackageAsset(FindPackageAssetRequest)}
032 */
033public class FindPackageAssetRequest {
034
035        private static final int DEFAULT_PAGE_NUMBER = 0;
036        private static final int DEFAULT_PAGE_SIZE = 50;
037
038        private final FhirVersionEnum myFhirVersion;
039        private final String myCanonicalUrl;
040        private final String myPackageId;
041        private final PageRequest myPageRequest;
042
043        @Nullable
044        private final String myVersion;
045
046        public static FindPackageAssetRequest withVersion(
047                        FhirVersionEnum theFhirVersion, String theCanonicalUrl, String thePackageId, String theVersion) {
048                return new FindPackageAssetRequest(theFhirVersion, theCanonicalUrl, thePackageId, theVersion, null);
049        }
050
051        public static FindPackageAssetRequest withVersion(
052                        FhirVersionEnum theFhirVersion,
053                        String theCanonicalUrl,
054                        String thePackageId,
055                        String theVersion,
056                        int thePageNumber,
057                        int thePageSize) {
058                return new FindPackageAssetRequest(
059                                theFhirVersion, theCanonicalUrl, thePackageId, theVersion, PageRequest.of(thePageNumber, thePageSize));
060        }
061
062        public static FindPackageAssetRequest noVersion(
063                        FhirVersionEnum theFhirVersion, String theCanonicalUrl, String thePackageId) {
064                return new FindPackageAssetRequest(theFhirVersion, theCanonicalUrl, thePackageId, null, null);
065        }
066
067        public static FindPackageAssetRequest noVersion(
068                        FhirVersionEnum theFhirVersion,
069                        String theCanonicalUrl,
070                        String thePackageId,
071                        int thePageNumber,
072                        int thePageSize) {
073                return new FindPackageAssetRequest(
074                                theFhirVersion, theCanonicalUrl, thePackageId, null, PageRequest.of(thePageNumber, thePageSize));
075        }
076
077        private FindPackageAssetRequest(
078                        FhirVersionEnum theFhirVersion,
079                        String theCanonicalUrl,
080                        String thePackageId,
081                        @Nullable String theVersion,
082                        @Nullable PageRequest thePageRequest) {
083                this.myFhirVersion = theFhirVersion;
084                this.myCanonicalUrl = theCanonicalUrl;
085                this.myPackageId = thePackageId;
086                this.myVersion = theVersion;
087                this.myPageRequest =
088                                Optional.ofNullable(thePageRequest).orElse(PageRequest.of(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE));
089        }
090
091        public FhirVersionEnum getFhirVersion() {
092                return myFhirVersion;
093        }
094
095        public String getCanonicalUrl() {
096                return myCanonicalUrl;
097        }
098
099        public String getPackageId() {
100                return myPackageId;
101        }
102
103        @Nullable
104        public String getVersion() {
105                return myVersion;
106        }
107
108        public PageRequest getPageRequest() {
109                return myPageRequest;
110        }
111
112        @Override
113        public boolean equals(Object object) {
114                if (object == null || getClass() != object.getClass()) {
115                        return false;
116                }
117                FindPackageAssetRequest that = (FindPackageAssetRequest) object;
118                return myFhirVersion == that.myFhirVersion
119                                && Objects.equals(myCanonicalUrl, that.myCanonicalUrl)
120                                && Objects.equals(myPackageId, that.myPackageId)
121                                && Objects.equals(myVersion, that.myVersion);
122        }
123
124        @Override
125        public int hashCode() {
126                return Objects.hash(myFhirVersion, myCanonicalUrl, myPackageId, myVersion);
127        }
128
129        @Override
130        public String toString() {
131                return new StringJoiner(", ", FindPackageAssetRequest.class.getSimpleName() + "[", "]")
132                                .add("myFhirVersion=" + myFhirVersion)
133                                .add("myCanonicalUrl='" + myCanonicalUrl + "'")
134                                .add("myPackageId='" + myPackageId + "'")
135                                .add("myPageRequest=" + myPageRequest)
136                                .add("myVersion='" + myVersion + "'")
137                                .toString();
138        }
139}