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.List;
019
020import org.ametys.cms.content.ContentHelper;
021import org.ametys.odf.program.Program;
022import org.ametys.plugins.repository.query.expression.Expression;
023import org.ametys.plugins.repository.query.expression.Expression.Operator;
024import org.ametys.plugins.repository.query.expression.StringExpression;
025
026/**
027 * Metadata restriction rule
028 */
029public class OdfMetadataRestrictionRule implements OdfRestrictionRule
030{
031    private final ContentHelper _contentHelper;
032    private final String _metadataPath;
033    private final String _value;
034    
035    /**
036     * Constructor for rule relating to the value of a metadata
037     * @param contentHelper The content helper component
038     * @param metadataPath The path of metadata
039     * @param value The metadata value
040     */
041    public OdfMetadataRestrictionRule(ContentHelper contentHelper, String metadataPath, String value)
042    {
043        _contentHelper = contentHelper;
044        _metadataPath = metadataPath;
045        _value = value;
046    }
047    
048    @Override
049    public Expression getExpression()
050    {
051        return new StringExpression(_metadataPath, Operator.EQ, _value);
052    }
053    
054    @Override
055    public boolean contains(Program program)
056    {
057        List<Object> values = _contentHelper.getMetadataValues(program, _metadataPath, null, false, false);
058        return values.contains(_value);
059    }
060    
061    @Override
062    public boolean hasOrgunitRestrictions()
063    {
064        return false;
065    }
066}