001/*
002 * Copyright 2010-2014 Ning, Inc.
003 * Copyright 2014-2015 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017package com.ning.billing.recurly;
018
019import com.ning.billing.recurly.model.Subscription;
020import com.ning.billing.recurly.model.Transaction;
021import org.joda.time.DateTime;
022
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * This class is responsible for handling query parameters to pageable resources in the Recurly API.
028 * See the pagination docs (https://dev.recurly.com/docs/pagination) for the parameters available on
029 * every endpoint.
030 */
031public class QueryParams {
032    private static final String RECURLY_PAGE_SIZE_KEY = "recurly.page.size";
033    private static final Integer DEFAULT_PAGE_SIZE = 20;
034
035    public enum Sort {
036        CREATED_AT("created_at"),
037        UPDATED_AT("updated_at");
038
039        private final String type;
040
041        private Sort(final String type) {
042            this.type = type;
043        }
044
045        public String getType() {
046            return type;
047        }
048    }
049
050    public enum Order {
051        ASC("asc"),
052        DESC("desc");
053
054        private final String type;
055
056        private Order(final String type) {
057            this.type = type;
058        }
059
060        public String getType() {
061            return type;
062        }
063    }
064
065    public enum DateTimeType {
066        USAGE("usage"),
067        RECORDING("recording");
068
069        private final String type;
070
071       private DateTimeType(final String type) {
072           this.type = type;
073       }
074
075       public String getType() {
076           return type;
077       }
078    }
079
080    public enum BillingStatus {
081        ALL("all"), UNBILLED("unbilled"), BILLED("billed");
082
083        private final String type;
084
085        private BillingStatus(final String type) {
086            this.type = type;
087        }
088
089        public String getType() {
090            return type;
091        }
092    }
093
094    private Map<String,String> params;
095
096    public QueryParams() {
097        params = new HashMap<String, String>();
098
099        Integer pageSize;
100
101        try {
102            pageSize = new Integer(System.getProperty(RECURLY_PAGE_SIZE_KEY));
103        } catch (NumberFormatException nfex) {
104            pageSize = DEFAULT_PAGE_SIZE;
105        }
106
107        setPerPage(pageSize);
108    }
109
110    public void setPerPage(final int perPage) {
111        params.put("per_page", Integer.toString(perPage));
112    }
113
114    public void setSort(final Sort sortField) {
115        params.put("sort", sortField.getType());
116    }
117
118    public void setOrder(final Order orderDirection) {
119        params.put("order", orderDirection.getType());
120    }
121
122    public void setBeginTime(final DateTime beginTime) {
123        params.put("begin_time", formatDate(beginTime));
124    }
125
126    public void setEndTime(final DateTime endTime) {
127        params.put("end_time", formatDate(endTime));
128    }
129
130    // Parameters to support List Subscription Add-On's Usage
131    public void setStartDateTime(final DateTime startDateTime) {
132        params.put("start_datetime", formatDate(startDateTime));
133    }
134
135    public void setEndDateTime(final DateTime endDateTime) {
136        params.put("end_datetime", formatDate(endDateTime));
137    }
138
139    private String formatDate(final DateTime dateTime) {
140        return dateTime.toDateTimeISO().toString();
141    }
142
143    public void setDateTimeType(final DateTimeType dateTimeType) {
144        params.put("datetime_type", dateTimeType.getType());
145    }
146
147    public void setBillingStatus(final BillingStatus billingStatus) {
148        params.put("biiling_status", billingStatus.getType());
149    }
150
151    public void put(final String key, final String value) { params.put(key, value); }
152
153    @Override
154    public String toString() {
155        StringBuilder sb = new StringBuilder();
156
157        for (Map.Entry<String,String> entry : params.entrySet()) {
158            if (sb.length() > 0) {
159                sb.append("&");
160            } else {
161                sb.append("?");
162            }
163
164            sb.append(String.format("%s=%s", entry.getKey(), entry.getValue()));
165        }
166
167        return sb.toString();
168    }
169}