001/*
002 *  Copyright 2023 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.question.types;
017
018import java.util.List;
019
020import org.ametys.plugins.forms.repository.FormQuestion;
021import org.ametys.runtime.model.ElementDefinition;
022import org.ametys.runtime.model.ModelItem;
023import org.ametys.runtime.model.SimpleViewItemGroup;
024import org.ametys.runtime.model.ViewElement;
025import org.ametys.runtime.model.ViewItemGroup;
026
027/**
028 * Default abstract class to represent a question type.
029 * This default implementation allow to define an illustration, a description and mark the question as mandatory
030 */
031public abstract class AbstractFormQuestionType extends AbstractStaticFormQuestionType implements IllustrableFormQuestionType, DescriptibleFormQuestionType, MandatoryAwareFormQuestionType, ConfidentialAwareQuestionType
032{
033    @Override
034    protected List<ModelItem> _getModelItems()
035    {
036        List<ModelItem> modelItems = super._getModelItems();
037        
038        //DESCRIPTION
039        modelItems.add(getDescriptionModelItem());
040        
041        //ILLUSTRATION & ALTERNATE TEXT
042        modelItems.addAll(getIllustrationModelItems());
043        
044        //MANDATORY
045        modelItems.add(getMandatoryModelItem());
046        
047        //CONFIDENTIALITY
048        modelItems.add(getConfidentialityModelItem());
049        
050        return modelItems;
051    }
052    
053    @Override
054    protected List<ViewItemGroup> _getTabs()
055    {
056        return List.of(
057                _getMainTab(),
058                _getAdvancedTab(),
059                _getRulesTab(),
060                getIllustrationTab(getModel())
061                );
062    }
063    
064    /**
065     * Define the content of the main tab
066     * @return the main tab definition
067     */
068    protected SimpleViewItemGroup _getMainTab() 
069    {
070        SimpleViewItemGroup fieldset = super._createMainTab();
071        
072        ViewElement title = new ViewElement();
073        title.setDefinition((ElementDefinition< ? >) getModel().getModelItem(FormQuestion.ATTRIBUTE_TITLE));
074        fieldset.addViewItem(title);
075        
076        fieldset.addViewItem(getDescriptionViewElement(getModel()));
077        
078        fieldset.addViewItem(getMandatoryViewElement(getModel()));
079        
080        return fieldset;
081    }
082    
083    /**
084     * Define the content of the advanced tab
085     * @return the advanced tab definition
086     */
087    protected SimpleViewItemGroup _getAdvancedTab()
088    {
089        SimpleViewItemGroup advancedFieldset = super._createAdvancedTab();
090        
091        ViewElement nameForForms = new ViewElement();
092        nameForForms.setDefinition((ElementDefinition< ? >) getModel().getModelItem(FormQuestion.ATTRIBUTE_NAME_FOR_FORM));
093        advancedFieldset.addViewItem(nameForForms);
094        
095        advancedFieldset.addViewItem(getConfidentialityViewElement(getModel()));
096        
097        return advancedFieldset;
098    }
099    
100    @Override
101    protected ModelItem _getEntryModelItem(FormQuestion question)
102    {
103        // Add the mandatory validator to the entry model item
104        ModelItem item = super._getEntryModelItem(question);
105        ((ElementDefinition) item).setValidator(getMandatoryValidator(question));
106        return item;
107    }
108    
109    @Override
110    public List<String> getFieldToDisableIfFormPublished(FormQuestion question)
111    {
112        List<String> fieldNames =  super.getFieldToDisableIfFormPublished(question);
113        fieldNames.add(MandatoryAwareFormQuestionType.ATTRIBUTE_MANDATORY);
114        fieldNames.add(ConfidentialAwareQuestionType.ATTRIBUTE_CONFIDENTIALITY);
115        return fieldNames;
116    }
117}