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 *     troger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.impl;
023
024import java.util.ArrayList;
025import java.util.List;
026
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.Filter;
029
030/**
031 * A filter based on the document's life cycle.
032 *
033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
034 */
035public class LifeCycleFilter implements Filter {
036
037    private static final long serialVersionUID = -6222667096842773182L;
038
039    private final List<String> accepted;
040
041    private final List<String> excluded;
042
043    /**
044     * Generic constructor.
045     * <p>
046     * To be accepted, the document must have its lifecycle state in the {@code required} list and the {@code excluded}
047     * list must not contain its lifecycle state.
048     *
049     * @param accepted the list of accepted lifecycle states
050     * @param excluded the list of excluded lifecycle states
051     */
052    public LifeCycleFilter(List<String> accepted, List<String> excluded) {
053        this.accepted = accepted;
054        this.excluded = excluded;
055    }
056
057    /**
058     * Convenient constructor to filter on a lifecycle state.
059     *
060     * @param lifeCycle the lifecycle to filter on
061     * @param isRequired if {@code true} accepted documents must have this lifecycle state, if {@code false} accepted
062     *            documents must not have this lifecycle state.
063     */
064    public LifeCycleFilter(String lifeCycle, boolean isRequired) {
065        if (isRequired) {
066            accepted = new ArrayList<String>();
067            excluded = null;
068            accepted.add(lifeCycle);
069        } else {
070            excluded = new ArrayList<String>();
071            accepted = null;
072            excluded.add(lifeCycle);
073        }
074    }
075
076    @Override
077    public boolean accept(DocumentModel docModel) {
078        String lifeCycleState = docModel.getCurrentLifeCycleState();
079        if (excluded != null) {
080            if (excluded.contains(lifeCycleState)) {
081                return false;
082            }
083        }
084        if (accepted != null) {
085            if (!accepted.contains(lifeCycleState)) {
086                return false;
087            }
088        }
089        return true;
090    }
091
092}