001/*
002 *  Copyright 2011 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.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.core.right.RightManager.RightResult;
028import org.ametys.core.ui.SimpleMenu;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.synchronization.SynchronizeComponent;
033
034/**
035 * Abstract class for a {@link SimpleMenu}
036 */
037public abstract class AbstractPageMenu extends SimpleMenu
038{
039    /** The Ametys object resolver */
040    protected AmetysObjectResolver _resolver;
041    /** The synchronize helper */
042    protected SynchronizeComponent _synchronizeComponent;
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
049        _synchronizeComponent = (SynchronizeComponent) smanager.lookup(SynchronizeComponent.ROLE);
050    }
051    
052    /**
053     * Get the default page's parameters
054     * @param page The page
055     * @return The default parameters
056     */
057    protected Map<String, Object> getPageDefaultParameters (Page page)
058    {
059        Map<String, Object> pageParams = new HashMap<>();
060        pageParams.put("id", page.getId());
061        pageParams.put("title", page.getTitle());
062        
063        return pageParams;
064    }
065    
066    /**
067     * Determines if user has convenient right on page
068     * @param page The page
069     * @return true if the user has convenient right
070     */
071    protected boolean hasRight (Page page)
072    {
073        if (_rights.isEmpty())
074        {
075            return true;
076        }
077        
078        for (String rightToCheck : _rights.keySet())
079        {
080            if (StringUtils.isNotEmpty(rightToCheck)
081                && _rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, page) == RightResult.RIGHT_ALLOW)
082            {
083                return true;
084            }
085        }
086                
087        return false;
088    }
089    
090    /**
091     * Get the description when user has no right on page
092     * @param page the page
093     * @return the description
094     */
095    protected I18nizableText getNoRightPageDescription (Page page)
096    {
097        List<String> i18nParameters = new ArrayList<>();
098        i18nParameters.add(page.getTitle());
099        
100        I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-page-description");
101        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
102    }
103    
104    /**
105     * Get the description when page is not modifiable
106     * @param page the page
107     * @return the description
108     */
109    protected I18nizableText getNoModifiablePageDescription (Page page)
110    {
111        List<String> i18nParameters = new ArrayList<>();
112        i18nParameters.add(page.getTitle());
113        
114        I18nizableText ed = (I18nizableText) this._script.getParameters().get("nomodifiable-page-description");
115        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
116    }
117    
118    /**
119     * Get the description when page fills all conditions
120     * @param page the page
121     * @return the description
122     */
123    protected I18nizableText getAllRightPageDescription (Page page)
124    {
125        List<String> i18nParameters = new ArrayList<>();
126        i18nParameters.add(page.getTitle());
127        
128        I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-page-description");
129        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
130    }
131
132}