001/*
002 *  Copyright 2021 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.forms.helper;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021import org.apache.commons.lang3.StringUtils;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import org.ametys.plugins.forms.question.FormQuestionDataTypeExtensionPoint;
026import org.ametys.plugins.repository.model.RepeaterDefinition;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.DefaultElementDefinition;
029import org.ametys.runtime.model.ElementDefinition;
030import org.ametys.runtime.model.exception.BadItemTypeException;
031import org.ametys.runtime.model.type.ElementType;
032import org.ametys.runtime.model.type.ModelItemType;
033import org.ametys.runtime.parameter.Validator;
034
035/**
036 * Helper to create element definition for form
037 */
038public class FormElementDefinitionHelper implements Serviceable
039{
040    /** The form question data type extension point */
041    protected static FormQuestionDataTypeExtensionPoint _formQuestionDataTypeExtensionPoint;
042    
043    /** The logger */
044    protected static Logger __logger = LoggerFactory.getLogger(FormElementDefinitionHelper.class);
045    
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _formQuestionDataTypeExtensionPoint = (FormQuestionDataTypeExtensionPoint) manager.lookup(FormQuestionDataTypeExtensionPoint.ROLE);
049    }
050    
051    /**
052     * Create element definition
053     * @param name the name
054     * @param typeId the type id
055     * @param labelKey the label i18n key. Can be null.
056     * @param descKey the desc i18n key. Can be null.
057     * @param validator the validator. Can be null.
058     * @return the element definition
059     */
060    @SuppressWarnings("unchecked")
061    public static ElementDefinition getElementDefinition(String name, String typeId, String labelKey, String descKey, Validator validator)
062    {
063        ModelItemType type = _formQuestionDataTypeExtensionPoint.getExtension(typeId);
064        if (!(type instanceof ElementType))
065        {
066            throw new BadItemTypeException("The type '" + typeId + "' (used for data '" + name + "') can not be used for an element definition.");
067        }
068        else
069        {
070            ElementDefinition elementDefinition = new DefaultElementDefinition(name, false, (ElementType) type);
071            if (StringUtils.isNotBlank(labelKey))
072            {
073                elementDefinition.setLabel(new I18nizableText("plugin.forms", labelKey)); 
074            }
075
076            if (StringUtils.isNotBlank(descKey))
077            {
078                elementDefinition.setDescription(new I18nizableText("plugin.forms", descKey)); 
079            }
080            
081            if (validator != null)
082            {
083                elementDefinition.setValidator(validator);
084            }
085            
086            return elementDefinition;
087        }
088    }
089    
090    /**
091     * Create repeater definition 
092     * @param datapath path of the parent element
093     * @param childElements the repeater's children
094     * @param labelKey the label i18n key. Can be null.
095     * @param descKey the desc i18n key. Can be null.
096     * @param addKey the add i18n key. Can be null.
097     * @param deleteKey the delete i18n key. Can be null.
098     * @param maxSize number maximum of entries
099     * @return the created repeater
100     */
101    public static RepeaterDefinition getRepeaterDefinition(String datapath, ElementDefinition[] childElements, String labelKey, String descKey, String addKey, String deleteKey, Integer maxSize)
102    {
103        try
104        {
105            RepeaterDefinition repeater = RepeaterDefinition.of(datapath, FormQuestionDataTypeExtensionPoint.ROLE, childElements);
106            repeater.setLabel(new I18nizableText("plugin.forms", labelKey));
107            repeater.setDescription(new I18nizableText("plugin.forms", descKey));
108            repeater.setAddLabel(new I18nizableText("plugin.forms", addKey));
109            repeater.setDeleteLabel(new I18nizableText("plugin.forms", deleteKey));
110            repeater.setMaxSize(maxSize);
111            
112            return repeater;
113        }
114        catch (Exception e)
115        {
116            __logger.error("An error occured while creating repeater definition", e);
117        }
118        return null;
119    }
120}