001/*
002 *  Copyright 2019 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.ugc.transformation.xslt;
017
018import java.util.Optional;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.repository.UnknownAmetysObjectException;
026import org.ametys.plugins.ugc.page.UGCPage;
027import org.ametys.plugins.ugc.page.UGCPageHandler;
028import org.ametys.web.transformation.xslt.AmetysXSLTHelper;
029
030/**
031 * Helper component for UGC to be used from XSL stylesheets.
032 */
033public class UgcXSLTHelper extends AmetysXSLTHelper
034{
035    private static UGCPageHandler _ugcPageHandler;
036
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        super.service(manager);
041        _ugcPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE);
042    }
043    
044    /**
045     * Gets the id of the page of the given UG Content
046     * @param contentId The UGC content id
047     * @return the id of the page of the given UG Content
048     */
049    public static String getUgcPage(String contentId)
050    {
051        return getUgcPage(contentId, null/* default value for site */);
052    }
053    
054    /**
055     * Gets the id of the page of the given UG Content
056     * @param contentId The UGC content id
057     * @param siteName the site name. Can be null to take the current one.
058     * @return the id of the page of the given UG Content
059     */
060    public static String getUgcPage(String contentId, String siteName)
061    {
062        if (contentId == null)
063        {
064            return null;
065        }
066        
067        Content content;
068        try
069        {
070            content = _ametysObjectResolver.resolveById(contentId);
071        }
072        catch (UnknownAmetysObjectException e)
073        {
074            return null;
075        }
076        
077        String site = StringUtils.isNotBlank(siteName) ? siteName : site();
078        String sitemap = lang();
079        for (String type : content.getTypes())
080        {
081            Optional<UGCPage> ugcPage = _ugcPageHandler.getUgcPage(content, site, sitemap, type);
082            if (ugcPage.isPresent())
083            {
084                return ugcPage.get().getId();
085            }
086        }
087        
088        return null;
089    }
090}