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.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.core.ui.Callable;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.UnknownAmetysObjectException;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.Page.LinkType;
031import org.ametys.web.repository.page.Page.PageType;
032
033/**
034 * Page live UI item
035 */
036public class PreviewPageClientSideElement extends AbstractPageClientSideElement
037{
038    /**
039     * Get the preview status of given pages
040     * @param pageIds The page id
041     * @return the result
042     */
043    @Callable
044    public Map<String, Object> getStatus (List<String> pageIds)
045    {
046        Map<String, Object> results = new HashMap<>();
047        
048        results.put("invalid-pages", new ArrayList<Map<String, Object>>());
049        results.put("allright-pages", new ArrayList<Map<String, Object>>());
050        
051        for (String pageId : pageIds)
052        {
053            Page page = _resolver.resolveById(pageId);
054
055            Map<String, Object> pageParams = getPageDefaultParameters(page);
056            
057            if (_isPreviewable(page))
058            {
059                pageParams.put("description", getAllRightPageDescription(page));
060                
061                @SuppressWarnings("unchecked")
062                List<Map<String, Object>> allrightPages = (List<Map<String, Object>>) results.get("allright-pages");
063                allrightPages.add(pageParams);
064            }
065            else
066            {
067                pageParams.put("description", _getInvalidPageDescription(page));
068                
069                @SuppressWarnings("unchecked")
070                List<Map<String, Object>> invalidPages = (List<Map<String, Object>>) results.get("invalid-pages");
071                invalidPages.add(pageParams);
072            }
073        }
074        
075        return results;
076    }
077    
078    /**
079     * Get i18n description for invalid page
080     * @param page The page
081     * @return The {@link I18nizableText} description
082     */
083    private I18nizableText _getInvalidPageDescription (Page page)
084    {
085        List<String> i18nParameters = new ArrayList<>();
086        i18nParameters.add(page.getTitle());
087        
088        I18nizableText ed = (I18nizableText) this._script.getParameters().get("invalid-page-description");
089        return  new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
090    }
091    
092
093    /**
094     * Determine if this page is previewable
095     * @param page The page to look at
096     * @return true if the page can be previewed
097     */
098    private boolean _isPreviewable(Page page)
099    {
100        // Check for infinitive loop redirection
101        ArrayList<String> pagesSequence = new ArrayList<>();
102        pagesSequence.add(page.getId());
103        if (_isInfiniteRedirection (page, pagesSequence))
104        {
105            getLogger().error("An infinite loop redirection was detected for page '" + page.getPathInSitemap() + "'");
106            return false;
107        }
108        
109        if (page.getType() == PageType.LINK && LinkType.PAGE.equals(page.getURLType()))
110        {
111            return _isPageExist(page.getURL()) && _isPreviewable((Page) _resolver.resolveById(page.getURL()));
112        }
113        
114        if (page.getType() != PageType.NODE)
115        {
116            return true;
117        }
118        else
119        {
120            for (Page subPage : page.getChildrenPages())
121            {
122                if (_isPreviewable(subPage))
123                {
124                    return true;
125                }
126            }
127        }
128        return false;
129    }
130    
131    private boolean _isPageExist (String id)
132    {
133        try
134        {
135            _resolver.resolveById(id);
136            return true;
137        }
138        catch (UnknownAmetysObjectException e)
139        {
140            return false;
141        }
142    }
143    
144    private boolean _isInfiniteRedirection (Page page, List<String> pagesSequence)
145    {
146        Page redirectPage = _getPageRedirection (page);
147        if (redirectPage == null)
148        {
149            return false;
150        }
151        
152        if (pagesSequence.contains(redirectPage.getId()))
153        {
154            return true;
155        }
156        
157        pagesSequence.add(redirectPage.getId());
158        return _isInfiniteRedirection (redirectPage, pagesSequence);
159    }
160    
161    private Page _getPageRedirection (Page page)
162    {
163        if (PageType.LINK.equals(page.getType()) && LinkType.PAGE.equals(page.getURLType()))
164        {
165            try
166            {
167                String pageId = page.getURL();
168                return _resolver.resolveById(pageId);
169            }
170            catch (AmetysRepositoryException e)
171            {
172                return null;
173            }
174        }
175        else if (PageType.NODE.equals(page.getType()))
176        {
177            AmetysObjectIterable<? extends Page> childPages = page.getChildrenPages();
178            Iterator<? extends Page> it = childPages.iterator();
179            if (it.hasNext())
180            {
181                return it.next();
182            }
183        }
184        
185        return null;
186    }
187}