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 */
016
017package org.ametys.plugins.workspaces.project.helper;
018
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.logger.LogEnabled;
027import org.apache.avalon.framework.logger.Logger;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031import org.apache.cocoon.components.ContextHelper;
032import org.apache.cocoon.environment.Request;
033import org.apache.commons.lang3.StringUtils;
034import org.w3c.dom.Node;
035
036import org.ametys.core.util.dom.MapElement;
037import org.ametys.plugins.workspaces.project.ProjectManager;
038import org.ametys.plugins.workspaces.project.objects.Project;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.repository.site.SiteManager;
041
042/**
043 * Helper component to be used from XSL stylesheets to get info related to projects.
044 */
045public class ProjectXsltHelper implements Serviceable, Contextualizable, LogEnabled
046{
047    private static Logger _logger;
048    private static Context _context;
049    private static SiteManager _siteManager;
050    private static ProjectManager _projectManager;
051    
052    @Override
053    public void contextualize(Context context) throws ContextException
054    {
055        _context = context;
056    }
057    
058    @Override
059    public void enableLogging(Logger logger)
060    {
061        _logger = logger;
062    }
063    
064    @Override
065    public void service(ServiceManager manager) throws ServiceException
066    {
067        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
068        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
069    }
070    
071    /**
072     * Returns the current project
073     * @return the current project
074     */
075    public static String project()
076    {
077        Project project = null;
078        
079        Request request = ContextHelper.getRequest(_context);
080        String siteName = (String) request.getAttribute("site");
081        
082        if (StringUtils.isNotEmpty(siteName) && _siteManager.hasSite(siteName))
083        {
084            Site site = _siteManager.getSite(siteName);
085            List<Project> projects = _projectManager.getProjectsForSite(site);
086            
087            project = !projects.isEmpty() ? projects.get(0) : null;
088        }
089        
090        if (project == null && _logger.isWarnEnabled())
091        {
092            String warnMsg = String.format("No project was found for site '%s'.", siteName);
093            _logger.warn(warnMsg);
094        }
095        
096        return project != null ? project.getName() : null;
097    }
098    
099    /**
100     * True if the resource comes from workspaces
101     * @param resourceId the resource id
102     * @return true if the resource comes from workspaces
103     */
104    public static boolean isResourceFromWorkspace(String resourceId)
105    {
106        return _projectManager.getParentProject(resourceId) != null;
107    }
108    
109    /**
110     * Get the site of a project's resource
111     * @param projectResourceId The resource id
112     * @return The site &lt;site id="site://xxx" name="siteName"&gt;&lt;title&gt;Site's titleX&lt;/title&gt;&lt;url&gt;http://...&lt;/url&gt;/site&gt;
113     */
114    public static Node resourceSite(String projectResourceId)
115    {
116        Project project = _projectManager.getParentProject(projectResourceId);
117        if (project != null)
118        {
119            Site site = project.getSites().iterator().next();
120            
121            Map<String, String> attributes = new HashMap<>();
122            attributes.put("name", site.getName());
123            attributes.put("id", site.getId());
124            
125            Map<String, String> values = new HashMap<>();
126            values.put("title", site.getTitle());
127            values.put("url", site.getUrl());
128            
129            return new MapElement("site", attributes, values);
130        }
131        
132        return null;
133    }
134}