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.model.ContentRestrictedCompositeDefinition;
024import org.ametys.cms.model.restrictions.ContentRestrictedModelItemHelper;
025import org.ametys.plugins.repository.model.parsing.CompositeDefinitionParser;
026import org.ametys.runtime.model.Model;
027import org.ametys.runtime.model.ModelItemGroup;
028import org.ametys.runtime.model.type.ModelItemType;
029import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
030
031/**
032 * This class parses the content restricted composite definition
033 */
034public class ContentRestrictedCompositeDefinitionParser extends CompositeDefinitionParser
035{
036    /**
037     * Creates a content restricted composite definition parser.
038     * @param modelItemTypeExtensionPoint the extension point to use to get available model items types
039     */
040    public ContentRestrictedCompositeDefinitionParser(AbstractThreadSafeComponentExtensionPoint<? extends ModelItemType> modelItemTypeExtensionPoint)
041    {
042        super(modelItemTypeExtensionPoint);
043    }
044    
045    @Override
046    public ContentRestrictedCompositeDefinition parse(ServiceManager serviceManager, String pluginName, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
047    {
048        ContentRestrictedCompositeDefinition definition = (ContentRestrictedCompositeDefinition) super.parse(serviceManager, pluginName, definitionConfig, model, parent);
049        
050        try
051        {
052            ContentRestrictedModelItemHelper restrictedModelItemHelper = (ContentRestrictedModelItemHelper) serviceManager.lookup(ContentRestrictedModelItemHelper.ROLE);
053            definition.setRestrictions(restrictedModelItemHelper._parseRestrictions(definitionConfig));
054        }
055        catch (ServiceException e)
056        {
057            throw new ConfigurationException("Unable to resolve restrictions on the element '" + definition.getName() + "'.", e);
058        }
059        
060        return definition;
061    }
062
063    @Override
064    protected ContentRestrictedCompositeDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
065    {
066        return new ContentRestrictedCompositeDefinition();
067    }
068}