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 final List<String> accepted; 038 039 private final List<String> excluded; 040 041 /** 042 * Generic constructor. 043 * <p> 044 * To be accepted, the document must have its lifecycle state in the {@code required} list and the {@code excluded} 045 * list must not contain its lifecycle state. 046 * 047 * @param accepted the list of accepted lifecycle states 048 * @param excluded the list of excluded lifecycle states 049 */ 050 public LifeCycleFilter(List<String> accepted, List<String> excluded) { 051 this.accepted = accepted; 052 this.excluded = excluded; 053 } 054 055 /** 056 * Convenient constructor to filter on a lifecycle state. 057 * 058 * @param lifeCycle the lifecycle to filter on 059 * @param isRequired if {@code true} accepted documents must have this lifecycle state, if {@code false} accepted 060 * documents must not have this lifecycle state. 061 */ 062 public LifeCycleFilter(String lifeCycle, boolean isRequired) { 063 if (isRequired) { 064 accepted = new ArrayList<>(); 065 excluded = null; 066 accepted.add(lifeCycle); 067 } else { 068 excluded = new ArrayList<>(); 069 accepted = null; 070 excluded.add(lifeCycle); 071 } 072 } 073 074 @Override 075 public boolean accept(DocumentModel docModel) { 076 String lifeCycleState = docModel.getCurrentLifeCycleState(); 077 if (excluded != null) { 078 if (excluded.contains(lifeCycleState)) { 079 return false; 080 } 081 } 082 if (accepted != null) { 083 if (!accepted.contains(lifeCycleState)) { 084 return false; 085 } 086 } 087 return true; 088 } 089 090}