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            List resultList = (List) resultMap.get("events");
060            List<Map<String, Object>> jsonResult = new ArrayList<>();
061            for (Object object : resultList)
062            {
063                Map<String, Object> addEventInfos;
064                if (object instanceof Map)
065                {
066                    @SuppressWarnings("unchecked")
067                    Map<String, Object> event = (Map<String, Object>) object;
068                    Map<String, Object> project = projects.get(event.get("projectName"));
069                    addEventInfos = _feedHelper.getEventInfos(event, project, lang);
070                }
071                else
072                {
073                    addEventInfos = new HashMap<>();
074                }
075                if (addEventInfos != null)
076                {
077                    jsonResult.add(addEventInfos);
078                }
079            }
080            
081            Map<String, Object> output = new HashMap<>();
082            output.put("items", jsonResult);
083            return output;
084        }
085        
086        // Nom de l'action (membre ajouté, nouvel évènement, etc…)
087        // Short description
088        return EMPTY_MAP;
089    }
090
091}