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.packages;
021
022import ca.uhn.fhir.model.api.annotation.ExampleSupplier;
023import com.fasterxml.jackson.annotation.JsonAutoDetect;
024import com.fasterxml.jackson.annotation.JsonIgnore;
025import com.fasterxml.jackson.annotation.JsonInclude;
026import com.fasterxml.jackson.annotation.JsonProperty;
027import com.fasterxml.jackson.annotation.JsonPropertyOrder;
028import io.swagger.v3.oas.annotations.media.Schema;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.function.Supplier;
033
034@Schema(name = "PackageInstallationSpec", description = "Defines a set of instructions for package installation")
035@JsonPropertyOrder({
036        "name",
037        "version",
038        "packageUrl",
039        "installMode",
040        "installResourceTypes",
041        "validationMode",
042        "reloadExisting"
043})
044@ExampleSupplier({PackageInstallationSpec.ExampleSupplier.class, PackageInstallationSpec.ExampleSupplier2.class})
045@JsonInclude(JsonInclude.Include.NON_NULL)
046@JsonAutoDetect(
047                creatorVisibility = JsonAutoDetect.Visibility.NONE,
048                fieldVisibility = JsonAutoDetect.Visibility.NONE,
049                getterVisibility = JsonAutoDetect.Visibility.NONE,
050                isGetterVisibility = JsonAutoDetect.Visibility.NONE,
051                setterVisibility = JsonAutoDetect.Visibility.NONE)
052public class PackageInstallationSpec {
053
054        @Schema(description = "The direct package URL")
055        @JsonProperty("packageUrl")
056        private String myPackageUrl;
057
058        @Schema(description = "The NPM package Name")
059        @JsonProperty("name")
060        private String myName;
061
062        @Schema(description = "The direct package version")
063        @JsonProperty("version")
064        private String myVersion;
065
066        @Schema(
067                        description =
068                                        "Should resources from this package be extracted from the package and installed into the repository individually")
069        @JsonProperty("installMode")
070        private InstallModeEnum myInstallMode;
071
072        @Schema(
073                        description =
074                                        "If resources are being installed individually, this is list provides the resource types to install. By default, all conformance resources will be installed.")
075        @JsonProperty("installResourceTypes")
076        private List<String> myInstallResourceTypes;
077
078        @Schema(description = "Should dependencies be automatically resolved, fetched and installed with the same settings")
079        @JsonProperty("fetchDependencies")
080        private boolean myFetchDependencies;
081
082        @Schema(
083                        description =
084                                        "Should existing resources be reloaded. Defaults to true, but can be set to false to avoid re-index operations for existing search parameters")
085        @JsonProperty("reloadExisting")
086        private boolean myReloadExisting = true;
087
088        @Schema(
089                        description =
090                                        "Any values provided here will be interpreted as a regex. Dependencies with an ID matching any regex will be skipped.")
091        private List<String> myDependencyExcludes;
092
093        @JsonIgnore
094        private byte[] myPackageContents;
095
096        public List<String> getDependencyExcludes() {
097                if (myDependencyExcludes == null) {
098                        myDependencyExcludes = new ArrayList<>();
099                }
100                return myDependencyExcludes;
101        }
102
103        public void setDependencyExcludes(List<String> theDependencyExcludes) {
104                myDependencyExcludes = theDependencyExcludes;
105        }
106
107        public boolean isFetchDependencies() {
108                return myFetchDependencies;
109        }
110
111        public PackageInstallationSpec setFetchDependencies(boolean theFetchDependencies) {
112                myFetchDependencies = theFetchDependencies;
113                return this;
114        }
115
116        public String getPackageUrl() {
117                return myPackageUrl;
118        }
119
120        public PackageInstallationSpec setPackageUrl(String thePackageUrl) {
121                myPackageUrl = thePackageUrl;
122                return this;
123        }
124
125        public InstallModeEnum getInstallMode() {
126                return myInstallMode;
127        }
128
129        public PackageInstallationSpec setInstallMode(InstallModeEnum theInstallMode) {
130                myInstallMode = theInstallMode;
131                return this;
132        }
133
134        public List<String> getInstallResourceTypes() {
135                if (myInstallResourceTypes == null) {
136                        myInstallResourceTypes = new ArrayList<>();
137                }
138                return myInstallResourceTypes;
139        }
140
141        public void setInstallResourceTypes(List<String> theInstallResourceTypes) {
142                myInstallResourceTypes = theInstallResourceTypes;
143        }
144
145        public String getName() {
146                return myName;
147        }
148
149        public PackageInstallationSpec setName(String theName) {
150                myName = theName;
151                return this;
152        }
153
154        public String getVersion() {
155                return myVersion;
156        }
157
158        public PackageInstallationSpec setVersion(String theVersion) {
159                myVersion = theVersion;
160                return this;
161        }
162
163        public byte[] getPackageContents() {
164                return myPackageContents;
165        }
166
167        public PackageInstallationSpec setPackageContents(byte[] thePackageContents) {
168                myPackageContents = thePackageContents;
169                return this;
170        }
171
172        public boolean isReloadExisting() {
173                return myReloadExisting;
174        }
175
176        public void setReloadExisting(boolean reloadExisting) {
177                this.myReloadExisting = reloadExisting;
178        }
179
180        public PackageInstallationSpec addDependencyExclude(String theExclude) {
181                getDependencyExcludes().add(theExclude);
182                return this;
183        }
184
185        public PackageInstallationSpec addInstallResourceTypes(String... theResourceTypes) {
186                for (String next : theResourceTypes) {
187                        getInstallResourceTypes().add(next);
188                }
189                return this;
190        }
191
192        public enum InstallModeEnum {
193                STORE_ONLY,
194                STORE_AND_INSTALL
195        }
196
197        public enum ValidationModeEnum {
198                NOT_AVAILABLE,
199                AVAILABLE
200        }
201
202        public static class ExampleSupplier implements Supplier<PackageInstallationSpec> {
203
204                @Override
205                public PackageInstallationSpec get() {
206                        return new PackageInstallationSpec()
207                                        .setName("hl7.fhir.us.core")
208                                        .setVersion("3.1.0")
209                                        .setInstallMode(InstallModeEnum.STORE_ONLY)
210                                        .setFetchDependencies(true);
211                }
212        }
213
214        public static class ExampleSupplier2 implements Supplier<PackageInstallationSpec> {
215
216                @Override
217                public PackageInstallationSpec get() {
218                        return new PackageInstallationSpec()
219                                        .setName("com.example.my-resources")
220                                        .setVersion("1.0")
221                                        .setPackageUrl("classpath:/my-resources.tgz")
222                                        .setInstallMode(InstallModeEnum.STORE_AND_INSTALL)
223                                        .addInstallResourceTypes("Organization", "Medication", "PlanDefinition", "SearchParameter");
224                }
225        }
226}