001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 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        public R setResourceTypes(Set<String> theResourceTypes) {
063                myResourceTypes = theResourceTypes;
064                return (R) this;
065        }
066
067        @SuppressWarnings("unchecked")
068        public R addResourceType(String theResourceType) {
069                Validate.notBlank(theResourceType);
070                getResourceTypes().add(theResourceType);
071                return (R) this;
072        }
073
074        public int getTargetIndex(String theTargetId) {
075                return Iterables.indexOf(getTargets(), target -> target.getTargetId().equals(theTargetId));
076        }
077
078        @Override
079        public List<GatewayRouteTargetJson> getTargets() {
080                if (myTargets == null) {
081                        myTargets = new ArrayList<>();
082                }
083                return myTargets;
084        }
085
086        public void setTargets(List<GatewayRouteTargetJson> theTargets) {
087                myTargets = theTargets;
088        }
089
090        public R addTarget(GatewayRouteTargetJson theTarget) {
091                Validate.notNull(theTarget);
092                getTargets().add(theTarget);
093                return (R) this;
094        }
095
096        @Override
097        public boolean isParallel() {
098                return myParallel;
099        }
100
101        public R setParallel(boolean theParallel) {
102                myParallel = theParallel;
103                return (R) this;
104        }
105
106        public R clone() throws CloneNotSupportedException {
107                List<GatewayRouteTargetJson> clonedTargets = new ArrayList<>();
108                for (GatewayRouteTargetJson target : myTargets) {
109                        Object clone1 = target.clone();
110                        clonedTargets.add((GatewayRouteTargetJson) clone1);
111                }
112                R clone = (R) super.clone();
113                clone.setTargets(clonedTargets);
114                return clone;
115        }
116}