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.ametys.core.ui.Callable;
024import org.ametys.core.ui.ClientSideElement;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.web.repository.page.ModifiablePage;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.page.Page.PageType;
029
030/**
031 * This {@link ClientSideElement} creates a button representing the type of page
032 *
033 */
034public class BlankPageClientSideElement extends AbstractPageClientSideElement
035{
036    /**
037     * Get the pages' status
038     * @param pageIds The page ids
039     * @return the page status
040     */
041    @Callable
042    public Map<String, Object> getStatus (List<String> pageIds)
043    {
044        Map<String, Object> results = new HashMap<>();
045        
046        results.put("nomodifiable-pages", new ArrayList<Map<String, Object>>());
047        results.put("noright-pages", new ArrayList<Map<String, Object>>());
048        results.put("blank-pages", new ArrayList<Map<String, Object>>());
049        results.put("noblank-pages", new ArrayList<Map<String, Object>>());
050        
051        for (String pageId : pageIds)
052        {
053            Page page = _resolver.resolveById(pageId);
054            
055            if (!(page instanceof ModifiablePage))
056            {
057                Map<String, Object> pageParams = getPageDefaultParameters(page);
058                pageParams.put("description", getNoModifiablePageDescription(page));
059
060                @SuppressWarnings("unchecked")
061                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("nomodifiable-pages");
062                norightPages.add(pageParams);
063            }
064            
065            if (!hasRight(page))
066            {
067                Map<String, Object> pageParams = getPageDefaultParameters(page);
068                pageParams.put("description", getNoRightPageDescription(page));
069
070                @SuppressWarnings("unchecked")
071                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages");
072                norightPages.add(pageParams);
073            }
074            
075            Map<String, Object> pageParams = getPageDefaultParameters(page);
076            
077            if (page.getType() == PageType.NODE)
078            {
079                @SuppressWarnings("unchecked")
080                List<Map<String, Object>> blankPages = (List<Map<String, Object>>) results.get("blank-pages");
081                pageParams.put("description", getBlankPageDescription(page));
082                blankPages.add(pageParams);
083            }
084            else
085            {
086                @SuppressWarnings("unchecked")
087                List<Map<String, Object>> noBlankPages = (List<Map<String, Object>>) results.get("noblank-pages");
088                pageParams.put("description", getNoBlankPageDescription(page));
089                noBlankPages.add(pageParams);
090            }
091        }
092        
093        return results;
094    }
095    
096    /**
097     * Get the description the page is a blank page
098     * @param page the page
099     * @return the description
100     */
101    protected I18nizableText getBlankPageDescription (Page page)
102    {
103        List<String> i18nParameters = new ArrayList<>();
104        i18nParameters.add(page.getTitle());
105        
106        I18nizableText ed = (I18nizableText) this._script.getParameters().get("blank-page-description");
107        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
108    }
109    
110    /**
111     * Get the description the page is not a blank page
112     * @param page the page
113     * @return the description
114     */
115    protected I18nizableText getNoBlankPageDescription (Page page)
116    {
117        List<String> i18nParameters = new ArrayList<>();
118        i18nParameters.add(page.getTitle());
119        
120        I18nizableText ed = (I18nizableText) this._script.getParameters().get("noblank-page-description");
121        return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
122    }
123    
124}