001/*
002 *  Copyright 2013 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 */
016package org.ametys.web.explorer;
017
018import org.apache.commons.lang.StringUtils;
019
020import org.ametys.plugins.explorer.ExplorerNode;
021import org.ametys.plugins.explorer.resources.Resource;
022import org.ametys.plugins.explorer.resources.ResourceCollection;
023
024/**
025 * Helper methods related to {@link Resource}s and {@link ResourceCollection}s.
026 */
027public final class ResourceHelper
028{
029    
030    /** The shared resource base path in the repository. */
031    public static final String SHARED_RESOURCE_PATH = "/ametys:plugins/web-explorer/shared-resources";
032    
033    /** The shared resource prefix. */
034    public static final String SHARED_RESOURCE_PREFIX = SHARED_RESOURCE_PATH + "/";
035    
036    private ResourceHelper()
037    {
038        // Hides the default constructor.
039    }
040    
041    /**
042     * Test if a resource belongs to a shared tree.
043     * @param resource the resource to test.
044     * @return true if the resource belongs to a shared tree, false otherwise.
045     */
046    public static final boolean isShared(Resource resource)
047    {
048        return isShared(resource.getPath());
049    }
050    
051    /**
052     * Test if a resource collection belongs to a shared tree.
053     * @param collection the resource collection to test.
054     * @return true if the resource collection belongs to a shared tree, false otherwise.
055     */
056    public static final boolean isShared(ResourceCollection collection)
057    {
058        return isShared(collection.getPath());
059    }
060    
061    /**
062     * Test if an explorer node belongs to a shared tree.
063     * @param explorerNode the explorer node to test.
064     * @return true if the explorer node belongs to a shared tree, false otherwise.
065     */
066    public static final boolean isShared(ExplorerNode explorerNode)
067    {
068        return isShared(explorerNode.getPath());
069    }
070    
071    /**
072     * Test if a resource or resource collection belongs to a shared tree.
073     * @param objectPath the path of the resource or resource collection AmetysObject.
074     * @return true if the resource belongs to a shared tree, false otherwise.
075     */
076    public static final boolean isShared(String objectPath)
077    {
078        return objectPath.startsWith(SHARED_RESOURCE_PREFIX);
079    }
080    
081    /**
082     * Get the shared root name of a shared resource.
083     * @param resource the resource.
084     * @return the shared root name of the resource, or null if the resource is not shared.
085     */
086    public static final String getSharedRootName(Resource resource)
087    {
088        return getSharedRootName(resource.getPath());
089    }
090    /**
091     * Get the shared root name of a shared resource collection.
092     * @param collection the resource collection.
093     * @return the shared root name of the resource collection, or null if the collection is not shared.
094     */
095    public static final String getSharedRootName(ResourceCollection collection)
096    {
097        return getSharedRootName(collection.getPath());
098    }
099    
100    /**
101     * Get the shared root name of a shared explorer node.
102     * @param explorerNode the explorer node.
103     * @return the shared root name of the explorer node, or null if the node is not shared.
104     */
105    public static final String getSharedRootName(ExplorerNode explorerNode)
106    {
107        return getSharedRootName(explorerNode.getPath());
108    }
109    
110    /**
111     * Get the shared root name of a shared resource or collection
112     * @param objectPath the path of the resource or collection AmetysObject.
113     * @return the shared root name of the resource, or null if the resource is not shared.
114     */
115    public static final String getSharedRootName(String objectPath)
116    {
117        if (isShared(objectPath))
118        {
119            return StringUtils.substringBefore(objectPath.substring(SHARED_RESOURCE_PREFIX.length()), "/");
120        }
121        
122        return null;
123    }
124    
125    /**
126     * Get the rights context of an explorer node.
127     * @param explorerNode the explorer node.
128     * @return the explorer node rights context.
129     */
130    public static final String getRightsContext(ExplorerNode explorerNode)
131    {
132        return getRightsContext(explorerNode.getPath(), explorerNode.getExplorerPath());
133    }
134    
135    /**
136     * Get the rights context of a resource.
137     * @param objectPath the path of the resource or collection AmetysObject.
138     * @param resourcePath the path of the resource in the resource explorer.
139     * @return the resource rights context.
140     */
141    public static final String getRightsContext(String objectPath, String resourcePath)
142    {
143        StringBuilder context = new StringBuilder();
144        
145        if (isShared(objectPath))
146        {
147            context.append("/shared-resources-").append(getSharedRootName(objectPath));
148        }
149        else
150        {
151            context.append("/resources");
152        }
153        
154        context.append(resourcePath);
155        
156        return context.toString();
157    }
158    
159}