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.question.computing;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.user.CurrentUserProvider;
027import org.ametys.core.user.User;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.core.user.UserManager;
030import org.ametys.plugins.forms.helper.FormElementDefinitionHelper;
031import org.ametys.plugins.forms.repository.FormEntry;
032import org.ametys.plugins.forms.repository.FormQuestion;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.model.ElementDefinition;
035import org.ametys.runtime.model.ModelItem;
036import org.ametys.runtime.model.StaticEnumerator;
037import org.ametys.runtime.model.ViewElement;
038import org.ametys.runtime.model.ViewItem;
039import org.ametys.runtime.model.type.ModelItemTypeConstants;
040
041/**
042 * Class for creating user computed field
043 */
044public class UserComputingType extends AbstractStaticComputingType
045{
046    /** The user info attribute */
047    public static final String ATTRIBUTE_USER_INFO = "user-info";
048    
049    /** Name of email userInfoStaticEnumerator entry */
050    public static final String EMAIL_USER_VALUE = "email";
051    /** Name of id userInfoStaticEnumerator entry */
052    public static final String ID_USER_VALUE = "id";
053    /** Name of fullname userInfoStaticEnumerator entry */
054    public static final String FULLNAME_USER_VALUE = "fullName";
055    /** Name of firstName userInfoStaticEnumerator entry */
056    public static final String FIRSTNAME_USER_VALUE = "firstName";
057    /** Name of lastName userInfoStaticEnumerator entry */
058    public static final String LASTNAME_USER_VALUE = "lastName";
059    
060    /** The current user provider */
061    protected CurrentUserProvider _currentUserProvider;
062    /** The users manager */
063    protected UserManager _userManager;
064    /** Map of ModelItems specific to ManualSourceType */
065    protected Map<String, ModelItem> _userComputingItems;
066    
067    @Override
068    public void service(ServiceManager manager) throws ServiceException
069    {
070        super.service(manager);
071        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
072        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
073    }
074    
075    public String getComputedValue(FormQuestion computedQuestion, FormEntry entry)
076    {
077        UserIdentity userIdentity = _currentUserProvider.getUser();
078        User user = userIdentity != null ? _userManager.getUser(userIdentity) : null;
079        return user != null ? _getUserInfoValue(computedQuestion, user) : null;
080    }
081
082    public Map<String, ModelItem> getModelItems()
083    {
084        _userComputingItems = new HashMap<>();
085        
086        ElementDefinition<String> userInfos = FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_USER_INFO, ModelItemTypeConstants.STRING_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_COMPUTING_USERS", "PLUGINS_FORMS_QUESTIONS_DIALOG_COMPUTING_USERS_DESC", null);
087        StaticEnumerator<String> userInfoStaticEnumerator = new StaticEnumerator<>();
088        userInfoStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_EMAIL"), EMAIL_USER_VALUE);
089        userInfoStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_ID"), ID_USER_VALUE);
090        userInfoStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_FULLNAME"), FULLNAME_USER_VALUE);
091        userInfoStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_FIRSTNAME"), FIRSTNAME_USER_VALUE);
092        userInfoStaticEnumerator.add(new I18nizableText("plugin.forms", "PLUGINS_FORMS_QUESTIONS_DIALOG_SIMPLE_TEXT_AUTOFILL_LASTNAME"), LASTNAME_USER_VALUE);
093        userInfos.setEnumerator(userInfoStaticEnumerator);
094        userInfos.setDefaultValue(FULLNAME_USER_VALUE);
095        
096        _userComputingItems.put(userInfos.getName(), userInfos);
097       
098        return _userComputingItems;
099    }
100
101    public List<ViewItem> getViewElements()
102    {
103        List<ViewItem> viewElements = new ArrayList<>();
104        
105        ViewElement userSelect = new ViewElement();
106        userSelect.setDefinition((ElementDefinition< ? >) _userComputingItems.get(ATTRIBUTE_USER_INFO));
107        viewElements.add(userSelect);
108        
109        return viewElements;
110    }
111
112   /**
113    * Get user value
114    * @param question the question
115    * @param user the user
116    * @return the user value.<code>null</code> if there is not user value
117    */
118    protected String _getUserInfoValue(FormQuestion question, User user) 
119    {
120        String userInfo = question.getValue(ATTRIBUTE_USER_INFO);
121        switch (userInfo)
122        {
123            case EMAIL_USER_VALUE:
124                return user.getEmail();
125            case ID_USER_VALUE:
126                return user.getIdentity().getLogin(); 
127            case FULLNAME_USER_VALUE:
128                return user.getFullName();
129            case FIRSTNAME_USER_VALUE:
130                return user.getFirstName();
131            case LASTNAME_USER_VALUE:
132                return user.getLastName();
133            default:
134                return null;
135        }
136    }
137
138    public String getStorageType(FormQuestion question)
139    {
140        return ModelItemTypeConstants.STRING_TYPE_ID;
141    }
142    
143    @Override
144    public List<String> getFieldToDisableIfFormPublished()
145    {
146        List<String> fieldNames =  super.getFieldToDisableIfFormPublished();
147        fieldNames.add(ATTRIBUTE_USER_INFO);
148        return fieldNames;
149    }
150}