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.voice;
017
018import com.vonage.client.QueryParamsRequest;
019import java.time.format.DateTimeFormatter;
020import java.util.Date;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024/**
025 * Filter options for {@link VoiceClient#listCalls(CallsFilter)}.
026 */
027public class CallsFilter implements QueryParamsRequest {
028    private final CallStatus status;
029    private final Date dateStart, dateEnd;
030    private final Integer pageSize, recordIndex;
031    private final CallOrder order;
032    private final String conversationUuid;
033
034    private CallsFilter(Builder builder) {
035        this.status = builder.status;
036        this.dateStart = builder.dateStart;
037        this.dateEnd = builder.dateEnd;
038        this.pageSize = builder.pageSize;
039        this.recordIndex = builder.recordIndex;
040        this.order = builder.order;
041        this.conversationUuid = builder.conversationUuid;
042    }
043
044    public CallStatus getStatus() {
045        return status;
046    }
047
048    public Date getDateStart() {
049        return dateStart;
050    }
051
052    public Date getDateEnd() {
053        return dateEnd;
054    }
055
056    public Integer getPageSize() {
057        return pageSize;
058    }
059
060    public Integer getRecordIndex() {
061        return recordIndex;
062    }
063
064    public CallOrder getOrder() {
065        return order;
066    }
067
068    public String getConversationUuid() {
069        return conversationUuid;
070    }
071
072    @Override
073    public Map<String, String> makeParams() {
074        Map<String, String> params = new LinkedHashMap<>();
075        conditionalAdd(params, "status", this.status);
076        conditionalAdd(params, "date_start", this.dateStart);
077        conditionalAdd(params, "date_end", this.dateEnd);
078        conditionalAdd(params, "page_size", this.pageSize);
079        conditionalAdd(params, "record_index", this.recordIndex);
080        conditionalAdd(params, "order", (this.order != null) ? this.order.getCallOrder() : null);
081        conditionalAdd(params, "conversation_uuid", this.conversationUuid);
082        return params;
083    }
084
085    private void conditionalAdd(Map<String, String> params, String name, String value) {
086        if (value != null) {
087            params.put(name, value);
088        }
089    }
090
091    private void conditionalAdd(Map<String, String> params, String name, Date value) {
092        if (value != null) {
093            params.put(name, DateTimeFormatter.ISO_INSTANT.format(value.toInstant()));
094        }
095    }
096
097    private void conditionalAdd(Map<String, String> params, String name, Object value) {
098        if (value != null) {
099            params.put(name, value.toString());
100        }
101    }
102
103    public static Builder builder() {
104        return new Builder();
105    }
106
107    public static class Builder {
108        private CallStatus status;
109        private Date dateStart, dateEnd;
110        private Integer pageSize, recordIndex;
111        private CallOrder order;
112        private String conversationUuid;
113
114        Builder() {}
115
116        /**
117         * @param status The status of the calls to lookup.
118         *
119         * @return This Builder to keep building.
120         */
121        public Builder status(CallStatus status) {
122            this.status = status;
123            return this;
124        }
125
126        /**
127         * @param dateStart The minimum in the date range of the calls to lookup.
128         *
129         * @return This Builder to keep building.
130         */
131        public Builder dateStart(Date dateStart) {
132            this.dateStart = dateStart;
133            return this;
134        }
135
136        /**
137         * @param dateEnd The maximum in the date range of calls to lookup.
138         *
139         * @return This Builder to keep building.
140         */
141        public Builder dateEnd(Date dateEnd) {
142            this.dateEnd = dateEnd;
143            return this;
144        }
145
146        /**
147         * @param pageSize The number of calls in the response.
148         *
149         * @return This Builder to keep building.
150         */
151        public Builder pageSize(Integer pageSize) {
152            this.pageSize = pageSize;
153            return this;
154        }
155
156        /**
157         * @param recordIndex The starting index.
158         *
159         * @return This Builder to keep building.
160         */
161        public Builder recordIndex(Integer recordIndex) {
162            this.recordIndex = recordIndex;
163            return this;
164        }
165
166        /**
167         * @param order The order of the calls.
168         *
169         * @return This Builder to keep building.
170         */
171        public Builder order(CallOrder order) {
172            this.order = order;
173            return this;
174        }
175
176        /**
177         * @param conversationUuid The specific conversation to return calls for.
178         *
179         * @return This Builder to keep building.
180         */
181        public Builder conversationUuid(String conversationUuid) {
182            this.conversationUuid = conversationUuid;
183            return this;
184        }
185
186        /**
187         * Constructs an instance of {@linkplain CallsFilter}.
188         *
189         * @return A new {@link CallsFilter} object with the stored builder options.
190         */
191        public CallsFilter build() {
192            return new CallsFilter(this);
193        }
194    }
195}