001/*
002 *  Copyright 2010 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.web.clientsideelement;
017
018import java.util.List;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.contenttype.ContentType;
025import org.ametys.core.ui.Callable;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.web.repository.page.PageDAO;
028
029/**
030 * This element finally creates a gallery button with one item per content type  
031 */
032public class ContentTypesGallery extends org.ametys.cms.clientsideelement.ContentTypesGallery
033{
034    /** The DAO for pages */
035    protected PageDAO _pageDAO;
036    /** Repository content */
037    protected AmetysObjectResolver _resolver;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _pageDAO = (PageDAO) smanager.lookup(PageDAO.ROLE);
044        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
045    }
046    
047    /**
048     * Get the available content types for given page and zone
049     * @param pageId The page's id
050     * @param zoneName The zone's name
051     * @return The available content types
052     */
053    @Callable
054    public List<String> getAvailableContentTypes (String pageId, String zoneName)
055    {
056        return _pageDAO.getAvailableContentTypes(pageId, zoneName).stream()
057                .map(info -> (String) info.get("id"))
058                .collect(Collectors.toList());
059    }
060    
061    @Override
062    protected boolean hasRight(ContentType cType)
063    {
064        // Always returns true, rights will be check later on client side 
065        return true;
066    }
067}