001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.voice;
023
024
025import com.fasterxml.jackson.databind.util.ISO8601Utils;
026import org.apache.http.NameValuePair;
027import org.apache.http.message.BasicNameValuePair;
028
029import java.util.ArrayList;
030import java.util.Date;
031import java.util.List;
032
033public class CallsFilter {
034    private CallStatus status;
035    private Date dateStart;
036    private Date dateEnd;
037    private Integer pageSize;
038    private Integer recordIndex;
039    private CallOrder order;
040    private String conversationUuid;
041
042    private CallsFilter(Builder builder) {
043        this.status = builder.status;
044        this.dateStart = builder.dateStart;
045        this.dateEnd = builder.dateEnd;
046        this.pageSize = builder.pageSize;
047        this.recordIndex = builder.recordIndex;
048        this.order = builder.order;
049        this.conversationUuid = builder.conversationUuid;
050    }
051
052    public CallStatus getStatus() {
053        return status;
054    }
055
056    public Date getDateStart() {
057        return dateStart;
058    }
059
060    public Date getDateEnd() {
061        return dateEnd;
062    }
063
064    public Integer getPageSize() {
065        return pageSize;
066    }
067
068    public Integer getRecordIndex() {
069        return recordIndex;
070    }
071
072    public CallOrder getOrder() {
073        return order;
074    }
075
076    public String getConversationUuid() {
077        return conversationUuid;
078    }
079
080    List<NameValuePair> toUrlParams() {
081        List<NameValuePair> result = new ArrayList<>(10);
082        conditionalAdd(result, "status", this.status);
083        conditionalAdd(result, "date_start", this.dateStart);
084        conditionalAdd(result, "date_end", this.dateEnd);
085        conditionalAdd(result, "page_size", this.pageSize);
086        conditionalAdd(result, "record_index", this.recordIndex);
087        conditionalAdd(result, "order", (this.order != null) ? this.order.getCallOrder() : null);
088        conditionalAdd(result, "conversation_uuid", this.conversationUuid);
089
090        return result;
091    }
092
093    private void conditionalAdd(List<NameValuePair> params, String name, String value) {
094        if (value != null) {
095            params.add(new BasicNameValuePair(name, value));
096        }
097    }
098
099    private void conditionalAdd(List<NameValuePair> params, String name, Date value) {
100        if (value != null) {
101            params.add(new BasicNameValuePair(name, ISO8601Utils.format(value)));
102        }
103    }
104
105    private void conditionalAdd(List<NameValuePair> params, String name, Object value) {
106        if (value != null) {
107            params.add(new BasicNameValuePair(name, value.toString()));
108        }
109    }
110
111    public static Builder builder() {
112        return new Builder();
113    }
114
115    public static class Builder {
116        private CallStatus status;
117        private Date dateStart;
118        private Date dateEnd;
119        private Integer pageSize;
120        private Integer recordIndex;
121        private CallOrder order;
122        private String conversationUuid;
123
124        /**
125         * @param status The status of the calls to lookup.
126         *
127         * @return The {@link Builder} to keep building.
128         */
129        public Builder status(CallStatus status) {
130            this.status = status;
131            return this;
132        }
133
134        /**
135         * @param dateStart The minimum in the date range of the calls to lookup.
136         *
137         * @return The {@link Builder} to keep building.
138         */
139        public Builder dateStart(Date dateStart) {
140            this.dateStart = dateStart;
141            return this;
142        }
143
144        /**
145         * @param dateEnd The maximum in the date range of calls to lookup.
146         *
147         * @return The {@link Builder} to keep building.
148         */
149        public Builder dateEnd(Date dateEnd) {
150            this.dateEnd = dateEnd;
151            return this;
152        }
153
154        /**
155         * @param pageSize The number of calls in the response.
156         *
157         * @return The {@link Builder} to keep building.
158         */
159        public Builder pageSize(Integer pageSize) {
160            this.pageSize = pageSize;
161            return this;
162        }
163
164        /**
165         * @param recordIndex The starting index.
166         *
167         * @return The {@link Builder} to keep building.
168         */
169        public Builder recordIndex(Integer recordIndex) {
170            this.recordIndex = recordIndex;
171            return this;
172        }
173
174        /**
175         * @param order The order of the calls.
176         *
177         * @return The {@link Builder} to keep building.
178         */
179        public Builder order(CallOrder order) {
180            this.order = order;
181            return this;
182        }
183
184        /**
185         * @param conversationUuid The specific conversation to return calls for.
186         *
187         * @return The {@link Builder} to keep building.
188         */
189        public Builder conversationUuid(String conversationUuid) {
190            this.conversationUuid = conversationUuid;
191            return this;
192        }
193
194        /**
195         * @return A new {@link CallsFilter} object with the stored builder options.
196         */
197        public CallsFilter build() {
198            return new CallsFilter(this);
199        }
200    }
201}