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