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 */
016
017package org.ametys.web.repository.dom;
018
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.w3c.dom.Element;
023
024import org.ametys.core.right.RightManager;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.core.util.dom.AmetysAttribute;
027import org.ametys.web.renderingcontext.RenderingContextHandler;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.page.Page.PageType;
030import org.ametys.web.repository.page.PagesContainer;
031
032/**
033 * DOM {@link Element} wrapping a {@link Page}.
034 */
035public class PageElement extends AbstractPagesContainerElement<Page>
036{
037    /**
038     * Constructor.
039     * @param page the wrapped {@link Page}.
040     * @param rightManager the right manager
041     * @param currentPagePath the path of the current page, or null if none.
042     * @param renderingContextHandler the {@link RenderingContextHandler}.
043     * @param userIdentity the login of the current user, or null if none.
044     * @param includeInvisiblePages Should return child invisible pages
045     */
046    public PageElement(Page page, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity, boolean includeInvisiblePages)
047    {
048        this(page, null, rightManager, renderingContextHandler, currentPagePath, userIdentity, includeInvisiblePages);
049    }
050    
051    /**
052     * Constructor.
053     * @param page the wrapped {@link Page}.
054     * @param parent the parent container.
055     * @param rightManager the right manager
056     * @param currentPagePath the path of the current page, or null if none.
057     * @param renderingContextHandler the {@link RenderingContextHandler}.
058     * @param userIdentity the login of the current user, or null if none.
059     * @param includeInvisiblePages Should return child invisible pages
060     */
061    public PageElement(Page page, AbstractPagesContainerElement<PagesContainer> parent, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity, boolean includeInvisiblePages)
062    {
063        super(page, parent, rightManager, renderingContextHandler, currentPagePath, userIdentity, includeInvisiblePages);
064    }
065    
066    @Override
067    public String getTagName()
068    {
069        return "page";
070    }
071    
072    @Override
073    protected Map<String, AmetysAttribute> _lookupAttributes()
074    {
075        Map<String, AmetysAttribute> result = new LinkedHashMap<>();
076        
077        String path = _object.getPathInSitemap();
078        PageType type = _object.getType();
079        
080        result.put(NAMESPACE_PREFIX + ":id", new AmetysAttribute("id", NAMESPACE_PREFIX + ":id", NAMESPACE_URI, _object.getId(), this));
081        result.put(NAMESPACE_PREFIX + ":name", new AmetysAttribute("name", NAMESPACE_PREFIX + ":name", NAMESPACE_URI, _object.getName(), this));
082        result.put(NAMESPACE_PREFIX + ":title", new AmetysAttribute("title", NAMESPACE_PREFIX + ":title", NAMESPACE_URI, _object.getTitle(), this));
083        result.put(NAMESPACE_PREFIX + ":long-title", new AmetysAttribute("long-title", NAMESPACE_PREFIX + ":long-title", NAMESPACE_URI, _object.getLongTitle(), this));
084        result.put(NAMESPACE_PREFIX + ":path", new AmetysAttribute("path", NAMESPACE_PREFIX + ":path", NAMESPACE_URI, path, this));
085        result.put(NAMESPACE_PREFIX + ":type", new AmetysAttribute("type", NAMESPACE_PREFIX + ":type", NAMESPACE_URI, type.name(), this));
086        
087        if (!_rightManager.hasAnonymousReadAccess(_object))
088        {
089            result.put(NAMESPACE_PREFIX + ":restricted", new AmetysAttribute("restricted", NAMESPACE_PREFIX + ":restricted", NAMESPACE_URI, "true", this));
090        }
091        
092        // Add a flag if there is data
093        if (type == PageType.CONTAINER)
094        {
095            result.put(NAMESPACE_PREFIX + ":container", new AmetysAttribute("container", NAMESPACE_PREFIX + ":container", NAMESPACE_URI, "true", this));
096        }
097        // Add URL if found
098        else if (type == PageType.LINK)
099        {
100            result.put(NAMESPACE_PREFIX + ":link", new AmetysAttribute("link", NAMESPACE_PREFIX + ":link", NAMESPACE_URI, _object.getURL(), this));
101            result.put(NAMESPACE_PREFIX + ":link-type", new AmetysAttribute("link-type", NAMESPACE_PREFIX + ":link-type", NAMESPACE_URI, _object.getURLType().name(), this));
102        }
103
104        // Current status (in-path, current)
105        
106        // if the page is in the path
107        boolean isCurrentPage = _currentPagePath != null ? _currentPagePath.equals(path) : false;
108
109        if ((_currentPagePath != null && _currentPagePath.startsWith(path + "/")) || isCurrentPage)
110        {
111            result.put(NAMESPACE_PREFIX + ":in-path", new AmetysAttribute("in-path", NAMESPACE_PREFIX + ":in-path", NAMESPACE_URI, "true", this));
112        }
113
114        // if the page is the current page
115        if (isCurrentPage)
116        {
117            result.put(NAMESPACE_PREFIX + ":current", new AmetysAttribute("current", NAMESPACE_PREFIX + ":current", NAMESPACE_URI, "true", this));
118        }
119        
120        if (!_object.isVisible())
121        {
122            result.put(NAMESPACE_PREFIX + ":invisible", new AmetysAttribute("invisible", NAMESPACE_PREFIX + ":invisible", NAMESPACE_URI, "true", this));
123        }
124        
125        // Metadata
126        result.putAll(super._lookupAttributes());
127
128        // tags
129        for (String tag : _object.getTags())
130        {
131            // FIXME use nested element not this dirty attributes
132            result.put("PLUGIN_TAGS_" + tag, new AmetysAttribute("PLUGIN_TAGS_" + tag, "PLUGIN_TAGS_" + tag, null, "empty", this));
133        }
134                
135        return result;
136    }
137}