Java tutorial
package org.getwheat.harvest.library.request; import java.util.Date; import org.getwheat.harvest.library.Transformer; import org.getwheat.harvest.library.model.HttpMethod; import org.getwheat.harvest.library.model.RequestParameter; import org.getwheat.harvest.library.model.RequestParameterValue; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /* * Copyright 2011 by Christopher Smith * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ /** * BaseReportRequest provides common Report Request functionality and is meant to be extended. * * @author Christopher Smith */ public class BaseReportRequest<T> extends BaseRequest<T> { private static final Logger LOG = LoggerFactory.getLogger(BaseReportRequest.class); private static final String RANGE_DATE_PATTERN = "yyyyMMdd"; private transient final ParameterHelper helper = new ParameterHelper(); /** * Default constructor. * * @param transformer * @param method * @param resourcePath * @param updatedSince */ protected BaseReportRequest(final Transformer<T> transformer, final HttpMethod method, final String resourcePath, final Date updatedSince) { super(transformer, method, resourcePath, updatedSince); } /** * Adds a filter property to the request. * <p /> * Entries that either have a null parameter and/or value are ignored. * * @param parameter * @param value */ protected void addFilter(final RequestParameter parameter, final Object value) { if (parameter != null && value != null) { helper.addParameter(parameter, value.toString(), getParameters()); } } /** * Adds a filter property to the request. * <p /> * Entries that either have a null parameter and/or value are ignored. * * @param parameter * @param value */ protected void addFilter(final RequestParameter parameter, final Boolean value) { if (parameter != null && value != null) { String parameterValue = RequestParameterValue.NO.getParameterValue(); if (value) { parameterValue = RequestParameterValue.YES.getParameterValue(); } helper.addParameter(parameter, parameterValue, getParameters()); } } /** * Adds common report filters to the request. * <p /> * Entries that have a null value are ignored. * * @param billable * @param onlyBilled * @param onlyUnbilled * @param closed */ protected void addCommonFilters(final Boolean billable, final Boolean onlyBilled, final Boolean onlyUnbilled, final Boolean closed) { addFilter(RequestParameter.BILLABLE, billable); addFilter(RequestParameter.ONLY_BILLED, onlyBilled); addFilter(RequestParameter.ONLY_UNBILLED, onlyUnbilled); addFilter(RequestParameter.IS_CLOSED, closed); } /** * Adds the report range date to the request. * * @param rangeFrom * @param rangeTo */ protected void addRange(final Date rangeFrom, final Date rangeTo) { addRangeDate(RequestParameter.FROM, rangeFrom); addRangeDate(RequestParameter.TO, rangeTo); } private void addRangeDate(final RequestParameter parameter, final Date value) { if (parameter != null && value != null) { final DateTimeFormatter formatter = DateTimeFormat.forPattern(RANGE_DATE_PATTERN); final DateTime date = new DateTime(value.getTime()); try { helper.addParameter(parameter, formatter.print(date), getParameters()); } catch (Exception ex) { LOG.warn("", ex); } } } }