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.userdirectory.forms;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang.StringUtils;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
030import org.ametys.core.user.User;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.forms.dao.FormDAO;
033import org.ametys.plugins.forms.question.autofill.AbstractStaticAutoFillSource;
034import org.ametys.plugins.forms.repository.FormQuestion;
035import org.ametys.runtime.model.ElementDefinition;
036import org.ametys.runtime.model.ModelItem;
037import org.ametys.runtime.model.ViewElement;
038import org.ametys.runtime.model.ViewItem;
039import org.ametys.runtime.model.type.ModelItemTypeConstants;
040
041/**
042 * Class for adding user directory autofill to SimpleTextQUestionType
043 */
044public class UDUserAutoFillSource extends AbstractStaticAutoFillSource 
045{
046    /** Map of ModelItems specific to DefaultStaticAutoFillSource */
047    protected Map<String, ModelItem> _defaultAutoFillItems;
048    
049    /** The content type extension point */
050    protected ContentTypeExtensionPoint _contentTypeEP;
051    
052    /** The form question ud helper */
053    protected FormQuestionUDHelper _formQuestionUDHelper;
054    
055    /** The form DAO */
056    protected FormDAO _formDAO;
057    
058    @Override
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        super.service(manager);
062        _formQuestionUDHelper = (FormQuestionUDHelper) manager.lookup(FormQuestionUDHelper.ROLE);
063        _contentTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
064        _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE);
065    }
066    
067    public String getAutofillValue(FormQuestion question)
068    {
069        UserIdentity userIdentity = _currentUserProvider.getUser();
070        return (String) _formQuestionUDHelper.getUserContentAttributeValue(question, userIdentity);
071    }
072
073    public Map<String, ModelItem> getModelItems()
074    {
075        _defaultAutoFillItems = _formQuestionUDHelper.getUDModelItems(List.of(ModelItemTypeConstants.STRING_TYPE_ID));
076        return _defaultAutoFillItems;
077    }
078
079    public List<ViewItem> getViewElements()
080    {
081        List<ViewItem> viewElements = new ArrayList<>();
082        
083        for (String attributeId : _defaultAutoFillItems.keySet())
084        {
085            ViewElement userSelect = new ViewElement();
086            userSelect.setDefinition((ElementDefinition< ? >) _defaultAutoFillItems.get(attributeId));
087            viewElements.add(userSelect);
088        }
089        
090        return viewElements;
091    }
092
093    @Override
094    public void saxAdditionalInfos(ContentHandler contentHandler, FormQuestion question) throws SAXException
095    {
096        UserIdentity userIdentity = _currentUserProvider.getUser();
097        User user = userIdentity != null ? _userManager.getUser(userIdentity) : null;
098        String autofill = user != null ? getAutofillValue(question) : null;
099        if (StringUtils.isNotBlank(autofill))
100        {
101            XMLUtils.createElement(contentHandler, "default-value", autofill);
102        }
103    }
104    
105    public boolean isCacheable(FormQuestion question)
106    {
107        return false;
108    }
109}