001/*
002 *  Copyright 2010 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.ametys.core.ui.Callable;
024import org.ametys.core.ui.ClientSideElement;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.repository.page.ModifiablePage;
028import org.ametys.web.repository.page.Page;
029
030/**
031 * This {@link ClientSideElement} creates a button representing the 'visible' status of a {@link Page}
032 *
033 */
034public class PageVisibilityClientSideElement extends AbstractPageClientSideElement
035{
036    
037    /**
038     * Get the visible status of given pages
039     * @param pageIds The page id
040     * @return the result
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("visible-pages", new ArrayList<Map<String, Object>>());
050        results.put("invisible-pages", new ArrayList<Map<String, Object>>());
051        results.put("parent-invisible-pages", new ArrayList<Map<String, Object>>());
052    
053        for (String pageId : pageIds)
054        {
055            Page page = _resolver.resolveById(pageId);
056            
057            Map<String, Object> pageParams = getPageDefaultParameters(page);
058            
059            if (!(page instanceof ModifiablePage))
060            {
061                pageParams.put("description", getNoModifiablePageDescription(page));
062
063                @SuppressWarnings("unchecked")
064                List<Map<String, Object>> nomodifiablePages = (List<Map<String, Object>>) results.get("nomodifiable-pages");
065                nomodifiablePages.add(pageParams);
066            }
067            else if (!hasRight(page))
068            {
069                pageParams.put("description", getNoRightPageDescription(page));
070
071                @SuppressWarnings("unchecked")
072                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages");
073                norightPages.add(pageParams);
074            }
075            else
076            {
077                if (!page.isVisible())
078                {
079                    pageParams.put("description", _getInvisibleDescription(page));
080                    
081                    @SuppressWarnings("unchecked")
082                    List<Map<String, Object>> invisiblePages = (List<Map<String, Object>>) results.get("invisible-pages");
083                    invisiblePages.add(pageParams);
084                }
085                else if (_isParentInvisible(page))
086                {
087                    pageParams.put("description", _getParentInvisibleDescription(page));
088                    
089                    @SuppressWarnings("unchecked")
090                    List<Map<String, Object>> parentInvisiblePages = (List<Map<String, Object>>) results.get("parent-invisible-pages");
091                    parentInvisiblePages.add(pageParams);
092                }
093                else
094                {
095                    pageParams.put("description", _getVisibleDescription(page));
096                    
097                    @SuppressWarnings("unchecked")
098                    List<Map<String, Object>> visiblePages = (List<Map<String, Object>>) results.get("visible-pages");
099                    visiblePages.add(pageParams);
100                    
101                }
102            }
103        }
104        
105        return results;
106    }
107    
108    private I18nizableText _getInvisibleDescription (Page page)
109    {
110        List<String> i18nParameters = new ArrayList<>();
111        i18nParameters.add(page.getTitle());
112        
113        I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-invisible-description");
114        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
115    }
116    
117    private I18nizableText _getParentInvisibleDescription (Page page)
118    {
119        List<String> i18nParameters = new ArrayList<>();
120        i18nParameters.add(page.getTitle());
121        
122        I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-parent-invisible-description");
123        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
124    }
125    
126    private I18nizableText _getVisibleDescription (Page page)
127    {
128        List<String> i18nParameters = new ArrayList<>();
129        i18nParameters.add(page.getTitle());
130        
131        I18nizableText ed = (I18nizableText) this._script.getParameters().get("page-visible-description");
132        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
133    }
134
135    private boolean _isParentInvisible (Page page)
136    {
137        AmetysObject parent = page.getParent();
138        while (parent != null && parent instanceof Page)
139        {
140            boolean invisible = !((Page) parent).isVisible();
141            if (invisible)
142            {
143                return true;
144            }
145            parent = parent.getParent();
146        }
147        return false;
148    }
149}