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