001/*
002 *  Copyright 2017 Anyware Services
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 */
016package org.ametys.plugins.odfweb.restrictions.rules;
017
018import java.util.Collection;
019import java.util.Collections;
020
021import org.ametys.odf.program.Program;
022import org.ametys.plugins.repository.query.expression.Expression;
023import org.ametys.plugins.repository.query.expression.NotExpression;
024
025/**
026 * Not restriction rule. Negate a set of rules.
027 */
028public class OdfNotRestrictionRule implements OdfRestrictionRule
029{
030    private final OdfRestrictionRule _rule;
031    
032    /**
033     * Not restriction rule constructor.
034     * @param rules The list of rules on which a "not" operator must be applied
035     */
036    public OdfNotRestrictionRule(Collection<OdfRestrictionRule> rules)
037    {
038        rules.removeAll(Collections.singleton(null));
039        
040        if (rules.size() > 1)
041        {
042            _rule = new OdfAndRestrictionRule(rules);
043        }
044        else if (!rules.isEmpty())
045        {
046            _rule = rules.iterator().next();
047        }
048        else
049        {
050            throw new IllegalArgumentException("At least one non-null rule is required.");
051        }
052    }
053    
054    @Override
055    public boolean contains(Program program)
056    {
057        return !_rule.contains(program);
058    }
059    
060    @Override
061    public Expression getExpression()
062    {
063        Expression expr = _rule.getExpression();
064        return expr != null ? new NotExpression(expr) : null;
065    }
066    
067    @Override
068    public boolean hasOrgunitRestrictions()
069    {
070        return _rule.hasOrgunitRestrictions();
071    }
072}