001/*
002 *  Copyright 2015 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 */
016
017package org.ametys.odf.catalog;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032
033import org.ametys.core.cocoon.JSonReader;
034
035/**
036 * Get the catalogs
037 *
038 */
039public class GetCatalogsAction extends ServiceableAction
040{
041    /** The catalog manager */
042    private CatalogsManager _catalogsManager;
043    
044    /** The catalog DAO */
045    private CatalogDAO _catalogDAO;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _catalogsManager = (CatalogsManager) serviceManager.lookup(CatalogsManager.ROLE);
052        _catalogDAO = (CatalogDAO) serviceManager.lookup(CatalogDAO.ROLE);
053    }
054    
055    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
056    {
057        Map<String, Object> result = new HashMap<>();
058        
059        List<Map<String, Object>> catalogsProperties = new ArrayList<>();
060        
061        Request request = ObjectModelHelper.getRequest(objectModel);
062        
063        List<Catalog> catalogs = _catalogsManager.getCatalogs();
064        
065        for (Catalog catalog : catalogs)
066        {
067            catalogsProperties.add(_catalogDAO.getCatalogProperties(catalog));
068        }
069        
070        result.put("catalogs", catalogsProperties);
071        
072        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
073        return EMPTY_MAP;
074    }
075
076}