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