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.proactiveconnect;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.Jsonable;
020import com.vonage.client.JsonableBaseObject;
021import java.time.Instant;
022import java.util.Arrays;
023import java.util.List;
024import java.util.UUID;
025
026/**
027 * Represents a Proactive Connect list.
028 */
029public class ContactsList extends JsonableBaseObject {
030        UUID id;
031        private String name, description;
032        private List<String> tags;
033        private List<ListAttribute> attributes;
034        private Datasource datasource;
035        private Instant createdAt, updatedAt;
036        private Integer itemsCount;
037        private SyncStatus syncStatus;
038
039        ContactsList() {
040        }
041
042        ContactsList(Builder builder) {
043                if ((name = builder.name) == null || name.trim().isEmpty()) {
044                        throw new IllegalArgumentException("List name is required.");
045                }
046                if ((tags = builder.tags) != null) {
047                        if (tags.size() > 10) {
048                                throw new IllegalStateException("Too many tags provided. Maximum is 10.");
049                        }
050                        for (String tag : tags) {
051                                if (tag == null || tag.trim().isEmpty()) {
052                                        throw new IllegalArgumentException("Tag cannot be null or blank.");
053                                }
054                                if (tag.length() > 15) {
055                                        throw new IllegalArgumentException("Tag cannot be longer than 15 characters.");
056                                }
057                        }
058                }
059                description = builder.description;
060                attributes = builder.attributes;
061                datasource = builder.datasource;
062        }
063
064        /**
065         * The name of the resource (max 255 characters).
066         *
067         * @return Resource name or {@code null} if unknown.
068         */
069        @JsonProperty("name")
070        public String getName() {
071                return name;
072        }
073
074        /**
075         * The description of the resource (max 1024 characters).
076         *
077         * @return Resource description or {@code null} if unknown.
078         */
079        @JsonProperty("description")
080        public String getDescription() {
081                return description;
082        }
083
084        /**
085         * Up to 10 custom strings assigned with a resource - each must be between 1 and 15 characters.
086         *
087         * @return The list of tags or {@code null} if unknown.
088         */
089        @JsonProperty("tags")
090        public List<String> getTags() {
091                return tags;
092        }
093
094        /**
095         * The list attributes.
096         *
097         * @return The list's attributes or {@code null} if unknown.
098         */
099        @JsonProperty("attributes")
100        public List<ListAttribute> getAttributes() {
101                return attributes;
102        }
103
104        /**
105         * The list's datasource.
106         *
107         * @return The datasource or {@code null} if unknown.
108         */
109        @JsonProperty("datasource")
110        public Datasource getDatasource() {
111                return datasource;
112        }
113
114        /**
115         * The creation timestamp in ISO 8601 format.
116         *
117         * @return The creation time or {@code null} if unknown.
118         */
119        @JsonProperty("created_at")
120        public Instant getCreatedAt() {
121                return createdAt;
122        }
123
124        /**
125         * The last update timestamp in ISO 8601 format.
126         *
127         * @return The last updated time or {@code null} if unknown.
128         */
129        @JsonProperty("updated_at")
130        public Instant getUpdatedAt() {
131                return updatedAt;
132        }
133
134        /**
135         * The total number of list items.
136         *
137         * @return The number of items in the list or {@code null} if unknown.
138         */
139        @JsonProperty("items_count")
140        public Integer getItemsCount() {
141                return itemsCount;
142        }
143
144        /**
145         * Unique identifier for this list.
146         *
147         * @return The list ID or {@code null} if unknown.
148         */
149        @JsonProperty("id")
150        public UUID getId() {
151                return id;
152        }
153
154        /**
155         * Synchronization status between the list content (items) and its datasource.
156         *
157         * @return The synchronisation status, or {@code null} if unknown.
158         */
159        @JsonProperty("sync_status")
160        public SyncStatus getSyncStatus() {
161                return syncStatus;
162        }
163
164        /**
165         * Creates an instance of this class from a JSON payload.
166         *
167         * @param json The JSON string to parse.
168         * @return An instance of this class with the fields populated, if present.
169         */
170        public static ContactsList fromJson(String json) {
171                return Jsonable.fromJson(json);
172        }
173        
174        /**
175         * Entry point for constructing an instance of this class.
176         *
177         * @param name The name of the resource (max 255 characters).
178         *
179         * @return A new Builder.
180         */
181        public static Builder builder(String name) {
182                return new Builder(name);
183        }
184        
185        public static class Builder {
186                private final String name;
187                private String description;
188                private List<String> tags;
189                private List<ListAttribute> attributes;
190                private Datasource datasource;
191        
192                Builder(String name) {
193                        this.name = name;
194                }
195
196                /**
197                 * Sets the resource description.
198                 *
199                 * @param description The description of the resource (max 1024 characters).
200                 *
201                 * @return This builder.
202                 */
203                public Builder description(String description) {
204                        this.description = description;
205                        return this;
206                }
207
208                /**
209                 * Sets the list's tags.
210                 *
211                 * @param tags Up to 10 custom strings assigned with a resource - each must be between 1 and 15 characters.
212                 *
213                 * @return This builder.
214                 * @see #tags(String...)
215                 */
216                public Builder tags(List<String> tags) {
217                        this.tags = tags;
218                        return this;
219                }
220
221                /**
222                 * Sets the tags.
223                 *
224                 * @param tags Up to 10 custom strings assigned with a resource - each must be between 1 and 15 characters.
225                 *
226                 * @return This builder.
227                 * @see #tags(List)
228                 */
229                public Builder tags(String... tags) {
230                        return tags(Arrays.asList(tags));
231                }
232
233                /**
234                 * Sets the list attributes.
235                 *
236                 * @param attributes The list attributes as an array (or varargs) of {@code ListAttribute}s.
237                 *
238                 * @return This builder.
239                 */
240                public Builder attributes(ListAttribute... attributes) {
241                        return attributes(Arrays.asList(attributes));
242                }
243
244                /**
245                 * Sets the list attributes.
246                 *
247                 * @param attributes The list of attributes.
248                 *
249                 * @return This builder.
250                 */
251                public Builder attributes(List<ListAttribute> attributes) {
252                        this.attributes = attributes;
253                        return this;
254                }
255
256                /**
257                 * Sets the datasource.
258                 *
259                 * @param datasource The datasource type.
260                 *
261                 * @return This builder.
262                 */
263                public Builder datasource(Datasource datasource) {
264                        this.datasource = datasource;
265                        return this;
266                }
267
268        
269                /**
270                 * Builds the {@linkplain ContactsList}.
271                 *
272                 * @return An instance of ContactsList, populated with all fields from this builder.
273                 */
274                public ContactsList build() {
275                        return new ContactsList(this);
276                }
277        }
278}