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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.tree;
016
017import java.util.List;
018
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.LifeCycleConstants;
021
022/**
023 * Default filter for tree elements.
024 * <p>
025 * Filters using facets and types criteria. Also filters documents that are in the "deleted" life cycle state.
026 *
027 * @author Anahide Tchertchian
028 */
029public class DefaultDocumentTreeFilter implements DocumentTreeFilter {
030
031    private static final long serialVersionUID = 1L;
032
033    protected List<String> includedFacets;
034
035    protected List<String> excludedFacets;
036
037    protected List<String> excludedTypes;
038
039    @Override
040    public boolean accept(DocumentModel document) {
041        String docType = document.getType();
042        if (excludedTypes != null && excludedTypes.contains(docType)) {
043            return false;
044        }
045        // exclude deleted documents from tree
046        if (LifeCycleConstants.DELETED_STATE.equals(document.getCurrentLifeCycleState())) {
047            return false;
048        }
049        // XXX AT: this could have not been copied from FacetFilter if fields
050        // were not private there.
051        if (excludedFacets != null) {
052            for (String exc : excludedFacets) {
053                if (document.hasFacet(exc)) {
054                    return false;
055                }
056            }
057        }
058        if (includedFacets != null) {
059            for (String req : includedFacets) {
060                if (!document.hasFacet(req)) {
061                    return false;
062                }
063            }
064        }
065        return true;
066    }
067
068    @Override
069    public List<String> getIncludedFacets() {
070        return includedFacets;
071    }
072
073    @Override
074    public void setIncludedFacets(List<String> includedFacets) {
075        this.includedFacets = includedFacets;
076    }
077
078    @Override
079    public List<String> getExcludedFacets() {
080        return excludedFacets;
081    }
082
083    @Override
084    public void setExcludedFacets(List<String> excludedFacets) {
085        this.excludedFacets = excludedFacets;
086    }
087
088    @Override
089    public List<String> getExcludedTypes() {
090        return excludedTypes;
091    }
092
093    @Override
094    public void setExcludedTypes(List<String> excludedTypes) {
095        this.excludedTypes = excludedTypes;
096    }
097
098}