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.plugins.mobileapp.PostConstants;
028import org.ametys.plugins.mobileapp.QueriesHelper;
029import org.ametys.plugins.queriesdirectory.Query;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * Returns the list of feeds for a user
035 */
036public class GetFeedsAction extends AbstractLoggedAction
037{
038    /** The Ametys object resolver */
039    protected QueriesHelper _queryHelper;
040
041    /** Authentication Token Manager */
042    protected SiteManager _siteManager;
043
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _queryHelper = (QueriesHelper) smanager.lookup(QueriesHelper.ROLE);
049        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
050    }
051
052    @Override
053    protected Map<String, Object> doLoggedInAction(Request request, Map<String, Object> jsonParams)
054    {
055        Map<String, Object> result = new HashMap<>();
056
057        String siteName = (String) getParameter(PostConstants.SITE_NAME, jsonParams, request);
058
059        Site site = _siteManager.getSite(siteName);
060
061        List<Query> queries = _queryHelper.getQueries(site);
062
063        List<Map<String, String>> feedList = new ArrayList<>();
064
065        for (Query query : queries)
066        {
067            Map<String, String> queryMap = new HashMap<>();
068            queryMap.put("name", query.getTitle());
069            queryMap.put("feed_id", query.getId());
070            feedList.add(queryMap);
071        }
072
073        result.put("feed_list", feedList);
074
075        return result;
076    }
077
078}