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