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.transformation.xslt;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.commons.lang.StringUtils;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
025import org.ametys.plugins.userdirectory.UserDirectoryHelper;
026import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver;
027import org.ametys.plugins.userdirectory.page.UserPage;
028
029/**
030 * Helper component to be used from XSL stylesheets.
031 */
032public class UserXSLTHelper extends org.ametys.web.transformation.xslt.AmetysXSLTHelper
033{
034    /** The DAO for synchronizable contents collections */
035    protected static SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
036    /** The resolver for user directory pages */
037    protected static UserDirectoryPageResolver _userDirectoryPageResolver;
038    /** The user directory helper */
039    protected static UserDirectoryHelper _userDirectoryHelper;
040    
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
047        _userDirectoryPageResolver = (UserDirectoryPageResolver) manager.lookup(UserDirectoryPageResolver.ROLE);
048        _userDirectoryHelper = (UserDirectoryHelper) manager.lookup(UserDirectoryHelper.ROLE);
049    }
050    
051    /**
052     * Gets the id of the content of the given user
053     * @param lang the content language
054     * @param userIdentity The user to get content (such as login#population)
055     * @return the id of the content of the given user
056     */
057    public static String getUserContent(String lang, String userIdentity)
058    {
059        if (StringUtils.isBlank(userIdentity))
060        {
061            return null;
062        }
063        
064        UserIdentity user = UserIdentity.stringToUserIdentity(userIdentity);
065        Content userContent = _userDirectoryHelper.getUserContent(user, lang);
066        if (userContent != null)
067        {
068            return userContent.getId();
069        }
070        
071        return null;
072    }
073    
074    /**
075     * Gets the id of the content of the current connected user
076     * @param lang the content language
077     * @return the id of the content of the current connected user
078     */
079    public static String getCurrentUserContent(String lang)
080    {
081        UserIdentity currentUser = _currentUserProvider.getUser();
082        if (currentUser == null)
083        {
084            return null;
085        }
086        
087        Content userContent = _userDirectoryHelper.getUserContent(currentUser, lang);
088        if (userContent != null)
089        {
090            return userContent.getId();
091        }
092        
093        return null;
094    }
095    
096    /**
097     * Gets the id of the page of the current user
098     * @param lang The language
099     * @return the id of the page of the user content or null if not found
100     */
101    public static String getCurrentUserPage(String lang)
102    {
103        String contentId = getCurrentUserContent(lang);
104        
105        if (contentId != null)
106        {
107            return getUserPage(contentId);
108        }
109        
110        return null;
111    }
112    
113    
114    /**
115     * Gets the id of the page of the user content
116     * @param contentId The user content id
117     * @return the id of the page of the user content
118     */
119    public static String getUserPage(String contentId)
120    {
121        String siteName = site();
122        return getUserPage(contentId, siteName);
123    }
124    
125    /**
126     * Gets the id of the page of the user content
127     * @param contentId The user content id
128     * @param siteName the site name
129     * @return the id of the page of the user content
130     */
131    public static String getUserPage(String contentId, String siteName)
132    {
133        if (contentId == null)
134        {
135            return null;
136        }
137        
138        Content content = _ametysObjectResolver.resolveById(contentId);
139        if (content == null)
140        {
141            return null;
142        }
143        
144        UserPage userPage = _userDirectoryPageResolver.getUserPage(content, StringUtils.isBlank(siteName) ? site() : siteName, lang());
145        return userPage != null ? userPage.getId() : null; 
146    }
147}