001/*
002 *  Copyright 2016 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.plugins.explorer.resources;
017
018import org.ametys.plugins.repository.AmetysObject;
019import org.ametys.plugins.repository.TraversableAmetysObject;
020
021/**
022 * Helper class that provides utility methods to work with resources.
023 */
024public final class ResourceHelper
025{
026    
027    private ResourceHelper()
028    {
029        // Hide the default constructor.
030    }
031    
032    /**
033     * Get the resource root of a resource.
034     * @param resource The resource.
035     * @return The resource root.
036     */
037    public static TraversableAmetysObject getResourceRoot(Resource resource)
038    {
039        return _getResourceRoot(resource);
040    }
041    
042    /**
043     * Get the resource root of a resource collection.
044     * @param collection The resource collection.
045     * @return The resource root.
046     */
047    public static TraversableAmetysObject getResourceRoot(ResourceCollection collection)
048    {
049        return _getResourceRoot(collection);
050    }
051    
052    /**
053     * Get the resource root of an ametys object.
054     * @param object The object.
055     * @return The resource root of the ametys object.
056     */
057    private static TraversableAmetysObject _getResourceRoot(AmetysObject object)
058    {
059        AmetysObject current = object;
060        TraversableAmetysObject parent = object.getParent();
061        
062        while (parent != null && parent instanceof ResourceCollection)
063        {
064            current = parent;
065            parent = current.getParent();
066        }
067        
068        return current instanceof TraversableAmetysObject ? (TraversableAmetysObject) current : null;
069    }
070    
071}