001/*
002 *  Copyright 2022 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.workspaces.documents;
017
018import java.util.Collections;
019
020import org.ametys.cms.transformation.URIResolver;
021import org.ametys.plugins.explorer.resources.Resource;
022import org.ametys.plugins.repository.AmetysObject;
023import org.ametys.plugins.repository.UnknownAmetysObjectException;
024import org.ametys.plugins.workspaces.project.objects.Project;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugin.component.PluginAware;
027import org.ametys.web.editor.ResourceURIResolver;
028
029/**
030 * {@link URIResolver} for type "project-resource". <br>
031 * These links point to a file from the resources of a project.
032 */
033public class ProjectResourceURIResolver extends ResourceURIResolver implements PluginAware
034{
035    private String _pluginName;
036    
037    public void setPluginInfo(String pluginName, String featureName, String id)
038    {
039        _pluginName = pluginName;
040    }
041    
042    @Override
043    public String getType()
044    {
045        return "project-resource";
046    }
047    
048    @Override
049    public I18nizableText getLabel(String uri)
050    {
051        try
052        {
053            Resource resource = (Resource) _resolver.resolveById(uri);
054            return new I18nizableText("plugin" + _pluginName, "PLUGINS_WORKSPACES_LINK_RESOURCE_LABEL", Collections.singletonList(resource.getResourcePath()));
055        }
056        catch (UnknownAmetysObjectException e)
057        {
058            return new I18nizableText("plugin" + _pluginName, "PLUGINS_WORKSPACES_LINK_RESOURCE_UNKNOWN");
059        }
060    }
061
062    @Override
063    protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal)
064    {
065        String prefix = super.getUriPrefix(object, download, absolute, internal);
066        return prefix + "/_plugins/" + _pluginName + "/" + _getProject(object).getName();
067    }
068    
069    @Override
070    protected String _getSiteName(Resource resource)
071    {
072        Project project = _getProject(resource);
073        
074        return project.getSite().getName();
075    }
076    
077    /**
078     * Retrieves parent project
079     * @param resource The resource which belongs to a project
080     * @return The parent project
081     */
082    protected Project _getProject(AmetysObject resource)
083    {
084        AmetysObject parent = resource.getParent();
085        while (parent != null)
086        {
087            if (parent instanceof Project)
088            {
089                return (Project) parent;
090            }
091            
092            parent = parent.getParent();
093        }
094        
095        return null;
096    }
097}