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.userdirectory.page;
018
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
027import org.ametys.plugins.repository.AmetysObjectFactory;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030import org.ametys.web.repository.page.Page;
031
032/**
033 * {@link AmetysObjectFactory} handling {@link UserPage}.
034 */
035public class UserPageFactory extends AbstractUserDirectoryPageFactory implements AmetysObjectFactory<UserPage>
036{
037    private SynchronizableContentsCollectionHelper _sccHelper;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043
044        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
045    }
046
047    /**
048     * Create a User page.
049     * @param root the root page.
050     * @param syncContent the synchronized content
051     * @param path the path
052     * @return The <code>UserPager</code> created
053     */
054    public UserPage createUserPage(Page root, Content syncContent, String path)
055    {
056        return new UserPage(root, getConfiguration(), getScheme(), this, syncContent, path);
057    }
058    
059    public UserPage getAmetysObjectById(String id) throws AmetysRepositoryException
060    {
061        // E.g: uduser://path?rootId=...&contentId=...
062        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId=");
063        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
064        Page root = _resolver.resolveById(rootId);
065        
066        String contentId = StringUtils.substringAfter(id, "&contentId=");
067        Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path);
068        if (userPagesContent.values().contains(contentId))
069        {
070            Content syncContent = _resolver.resolveById(contentId);
071            return createUserPage(root, syncContent, path);
072        }
073        else
074        {
075            throw new UnknownAmetysObjectException("No user content for id " + contentId);
076        }
077    }
078
079    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
080    {
081        // Id is like uduser://path?rootId=...&contentId=...
082        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId=");
083        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
084        if (!_resolver.hasAmetysObjectForId(rootId))
085        {
086            return false;
087        }
088        
089        Page root = _resolver.resolveById(rootId);
090        
091        String contentId = StringUtils.substringAfter(id, "&contentId=");
092        Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path);
093        return userPagesContent.values().contains(contentId);
094    }
095
096    public String getScheme()
097    {
098        return "uduser";
099    }
100    
101    /**
102     * Get the synchronizable contents collection helper
103     * @return The <code>SynchronizableContentsCollectionHelper</code>
104     */
105    public SynchronizableContentsCollectionHelper getSccHelper()
106    {
107        return _sccHelper;
108    }
109}