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