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.plugins.repository.model;
017
018import org.apache.avalon.framework.service.ServiceException;
019
020import org.ametys.plugins.repository.data.type.ModelItemTypeConstants;
021import org.ametys.runtime.model.ModelItem;
022import org.ametys.runtime.model.ModelItemGroup;
023import org.ametys.runtime.model.exception.UnknownTypeException;
024import org.ametys.runtime.model.type.ModelItemType;
025import org.ametys.runtime.plugin.ExtensionPoint;
026
027/**
028 * Definition of a composite
029 */
030public class CompositeDefinition extends ModelItemGroup
031{
032   /**
033    * Default constructor.
034    */
035    public CompositeDefinition()
036    {
037        super();
038    }
039
040    /**
041     * Constructor used to create simple models and items 
042     * @param name the name of the definition
043     * @param type the type of the composite
044     * @param children the composite's children
045     */
046    public CompositeDefinition(String name, ModelItemType type, ModelItem... children)
047    {
048        super(name, children);
049        setType(type);
050    }
051
052    /**
053     * Creates a {@link CompositeDefinition}
054     * @param name the composite's name
055     * @param availableTypesExtensionPoint the role of the extension point containing all available types for this {@link ModelItem}
056     * @param children the composite's children
057     * @return the created {@link CompositeDefinition}
058     * @throws UnknownTypeException if the composite type is not available in the extension point
059     * @throws ServiceException if an error occurs while getting the extension point of available types
060     */
061    public static CompositeDefinition of(String name, String availableTypesExtensionPoint, ModelItem... children) throws UnknownTypeException, ServiceException
062    {
063        @SuppressWarnings("unchecked")
064        ExtensionPoint<ModelItemType> availableTypes = (ExtensionPoint<ModelItemType>) __serviceManager.lookup(availableTypesExtensionPoint);
065        if (!availableTypes.hasExtension(ModelItemTypeConstants.COMPOSITE_TYPE_ID))
066        {
067            throw new UnknownTypeException("The type '" + ModelItemTypeConstants.COMPOSITE_TYPE_ID + "' (used for data '" + name + "') is not available for the given extension point.");
068        }
069        else
070        {
071            ModelItemType type = availableTypes.getExtension(ModelItemTypeConstants.COMPOSITE_TYPE_ID);
072            return new CompositeDefinition(name, type, children);
073        }
074    }
075}