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.web.repository.page.ModifiablePage;
026import org.ametys.web.repository.page.Page;
027import org.ametys.web.repository.page.Page.PageType;
028
029/**
030 * This {@link ClientSideElement} creates a button representing the type of page
031 *
032 */
033public class LinkPageClientSideElement extends AbstractPageMenu
034{
035    /**
036     * Get the pages' status
037     * @param pageIds The page ids
038     * @return the page status
039     */
040    @Callable
041    public Map<String, Object> getStatus (List<String> pageIds)
042    {
043        Map<String, Object> results = new HashMap<>();
044        
045        results.put("nomodifiable-pages", new ArrayList<Map<String, Object>>());
046        results.put("noright-pages", new ArrayList<Map<String, Object>>());
047        results.put("allright-pages", new ArrayList<Map<String, Object>>());
048        
049        for (String pageId : pageIds)
050        {
051            Page page = _resolver.resolveById(pageId);
052            
053            if (!(page instanceof ModifiablePage))
054            {
055                Map<String, Object> pageParams = getPageDefaultParameters(page);
056                pageParams.put("description", getNoModifiablePageDescription(page));
057
058                @SuppressWarnings("unchecked")
059                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("nomodifiable-pages");
060                norightPages.add(pageParams);
061            }
062            else if (!hasRight(page))
063            {
064                Map<String, Object> pageParams = getPageDefaultParameters(page);
065                pageParams.put("description", getNoRightPageDescription(page));
066
067                @SuppressWarnings("unchecked")
068                List<Map<String, Object>> norightPages = (List<Map<String, Object>>) results.get("noright-pages");
069                norightPages.add(pageParams);
070            }
071            else
072            {
073                Map<String, Object> pageParams = getPageDefaultParameters(page);
074                pageParams.put("description", getAllRightPageDescription(page));
075                
076                if (page.getType() == PageType.LINK)
077                {
078                    pageParams.put("url", page.getURL());
079                    pageParams.put("urlType", page.getURLType());
080                }
081                
082                @SuppressWarnings("unchecked")
083                List<Map<String, Object>> allRightPages = (List<Map<String, Object>>) results.get("allright-pages");
084                allRightPages.add(pageParams);
085            }
086            
087        }
088        
089        return results;
090    }
091}