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