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.ModelItemTypeExtensionPoint;
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(ModelItemTypeExtensionPoint modelItemTypeExtensionPoint)
041    {
042        super(modelItemTypeExtensionPoint);
043    }
044    
045    @Override
046    @SuppressWarnings("unchecked")
047    public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException
048    {
049        ContentRestrictedCompositeDefinition definition = (ContentRestrictedCompositeDefinition) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent);
050        
051        try
052        {
053            ContentRestrictedModelItemHelper restrictedModelItemHelper = (ContentRestrictedModelItemHelper) serviceManager.lookup(ContentRestrictedModelItemHelper.ROLE);
054            definition.setRestrictions(restrictedModelItemHelper._parseRestrictions(definitionConfig));
055        }
056        catch (ServiceException e)
057        {
058            throw new ConfigurationException("Unable to resolve restrictions on the element '" + definition.getName() + "'.", e);
059        }
060        
061        return (T) definition;
062    }
063
064    @Override
065    protected ContentRestrictedCompositeDefinition _createModelItem(Configuration itemConfig) throws ConfigurationException
066    {
067        return new ContentRestrictedCompositeDefinition();
068    }
069}