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.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;
027
028/**
029 * {@link URIResolver} for type "project-resource". <br>
030 * These links point to a file from the resources of a project.
031 */
032public class ProjectResourceURIResolver extends org.ametys.web.editor.ResourceURIResolver implements PluginAware
033{
034    /** plugin name */
035    protected 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    protected String getUriPrefix (AmetysObject object, boolean download, boolean absolute, boolean internal)
050    {
051        Project project = null;
052        String projectName = null;
053        String siteName = null;
054        
055        if (object instanceof Resource)
056        {
057            Resource resource = (Resource) object;
058            project = _getProject(resource);
059            projectName = project.getName();
060            
061            siteName = project.getSites().iterator().next().getName();
062        }
063        
064        if (internal)
065        {
066            return "cocoon://plugins/" + _pluginName + "/" + projectName;
067        }
068        else if (absolute)
069        {
070            return _webPrefixHandler.getAbsoluteUriPrefix(siteName) + "/_plugins/" + _pluginName + "/" + projectName;
071        }
072        else
073        {
074            return _webPrefixHandler.getUriPrefix(siteName) + "/_plugins/" + _pluginName + "/" + projectName;
075        }
076    }
077    
078    @Override
079    public I18nizableText getLabel(String uri)
080    {
081        try
082        {
083            Resource resource = (Resource) _resolver.resolveById(uri);
084            return new I18nizableText("plugin" + _pluginName, "PLUGINS_WORKSPACES_LINK_RESOURCE_LABEL", Collections.singletonList(resource.getResourcePath()));
085        }
086        catch (UnknownAmetysObjectException e)
087        {
088            return new I18nizableText("plugin" + _pluginName, "PLUGINS_WORKSPACES_LINK_RESOURCE_UNKNOWN");
089        }
090    }
091    
092    /**
093     * Retrieves parent project
094     * @param resource The resource which belongs to a project
095     * @return The parent project
096     */
097    protected Project _getProject (AmetysObject resource)
098    {
099        AmetysObject parent = resource.getParent();
100        while (parent != null)
101        {
102            if (parent instanceof Project)
103            {
104                return (Project) parent;
105            }
106            
107            parent = parent.getParent();
108        }
109        return null;
110    }
111}