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
023import com.fasterxml.jackson.annotation.JsonAutoDetect;
024import com.fasterxml.jackson.annotation.JsonInclude;
025import com.fasterxml.jackson.annotation.JsonProperty;
026import io.swagger.v3.oas.annotations.media.Schema;
027
028import java.util.ArrayList;
029import java.util.List;
030
031@Schema(description = "Represents an NPM package search response")
032@JsonInclude(JsonInclude.Include.NON_NULL)
033@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
034public class NpmPackageSearchResultJson {
035
036        @JsonProperty("objects")
037        private List<ObjectElement> myObjects;
038        @JsonProperty("total")
039        private int myTotal;
040
041        public List<ObjectElement> getObjects() {
042                if (myObjects == null) {
043                        myObjects = new ArrayList<>();
044                }
045                return myObjects;
046        }
047
048        public ObjectElement addObject() {
049                ObjectElement object = new ObjectElement();
050                getObjects().add(object);
051                return object;
052        }
053
054        public int getTotal() {
055                return myTotal;
056        }
057
058        public void setTotal(int theTotal) {
059                myTotal = theTotal;
060        }
061
062        public boolean hasPackageWithId(String thePackageId) {
063                return getObjects().stream().anyMatch(t -> t.getPackage().getName().equals(thePackageId));
064        }
065
066        public Package getPackageWithId(String thePackageId) {
067                return getObjects().stream().map(t -> t.getPackage()).filter(t -> t.getName().equals(thePackageId)).findFirst().orElseThrow(() -> new IllegalArgumentException());
068        }
069
070        @JsonInclude(JsonInclude.Include.NON_NULL)
071        @JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
072        public static class ObjectElement {
073
074                @JsonProperty("package")
075                private Package myPackage;
076
077                public Package getPackage() {
078                        if (myPackage == null) {
079                                myPackage = new Package();
080                        }
081                        return myPackage;
082                }
083        }
084
085        @JsonInclude(JsonInclude.Include.NON_NULL)
086        @JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
087        public static class Package {
088
089                @JsonProperty("name")
090                private String myName;
091                @JsonProperty("version")
092                private String myVersion;
093                @JsonProperty("description")
094                private String myDescription;
095                @JsonProperty("fhirVersion")
096                private List<String> myFhirVersion;
097                @Schema(description = "The size of this package in bytes", example = "1000")
098                @JsonProperty("_bytes")
099                private long myBytes;
100
101                public long getBytes() {
102                        return myBytes;
103                }
104
105                public Package setBytes(long theBytes) {
106                        myBytes = theBytes;
107                        return this;
108                }
109
110                public String getName() {
111                        return myName;
112                }
113
114                public Package setName(String theName) {
115                        myName = theName;
116                        return this;
117                }
118
119                public String getDescription() {
120                        return myDescription;
121                }
122
123                public Package setDescription(String theDescription) {
124                        myDescription = theDescription;
125                        return this;
126                }
127
128                public List<String> getFhirVersion() {
129                        if (myFhirVersion == null) {
130                                myFhirVersion = new ArrayList<>();
131                        }
132                        return myFhirVersion;
133                }
134
135                public String getVersion() {
136                        return myVersion;
137                }
138
139                public Package setVersion(String theVersion) {
140                        myVersion = theVersion;
141                        return this;
142                }
143
144                public Package addFhirVersion(String theFhirVersionId) {
145                        if (!getFhirVersion().contains(theFhirVersionId)) {
146                                getFhirVersion().add(theFhirVersionId);
147                                getFhirVersion().sort(PackageVersionComparator.INSTANCE);
148                        }
149                        return this;
150                }
151        }
152}