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