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