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