001/* 002 * Copyright 2024 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.autofill; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.ametys.plugins.forms.helper.FormElementDefinitionHelper; 024import org.ametys.plugins.forms.repository.FormQuestion; 025import org.ametys.runtime.model.ElementDefinition; 026import org.ametys.runtime.model.ModelItem; 027import org.ametys.runtime.model.ViewElement; 028import org.ametys.runtime.model.ViewItem; 029import org.ametys.runtime.model.type.ModelItemTypeConstants; 030import org.ametys.runtime.parameter.DefaultValidator; 031 032/** 033 * Class for adding custom value as default value in text fields 034 */ 035public class CustomValueAutoFillSource extends AbstractStaticAutoFillSource 036{ 037 /** Constant for default value attribute. */ 038 public static final String ATTRIBUTE_DEFAULT_VALUE = "default-value"; 039 040 041 /** Map of ModelItems specific to CustomValueAutoFillSource */ 042 protected Map<String, ModelItem> _autoFillItems; 043 044 public String getAutofillValue(FormQuestion question) 045 { 046 return question.getValue(ATTRIBUTE_DEFAULT_VALUE); 047 } 048 049 public Map<String, ModelItem> getModelItems() 050 { 051 _autoFillItems = new HashMap<>(); 052 053 //DEFAULT VALUE 054 ElementDefinition defaultValue = FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_DEFAULT_VALUE, ModelItemTypeConstants.STRING_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_DEFAULT_VALUE", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_DEFAULT_VALUE_DESC", new DefaultValidator(null, true)); 055 _autoFillItems.put(ATTRIBUTE_DEFAULT_VALUE, defaultValue); 056 057 return _autoFillItems; 058 } 059 060 public List<ViewItem> getViewElements() 061 { 062 List<ViewItem> viewElements = new ArrayList<>(); 063 064 ViewElement defaultValue = new ViewElement(); 065 defaultValue.setDefinition((ElementDefinition< ? >) _autoFillItems.get(ATTRIBUTE_DEFAULT_VALUE)); 066 viewElements.add(defaultValue); 067 068 return viewElements; 069 } 070 071 public boolean isCacheable(FormQuestion question) 072 { 073 return true; 074 } 075 076}