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.core.user.User;
024import org.ametys.core.user.UserIdentity;
025import org.ametys.plugins.forms.helper.FormElementDefinitionHelper;
026import org.ametys.plugins.forms.repository.FormQuestion;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.ElementDefinition;
029import org.ametys.runtime.model.ModelItem;
030import org.ametys.runtime.model.StaticEnumerator;
031import org.ametys.runtime.model.ViewElement;
032import org.ametys.runtime.model.ViewItem;
033import org.ametys.runtime.model.type.ModelItemTypeConstants;
034import org.ametys.runtime.parameter.DefaultValidator;
035
036/**
037 * Class for using common User attributes as default value
038 */
039public class CurrentUserAutoFillSource extends AbstractStaticAutoFillSource 
040{
041    /** Constant for autofill attribute. */
042    public static final String ATTRIBUTE_AUTOFILL = "autofill";
043    
044    /** Name of email autofillStaticEnumerator entry */
045    public static final String EMAIL_AUTOFILL_VALUE = "email";
046    
047    /** Name of id autofillStaticEnumerator entry */
048    public static final String ID_AUTOFILL_VALUE = "id";
049    
050    /** Name of fullname autofillStaticEnumerator entry */
051    public static final String FULLNAME_AUTOFILL_VALUE = "fullName";
052    
053    /** Name of firstName autofillStaticEnumerator entry */
054    public static final String FIRSTNAME_AUTOFILL_VALUE = "firstName";
055    
056    /** Name of lastName autofillStaticEnumerator entry */
057    public static final String LASTNAME_AUTOFILL_VALUE = "lastName";
058    
059
060    /** Map of ModelItems specific to DefaultStaticAutoFillSource */
061    protected Map<String, ModelItem> _defaultAutoFillItems;
062    
063    public String getAutofillValue(FormQuestion question)
064    {
065        UserIdentity userIdentity = _currentUserProvider.getUser();
066        User user = userIdentity != null ? _userManager.getUser(userIdentity) : null;
067        if (user != null)
068        {
069            String autofill = question.getValue(ATTRIBUTE_AUTOFILL);
070            switch (autofill)
071            {
072                case "email":
073                    return user.getEmail();
074                case "id":
075                    return user.getIdentity().getLogin(); 
076                case "fullName":
077                    return user.getFullName();
078                case "firstName":
079                    return user.getFirstName();
080                case "lastName":
081                    return user.getLastName();
082                default:
083                    return null;
084            }
085        }
086        return null;
087    }
088
089    public Map<String, ModelItem> getModelItems()
090    {
091        _defaultAutoFillItems = new HashMap<>();
092        ElementDefinition<String> autofill = FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_AUTOFILL, ModelItemTypeConstants.STRING_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_DESC", new DefaultValidator(null, true));
093
094        StaticEnumerator<String> autofillStaticEnumerator = new StaticEnumerator<>();
095        autofillStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_EMAIL"), EMAIL_AUTOFILL_VALUE);
096        autofillStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_ID"), ID_AUTOFILL_VALUE);
097        autofillStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_FULLNAME"), FULLNAME_AUTOFILL_VALUE);
098        autofillStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_FIRSTNAME"), FIRSTNAME_AUTOFILL_VALUE);
099        autofillStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_LASTNAME"), LASTNAME_AUTOFILL_VALUE);
100        autofill.setEnumerator(autofillStaticEnumerator);
101        autofill.setDefaultValue(EMAIL_AUTOFILL_VALUE);
102        
103        _defaultAutoFillItems.put(ATTRIBUTE_AUTOFILL, autofill);
104        
105        return _defaultAutoFillItems;
106    }
107
108    public List<ViewItem> getViewElements()
109    {
110        List<ViewItem> viewElements = new ArrayList<>();
111        
112        ViewElement autofill = new ViewElement();
113        autofill.setDefinition((ElementDefinition< ? >) _defaultAutoFillItems.get(ATTRIBUTE_AUTOFILL));
114        viewElements.add(autofill);
115        
116        return viewElements;
117    }
118
119    public boolean isCacheable(FormQuestion question)
120    {
121        return false;
122    }
123
124}