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.time.ZonedDateTime;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.logger.LogEnabled;
028import org.apache.avalon.framework.logger.Logger;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.avalon.framework.service.Serviceable;
032import org.apache.cocoon.components.ContextHelper;
033import org.apache.cocoon.environment.Request;
034import org.apache.commons.lang3.StringUtils;
035import org.w3c.dom.Node;
036
037import org.ametys.core.util.DateUtils;
038import org.ametys.core.util.dom.MapElement;
039import org.ametys.plugins.workspaces.project.ProjectManager;
040import org.ametys.plugins.workspaces.project.objects.Project;
041import org.ametys.web.repository.site.Site;
042import org.ametys.web.repository.site.SiteManager;
043
044/**
045 * Helper component to be used from XSL stylesheets to get info related to projects.
046 */
047public class ProjectXsltHelper implements Serviceable, Contextualizable, LogEnabled
048{
049    private static Logger _logger;
050    private static Context _context;
051    private static SiteManager _siteManager;
052    private static ProjectManager _projectManager;
053    
054    @Override
055    public void contextualize(Context context) throws ContextException
056    {
057        _context = context;
058    }
059    
060    @Override
061    public void enableLogging(Logger logger)
062    {
063        _logger = logger;
064    }
065    
066    @Override
067    public void service(ServiceManager manager) throws ServiceException
068    {
069        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
070        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
071    }
072    
073    /**
074     * Returns the current project
075     * @return the current project
076     */
077    public static String project()
078    {
079        Project project = null;
080        
081        Request request = ContextHelper.getRequest(_context);
082        String siteName = (String) request.getAttribute("site");
083        
084        if (StringUtils.isNotEmpty(siteName) && _siteManager.hasSite(siteName))
085        {
086            Site site = _siteManager.getSite(siteName);
087            List<Project> projects = _projectManager.getProjectsForSite(site);
088            
089            project = !projects.isEmpty() ? projects.get(0) : null;
090        }
091        
092        if (project == null && _logger.isWarnEnabled())
093        {
094            String warnMsg = String.format("No project was found for site '%s'.", siteName);
095            _logger.warn(warnMsg);
096        }
097        
098        return project != null ? project.getName() : null;
099    }
100    
101    /**
102     * Returns the title of the current project
103     * @return the title of the current project
104     */
105    public static String projectTitle()
106    {
107        return projectTitle(project());
108    }
109    
110    /**
111     * Returns the title of the given project
112     * @param projectName The project to consider
113     * @return the title of the given project or empty otherwise
114     */
115    public static String projectTitle(String projectName)
116    {
117        String title = "";
118        
119        Project project = _projectManager.getProject(projectName);
120        if (project != null)
121        {
122            title = project.getTitle();
123        }
124        
125        return title;
126    }
127    
128    /**
129     * Returns the description of the current project
130     * @return the description of the current project
131     */
132    public static String projectDescription()
133    {
134        return projectDescription(project());
135    }
136    
137    /**
138     * Returns the description of the given project
139     * @param projectName The project to consider
140     * @return the description of the given project or empty otherwise
141     */
142    public static String projectDescription(String projectName)
143    {
144        String title = "";
145        
146        Project project = _projectManager.getProject(projectName);
147        if (project != null)
148        {
149            title = project.getDescription();
150        }
151        
152        return title;
153    }
154    
155    /**
156     * Returns the creation date of the current project
157     * @return the creation date of the current project at the ISO format or empty otherwise
158     */
159    public static String projectCreationDate()
160    {
161        return projectCreationDate(project());
162    }
163    
164    /**
165     * Returns the creation date of the given project
166     * @param projectName The project to consider
167     * @return the creation date of the given project at the ISO format or empty otherwise
168     */
169    public static String projectCreationDate(String projectName)
170    {
171        String creationDate = "";
172        
173        Project project = _projectManager.getProject(projectName);
174        if (project != null)
175        {
176            ZonedDateTime date = project.getCreationDate();
177            creationDate = date.format(DateUtils.getISODateTimeFormatter());
178        }
179        
180        return creationDate;
181    }
182    
183    /**
184     * True if the resource comes from workspaces
185     * @param resourceId the resource id
186     * @return true if the resource comes from workspaces
187     */
188    public static boolean isResourceFromWorkspace(String resourceId)
189    {
190        return _projectManager.getParentProject(resourceId) != null;
191    }
192    
193    /**
194     * Get the site of a project's resource
195     * @param projectResourceId The resource id
196     * @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;
197     */
198    public static Node resourceSite(String projectResourceId)
199    {
200        Project project = _projectManager.getParentProject(projectResourceId);
201        if (project != null)
202        {
203            Site site = project.getSites().iterator().next();
204            
205            Map<String, String> attributes = new HashMap<>();
206            attributes.put("name", site.getName());
207            attributes.put("id", site.getId());
208            
209            Map<String, String> values = new HashMap<>();
210            values.put("title", site.getTitle());
211            values.put("url", site.getUrl());
212            
213            return new MapElement("site", attributes, values);
214        }
215        
216        return null;
217    }
218}