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