001/*
002 *  Copyright 2019 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.cms.languages.Language;
028import org.ametys.cms.languages.LanguagesManager;
029import org.ametys.core.right.RightManager.RightResult;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.PagesContainer;
033import org.ametys.web.repository.sitemap.Sitemap;
034
035/**
036 * Abstract client side element for pages or sitemap.
037 *
038 */
039public abstract class AbstractPagesContainerClientSideElement extends AbstractPageClientSideElement
040{
041    private LanguagesManager _languageManager;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _languageManager = (LanguagesManager) smanager.lookup(LanguagesManager.ROLE);
048    }
049    
050    /**
051     * Get the default pages container's parameters
052     * @param pagesContainer The page or sitemap
053     * @return The default parameters
054     */
055    protected Map<String, Object> getPagesContainerDefaultParameters (PagesContainer pagesContainer)
056    {
057        if (pagesContainer instanceof Page)
058        {
059            return getPageDefaultParameters((Page) pagesContainer);
060        }
061        else
062        {
063            return getSitemapDefaultParameters((Sitemap) pagesContainer);
064        }
065    }
066    
067    /**
068     * Get the default sitemap's parameters
069     * @param sitemap The sitemap
070     * @return The default parameters
071     */
072    protected Map<String, Object> getSitemapDefaultParameters (Sitemap sitemap)
073    {
074        Map<String, Object> sitemapParams = new HashMap<>();
075        sitemapParams.put("id", sitemap.getId());
076        sitemapParams.put("title", getSitemapTitle(sitemap));
077        sitemapParams.put("name", sitemap.getSitemapName());
078        
079        return sitemapParams;
080    }
081    
082    /**
083     * Get the sitemap's title
084     * @param sitemap the sitemap
085     * @return the sitemap's title
086     */
087    protected I18nizableText getSitemapTitle(Sitemap sitemap)
088    {
089        Language language = _languageManager.getLanguage(sitemap.getName());
090        return language != null ? language.getLabel() : new I18nizableText(sitemap.getName());
091    }
092    
093    /**
094     * Determines if user has convenient right on pages container
095     * @param pagesContainer The page or sitemap
096     * @return true if the user has convenient right
097     */
098    protected boolean hasRight (PagesContainer pagesContainer)
099    {
100        if (pagesContainer instanceof Page)
101        {
102            return hasRight((Page) pagesContainer);
103        }
104        else
105        {
106            return hasRight((Sitemap) pagesContainer);
107        }
108    }
109    /**
110     * Determines if user has convenient right on sitemap
111     * @param sitemap The sitemap
112     * @return true if the user has convenient right
113     */
114    protected boolean hasRight (Sitemap sitemap)
115    {
116        if (_rights.isEmpty())
117        {
118            return true;
119        }
120        
121        for (String rightToCheck : _rights.keySet())
122        {
123            if (StringUtils.isNotEmpty(rightToCheck)
124                && _rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, sitemap) == RightResult.RIGHT_ALLOW)
125            {
126                return true;
127            }
128        }
129                
130        return false;
131    }
132    
133    /**
134     * Get the description when user has no right on pages container
135     * @param pagesContainer The page or sitemap
136     * @return the description
137     */
138    protected I18nizableText getNoRightPagesContainerDescription(PagesContainer pagesContainer)
139    {
140        if (pagesContainer instanceof Page)
141        {
142            return getNoRightPageDescription((Page) pagesContainer);
143        }
144        else
145        {
146            return getNoRightSitemapDescription((Sitemap) pagesContainer);
147        }
148    }
149    
150    /**
151     * Get the description when user has no right on sitemap
152     * @param sitemap The sitemap
153     * @return the description
154     */
155    protected I18nizableText getNoRightSitemapDescription(Sitemap sitemap)
156    {
157        List<String> i18nParameters = new ArrayList<>();
158        i18nParameters.add(sitemap.getSitemapName());
159        
160        I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-sitemap-description");
161        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
162    }
163    
164    /**
165     * Get i18n description for all right pages container
166     * @param pagesContainer The page or sitemap
167     * @return The {@link I18nizableText} description
168     */
169    protected I18nizableText getAllRightPagesContainerDescription(PagesContainer pagesContainer)
170    {
171        if (pagesContainer instanceof Page)
172        {
173            return getAllRightPageDescription((Page) pagesContainer);
174        }
175        else
176        {
177            return getAllRightSitemapDescription((Sitemap) pagesContainer);
178        }
179    }
180    /**
181     * Get i18n description for all right sitemap
182     * @param sitemap The sitemap
183     * @return The {@link I18nizableText} description
184     */
185    protected I18nizableText getAllRightSitemapDescription(Sitemap sitemap)
186    {
187        List<String> i18nParameters = new ArrayList<>();
188        i18nParameters.add(sitemap.getSitemapName());
189        
190        I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-sitemap-description");
191        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
192    }
193}