001/*
002 *  Copyright 2015 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.linkdirectory;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.core.cocoon.ActionResultGenerator;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.core.userpref.UserPreferencesException;
035import org.ametys.core.userpref.UserPreferencesManager;
036
037/**
038 * Create or set user preferences attached to a link directory service or the link directory input data mode for front-end users
039 */
040public class LinkDirectorySetUserPreferencesAction extends ServiceableAction
041{
042    /** The user preferences manager. */
043    private UserPreferencesManager _userPrefManager;
044
045    /** The current user provider */
046    private CurrentUserProvider _currentUserProvider;
047
048    private DirectoryHelper _directoryHelper;
049    
050    
051    @Override
052    public void service(ServiceManager serviceManager) throws ServiceException
053    {
054        super.service(serviceManager);
055        _userPrefManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE + ".FO");
056        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
057        _directoryHelper = (DirectoryHelper) serviceManager.lookup(DirectoryHelper.ROLE);
058    }
059    
060    @Override
061    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
062    {
063        Map<String, Object> result = new HashMap<>();
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        
066        // We save user preferences for the FO or BO users
067        UserIdentity user = _currentUserProvider.getUser();
068        if (user != null && StringUtils.isNotEmpty(user.getLogin()) && StringUtils.isNotEmpty(user.getPopulationId()))
069        {
070            String zoneItemId = request.getParameter("saving-id"); // can be empty (from some input data)
071            
072            String storageContext = _directoryHelper.getStorageContext(request, zoneItemId);
073            Map<String, String> contextVars = _directoryHelper.getContextVars(request);
074            
075            _setLinksPositionUserPref(request, storageContext, contextVars, user);
076            _setHiddenLinksUserPref(request, storageContext, contextVars, user);
077            
078            request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
079        }
080        
081        return EMPTY_MAP;
082    }
083
084    /**
085     * Set the links position user pref
086     * @param request the request
087     * @param storageContext the storage context
088     * @param contextVars the context vars
089     * @param user the current user
090     * @throws UserPreferencesException If failed to save user preferences
091     */
092    private void _setLinksPositionUserPref(Request request, String storageContext, Map<String, String> contextVars, UserIdentity user) throws UserPreferencesException
093    {
094        String linksPosition = _getLinksPosition(request);
095        
096        // If null, we don't change the user pref.
097        // However, if empty, we set to empty the user pref. 
098        if (linksPosition != null)  
099        {
100            // TODO it would be nice to change the name of this user pref but the storage is still the same, so for the moment we avoid the SQL migration
101            // Cf issue LINKS-141
102            // Change in org.ametys.plugins.linkdirectory.DirectoryHelper#saxLinks too
103            
104            _userPrefManager.addUserPreference(user, storageContext, contextVars, "checked-links", linksPosition);
105        }
106    }
107    
108    /**
109     * Get the ordered link ids from the request
110     * @param request the request
111     * @return the comma-separated string of ordered links ids
112     */
113    private String _getLinksPosition(Request request)
114    {
115        return request.getParameter("ordered-links");
116    }
117    
118    /**
119     * Set the hidden links user pref
120     * @param request the request
121     * @param storageContext the storage context
122     * @param contextVars the context vars
123     * @param user the current user
124     * @throws UserPreferencesException if failed to save user preferences
125     */
126    private void _setHiddenLinksUserPref(Request request, String storageContext, Map<String, String> contextVars, UserIdentity user) throws UserPreferencesException
127    {
128        String hiddenLinks = _getHiddenLinks(request);
129        
130        // If null, we don't change the user pref.
131        // However, if empty, we set to empty the user pref. 
132        if (hiddenLinks != null)  
133        {
134            _userPrefManager.addUserPreference(user, storageContext, contextVars, "hidden-links", hiddenLinks);
135        }
136        
137    }
138    
139    /**
140     * Get the hidden link ids from the request
141     * @param request the request
142     * @return the comma-separated string of hidden links ids
143     */
144    private String _getHiddenLinks(Request request)
145    {
146        return request.getParameter("hidden-links");
147    }
148}