001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
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.submit.interceptor.validator;
021
022import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscription;
023import ca.uhn.fhir.jpa.subscription.model.CanonicalSubscriptionChannelType;
024import jakarta.annotation.Nonnull;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import java.util.EnumMap;
029import java.util.List;
030import java.util.Map;
031
032public class SubscriptionChannelTypeValidatorFactory {
033        private static final Logger ourLog = LoggerFactory.getLogger(SubscriptionChannelTypeValidatorFactory.class);
034
035        private final Map<CanonicalSubscriptionChannelType, IChannelTypeValidator> myValidators =
036                        new EnumMap<>(CanonicalSubscriptionChannelType.class);
037
038        public SubscriptionChannelTypeValidatorFactory(@Nonnull List<IChannelTypeValidator> theValidorList) {
039                theValidorList.forEach(this::addChannelTypeValidator);
040        }
041
042        public IChannelTypeValidator getValidatorForChannelType(CanonicalSubscriptionChannelType theChannelType) {
043                return myValidators.getOrDefault(theChannelType, getNoopValidatorForChannelType(theChannelType));
044        }
045
046        public void addChannelTypeValidator(IChannelTypeValidator theValidator) {
047                myValidators.put(theValidator.getSubscriptionChannelType(), theValidator);
048        }
049
050        private IChannelTypeValidator getNoopValidatorForChannelType(CanonicalSubscriptionChannelType theChannelType) {
051                return new IChannelTypeValidator() {
052                        @Override
053                        public void validateChannelType(CanonicalSubscription theSubscription) {
054                                ourLog.debug(
055                                                "No validator for channel type {} was registered, will perform no-op validation.",
056                                                theChannelType);
057                        }
058
059                        @Override
060                        public CanonicalSubscriptionChannelType getSubscriptionChannelType() {
061                                return theChannelType;
062                        }
063                };
064        }
065}