001/*
002 *  Copyright 2010 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.web.site;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.ametys.runtime.model.CategorizedElementDefinitionWrapper;
023import org.ametys.web.repository.site.Site;
024
025/**
026 * Definition of a {@link Site} parameter.
027 * @param <T> Type of the element value
028 */
029public class SiteParameterWrapper<T> extends CategorizedElementDefinitionWrapper<T>
030{
031    /** The list of site types to which this parameter belongs, or null for no restriction. */
032    protected Set<String> _siteTypes = new HashSet<>();
033    
034    /**
035     * Default constructor.
036     */
037    public SiteParameterWrapper()
038    {
039        super();
040    }
041    
042    /**
043     * Constructor by copying an existing {@link SiteParameterWrapper}.
044     * @param wrapperToCopy The {@link SiteParameterWrapper} to copy
045     */
046    public SiteParameterWrapper(SiteParameterWrapper<T> wrapperToCopy)
047    {
048        super(wrapperToCopy);
049        
050        // Site types
051        setSiteTypes(wrapperToCopy.getSiteTypes());
052    }
053    
054    /**
055     * Get the site type restriction, null for no restriction.
056     * @return the site types or null for no restriction.
057     */
058    public Set<String> getSiteTypes()
059    {
060        return Collections.unmodifiableSet(_siteTypes);
061    }
062    
063    /**
064     * Set the site type restriction, null for no restriction.
065     * @param siteTypes the site types to set.
066     */
067    public void setSiteTypes(Set<String> siteTypes)
068    {
069        _siteTypes = new HashSet<>(siteTypes);
070    }
071    
072    /**
073     * Test if the parameter is to appear in the specified site type.
074     * @param siteType the site type name (i.e. 'default', 'blog'...)
075     * @return true to appear, false otherwise.
076     */
077    public boolean isInSiteType(String siteType)
078    {
079        return _siteTypes.isEmpty() || _siteTypes.contains(siteType);
080    }
081}