001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.verify2;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import java.util.UUID;
021import java.util.regex.Pattern;
022
023/**
024 * Represents a custom template.
025 *
026 * @since 8.13.0
027 */
028public final class Template extends JsonableBaseObject {
029        private static final Pattern NAME_REGEX = Pattern.compile("^[a-zA-Z0-9_\\-]+$");
030        private boolean isUpdateRequest;
031        @JsonProperty("name") private String name;
032        @JsonProperty("is_default") private Boolean isDefault;
033        @JsonProperty("template_id") UUID id;
034
035        private Template() {}
036
037        Template(String name, Boolean isDefault, UUID id) {
038                if ((this.name = name) != null && !NAME_REGEX.matcher(name).matches()) {
039                        throw new IllegalArgumentException("Invalid template name. Must match pattern: "+NAME_REGEX.pattern());
040                }
041                if ((this.isDefault = isDefault) == null && name == null) {
042                        throw new IllegalArgumentException("Must provide at least one field to update.");
043                }
044                this.id = id;
045                isUpdateRequest = true;
046        }
047
048        /**
049         * Reference name for the template.
050         *
051         * @return The template name.
052         */
053        @JsonProperty("name")
054        public String getName() {
055                return name;
056        }
057
058        /**
059         * Unique template identifier.
060         *
061         * @return The template ID, or {@code null} if this is a request object.
062         */
063        @JsonProperty("template_id")
064        public UUID getId() {
065                return isUpdateRequest ? null : id;
066        }
067
068        /**
069         * Whether this is the default template.
070         *
071         * @return {@code true} if this is the default template, or {@code null} if this is a request object.
072         */
073        @JsonProperty("is_default")
074        public Boolean isDefault() {
075                return isDefault;
076        }
077
078        @Override
079        public void updateFromJson(String json) {
080                isUpdateRequest = false;
081                super.updateFromJson(json);
082        }
083}