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.google.common.base.Charsets; 020 021import org.apache.http.NameValuePair; 022import org.apache.http.client.utils.URLEncodedUtils; 023import org.apache.http.message.BasicNameValuePair; 024import org.joda.time.DateTime; 025 026import java.util.ArrayList; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031/** 032 * This class is responsible for handling query parameters to pageable resources in the Recurly API. 033 * See the pagination docs (https://dev.recurly.com/docs/pagination) for the parameters available on 034 * every endpoint. 035 */ 036public class QueryParams { 037 private static final String RECURLY_PAGE_SIZE_KEY = "recurly.page.size"; 038 private static final Integer DEFAULT_PAGE_SIZE = 20; 039 040 public enum Sort { 041 CREATED_AT("created_at"), 042 UPDATED_AT("updated_at"); 043 044 private final String type; 045 046 private Sort(final String type) { 047 this.type = type; 048 } 049 050 public String getType() { 051 return type; 052 } 053 } 054 055 public enum Order { 056 ASC("asc"), 057 DESC("desc"); 058 059 private final String type; 060 061 private Order(final String type) { 062 this.type = type; 063 } 064 065 public String getType() { 066 return type; 067 } 068 } 069 070 public enum DateTimeType { 071 USAGE("usage"), 072 RECORDING("recording"); 073 074 private final String type; 075 076 private DateTimeType(final String type) { 077 this.type = type; 078 } 079 080 public String getType() { 081 return type; 082 } 083 } 084 085 public enum BillingStatus { 086 ALL("all"), UNBILLED("unbilled"), BILLED("billed"); 087 088 private final String type; 089 090 private BillingStatus(final String type) { 091 this.type = type; 092 } 093 094 public String getType() { 095 return type; 096 } 097 } 098 099 private Map<String,String> params; 100 101 public QueryParams() { 102 params = new HashMap<String, String>(); 103 104 Integer pageSize; 105 106 try { 107 pageSize = new Integer(System.getProperty(RECURLY_PAGE_SIZE_KEY)); 108 } catch (NumberFormatException nfex) { 109 pageSize = DEFAULT_PAGE_SIZE; 110 } 111 112 setPerPage(pageSize); 113 } 114 115 public void setPerPage(final int perPage) { 116 params.put("per_page", Integer.toString(perPage)); 117 } 118 119 public void setSort(final Sort sortField) { 120 params.put("sort", sortField.getType()); 121 } 122 123 public void setOrder(final Order orderDirection) { 124 params.put("order", orderDirection.getType()); 125 } 126 127 public void setBeginTime(final DateTime beginTime) { 128 params.put("begin_time", formatDate(beginTime)); 129 } 130 131 public void setEndTime(final DateTime endTime) { 132 params.put("end_time", formatDate(endTime)); 133 } 134 135 // Parameters to support List Subscription Add-On's Usage 136 public void setStartDateTime(final DateTime startDateTime) { 137 params.put("start_datetime", formatDate(startDateTime)); 138 } 139 140 public void setEndDateTime(final DateTime endDateTime) { 141 params.put("end_datetime", formatDate(endDateTime)); 142 } 143 144 private String formatDate(final DateTime dateTime) { 145 return dateTime.toDateTimeISO().toString(); 146 } 147 148 public void setDateTimeType(final DateTimeType dateTimeType) { 149 params.put("datetime_type", dateTimeType.getType()); 150 } 151 152 public void setBillingStatus(final BillingStatus billingStatus) { 153 params.put("biiling_status", billingStatus.getType()); 154 } 155 156 public void put(final String key, final String value) { params.put(key, value); } 157 158 @Override 159 public String toString() { 160 if (params.isEmpty()) return ""; 161 final List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size()); 162 for (Map.Entry<String, String> entry : params.entrySet()) { 163 pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 164 } 165 return '?' + URLEncodedUtils.format(pairList, Charsets.UTF_8); 166 } 167}