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 */
016package org.ametys.web.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.core.right.RightManager.RightResult;
026import org.ametys.core.ui.Callable;
027import org.ametys.core.ui.ClientSideElement;
028import org.ametys.web.repository.page.ModifiablePage;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.SitemapElement;
031
032/**
033 * This {@link ClientSideElement} creates a button to delete a page
034 *
035 */
036public class DeletePageClientSideElement extends AbstractPageClientSideElement
037{
038    /**
039     * Get the pages' status
040     * @param pageIds The page ids
041     * @return the page status
042     */
043    @Callable
044    public Map<String, Object> getStatus (List<String> pageIds)
045    {
046        Map<String, Object> results = new HashMap<>();
047        
048        results.put("nomodifiable-pages", new ArrayList<>());
049        results.put("noright-pages", new ArrayList<>());
050        results.put("allright-pages", new ArrayList<>());
051        
052        for (String pageId : pageIds)
053        {
054            Page page = _resolver.resolveById(pageId);
055            
056            if (!(page instanceof ModifiablePage))
057            {
058                Map<String, Object> pageParams = getPageDefaultParameters(page);
059                pageParams.put("description", getNoModifiablePageDescription(page));
060
061                @SuppressWarnings("unchecked")
062                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("nomodifiable-pages");
063                norightPages.add(pageParams);
064            }
065            else if (!hasRight(page))
066            {
067                Map<String, Object> pageParams = getPageDefaultParameters(page);
068                pageParams.put("description", getNoRightPageDescription(page));
069
070                @SuppressWarnings("unchecked")
071                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages");
072                norightPages.add(pageParams);
073            }
074            else
075            {
076                @SuppressWarnings("unchecked")
077                List<Map<String, Object>> allRightPages = (List<Map<String, Object>>) results.get("allright-pages");
078                Map<String, Object> pageParams = getPageDefaultParameters(page);
079                pageParams.put("description", getAllRightPageDescription(page));
080                allRightPages.add(pageParams);
081            }
082        }
083        
084        return results;
085    }
086    
087    @Override
088    protected boolean hasRight(SitemapElement sitemapElement)
089    {
090        if (_rights.isEmpty())
091        {
092            return true;
093        }
094        
095        // The right is check to parent page
096        for (String rightToCheck : _rights.keySet())
097        {
098            if (StringUtils.isNotEmpty(rightToCheck)
099                && _rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, sitemapElement.getParent()) == RightResult.RIGHT_ALLOW)
100            {
101                return true;
102            }
103        }
104                
105        return false;
106    }
107}