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.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;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.core.cocoon.ActionResultGenerator;
028import org.ametys.plugins.mobileapp.FeedHelper;
029import org.ametys.plugins.mobileapp.PostConstants;
030import org.ametys.runtime.plugin.PluginsManager;
031
032/**
033 * Returns the list of feeds for a user
034 */
035public class GetProjectsFeedAction extends AbstractLoggedAction
036{
037    /** Feed Helper */
038    private FeedHelper _feedHelper;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        if (PluginsManager.getInstance().isPluginActive("workspaces"))
045        {
046            _feedHelper = (FeedHelper) smanager.lookup(FeedHelper.ROLE);
047        }
048    }
049    
050    @Override
051    protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams)
052    {
053        if (_feedHelper != null)
054        {
055            Object result = request.getAttribute(ActionResultGenerator.MAP_REQUEST_ATTR);
056            if (result instanceof Map<?, ?>)
057            {
058                @SuppressWarnings("unchecked")
059                Map<String, Object> resultMap = (Map<String, Object>) result;
060                
061                String lang = (String) getParameter(PostConstants.LANG, jsonParams, request);
062                
063                Map<String, Map<String, Object>> projects = _feedHelper.getProjects();
064                
065                @SuppressWarnings("unchecked")
066                List<Map<String, Object>> resultList = ((List<Map<String, Object>>) resultMap.get("events")).stream()
067                                                                                                            .sorted((event1, event2) -> ((String) event2.get("date")).compareTo((String) event1.get("date")))
068                                                                                                            .toList();
069                
070                List<Map<String, Object>> jsonResult = new ArrayList<>();
071                for (Object object : resultList)
072                {
073                    Map<String, Object> addActivityInfos;
074                    if (object instanceof Map)
075                    {
076                        @SuppressWarnings("unchecked")
077                        Map<String, Object> activity = (Map<String, Object>) object;
078                        Map<String, Object> project = projects.get(activity.get("projectName"));
079                        addActivityInfos = _feedHelper.getActivityInfos(activity, project, lang);
080                    }
081                    else
082                    {
083                        addActivityInfos = new HashMap<>();
084                    }
085                    if (addActivityInfos != null)
086                    {
087                        jsonResult.add(addActivityInfos);
088                    }
089                }
090                
091                Map<String, Object> output = new HashMap<>();
092                output.put("items", jsonResult);
093                return output;
094            }
095            
096            // Nom de l'action (membre ajouté, nouvel évènement, etc…)
097            // Short description
098        }
099        return EMPTY_MAP;
100    }
101}