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