001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2026 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.fhirgw.json;
011
012import com.fasterxml.jackson.annotation.JsonProperty;
013import com.google.common.collect.Iterables;
014import io.swagger.v3.oas.annotations.media.Schema;
015import org.apache.commons.lang3.Validate;
016
017import java.util.ArrayList;
018import java.util.HashSet;
019import java.util.List;
020import java.util.Set;
021
022public abstract class BaseRouteJson<R extends IBaseRouteJson> implements IBaseRouteJson, Cloneable {
023        @JsonProperty("id")
024        @Schema(description = "A unique ID for this route")
025        private String myId;
026
027        @JsonProperty("resourceTypes")
028        @Schema(description = "The resource type (e.g. 'Patient') that this route applies to")
029        private Set<String> myResourceTypes;
030
031        @JsonProperty("targets")
032        @Schema(description = "A list of gateway target server IDs that this route should direct operations to")
033        private List<GatewayRouteTargetJson> myTargets;
034
035        @JsonProperty("parallel")
036        @Schema(
037                        description =
038                                        "If the search needs to invoke multiple target endpoints, should the calls be invoked in parallel (i.e. multi-threaded)")
039        private boolean myParallel;
040
041        @Override
042        public String getId() {
043                return myId;
044        }
045
046        @SuppressWarnings("unchecked")
047        public R setId(String theId) {
048                myId = theId;
049                return (R) this;
050        }
051
052        @Override
053        public Set<String> getResourceTypes() {
054                if (myResourceTypes == null) {
055                        myResourceTypes = new HashSet<>();
056                }
057                return myResourceTypes;
058        }
059
060        public BaseRouteJson() {}
061
062        @SuppressWarnings("unchecked")
063        public R setResourceTypes(Set<String> theResourceTypes) {
064                myResourceTypes = theResourceTypes;
065                return (R) this;
066        }
067
068        @SuppressWarnings("unchecked")
069        public R addResourceType(String theResourceType) {
070                Validate.notBlank(theResourceType);
071                getResourceTypes().add(theResourceType);
072                return (R) this;
073        }
074
075        public int getTargetIndex(String theTargetId) {
076                return Iterables.indexOf(getTargets(), target -> target.getTargetId().equals(theTargetId));
077        }
078
079        @Override
080        public List<GatewayRouteTargetJson> getTargets() {
081                if (myTargets == null) {
082                        myTargets = new ArrayList<>();
083                }
084                return myTargets;
085        }
086
087        @Override
088        public void setTargets(List<GatewayRouteTargetJson> theTargets) {
089                myTargets = theTargets;
090        }
091
092        public R addTarget(GatewayRouteTargetJson theTarget) {
093                Validate.notNull(theTarget, "theTarget cannot be null");
094                getTargets().add(theTarget);
095                return (R) this;
096        }
097
098        @Override
099        public boolean isParallel() {
100                return myParallel;
101        }
102
103        public R setParallel(boolean theParallel) {
104                myParallel = theParallel;
105                return (R) this;
106        }
107
108        @SuppressWarnings("unchecked")
109        @Override
110        public R clone() throws CloneNotSupportedException {
111                List<GatewayRouteTargetJson> clonedTargets = new ArrayList<>();
112                for (GatewayRouteTargetJson target : myTargets) {
113                        Object clone1 = target.clone();
114                        clonedTargets.add((GatewayRouteTargetJson) clone1);
115                }
116                R clone = (R) super.clone();
117                clone.setTargets(clonedTargets);
118                return clone;
119        }
120}