001/*-
002 * #%L
003 * HAPI FHIR Storage api
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.subscription.model;
021
022import ca.uhn.fhir.i18n.Msg;
023import jakarta.annotation.Nonnull;
024import jakarta.annotation.Nullable;
025import org.hl7.fhir.dstu2.model.Subscription;
026import org.hl7.fhir.exceptions.FHIRException;
027import org.hl7.fhir.r5.model.Coding;
028
029import static org.apache.commons.lang3.StringUtils.isBlank;
030
031public enum CanonicalSubscriptionChannelType {
032        /**
033         * The channel is executed by making a post to the URI. If a payload is included, the URL is interpreted as the service base, and an update (PUT) is made.
034         */
035        RESTHOOK,
036        /**
037         * The channel is executed by sending a packet across a web socket connection maintained by the client. The URL identifies the websocket, and the client binds to this URL.
038         */
039        WEBSOCKET,
040        /**
041         * The channel is executed by sending an email to the email addressed in the URI (which must be a mailto:).
042         */
043        EMAIL,
044        /**
045         * The channel is executed by sending an SMS message to the phone number identified in the URL (tel:).
046         */
047        SMS,
048        /**
049         * The channel is executed by sending a message (e.g. a Bundle with a MessageHeader resource etc.) to the application identified in the URI.
050         */
051        MESSAGE,
052        /**
053         * added to help the parsers with the generic types
054         */
055        NULL;
056
057        public static CanonicalSubscriptionChannelType fromCode(@Nullable String theSystem, @Nonnull String codeString)
058                        throws FHIRException {
059                if (isBlank(codeString)) {
060                        return null;
061                } else if ("rest-hook".equals(codeString)) {
062                        if (theSystem == null
063                                        || theSystem.equals("http://terminology.hl7.org/CodeSystem/subscription-channel-type")) {
064                                return RESTHOOK;
065                        }
066                } else if ("websocket".equals(codeString)) {
067                        if (theSystem == null
068                                        || theSystem.equals("http://terminology.hl7.org/CodeSystem/subscription-channel-type")) {
069                                return WEBSOCKET;
070                        }
071                } else if ("email".equals(codeString)) {
072                        if (theSystem == null
073                                        || theSystem.equals("http://terminology.hl7.org/CodeSystem/subscription-channel-type")) {
074                                return EMAIL;
075                        }
076                } else if ("sms".equals(codeString)) {
077                        if (theSystem == null
078                                        || theSystem.equals("http://terminology.hl7.org/CodeSystem/subscription-channel-type")) {
079                                return SMS;
080                        }
081                } else if ("message".equals(codeString)) {
082                        if (theSystem == null
083                                        || theSystem.equals("http://terminology.hl7.org/CodeSystem/subscription-channel-type")) {
084                                return MESSAGE;
085                        }
086                }
087
088                throw new FHIRException(Msg.code(569) + "Unknown SubscriptionChannelType code '" + codeString + "'");
089        }
090
091        public String toCode() {
092                switch (this) {
093                        case RESTHOOK:
094                                return "rest-hook";
095                        case WEBSOCKET:
096                                return "websocket";
097                        case EMAIL:
098                                return "email";
099                        case SMS:
100                                return "sms";
101                        case MESSAGE:
102                                return "message";
103                        case NULL:
104                        default:
105                                return "?";
106                }
107        }
108
109        public String getSystem() {
110                switch (this) {
111                        case RESTHOOK:
112                        case WEBSOCKET:
113                        case EMAIL:
114                        case SMS:
115                        case MESSAGE:
116                                return "http://terminology.hl7.org/CodeSystem/subscription-channel-type";
117                        case NULL:
118                        default:
119                                return "?";
120                }
121        }
122
123        public String getDefinition() {
124                switch (this) {
125                        case RESTHOOK:
126                                return "The channel is executed by making a post to the URI. If a payload is included, the URL is interpreted as the service base, and an update (PUT) is made.";
127                        case WEBSOCKET:
128                                return "The channel is executed by sending a packet across a web socket connection maintained by the client. The URL identifies the websocket, and the client binds to this URL.";
129                        case EMAIL:
130                                return "The channel is executed by sending an email to the email addressed in the URI (which must be a mailto:).";
131                        case SMS:
132                                return "The channel is executed by sending an SMS message to the phone number identified in the URL (tel:).";
133                        case MESSAGE:
134                                return "The channel is executed by sending a message (e.g. a Bundle with a MessageHeader resource etc.) to the application identified in the URI.";
135                        case NULL:
136                        default:
137                                return "?";
138                }
139        }
140
141        public String getDisplay() {
142                switch (this) {
143                        case RESTHOOK:
144                                return "Rest Hook";
145                        case WEBSOCKET:
146                                return "Websocket";
147                        case EMAIL:
148                                return "Email";
149                        case SMS:
150                                return "SMS";
151                        case MESSAGE:
152                                return "Message";
153                        case NULL:
154                        default:
155                                return "?";
156                }
157        }
158
159        public Subscription.SubscriptionChannelType toCanonical() {
160                return Subscription.SubscriptionChannelType.fromCode(toCode());
161        }
162
163        public Coding toR5Coding() {
164                return new Coding().setSystem(getSystem()).setCode(toCode());
165        }
166}