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.users;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import com.fasterxml.jackson.annotation.JsonProperty;
020import com.vonage.client.Jsonable;
021import com.vonage.client.JsonableBaseObject;
022import com.vonage.client.users.channels.Channel;
023import com.vonage.client.users.channels.Channels;
024import java.net.URI;
025import java.util.Arrays;
026import java.util.Collection;
027import java.util.Map;
028
029/**
030 * Represents a Vonage User (both request and response).
031 */
032public class User extends BaseUser {
033    @JsonProperty("display_name") private String displayName;
034    @JsonProperty("image_url") private URI imageUrl;
035    @JsonProperty("channels") private Channels channels;
036    @JsonProperty("properties") private Properties properties;
037    @JsonProperty("custom_data") private Map<String, ?> customData;
038
039    protected User() {
040    }
041
042    protected User(Builder builder) {
043        name = builder.name;
044        displayName = builder.displayName;
045        imageUrl = builder.imageUrl;
046        channels = builder.channels;
047        if (builder.customData != null) {
048            properties = new Properties();
049            properties.customData = builder.customData;
050        }
051    }
052
053    /**
054     * A string to be displayed as username. It does not need to be unique.
055     *
056     * @return The user's friendly name.
057     */
058    public String getDisplayName() {
059        return displayName;
060    }
061
062    /**
063     * An image URL to associate with the user.
064     *
065     * @return The image URL, or {@code null} if not specified.
066     */
067    public URI getImageUrl() {
068        return imageUrl;
069    }
070
071    /**
072     * Custom key/value pairs to associate with the user.
073     *
074     * @return The custom data as a Map, or {@code null} if not specified.
075     */
076    @JsonIgnore
077    public Map<String, ?> getCustomData() {
078        return properties != null ? properties.customData : customData;
079    }
080
081    /**
082     * The communication channels available to the user.
083     *
084     * @return The channels object, or {@code null} if unknown.
085     */
086    public Channels getChannels() {
087        return channels;
088    }
089
090    /**
091     * Constructs a user from the JSON payload.
092     *
093     * @param json The JSON structure containing the fields of this class.
094     *
095     * @return A new User instance.
096     */
097    public static User fromJson(String json) {
098        return Jsonable.fromJson(json);
099    }
100
101    /**
102     * Entry point for creating an instance of this class.
103     * 
104     * @return A new Builder.
105     */
106    public static Builder builder() {
107        return new Builder();
108    }
109
110    public static class Builder {
111        private String name, displayName;
112        private URI imageUrl;
113        private Map<String, ?> customData;
114        private Channels channels;
115
116        Builder() {}
117
118        /**
119         * Unique name for a user.
120         *
121         * @param name The user's name.
122         *
123         * @return This builder.
124         */
125        public Builder name(String name) {
126            this.name = name;
127            return this;
128        }
129
130        /**
131         * Set the display name for the user. This does nbot need to be unique.
132         *
133         * @param displayName The user's display name.
134         *
135         * @return This builder.
136         */
137        public Builder displayName(String displayName) {
138            this.displayName = displayName;
139            return this;
140        }
141
142        /**
143         * An image URL to associate with the user.
144         *
145         * @param imageUrl The image URL as a string.
146         *
147         * @return This builder.
148         */
149        public Builder imageUrl(String imageUrl) {
150            this.imageUrl = URI.create(imageUrl);
151            return this;
152        }
153
154        /**
155         * Arbitrary freeform data to associate with the user. Note that this is
156         * not additive: the value set here will replace any existing custom data when updating a user.
157         *
158         * @param customData Custom key/value pairs map.
159         *
160         * @return This builder.
161         */
162        public Builder customData(Map<String, ?> customData) {
163            this.customData = customData;
164            return this;
165        }
166
167        /**
168         * Sets the communication channels for this user.
169         *
170         * @param channels The channels to associate with the user.
171         *
172         * @return This builder.
173         */
174        public Builder channels(Channel... channels) {
175            return channels(Arrays.asList(channels));
176        }
177
178        /**
179         * Sets the communication channels for this user.
180         *
181         * @param channels The collection of channels to associate with the user.
182         *
183         * @return This builder.
184         */
185        public Builder channels(Collection<? extends Channel> channels) {
186            this.channels = new Channels(channels);
187            return this;
188        }
189
190        /**
191         * Builds the Application object.
192         *
193         * @return A new Application containing the configured properties.
194         */
195        public User build() {
196            return new User(this);
197        }
198    }
199
200    /**
201     * Represents the "properties" field of a User object.
202     */
203    static class Properties extends JsonableBaseObject {
204        @JsonProperty("custom_data") Map<String, ?> customData;
205    }
206}