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 depth The depth to get. 1 for root pages, 0 for this node only, -1 for infinite.
045     * @param includeInvisiblePages Should return child invisible pages
046     */
047    public PageElement(Page page, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity, long depth, boolean includeInvisiblePages)
048    {
049        this(page, null, rightManager, renderingContextHandler, currentPagePath, userIdentity, depth, includeInvisiblePages);
050    }
051    
052    /**
053     * Constructor.
054     * @param page the wrapped {@link Page}.
055     * @param parent the parent container.
056     * @param rightManager the right manager
057     * @param currentPagePath the path of the current page, or null if none.
058     * @param renderingContextHandler the {@link RenderingContextHandler}.
059     * @param userIdentity the login of the current user, or null if none.
060     * @param depth The depth to get. 1 for root pages, 0 for this node only, -1 for infinite.
061     * @param includeInvisiblePages Should return child invisible pages
062     */
063    public PageElement(Page page, AbstractPagesContainerElement<PagesContainer> parent, RightManager rightManager, RenderingContextHandler renderingContextHandler, String currentPagePath, UserIdentity userIdentity, long depth, boolean includeInvisiblePages)
064    {
065        super(page, parent, rightManager, renderingContextHandler, currentPagePath, userIdentity, depth, includeInvisiblePages);
066    }
067    
068    @Override
069    public String getTagName()
070    {
071        return "page";
072    }
073    
074    @Override
075    protected Map<String, AmetysAttribute> _lookupAttributes()
076    {
077        Map<String, AmetysAttribute> result = new LinkedHashMap<>();
078        
079        String path = _object.getPathInSitemap();
080        PageType type = _object.getType();
081        
082        result.put(NAMESPACE_PREFIX + ":id", new AmetysAttribute("id", NAMESPACE_PREFIX + ":id", NAMESPACE_URI, _object.getId(), this));
083        result.put(NAMESPACE_PREFIX + ":name", new AmetysAttribute("name", NAMESPACE_PREFIX + ":name", NAMESPACE_URI, _object.getName(), this));
084        result.put(NAMESPACE_PREFIX + ":title", new AmetysAttribute("title", NAMESPACE_PREFIX + ":title", NAMESPACE_URI, _object.getTitle(), this));
085        result.put(NAMESPACE_PREFIX + ":long-title", new AmetysAttribute("long-title", NAMESPACE_PREFIX + ":long-title", NAMESPACE_URI, _object.getLongTitle(), this));
086        result.put(NAMESPACE_PREFIX + ":path", new AmetysAttribute("path", NAMESPACE_PREFIX + ":path", NAMESPACE_URI, path, this));
087        result.put(NAMESPACE_PREFIX + ":type", new AmetysAttribute("type", NAMESPACE_PREFIX + ":type", NAMESPACE_URI, type.name(), this));
088        
089        if (!_rightManager.hasAnonymousReadAccess(_object))
090        {
091            result.put(NAMESPACE_PREFIX + ":restricted", new AmetysAttribute("restricted", NAMESPACE_PREFIX + ":restricted", NAMESPACE_URI, "true", this));
092        }
093        
094        // Add a flag if there is data
095        if (type == PageType.CONTAINER)
096        {
097            result.put(NAMESPACE_PREFIX + ":container", new AmetysAttribute("container", NAMESPACE_PREFIX + ":container", NAMESPACE_URI, "true", this));
098        }
099        // Add URL if found
100        else if (type == PageType.LINK)
101        {
102            result.put(NAMESPACE_PREFIX + ":link", new AmetysAttribute("link", NAMESPACE_PREFIX + ":link", NAMESPACE_URI, _object.getURL(), this));
103            result.put(NAMESPACE_PREFIX + ":link-type", new AmetysAttribute("link-type", NAMESPACE_PREFIX + ":link-type", NAMESPACE_URI, _object.getURLType().name(), this));
104        }
105
106        // Current status (in-path, current)
107        
108        // if the page is in the path
109        boolean isCurrentPage = _currentPagePath != null ? _currentPagePath.equals(path) : false;
110
111        if ((_currentPagePath != null && _currentPagePath.startsWith(path + "/")) || isCurrentPage)
112        {
113            result.put(NAMESPACE_PREFIX + ":in-path", new AmetysAttribute("in-path", NAMESPACE_PREFIX + ":in-path", NAMESPACE_URI, "true", this));
114        }
115
116        // if the page is the current page
117        if (isCurrentPage)
118        {
119            result.put(NAMESPACE_PREFIX + ":current", new AmetysAttribute("current", NAMESPACE_PREFIX + ":current", NAMESPACE_URI, "true", this));
120        }
121        
122        if (!_object.isVisible())
123        {
124            result.put(NAMESPACE_PREFIX + ":invisible", new AmetysAttribute("invisible", NAMESPACE_PREFIX + ":invisible", NAMESPACE_URI, "true", this));
125        }
126        
127        // Metadata
128        result.putAll(super._lookupAttributes());
129
130        // tags
131        for (String tag : _object.getTags())
132        {
133            // FIXME use nested element not this dirty attributes
134            result.put("PLUGIN_TAGS_" + tag, new AmetysAttribute("PLUGIN_TAGS_" + tag, "PLUGIN_TAGS_" + tag, null, "empty", this));
135        }
136                
137        return result;
138    }
139}