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