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.avalon.framework.service.Serviceable;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
028import org.ametys.plugins.repository.AmetysObjectFactory;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.UnknownAmetysObjectException;
032import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
035import org.ametys.web.service.ServiceExtensionPoint;
036import org.ametys.web.skin.SkinsManager;
037
038/**
039 * {@link AmetysObjectFactory} handling {@link UserPage}.
040 */
041public class UserPageFactory implements AmetysObjectFactory<UserPage>, Serviceable
042{
043    private AmetysObjectResolver _resolver;
044    private UserDirectoryPageHandler _userDirectoryPageHandler;
045    private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
046    private SkinsManager _skinManager;
047    private ServiceExtensionPoint _serviceExtensionPoint;
048    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
054        _userDirectoryPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
055        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
056        _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
057        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
058        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
059    }
060
061    public UserPage getAmetysObjectById(String id) throws AmetysRepositoryException
062    {
063        // E.g: uduser://path?rootId=...&contentId=...
064        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId=");
065        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
066        Page root = _resolver.resolveById(rootId);
067        
068        String contentId = StringUtils.substringAfter(id, "&contentId=");
069        Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path);
070        if (userPagesContent.values().contains(contentId))
071        {
072            Content syncContent = _resolver.resolveById(contentId);
073            return new UserPage(root, syncContent, path, _resolver, _userDirectoryPageHandler, _synchronizableContentsCollectionDAO, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
074        }
075        else
076        {
077            throw new UnknownAmetysObjectException("No user content for id " + contentId);
078        }
079    }
080
081    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
082    {
083        // Id is like uduser://path?rootId=...&contentId=...
084        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId=");
085        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
086        if (!_resolver.hasAmetysObjectForId(rootId))
087        {
088            return false;
089        }
090        
091        Page root = _resolver.resolveById(rootId);
092        
093        String contentId = StringUtils.substringAfter(id, "&contentId=");
094        Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path);
095        return userPagesContent.values().contains(contentId);
096    }
097
098    public String getScheme()
099    {
100        return "uduser";
101    }
102    
103    SynchronizableContentsCollectionDAO _getSynchronizableContentsCollectionDAO()
104    {
105        return _synchronizableContentsCollectionDAO;
106    }
107}