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