001/*
002 *  Copyright 2011 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.web.userpref;
017
018import java.util.Arrays;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.SourceResolver;
030
031import org.ametys.core.observation.Event;
032import org.ametys.core.observation.ObservationManager;
033import org.ametys.core.user.CurrentUserProvider;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.core.userpref.UserPreferencesExtensionPoint;
036import org.ametys.core.userpref.UserPreferencesManager;
037import org.ametys.plugins.core.userpref.SetUserPreferencesAction;
038import org.ametys.web.ObservationConstants;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.repository.site.SiteManager;
041
042/**
043 * Action which saves the user preferences values into the database.
044 */
045public class SetFOUserPreferencesAction extends SetUserPreferencesAction
046{
047    /** The observation manager. */
048    protected ObservationManager _observationManager;
049    /** The site manager */
050    protected SiteManager _siteManager;
051    /** The current user provider */
052    protected CurrentUserProvider _currentUserProvider;
053    
054    @Override
055    public void service(ServiceManager serviceManager) throws ServiceException
056    {
057        super.service(serviceManager);
058        // Lookup the front-office preferences EP and manager instead of those in the runtime.
059        _userPrefEP = (UserPreferencesExtensionPoint) serviceManager.lookup(UserPreferencesExtensionPoint.ROLE + ".FO");
060        _userPrefManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE + ".FO");
061        _observationManager = (ObservationManager) serviceManager.lookup(ObservationManager.ROLE);
062        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
063        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
064    }
065    
066    @Override
067    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
068    {
069        Request request = ObjectModelHelper.getRequest(objectModel);
070        @SuppressWarnings("unchecked")
071        Map<String, Object> parentContext = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
072        
073        String storageContext = getStorageContext(request, parameters);
074        UserIdentity user = getUser(request, parameters);
075        
076        String submit = request.getParameter("submit");
077        
078        Map<String, String> results = new HashMap<>();
079        
080        if ("true".equals(submit))
081        {
082            List<String> preferenceIds = getPreferenceIds(request, parentContext);
083            Map<String, String> contextVars = getContextVars(request);
084            
085            results = setUserPreferences(request, storageContext, contextVars, user, preferenceIds);
086            
087            String siteName = (String) request.getAttribute("site");
088            
089            notifyUserPrefChanged(siteName, storageContext);
090        }
091        
092        return results;
093    }
094    
095    /**
096     * Notify observers that user preferences has changed
097     * @param siteName The site name
098     * @param context The user's preferences context
099     */
100    protected void notifyUserPrefChanged(String siteName, String context)
101    {
102        // Send an event indicating that the user preferences have changed.
103        // The target is the username, the context is provided as the single parameter.
104        Site site = _siteManager.getSite(siteName);
105        
106        Map<String, Object> eventParams = new HashMap<>();
107        eventParams.put(ObservationConstants.ARGS_SITE, site);
108        eventParams.put("context", context);
109        _observationManager.notify(new Event(ObservationConstants.EVENT_USER_PREFERENCES_CHANGED, _getCurrentUser(), eventParams));
110    }
111    
112    @SuppressWarnings("unchecked")
113    @Override
114    protected List<String> getPreferenceIds(Request request, Map<String, Object> parentContext)
115    {
116        Object preferenceIds = parentContext.get("preferences-ids");
117        if (preferenceIds instanceof String[])
118        {
119            // submited form
120            return Arrays.asList((String[]) preferenceIds);
121        }
122        else
123        {
124            // sent by ServerComm
125            return (List<String>) preferenceIds;
126        }
127    }
128    
129    @Override
130    protected Map<String, String> getContextVars(Request request)
131    {
132        Map<String, String> contextVars = new HashMap<>(super.getContextVars(request));
133        
134        String siteName = (String) request.getAttribute("site");
135        String language = (String) request.getAttribute("sitemapLanguage");
136        
137        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName);
138        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_LANGUAGE, language);
139        
140        return contextVars;
141    }
142    
143}