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