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