001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.components.fileselect;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035
036import java.util.List;
037import java.util.Set;
038
039import com.google.common.collect.Lists;
040import com.google.common.collect.Sets;
041import com.vaadin.v7.data.Container;
042import com.vaadin.v7.data.Item;
043
044/**
045 * Filter used to hide folder tree items which are not either navigation items themselves or are required to navigate
046 * from the site root to a navigation item.<p>
047 */
048public class CmsNavigationFilter implements Container.Filter {
049
050    /** Serial version id. */
051    private static final long serialVersionUID = 1L;
052
053    /** Paths included by the filter. */
054    private List<String> m_navigationPaths = Lists.newArrayList();
055
056    /**
057     * Creates a new instance.<p>
058     *
059     * @param cms the CMS context
060     * @param root the root folder
061     * @throws CmsException if something goes wrong
062     */
063    public CmsNavigationFilter(CmsObject cms, CmsResource root)
064    throws CmsException {
065
066        CmsObject rootCms = OpenCms.initCmsObject(cms);
067        rootCms.getRequestContext().setSiteRoot("");
068        Set<CmsResource> navResources = Sets.newHashSet(
069            rootCms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT));
070        navResources.addAll(rootCms.readResourcesWithProperty(CmsPropertyDefinition.PROPERTY_NAVPOS));
071        for (CmsResource res : navResources) {
072            if (!res.getRootPath().startsWith("/system/workplace")) {
073                // navigation properties are used for configuration on some workplace resources, so don't include those
074                m_navigationPaths.addAll(getAncestorPaths(res.getRootPath()));
075            }
076        }
077    }
078
079    /**
080     * @see com.vaadin.v7.data.Container.Filter#appliesToProperty(java.lang.Object)
081     */
082    public boolean appliesToProperty(Object propertyId) {
083
084        return false;
085    }
086
087    /**
088     * @see com.vaadin.v7.data.Container.Filter#passesFilter(java.lang.Object, com.vaadin.v7.data.Item)
089     */
090    public boolean passesFilter(Object itemId, Item item) throws UnsupportedOperationException {
091
092        CmsResource res = (CmsResource)item.getItemProperty(CmsResourceTreeContainer.PROPERTY_RESOURCE).getValue();
093        if (res == null) {
094            return true;
095        }
096        return m_navigationPaths.contains(res.getRootPath());
097    }
098
099    /**
100     * Calculates all ancestor paths for a given path.<p>
101     *
102     * @param path the path
103     * @return the list of ancestor paths
104     */
105    List<String> getAncestorPaths(String path) {
106
107        List<String> result = Lists.newArrayList();
108        while (path != null) {
109            result.add(path);
110            path = CmsResource.getParentFolder(path);
111        }
112        return result;
113    }
114
115}