001/*
002 *  Copyright 2020 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.mobileapp.action;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.environment.Request;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.plugins.mobileapp.PostConstants;
030import org.ametys.plugins.mobileapp.UserPreferencesHelper;
031
032/**
033 * Returns the list of feeds for a user
034 */
035public class SetNotificationsPrefsAction extends AbstractLoggedAction
036{
037    /** Parameter containing the requested feeds for notification */
038    protected static final String _NOTIF_FEEDS = "feeds";
039    
040    /** Parameter containing the requested projects for notification */
041    protected static final String _NOTIF_WORKSPACES = "workspaces";
042    
043    /** Parameter containing the requested projects names for notification */
044    protected static final String _NOTIF_PROJECTS = "projects";
045    
046    /** Parameter containing the requested projects for notification */
047    protected static final String _NOTIF_TYPES = "types";
048    
049    /** User Preferences Helper */
050    protected UserPreferencesHelper _userPreferencesHelper;
051    
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _userPreferencesHelper = (UserPreferencesHelper) smanager.lookup(UserPreferencesHelper.ROLE);
057    }
058    
059    @Override
060    protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams)
061    {
062        Map<String, Object> result = new HashMap<>();
063        String lang = (String) getParameter(PostConstants.LANG, jsonParams, request);
064
065        String pushToken = (String) getParameter(PostConstants.NOTIF_TOKEN, jsonParams, request);
066        
067        @SuppressWarnings("unchecked")
068        List<String> feeds = (List<String>) getParameter(_NOTIF_FEEDS, jsonParams, request);
069        
070        @SuppressWarnings("unchecked")
071        Map<String, List<String>> workspaces = (Map<String, List<String>>) getParameter(_NOTIF_WORKSPACES, jsonParams, request);
072        List<String> projects = (workspaces != null && workspaces.containsKey(_NOTIF_PROJECTS)) ? workspaces.get(_NOTIF_PROJECTS) : Collections.EMPTY_LIST;
073        List<String> types = (workspaces != null && workspaces.containsKey(_NOTIF_TYPES)) ? workspaces.get(_NOTIF_TYPES) : Collections.EMPTY_LIST;
074        
075        // Remove null items
076        feeds = feeds.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
077        types = types.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
078        projects = projects.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
079        
080        _userPreferencesHelper.setNotificationSettings(pushToken, feeds, projects, types, lang);
081        
082        return result;
083    }
084}