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.userdirectory.page;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.logger.AbstractLogEnabled;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
028import org.ametys.web.repository.page.Page;
029
030/**
031 * Resolves an user directory page path from the associated user content.
032 */
033public class UserDirectoryPageResolver extends AbstractLogEnabled implements Component, Serviceable
034{
035    /** The avalon role. */
036    public static final String ROLE = UserDirectoryPageResolver.class.getName();
037    
038    /** The ametys object resolver. */
039    protected AmetysObjectResolver _ametysResolver;
040    /** The user directory page handler */
041    protected UserDirectoryPageHandler _pageHandler;
042    
043    
044    @Override
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
048        _pageHandler = (UserDirectoryPageHandler) serviceManager.lookup(UserDirectoryPageHandler.ROLE);
049    }
050    
051    /**
052     * Return the user page
053     * @param userContent the user page content
054     * @param siteName The current site name.
055     * @param sitemapName The current sitemap name.
056     * @param contentTypeId the content type id
057     * @return the user page or null
058     */
059    public UserPage getUserPage(Content userContent, String siteName, String sitemapName, String contentTypeId)
060    {
061        String language = userContent.getLanguage();
062        if (language == null)
063        {
064            language = sitemapName;
065        }
066        Page userDirectoryRootPage = _pageHandler.getUserDirectoryRootPage(siteName, language, contentTypeId);
067        
068        if (userDirectoryRootPage == null)
069        {
070            return null;
071        }
072        
073        return getUserPage(userDirectoryRootPage, userContent);
074    }
075    
076    /**
077     * Return the user page
078     * @param userDirectoryRootPage the user directory root page
079     * @param userContent the user content
080     * @return the user page or null
081     */
082    public UserPage getUserPage(Page userDirectoryRootPage, Content userContent)
083    {
084        try
085        {
086            String pageId = getUserPageId(userDirectoryRootPage, userContent);
087            return pageId != null ? _ametysResolver.resolveById(pageId) : null;
088        }
089        catch (UnknownAmetysObjectException e)
090        {
091            return null;
092        }
093    }
094    
095    /**
096     * Return the user page identifier
097     * @param userDirectoryRootPage the user directory root page
098     * @param userContent the user content
099     * @return the user page or <code>null</code> if the user page does not exist
100     */
101    public String getUserPageId(Page userDirectoryRootPage, Content userContent)
102    {
103        // E.g: uduser://path?rootId=...&contentId=...
104        String userPath = _getUserPath(userDirectoryRootPage, userContent);
105        if (userPath != null)
106        {
107            return UserPage.getId(userPath, userDirectoryRootPage.getId(), userContent.getId());
108        }
109        return null;
110    }
111    
112    /**
113     * Get the path of a user content
114     * @param userDirectoryRootPage The user directory root page where to look into
115     * @param userContent The user content
116     * @return The path or <code>null</code> if the user page does not exist
117     */
118    private String _getUserPath(Page userDirectoryRootPage, Content userContent)
119    {
120        String value = _pageHandler.getTransformedClassificationMetadataValue(userDirectoryRootPage, userContent);
121        return value != null ? value.replaceAll("(.)(?!$)", "$1/") : null;
122    }
123}