001/*
002 * (C) Copyright 2006-2019 Nuxeo (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.util.Collection;
023import java.util.Collections;
024
025import org.nuxeo.ecm.core.api.NuxeoPrincipal;
026import org.nuxeo.ecm.core.api.impl.FacetFilter;
027import org.nuxeo.ecm.core.query.sql.model.SQLQuery;
028
029/**
030 * Filtering parameters that can be passed when executing a query.
031 * <p>
032 * This includes filtering on the BROWSE permission for the given principal, filtering on facets, and applying query
033 * transformers.
034 * <p>
035 * You can also include a limit and offset, to get a subset of the total.
036 *
037 * @author Florent Guillaume
038 */
039public class QueryFilter implements org.nuxeo.ecm.core.api.query.QueryFilter<SQLQuery.Transformer> {
040
041    public static final QueryFilter EMPTY = new QueryFilter(null, null, new String[0], null,
042            Collections.emptyList(), 0, 0);
043
044    protected final NuxeoPrincipal principal;
045
046    protected final String[] principals;
047
048    protected final String[] permissions;
049
050    protected final FacetFilter facetFilter;
051
052    protected final Collection<SQLQuery.Transformer> queryTransformers;
053
054    protected final long limit;
055
056    protected final long offset;
057
058    /**
059     * Constructs a query filter.
060     */
061    public QueryFilter(NuxeoPrincipal principal, String[] principals, String[] permissions, FacetFilter facetFilter,
062            Collection<SQLQuery.Transformer> queryTransformers, long limit, long offset) {
063        this.principal = principal;
064        this.principals = principals;
065        this.permissions = permissions;
066        this.facetFilter = facetFilter;
067        this.queryTransformers = queryTransformers;
068        this.limit = limit;
069        this.offset = offset;
070    }
071
072    public static QueryFilter withoutLimitOffset(QueryFilter other) {
073        return new QueryFilter( //
074                other.principal, //
075                other.principals, //
076                other.permissions, //
077                other.facetFilter, //
078                other.queryTransformers, //
079                0, 0);
080    }
081
082    @Override
083    public NuxeoPrincipal getPrincipal() {
084        return principal;
085    }
086
087    @Override
088    public String[] getPrincipals() {
089        return principals;
090    }
091
092    @Override
093    public String[] getPermissions() {
094        return permissions;
095    }
096
097    @Override
098    public FacetFilter getFacetFilter() {
099        return facetFilter;
100    }
101
102    @Override
103    public Collection<SQLQuery.Transformer> getQueryTransformers() {
104        return queryTransformers;
105    }
106
107    @Override
108    public long getLimit() {
109        return limit;
110    }
111
112    @Override
113    public long getOffset() {
114        return offset;
115    }
116
117    @Override
118    public String toString() {
119        return String.format("QueryFilter(principal=%s, limit=%d, offset=%d)", principal, limit, offset);
120    }
121}