001/*
002 *  Copyright 2018 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.cms.model.parsing;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.ContentStaticRelativeDisableConditions;
024import org.ametys.cms.data.holder.group.DataHolderCompositeDefinitionParser;
025import org.ametys.cms.model.ContentRestrictedCompositeDefinition;
026import org.ametys.cms.model.restrictions.ContentRestrictedModelItemHelper;
027import org.ametys.cms.model.restrictions.Restriction;
028import org.ametys.runtime.model.Model;
029import org.ametys.runtime.model.ModelItem;
030import org.ametys.runtime.model.ModelItemGroup;
031import org.ametys.runtime.model.disableconditions.DisableConditions;
032import org.ametys.runtime.model.type.ModelItemTypeExtensionPoint;
033import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
034
035/**
036 * This class parses the content restricted composite definition
037 */
038public class ContentRestrictedCompositeDefinitionParser extends DataHolderCompositeDefinitionParser
039{
040    private ThreadSafeComponentManager<Restriction> _restrictionManager;
041
042    /**
043     * Creates a content restricted composite definition parser.
044     * @param modelItemTypeExtensionPoint the extension point to use to get available model items types
045     * @param disableConditionsManager the disable conditions component manager
046     * @param restrictionManager the restrictions component manager.
047     */
048    public ContentRestrictedCompositeDefinitionParser(ModelItemTypeExtensionPoint modelItemTypeExtensionPoint, ThreadSafeComponentManager<DisableConditions> disableConditionsManager, ThreadSafeComponentManager<Restriction> restrictionManager)
049    {
050        super(modelItemTypeExtensionPoint, disableConditionsManager);
051        _restrictionManager = restrictionManager;
052    }
053    
054    @Override
055    @SuppressWarnings("unchecked")
056    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
057    {
058        ContentRestrictedCompositeDefinition definition = (ContentRestrictedCompositeDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
059        
060        try
061        {
062            ContentRestrictedModelItemHelper restrictedModelItemHelper = (ContentRestrictedModelItemHelper) serviceManager.lookup(ContentRestrictedModelItemHelper.ROLE);
063            restrictedModelItemHelper.parseAndSetRestriction(pluginName, definition, definitionConfig, _restrictionManager);
064        }
065        catch (ServiceException e)
066        {
067            throw new ConfigurationException("Unable to resolve restrictions on the element '" + definition.getName() + "'.", e);
068        }
069        
070        return (T) definition;
071    }
072
073    @Override
074    protected ContentRestrictedCompositeDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
075    {
076        return new ContentRestrictedCompositeDefinition();
077    }
078    
079    @Override
080    protected Class< ? extends DisableConditions> getDisableConditionsClass()
081    {
082        return ContentStaticRelativeDisableConditions.class;
083    }
084}