001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.query;
014
015import java.io.Serializable;
016import java.security.Principal;
017import java.util.Collection;
018import java.util.Collections;
019
020import org.nuxeo.ecm.core.api.impl.FacetFilter;
021import org.nuxeo.ecm.core.query.sql.model.SQLQuery;
022
023/**
024 * Filtering parameters that can be passed when executing a {@link FilterableQuery}.
025 * <p>
026 * This includes filtering on the BROWSE permission for the given principal, filtering on facets, and applying query
027 * transformers.
028 * <p>
029 * You can also include a limit and offset, to get a subset of the total.
030 *
031 * @author Florent Guillaume
032 */
033public class QueryFilter implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    public static final QueryFilter EMPTY = new QueryFilter(null, null, new String[0], null,
038            Collections.<SQLQuery.Transformer> emptyList(), 0, 0);
039
040    /** The principal. Note that this MUST be {@link Serializable}. */
041    protected final Principal principal;
042
043    protected final String[] principals;
044
045    protected final String[] permissions;
046
047    protected final FacetFilter facetFilter;
048
049    protected final Collection<SQLQuery.Transformer> queryTransformers;
050
051    protected final long limit;
052
053    protected final long offset;
054
055    /**
056     * Constructs a query filter.
057     * <p>
058     * Note that the principal MUST be {@link Serializable}.
059     */
060    public QueryFilter(Principal principal, String[] principals, String[] permissions, FacetFilter facetFilter,
061            Collection<SQLQuery.Transformer> queryTransformers, long limit, long offset) {
062        this.principal = principal;
063        this.principals = principals;
064        this.permissions = permissions;
065        this.facetFilter = facetFilter;
066        this.queryTransformers = queryTransformers;
067        this.limit = limit;
068        this.offset = offset;
069    }
070
071    public static QueryFilter withoutLimitOffset(QueryFilter other) {
072        return new QueryFilter( //
073                other.principal, //
074                other.principals, //
075                other.permissions, //
076                other.facetFilter, //
077                other.queryTransformers, //
078                0, 0);
079    }
080
081    public Principal getPrincipal() {
082        return principal;
083    }
084
085    public String[] getPrincipals() {
086        return principals;
087    }
088
089    public String[] getPermissions() {
090        return permissions;
091    }
092
093    public FacetFilter getFacetFilter() {
094        return facetFilter;
095    }
096
097    public Collection<SQLQuery.Transformer> getQueryTransformers() {
098        return queryTransformers;
099    }
100
101    public long getLimit() {
102        return limit;
103    }
104
105    public long getOffset() {
106        return offset;
107    }
108
109    @Override
110    public String toString() {
111        return String.format("QueryFilter(principal=%s, limit=%d, offset=%d)", principal, limit, offset);
112    }
113}