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;
017
018import java.util.Collections;
019import java.util.List;
020
021import org.ametys.odf.program.Program;
022import org.ametys.plugins.odfweb.restrictions.rules.OdfAndRestrictionRule;
023import org.ametys.plugins.odfweb.restrictions.rules.OdfRestrictionRule;
024import org.ametys.plugins.repository.query.expression.Expression;
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * A restriction on the ODF programs of a site.
029 */
030public class OdfProgramRestriction
031{
032    private final String _id;
033    private final I18nizableText _label;
034    private final OdfRestrictionRule _rule;
035    
036    /**
037     * Odf restriction constructor
038     * @param id Unique id of the restriction
039     * @param label The restriction label
040     * @param rule The rule of this restriction
041     */
042    public OdfProgramRestriction(String id, I18nizableText label, OdfRestrictionRule rule)
043    {
044        _id = id;
045        _label = label;
046        _rule = rule;
047        
048        if (_rule == null)
049        {
050            throw new IllegalArgumentException("At least one non-null rule is required.");
051        }
052    }
053    
054    /**
055     * Odf restriction constructor
056     * @param id Unique id of the restriction
057     * @param label The restriction label
058     * @param rules The set of rule of this restriction
059     */
060    public OdfProgramRestriction(String id, I18nizableText label, List<OdfRestrictionRule> rules)
061    {
062        this(id, label, _extractMainRule(rules));
063    }
064    
065    private static OdfRestrictionRule _extractMainRule(List<OdfRestrictionRule> rules)
066    {
067        rules.removeAll(Collections.<OdfRestrictionRule>singleton(null));
068        
069        OdfRestrictionRule rule = null;
070        if (rules.size() > 1)
071        {
072            rule = new OdfAndRestrictionRule(rules);
073        }
074        else if (!rules.isEmpty())
075        {
076            rule = rules.iterator().next();
077        }
078        else
079        {
080            throw new IllegalArgumentException("At least one non-null rule is required.");
081        }
082        
083        return rule;
084    }
085    
086    /**
087     * Id getter
088     * @return The id
089     */
090    public String getId()
091    {
092        return _id;
093    }
094    
095    /**
096     * Label getter
097     * @return The label
098     */
099    public I18nizableText getLabel()
100    {
101        return _label;
102    }
103    
104    /**
105     * Determines if the current restriction contains the program
106     * @param program The program to check
107     * @return <code>true</code> if the program is part of the restrictions
108     */
109    public boolean contains(Program program)
110    {
111        return _rule.contains(program);
112    }
113    
114    /**
115     * Translate the restriction into an expression that can be used for repository query.
116     * @return The restriction expression
117     */
118    public Expression getExpression()
119    {
120        return _rule.getExpression();
121    }
122    
123    /**
124     * Indicate if this restriction is related to orgunit
125     * @return true if it is the case
126     */
127    public boolean hasOrgunitRestrictions()
128    {
129        return _rule.hasOrgunitRestrictions();
130    }
131}