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.JsonIgnore;
019import com.fasterxml.jackson.annotation.JsonProperty;
020import com.fasterxml.jackson.databind.annotation.JsonSerialize;
021import com.fasterxml.jackson.databind.util.StdConverter;
022import com.vonage.client.JsonableBaseObject;
023import java.time.Instant;
024import java.util.Locale;
025import java.util.Objects;
026import java.util.UUID;
027
028/**
029 * Represents a custom template fragment.
030 *
031 * @since 8.13.0
032 */
033public final class TemplateFragment extends JsonableBaseObject {
034        @JsonIgnore private boolean isUpdateRequest;
035        private String text;
036        private Locale locale;
037        private FragmentChannel channel;
038        private Instant dateCreated, dateUpdated;
039        UUID fragmentId, templateId;
040
041        TemplateFragment() {}
042
043        TemplateFragment(String text, UUID templateId, UUID fragmentId) {
044                this.text = Objects.requireNonNull(text, "Fragment text is required.");
045                this.templateId = templateId;
046                this.fragmentId = fragmentId;
047                isUpdateRequest = true;
048        }
049
050        /**
051         * Create a new template fragment. All parameters are required.
052         *
053         * @param channel Channel type for the template.
054         * @param locale BCP-47 locale for the template.
055         * @param text Text content of the template. There are 4 reserved variables available to use:
056         *             {@code ${code}}, {@code ${brand}}, {@code ${time-limit}} and {@code ${time-limit-unit}}.
057         */
058        public TemplateFragment(FragmentChannel channel, String locale, String text) {
059                this(text, null, null);
060                this.channel = Objects.requireNonNull(channel, "Channel is required.");
061                this.locale = Locale.forLanguageTag(Objects.requireNonNull(locale, "Locale is required."));
062        }
063
064        /**
065         * Text content of the template.
066         *
067         * @return The template fragment text.
068         */
069        @JsonProperty("text")
070        public String getText() {
071                return text;
072        }
073
074        /**
075         * Template Locale in IETF BCP 47 format.
076         *
077         * @return The locale as an object, or {@code null} if this is an update request.
078         */
079        @JsonSerialize(converter = LocaleSerializer.class)
080        @JsonProperty("locale")
081        public Locale getLocale() {
082                return locale;
083        }
084
085        /**
086         * Channel type for the template.
087         *
088         * @return The channel as an enum, or {@code null} if this is an update request.
089         */
090        @JsonProperty("channel")
091        public FragmentChannel getChannel() {
092                return channel;
093        }
094
095        /**
096         * Unique fragment identifier.
097         *
098         * @return The template fragment ID, or {@code null} if this is a request object.
099         */
100        @JsonProperty("template_fragment_id")
101        public UUID getFragmentId() {
102                return isUpdateRequest ? null : fragmentId;
103        }
104
105        /**
106         * Unique identifier of the template this fragment belongs to.
107         *
108         * @return The parent template's ID, or {@code null} if unknown.
109         */
110        @JsonIgnore
111        public UUID getTemplateId() {
112                return templateId;
113        }
114
115        /**
116         * Timestamp of when the template fragment was created.
117         *
118         * @return The creation date-time in ISO-8601 format, or {@code null} if this is a request object.
119         */
120        @JsonProperty("date_created")
121        public Instant getDateCreated() {
122                return dateCreated;
123        }
124
125        /**
126         * Timestamp of when the template fragment was last updated.
127         *
128         * @return The latest update time in ISO-8601 format, or {@code null} if this is a request object.
129         */
130        @JsonProperty("date_updated")
131        public Instant getDateUpdated() {
132                return dateUpdated;
133        }
134
135        @Override
136        public void updateFromJson(String json) {
137                isUpdateRequest = false;
138                super.updateFromJson(json);
139        }
140
141        private static final class LocaleSerializer extends StdConverter<Locale, String> {
142
143                @Override
144                public String convert(Locale value) {
145                        return value == null ? null : value.toString().toLowerCase().replace('_', '-');
146                }
147        }
148}