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 java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.cocoon.ProcessingException;
022
023import org.ametys.plugins.repository.data.type.ModelItemTypeConstants;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.runtime.model.DefinitionContext;
026import org.ametys.runtime.model.ModelItem;
027import org.ametys.runtime.model.ModelItemGroup;
028import org.ametys.runtime.model.exception.UnknownTypeException;
029import org.ametys.runtime.model.type.ModelItemType;
030import org.ametys.runtime.plugin.ExtensionPoint;
031
032/**
033 * Definition of a repeater.
034 */
035public class RepeaterDefinition extends ModelItemGroup
036{
037    private int _initializeSize;
038    private int _minSize;
039    private int _maxSize;
040    private I18nizableText _addLabel;
041    private I18nizableText _delLabel;
042    private String _headerLabel;
043    
044    /**
045     * Default constructor.
046     */
047    public RepeaterDefinition()
048    {
049        super();
050    }
051    
052    /**
053     * Constructor used to create simple models and items 
054     * @param name the name of the definition
055     * @param type the type of the repeater
056     * @param children the repeater's children
057     */
058    public RepeaterDefinition(String name, ModelItemType type, ModelItem... children)
059    {
060        super(name, children);
061        setType(type);
062    }
063    
064    /**
065     * Retrieves the initial size.
066     * @return the initial size.
067     */
068    public int getInitialSize()
069    {
070        return _initializeSize;
071    }
072    
073    /**
074     * Set the initial size.
075     * @param size the initial size.
076     */
077    public void setInitialSize(int size)
078    {
079        _initializeSize = size;
080    }
081
082    /**
083     * Retrieves the minimum size.
084     * @return the minimum size.
085     */
086    public int getMinSize()
087    {
088        return _minSize;
089    }
090    
091    /**
092     * Set the minimum size.
093     * @param size the minimum size.
094     */
095    public void setMinSize(int size)
096    {
097        _minSize = size;
098    }
099    
100    /**
101     * Retrieves the maximum size.
102     * @return the maximum size or <code>-1</code> if unbounded.
103     */
104    public int getMaxSize()
105    {
106        return _maxSize;
107    }
108
109    /**
110     * Set the maximum size.
111     * @param size the maximum size or <code>-1</code> if unbounded.
112     */
113    public void setMaxSize(int size)
114    {
115        _maxSize = size;
116    }
117    
118    /**
119     * Retrieves the add label.
120     * @return the add label or <code>null</code> if none.
121     */
122    public I18nizableText getAddLabel()
123    {
124        return _addLabel;
125    }
126
127    /**
128     * Set the add label.
129     * @param label the add label or <code>null</code> if none.
130     */
131    public void setAddLabel(I18nizableText label)
132    {
133        _addLabel = label;
134    }
135    
136    /**
137     * Retrieves the delete label.
138     * @return the delete label or <code>null</code> if none.
139     */
140    public I18nizableText getDeleteLabel()
141    {
142        return _delLabel;
143    }
144
145    /**
146     * Set the delete label.
147     * @param label the delete label or <code>null</code> if none.
148     */
149    public void setDeleteLabel(I18nizableText label)
150    {
151        _delLabel = label;
152    }
153    
154    /**
155     * Get the header label.
156     * @return the header label or <code>null</code> if none.
157     */
158    public String getHeaderLabel()
159    {
160        return _headerLabel;
161    }
162    
163    /**
164     * Set the header label.
165     * @param label the header label or <code>null</code> if none.
166     */
167    public void setHeaderLabel(String label)
168    {
169        _headerLabel = label;
170    }
171    
172    @Override
173    protected Map<String, Object> _toJSON(DefinitionContext context, boolean includeChildren) throws ProcessingException
174    {
175        Map<String, Object> result = super._toJSON(context, includeChildren);
176        
177        result.put("type", getType().getId());
178        result.put("add-label", getAddLabel());
179        result.put("del-label", getDeleteLabel());
180        result.put("header-label", getHeaderLabel());
181        result.put("initial-size", getInitialSize());
182        result.put("min-size", getMinSize());
183        
184        if (getMaxSize() >= 0)
185        {
186            result.put("max-size", getMaxSize());
187        }
188        
189        return result;
190    }
191    
192    /**
193     * Creates a {@link RepeaterDefinition}
194     * @param name the repeater's name
195     * @param availableTypesExtensionPoint the role of the extension point containing all available types for this {@link ModelItem}
196     * @param children the repeater's children
197     * @return the created {@link RepeaterDefinition}
198     * @throws UnknownTypeException if the repeater type is not available in the extension point
199     * @throws ServiceException if an error occurs while getting the extension point of available types
200     */
201    public static RepeaterDefinition of(String name, String availableTypesExtensionPoint, ModelItem... children) throws UnknownTypeException, ServiceException
202    {
203        @SuppressWarnings("unchecked")
204        ExtensionPoint<ModelItemType> availableTypes = (ExtensionPoint<ModelItemType>) __serviceManager.lookup(availableTypesExtensionPoint);
205        if (!availableTypes.hasExtension(ModelItemTypeConstants.REPEATER_TYPE_ID))
206        {
207            throw new UnknownTypeException("The type '" + ModelItemTypeConstants.REPEATER_TYPE_ID + "' (used for data '" + name + "') is not available for the given extension point.");
208        }
209        else
210        {
211            ModelItemType type = availableTypes.getExtension(ModelItemTypeConstants.REPEATER_TYPE_ID);
212            return new RepeaterDefinition(name, type, children);
213        }
214    }
215}