001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.query;
021
022import java.io.Serializable;
023import java.security.Principal;
024import java.util.Collection;
025import java.util.Collections;
026
027import org.nuxeo.ecm.core.api.impl.FacetFilter;
028import org.nuxeo.ecm.core.query.sql.model.SQLQuery;
029
030/**
031 * Filtering parameters that can be passed when executing a {@link FilterableQuery}.
032 * <p>
033 * This includes filtering on the BROWSE permission for the given principal, filtering on facets, and applying query
034 * transformers.
035 * <p>
036 * You can also include a limit and offset, to get a subset of the total.
037 *
038 * @author Florent Guillaume
039 */
040public class QueryFilter implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    public static final QueryFilter EMPTY = new QueryFilter(null, null, new String[0], null,
045            Collections.<SQLQuery.Transformer> emptyList(), 0, 0);
046
047    /** The principal. Note that this MUST be {@link Serializable}. */
048    protected final Principal principal;
049
050    protected final String[] principals;
051
052    protected final String[] permissions;
053
054    protected final FacetFilter facetFilter;
055
056    protected final Collection<SQLQuery.Transformer> queryTransformers;
057
058    protected final long limit;
059
060    protected final long offset;
061
062    /**
063     * Constructs a query filter.
064     * <p>
065     * Note that the principal MUST be {@link Serializable}.
066     */
067    public QueryFilter(Principal principal, String[] principals, String[] permissions, FacetFilter facetFilter,
068            Collection<SQLQuery.Transformer> queryTransformers, long limit, long offset) {
069        this.principal = principal;
070        this.principals = principals;
071        this.permissions = permissions;
072        this.facetFilter = facetFilter;
073        this.queryTransformers = queryTransformers;
074        this.limit = limit;
075        this.offset = offset;
076    }
077
078    public static QueryFilter withoutLimitOffset(QueryFilter other) {
079        return new QueryFilter( //
080                other.principal, //
081                other.principals, //
082                other.permissions, //
083                other.facetFilter, //
084                other.queryTransformers, //
085                0, 0);
086    }
087
088    public Principal getPrincipal() {
089        return principal;
090    }
091
092    public String[] getPrincipals() {
093        return principals;
094    }
095
096    public String[] getPermissions() {
097        return permissions;
098    }
099
100    public FacetFilter getFacetFilter() {
101        return facetFilter;
102    }
103
104    public Collection<SQLQuery.Transformer> getQueryTransformers() {
105        return queryTransformers;
106    }
107
108    public long getLimit() {
109        return limit;
110    }
111
112    public long getOffset() {
113        return offset;
114    }
115
116    @Override
117    public String toString() {
118        return String.format("QueryFilter(principal=%s, limit=%d, offset=%d)", principal, limit, offset);
119    }
120}