All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gatein.api.page.PageQuery Maven / Gradle / Ivy

There is a newer version: 1.1.0.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.gatein.api.page;

import org.gatein.api.common.Filter;
import org.gatein.api.common.Pagination;
import org.gatein.api.internal.ObjectToStringBuilder;
import org.gatein.api.site.SiteId;
import org.gatein.api.site.SiteType;

/**
 * An immutable PageQuery object that can be used to query pages. This object is created by using the builder
 * {@link PageQuery.Builder}.
 *
 * @author Nick Scavelli
 */
public class PageQuery {
    private final SiteType siteType;
    private final String siteName;
    private final String displayName;

    // Common query fields
    private final Pagination pagination;
    private final Filter filter;

    private PageQuery(SiteType siteType, String siteName, String displayName, Pagination pagination, Filter filter) {
        this.siteType = siteType;
        this.siteName = siteName;
        this.displayName = displayName;
        this.pagination = pagination;
        this.filter = filter;
    }

    public Pagination getPagination() {
        return pagination;
    }

    /**
     * Convenience method for creating a new PageQuery with pagination set to the next page represented by by
     * {@link org.gatein.api.common.Pagination#getNext()}
     *
     * @return a new PageQuery with pagination set to the next page.
     */
    public PageQuery nextPage() {
        return new Builder().from(this).withNextPage().build();
    }

    /**
     * Convenience method for creating a new PageQuery with pagination set to the previous page represented by by
     * {@link org.gatein.api.common.Pagination#getPrevious()}
     *
     * @return a new PageQuery with pagination set to the previous page.
     */
    public PageQuery previousPage() {
        return new Builder().from(this).withPreviousPage().build();
    }

    public Filter getFilter() {
        return filter;
    }

    public SiteType getSiteType() {
        return siteType;
    }

    public String getSiteName() {
        return siteName;
    }

    public String getDisplayName() {
        return displayName;
    }

    @Override
    public String toString() {
        return ObjectToStringBuilder.toStringBuilder(PageQuery.class).add("siteType", siteType).add("siteName", siteName)
                .add("displayName", displayName).add("pagination", pagination).add("filter", filter).toString();
    }

    /**
     * The builder responsible for creating {@link PageQuery} objects.
     */
    public static class Builder {
        public static final int DEFAULT_LIMIT = 15;
        /**
         * The default pagination used for PageQuery's with a limit set to {@link Builder#DEFAULT_LIMIT}
         */
        public static final Pagination DEFAULT_PAGINATION = new Pagination(0, DEFAULT_LIMIT);

        private SiteType siteType;
        private String siteName;
        private String displayName;
        private Filter filter;
        private Pagination pagination;

        public Builder() {
            this.pagination = DEFAULT_PAGINATION;
        }

        /**
         * Convenience method for setting both the site type and site name.
         *
         * @param siteId the site id
         * @return the builder
         */
        public Builder withSiteId(SiteId siteId) {
            return withSiteType(siteId.getType()).withSiteName(siteId.getName());
        }

        /**
         * Sets the siteType for this builder to be used to query all sites that have the SiteType.
         *
         * @param siteType the site type
         * @return this builder
         */
        public Builder withSiteType(SiteType siteType) {
            this.siteType = siteType;
            return this;
        }

        /**
         * Sets the siteName for this builder to be used to query all sites that have the name.
         *
         *
         * @param siteName the site name
         * @return this builder
         */
        public Builder withSiteName(String siteName) {
            this.siteName = siteName;
            return this;
        }

        /**
         * Sets the display name for this builder to be used to query all sites that have the displayName.
         *
         * @param displayName the display name
         * @return this builder
         */
        public Builder withDisplayName(String displayName) {
            this.displayName = displayName;
            return this;
        }

        /**
         * Sets the pagination object of this builder to use the specified offset and limit.
         *
         * @param offset the offset of the pagination
         * @param limit the limit of the pagination
         * @return this builder
         */
        public Builder withPagination(int offset, int limit) {
            return withPagination(new Pagination(offset, limit));
        }

        /**
         * Sets the pagination object of this builder
         *
         * @param pagination the pagination object
         * @return this builder
         */
        public Builder withPagination(Pagination pagination) {
            this.pagination = pagination;
            return this;
        }

        /**
         * Sets the pagination of this builder to pagination.getNext() if it's not null.
         *
         * @return this builder
         */
        public Builder withNextPage() {
            if (pagination != null) {
                this.pagination = pagination.getNext();
            }

            return this;
        }

        /**
         * Sets the pagination of this builder to pagination.getPrevious() if it's not null.
         *
         * @return this builder
         */
        public Builder withPreviousPage() {
            if (pagination != null) {
                this.pagination = pagination.getPrevious();
            }

            return this;
        }

        /**
         * Sets the filter for this builder
         *
         * @param filter the filter
         * @return this builder
         */
        public Builder withFilter(Filter filter) {
            this.filter = filter;
            return this;
        }

        /**
         * Creates a new PageQuery object represented by the state of this builder
         *
         * @return a new PageQuery object
         */
        public PageQuery build() {
            return new PageQuery(siteType, siteName, displayName, pagination, filter);
        }

        /**
         * Creates a new builder from an existing PageQuery.
         *
         * @param query the query used to build the initial state of the builder
         * @return a new builder
         */
        public Builder from(PageQuery query) {
            return new Builder().withSiteType(query.getSiteType()).withSiteName(query.getSiteName())
                    .withDisplayName(query.getDisplayName()).withPagination(query.getPagination())
                    .withFilter(query.getFilter());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy