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 org.ametys.plugins.forms.helper.FormElementDefinitionHelper;
019import org.ametys.plugins.forms.repository.FormQuestion;
020import org.ametys.runtime.model.ElementDefinition;
021import org.ametys.runtime.model.Model;
022import org.ametys.runtime.model.ModelItem;
023import org.ametys.runtime.model.ViewElement;
024import org.ametys.runtime.model.type.ModelItemTypeConstants;
025import org.ametys.runtime.parameter.DefaultValidator;
026import org.ametys.runtime.parameter.Validator;
027
028/**
029 * Provide all the necessary method to make a question type required or not
030 */
031public interface MandatoryAwareFormQuestionType
032{
033    /** Constant for mandatory attribute. */
034    public static final String ATTRIBUTE_MANDATORY = "mandatory";
035    
036    /**
037     * Get the model item to add to the model items return by the method FormQuestionType.getModel
038     * @return the model item
039     */
040    public default ModelItem getMandatoryModelItem()
041    {
042        return FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_MANDATORY, ModelItemTypeConstants.BOOLEAN_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_QUESTION_MANDATORY", "PLUGINS_FORMS_QUESTIONS_DIALOG_QUESTION_MANDATORY_DESC", null);
043    }
044    
045    /**
046     * Create a view element to add in the view elements returned by FormQuestionType.getView
047     * @param model the model link to the view
048     * @return a view element
049     */
050    public default ViewElement getMandatoryViewElement(Model model)
051    {
052        ViewElement mandatory = new ViewElement();
053        mandatory.setDefinition((ElementDefinition< ? >) model.getModelItem(ATTRIBUTE_MANDATORY));
054        return mandatory;
055    }
056    
057    /**
058     * Create a validator that should be added to the element definition return by FormQuestionType.getEntryModel
059     * @param question the question
060     * @return a validator
061     */
062    public default Validator getMandatoryValidator(FormQuestion question)
063    {
064        return new DefaultValidator(null, isMandatory(question));
065    }
066
067    /**
068     * Check if the question is actually mandatory
069     * @param question the question to check
070     * @return true if the question is mandatory
071     */
072    public default boolean isMandatory(FormQuestion question)
073    {
074        return question.getValue(ATTRIBUTE_MANDATORY, false, false);
075    }
076}